← Introduction to Computer Science

Unit 18: Graphs

Nodes connected by edges instead of a strict hierarchy, and the handful of algorithms that spread across them, find a path, or connect them all.

7 topics · 26 questions

Each topic is a path, not a menu: work down it in order and every question builds on the one above. Click any of them to watch the answer draw itself.

Graph Terminology and Representations

  1. How it worksHow does an adjacency list store only a node's actual neighbors while an adjacency matrix stores every possible pair?
  2. Worked exampleBuild the adjacency list and the adjacency matrix for a graph with edges A-B, B-C, and A-C.
  3. Going furtherWhat happens to the memory used by an adjacency matrix once a sparse graph grows to a million nodes?
Your turn1 of 2
A graph has 697 nodes. Its adjacency matrix representation uses one cell per possible pair of nodes, giving n times n total cells. How many cells does the adjacency matrix for this graph have?

BFS Versus DFS Side by Side on the Same Graph

  1. How it worksHow does swapping a queue for a stack turn the very same traversal skeleton from BFS into DFS?
  2. Worked exampleTrace both BFS and DFS from node A on a graph with edges A-B, A-C, B-D, C-E and compare the two visit orders.
  3. Common mistakeWhy doesn't depth-first search guarantee the shortest path the way breadth-first search does on an unweighted graph?
  4. Going furtherWhat happens to both traversals once the graph contains a cycle that could send them back to an already-visited node?
  5. Ties togetherHow does the choice between BFS and DFS depend on whether you need the shortest path or just any path at all?
Your turn1 of 2
Both BFS and DFS are run from the same starting node on a graph of 349 nodes, and each traversal visits every one of the n nodes exactly once. How many total node visits occur across both traversals combined?

Topological Sort

  1. How it worksHow does topological sort repeatedly pick a node with no remaining incoming edges to build a valid order?
  2. Worked exampleTrace a topological sort on the prerequisite graph socks before shoes, shirt before jacket, shoes before nothing else.
  3. Common mistakeWhy doesn't every graph have a valid topological sort at all?
Your turn1 of 2
A topological sort repeatedly picks a node with no remaining incoming edges to build a valid order, for a graph of 349 nodes. How many total nodes get picked and added to the final order?

Shortest Paths with Dijkstra's Algorithm

  1. How it worksHow does Dijkstra's algorithm always expand the closest unvisited node next, using a priority queue to pick it?
  2. Worked exampleTrace Dijkstra's algorithm from node A on a graph with edges A-B weight 4, A-C weight 1, C-B weight 1.
  3. Common mistakeWhy doesn't Dijkstra's algorithm work correctly once a graph contains a negative edge weight?
  4. Going furtherWhat happens to Dijkstra's chosen path once one edge's weight is lowered after the algorithm already finished?
Your turn1 of 2
Dijkstra's algorithm expands the closest unvisited node next, using a priority queue. For a graph of 349 nodes, how many total nodes does the algorithm expand before every node has been visited?

Minimum Spanning Trees

  1. How it worksHow does Prim's algorithm grow a minimum spanning tree by repeatedly adding the cheapest edge that reaches a new node?
  2. Worked exampleTrace Prim's algorithm starting at node A on a graph with edges A-B weight 2, A-C weight 3, B-C weight 1.
  3. Common mistakeWhy isn't a minimum spanning tree the same thing as the shortest path between two particular nodes?
  4. Ties togetherHow does a minimum spanning tree differ from a shortest-path tree built from the same weighted graph?
Your turn1 of 2
Connecting 697 towns with the cheapest possible set of roads never needs more than n minus 1 roads, forming a minimum spanning tree. How many roads does a minimum spanning tree connecting n towns use?