Skip to main content
← Data Structures & Algorithms Study Guide

Unit 6 · Heavy

Graphs and Network Algorithms

The most general structure, and the one where representation choice changes the complexity of everything built on it. Many problems that do not look like graphs become easy once modelled as one.

Topics in this unit

1

Modelling as a Graph

Know

Recognising a graph problem is often the whole difficulty. States and transitions, dependencies, and relationships all become vertices and edges.

Apply

Define what a vertex is and what an edge means before choosing an algorithm.

Watch out

Missing that a puzzle is a shortest-path problem over states, and writing a bespoke search instead.

2

Traversal

Know

BFS uses a queue and finds fewest-edge paths; DFS uses a stack and underpins cycle detection and topological order.

Apply

Choose BFS for unweighted shortest paths and DFS for structural questions.

Watch out

Calling any edge to a visited vertex a cycle. Only an edge to a vertex still on the recursion stack closes one.

3

Shortest Paths

Know

Dijkstra settles vertices in increasing distance, which negative edges invalidate. Bellman-Ford relaxes repeatedly and tolerates them.

Apply

Check the weight assumption before choosing.

Watch out

Running Dijkstra with negative weights, which finalises a vertex a later edge could still improve.

4

Spanning Trees and Ordering

Know

Kruskal adds globally cheapest safe edges using union-find; Prim grows one tree. Topological order exists only for directed acyclic graphs.

Apply

Use topological order to sequence dependencies, and detect the cycle that makes it impossible.

Watch out

Attempting a topological sort on a graph containing a cycle, which has no valid answer.

Emphasized in this unit

Connections and techniques that receive extra attention in this unit.

  • • Recognising a disguised graph problem
  • • Checking the weight assumption before choosing
  • • Knowing when an ordering cannot exist

Varies by course

Related topics some schools attach to this unit and others leave out. Covered on request rather than assumed.

  • Network flow. An advanced extension.
  • Union-find. Taught with Kruskal in some tracks.

Check yourself

  • • Why does representation change traversal complexity?
  • • What exactly makes a back edge different from a cross edge?
  • • Why can a cyclic graph have no topological order?

Modeling drill

Model course prerequisites as a graph, produce a valid study order, and explain what a cycle in the data would mean.

VertexEdgeAdjacency listBFSDFSDijkstraSpanning treeTopological order

Need help with this topic?

Work through questions with a tutor and get help with the steps you find difficult.

Get help with this topic