图论,循序渐进

免费课程

第 1 章,第 3 课,共 5 课

字幕

Walks, Paths and Bridges

How to move through a graph, when it holds together, and how to draw it in one stroke.

本课为英语讲解。可在上方开启英文、阿拉伯文、德文、西班牙文或法文字幕。

文字版课程

阅读约 7 分钟以英文撰写。

Children in Germany grow up with a puzzle called the house of Nikolaus: draw a little house (a square with both diagonals and a roof on top) in one stroke, without lifting your pen and without tracing any line twice. Start at the tip of the roof and you get stuck with lines left over. Start at a top corner and you get stuck again. Start at a bottom corner and it works: eight lines, one stroke.

Why do the bottom corners work when nothing else does? The answer is Euler's rule from lesson one, plus a few precise words for moving through a graph.

Walks, trails and paths

When you move through a graph, you follow a route from vertex to vertex along the edges. There are four words for routes, and each forbids a little more than the one before:

RouteRule
WalkAny route at all. It may repeat edges and vertices.
TrailA walk that never repeats an edge. It may pass through a vertex twice.
PathA walk that never repeats a vertex, so it cannot repeat an edge either.
CycleA path that returns to where it started.

In our running graph (A to G, edges AB, AC, BC, BD, CE, DE, DF, FG), the route B, C, E, D and back to B is a cycle of length four, and A, B, C and back to A is a triangle.

A cycle of length four and the length of a route.A cycle of length four and the length of a route.

One small trap: the length of a route is its number of edges, not its number of vertices. A, B, C, A has length 3.

Distance

The distance between two vertices is the length of a shortest path between them.

From A to G, the path A, B, D, F, G uses four edges, and no path uses fewer, so the distance from A to G is 4. The route A, C, E, D, F, G also gets there, with five edges. It is a path, just not a shortest one.

Two paths from A to G; the shorter one sets the distance.Two paths from A to G; the shorter one sets the distance.

A useful fact: whenever there is a walk between two vertices, there is also a path. If a walk visits some vertex twice, cut out everything between the two visits. The result is still a walk between the same ends, and shorter. Repeat until no vertex repeats, and what is left is a path. So for questions of whether you can get somewhere, walks and paths are equally good.

Connected pieces

A graph is connected when you can get from every vertex to every other one. When you cannot, it falls apart into pieces called connected components. Each component is connected on its own, and no edge joins two different components.

Think of the Hawaiian islands. Each island has its own roads, but no road joins one island to another, so each island is a component of the road network.

This is why checking connectivity comes first with any new graph. Many real data sets arrive in several pieces, and between two pieces a route simply does not exist, whatever algorithm you run.

Bridges

Some edges matter more than others. A bridge is an edge whose removal splits the graph into more pieces. In the Florida Keys a single highway runs from island to island all the way to Key West; close the Seven Mile Bridge and every island beyond it is cut off from the mainland by road.

In our graph, the edges DF and FG are bridges. Remove either one and the graph splits in two.

DF and FG are the only bridges.DF and FG are the only bridges.

Here is the rule:

An edge is a bridge exactly when it lies on no cycle.

Why. If an edge uv lies on a cycle, the rest of the cycle is a detour from u to v, so any route that used uv can go around instead, and nothing gets cut off. If uv lies on no cycle, then after removing it there can be no route from u to v at all, because such a route together with uv would form a cycle. So its removal separates u from v.

That is why BD is not a bridge (it lies on the cycle B, C, E, D) while DF is.

One stroke: Euler trails

Back to the house. Drawing it in one stroke means finding a trail that uses every edge exactly once. That is called an Euler trail. If the trail also ends where it started, it is an Euler circuit.

Lesson one gave us the rule. For a connected graph:

  • an Euler circuit exists exactly when every vertex has even degree;
  • an Euler trail exists exactly when zero or two vertices are odd, and if two are odd, it must start at one of them and end at the other.

Label the house A (bottom left), B (bottom right), C (top right), D (top left) and E (roof tip). The degrees are A 3, B 3, C 4, D 4 and E 2. Exactly two vertices are odd, the bottom corners, so every one-stroke drawing starts in one bottom corner and finishes in the other. Starting at the roof tip cannot work, because E is even.

Lesson one only showed that the condition is necessary. To see that it is enough, we build the trail. Hierholzer's algorithm does it:

  1. From a valid start, walk along unused edges until you get stuck.
  2. If some edges are still unused, pick a vertex on your route that still has one, walk a loop through unused edges from there, and splice that loop into the route.
  3. Repeat until every edge is used.

On the house, start at A and walk A, B, D, A, C, B. At B you are stuck, and CD, DE and EC are unused. C is on the route, so walk the loop C, D, E, C and splice it in: A, B, D, A, C, D, E, C, B. All eight edges, each once.

Hierholzer's algorithm on the house: a first walk, then a spliced-in loop.Hierholzer's algorithm on the house: a first walk, then a spliced-in loop.

Why the first walk gets stuck in the right place: each time it passes through a vertex it uses two of that vertex's edges, one in and one out. So at an even vertex there is always an unused edge to leave by, and the walk can only get stuck at the other odd vertex (or, when every degree is even, back at its start). The unused edges then meet every vertex an even number of times, which is why they always close into loops.

This is not just a puzzle. Snow plows, street sweepers and mail carriers face the same question every day: how to cover every street without driving along any street twice.

Key ideas

  • A walk can repeat anything, a trail never repeats an edge, a path never repeats a vertex, and a cycle is a path that returns to its start. Length counts edges.
  • The distance between two vertices is the length of a shortest path. Every walk hides a path.
  • A graph can split into connected components. A bridge is an edge on no cycle, and removing it splits the graph further.
  • A connected graph has an Euler trail exactly when zero or two vertices are odd, and Hierholzer's algorithm builds it.

Check yourself

1. In the running graph, is A, B, C, A, B, D a walk, a trail or a path?

2. What is the distance from C to F in the running graph?

3. Remove the edge AC from the house. Can you still draw it in one stroke?

Practice on the platform

The introduction to graph theory guide covers routes and connectivity, and the Eulerian path guide goes deeper into Euler trails. Then open the Eulerian path visualizer, draw the house and let the algorithm trace it. It checks the degrees first, then follows Hierholzer's method edge by edge until all eight lines are drawn: A, B, C, D, A, C, E, D, B.

下一课

课程内容

已看 0 / 20

第 1 章Graph Theory Foundations

第 2 章Exploring a Graph请先完成第 1 章测验

第 3 章Shortest Paths请先完成第 2 章测验

第 4 章Connecting Cheaply请先完成第 3 章测验

第 5 章Hard Problems请先完成第 4 章测验

第 6 章Network Flows请先完成第 5 章测验