↑ ↓ to move · enter to open

002 · Graph theory · Search & optimization

graphs

A graph is a set of nodes joined by edges. Road maps, mazes, networks and bridges are all graphs. Three classic questions: how to find the shortest route, how to connect everything as cheaply as possible, and when a walk can cross every edge exactly once.

LAB 1

shortest paths

Every square is a node joined to its four neighbors. Stepping onto ground costs 1 and onto mud costs 5. Draw walls, drag the start or goal, and watch how each algorithm spreads out from the start.

  • visited (brighter = more recent)
  • frontier: next in line
  • path found
  • wall
  • mud (cost 5)
  • S start · G goal (drag either; click a wall or mud to erase it)

Try this: pick Maze, run Dijkstra, then switch to A* and compare Nodes visited.

0
Nodes visited
—
Path steps
—
Path cost

Dijkstra's algorithm1956

Scoreboardthis grid

AlgorithmVisitedStepsCost
  • best on this grid
  • found a path, but a costlier one

Try this: press Compare all to fill every row.

LAB 2

minimum spanning tree

Connect every town using the least total road length. Two greedy algorithms solve it in opposite ways. Kruskal takes the cheapest edges anywhere on the map. Prim grows one tree outward from a single town. Both end with the same total.

Kruskal · 1956
edges examined 0 · accepted 0total 0
Prim · 1957
towns joined 1total 0

How to read this: dots are towns and faint lines are roads you could build. Green roads are the ones each method keeps; the total is their combined length, in the same units as the map.

  • road just added
  • road skipped: it would close a loop (Kruskal)
  • roads Prim is choosing between
  • town not connected yet
  • Prim's starting town

Try this: press Step and compare which road each side adds.

How they differ

Kruskal sorts every edge by length and goes down the list. It keeps an edge unless both ends are already connected, because then it would close a loop. Node colors show the separate clusters merging. Rejected edges flash red.

Prim keeps one growing tree. At each step it adds the shortest edge leaving the tree. The amber edges are the candidates it chooses from.

Why greedy works herecut property

Split the towns into any two groups. The shortest edge crossing between them must belong to some minimum tree. Both algorithms only ever add such an edge, so neither can be beaten.

a tree on n towns always has exactly n − 1 edges
LAB 3

the seven bridges of königsberg

The people of Königsberg asked whether you could walk through the city crossing each of its seven bridges exactly once. Euler showed in 1736 that you can't, and graph theory started there. Click a district to start, then click glowing bridges to cross them.

  • glowing bridge: you can cross it now
  • gold bridge: already crossed
  • you

Your walk0 / 7 bridges

Pick a starting district.

As a graphdegree = bridges touching a district

How to read this: each circle is a district and each curve is a bridge. The number is how many bridges touch it.

  • odd number of bridges
  • even number
  • crossed on your walk

Euler's argument

Each time you walk through a district, you arrive on one bridge and leave on another. So any district you only pass through uses its bridges in pairs and needs an even number of them.

Only the start and the end can have an odd count. A walk over every bridge exists only if 0 or 2 districts have an odd degree. Königsberg has 4.