Skip to article content
Interactive Notes

Algorithms Visualized

Graph Traversal

Exploring every node with depth-first and breadth-first search

Depth-first search follows one path as far as it can before backtracking, while breadth-first search visits nodes in order of their distance from the start. Both visit every node and edge in O(n+m)O(n + m) time on a graph with nn nodes and mm edges. Kahn’s algorithm builds on them to order a directed acyclic graph so that every edge points forward.