Six Degrees

Six degrees of separation is the theory that everyone is six or fewer connections away. Given a friendship network, find the degrees of two people, return -1 if one of them cannot be connected.

Example

I: a = 1, b = 4

1------2-----4
 \          /
  \        /
   \--3--/

O: 2

I: a = 1, b= 4

1      2-----4
             /
           /
          3

O: -1

Approach

Level order traversal. Prune the entire search if the level is greater than 6.

Code

Last updated