Study guide: Graph Algorithms
Overview
Graph algorithms each answer a specific question: what can I reach, what is the shortest route, in what order should tasks run. Breadth-first and depth-first search explore a graph; Dijkstra, Bellman–Ford and Floyd–Warshall find shortest paths; topological sort orders a dependency graph.
Choosing the right one depends on three things: are edges weighted, can weights be negative, and is the graph directed and acyclic?
Key ideas explained
BFS and DFS
BFS explores in layers using a queue and finds the fewest-edge path in an unweighted graph. DFS dives as deep as possible using a stack or recursion and is the basis for cycle detection, connected components and topological sort. Both run in O(V + E) on an adjacency list.
Dijkstra's algorithm
Repeatedly settle the unsettled vertex with the smallest known distance and relax its outgoing edges. It requires non-negative weights and runs in O((V + E) log V) with a heap. A negative edge breaks it because a vertex already settled could later be improved.
Bellman–Ford and Floyd–Warshall
Bellman–Ford relaxes every edge V − 1 times, tolerates negative weights, and detects a negative cycle if a further pass still improves something; it runs in O(VE). Floyd–Warshall computes all-pairs shortest paths by dynamic programming over allowed intermediate vertices in O(V³).
Topological sort
A topological order lists vertices so every edge points forward. It exists exactly when the directed graph has no cycle, and it can be built with DFS finishing times or Kahn's algorithm, which repeatedly removes vertices with no incoming edges.
Worked examples
Dijkstra by hand
Problem. Undirected graph: A–B 4, A–C 2, C–B 1, B–D 5, C–D 8, D–E 3. Shortest distances from A?
- Settle A (0): B = 4, C = 2.
- Settle C (2): B improves to 3, D = 10.
- Settle B (3): D improves to 8.
- Settle D (8): E = 11.
Answer. A = 0, C = 2, B = 3, D = 8, E = 11. The route to E is A–C–B–D–E.
Topological orders
Problem. Tasks with dependencies A→B, A→C, B→D, C→D. List the valid orders.
- A has no prerequisites, so it comes first; D depends on both B and C, so it comes last.
- B and C can appear in either order.
Answer. A, B, C, D and A, C, B, D. Adding an edge D→A would create a cycle and no order would exist.
Enter these same values in the calculator above and compare each step with the working shown here.
Common mistakes to avoid
- Running Dijkstra on a graph with negative edge weights.
- Using BFS for weighted shortest paths. It only minimises the number of edges.
- Forgetting to mark vertices as visited, which causes infinite loops on cycles.
- Expecting a unique traversal order. BFS and DFS orders depend on how neighbours are listed.
- Confusing a single negative edge (fine for Bellman–Ford) with a negative cycle (no shortest path exists).
Where this shows up in computer science
- Link-state routing (OSPF) uses Dijkstra, and distance-vector routing (RIP) is built on Bellman–Ford.
- Build tools and course-prerequisite planners use topological sort.
- BFS measures degrees of separation in social networks; Floyd–Warshall suits all-pairs queries on small networks.
Test yourself
Try each question before opening the answer.
What is the time complexity of BFS on an adjacency-list graph?
O(V + E).
Why can Dijkstra fail when an edge is negative?
It assumes a settled vertex can never be improved, but a later negative edge can shorten a path to it.
Does a directed graph containing a cycle have a topological order?
No.