Module 8 · Graphs and Trees · BFS on Graph

Find if path exists

Start at the source and spread out: BFS takes vertices from a queue level by level, DFS goes as deep as it can and comes back at dead ends. Either way visited[] stops a cycle from sending us round forever, and the destination is reachable exactly when the search gets to it.

Approach