The social network framing is cute but once you see it's just shortest path on an unweighted graph, it's BFS and nothing else.
Use BFS from the source node, tracking visited nodes and hop count level by level. Stop when the target is found, returning the current hop count; if the queue empties, return -1.
Pro tip: Mention that BFS is optimal for unweighted graphs and handle edge cases like source equals target (return 0) and null inputs. Also, clarify whether the graph is directed or undirected, as it affects traversal.
Confirm if the graph is directed or undirected, and discuss edge cases such as source == target, null nodes, or disconnected graphs.
Select BFS for shortest path in unweighted graphs. Initialize a queue with the source node, a visited set, and a distance variable (or store distance in queue).
While the queue is not empty, process all nodes at the current level, incrementing hop count. For each node, enqueue unvisited neighbors and mark them visited.
If the target is found during traversal, return the current hop count. If the queue empties without finding the target, return -1.
State time complexity O(V+E) and space O(V). Mention potential optimizations like bidirectional BFS if needed.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.