Graph algorithms answer questions about structure and cost: does the graph contain a cycle, in what order can dependent tasks run (topological sort), what is the cheapest route between two vertices (Dijkstra), and how can all vertices be connected at minimum total weight (Prim and Kruskal). They build directly on BFS, DFS, queues, heaps, and union-find ideas from earlier sections.
Pick the algorithm from the question.
Fewest edges: BFS. Cheapest path with non-negative weights: Dijkstra. Cheapest connection of everything: Prim or Kruskal. Ordering dependencies: topological sort.
Path
Learning Path
Work through the stages in order, or jump to a topic from the index below (8 tutorials).
Common mistakes when learning graph algorithms in C.
neg
Negative weights with Dijkstra
Dijkstra's algorithm assumes non-negative weights; use Bellman-Ford otherwise.
dir
Directed vs undirected
Spanning trees are defined for undirected graphs; topological order for directed acyclic graphs.
inf
Overflow with INF
Do not add to an "infinite" distance; guard with a check.
Remember
Key Takeaways
Match algorithm to question.
Dijkstra: non-negative weights.
MSTs: connect everything cheaply.
One line: Graph algorithms answer questions about structure and cost: does the graph contain a cycle, in what order can dependent tasks run (topological sort), what is the cheapest route between two vertices (Dijkstra), and how can all vertices be connected at minimum total weight (Prim and Kruskal).
Frequently Asked Questions
Dijkstra's algorithm (non-negative weights) with an adjacency matrix or priority queue; BFS for unweighted graphs.
A subset of edges that connects all vertices with no cycles and the smallest possible total weight.
An ordering of the vertices of a directed acyclic graph such that every edge goes from earlier to later in the order.