'calculate graph API calls

Using the below map as input:

Map {
    'API_FACEBOOK' => ['API_GOOGLE','API_TWITTER','API_WHATSAPP','API_INSTAGRAM'],
    'API_GOOGLE' => [],
    'API_TWITTER' => ['API_TWITTER','API_MICROSOFT'],
    'API_WHATSAPP' => ['API_ORANGE'],
    'API_ORANGE' => ['API_TWITTER','API_SAMSUNG','API_APPLE'],
    'API_SAMSUNG' => [],
    'API_APPLE' => [],
    'API_MICROSOFT' => ['API_TWITTER']
}

I would like to calculate the call level of each API. How I can structure my input to have the below graph :

- API_FACEBOOK
    > API_GOOGLE
    > API_TWITTER
        > API_TWITTER
        > API_MICROSOFT
            > API_TWITTER
    > API_WHATSAPP
        > API_ORANGE
            > API_TWITTER
            > API_SAMSUNG
            > API_APPLE

So the output I searching for to calculate API's level is :

Level API_FACEBOOK : 7 (API_GOOGLE & API_TWITTER & API_MICROSOFT & API_WHATSAPP->API_ORANGE->API_SAMSUNG + API_APPLE)
Level API_GOOGLE : 0 
Level API_TWITTER : 1 (API_MICROSOFT)
Level API_WHATSAPP : 3 (API_ORANGE->API_SAMSUNG + API_APPLE)
Level API_ORANGE : 4 (API_TWITTER->API_MICROSOFT & API_SAMSUNG + API_APPLE)
Level API_SAMSUNG : 0
Level API_APPLE : 0 


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source