Graph theory: keeping track of connections
On the way from home to school, we might pass a shop, a park or a library. Which places can we reach directly? If there is no direct road, where can we go in between? To answer these questions, we can keep the places and their connections, and leave the appearance of the buildings aside.
Graph theory studies connections between objects. We represent objects by points and connections by lines; together, these form a graph. Here a graph is a record of connections, rather than the coordinate plot of a function. We will keep the same small map while we learn to describe and find routes.
Places become points and lines
There are six places below. The letters give us short names for them: is home and is school. A line means there is a direct road between its ends. For now, every road can be used in either direction. The length of a drawn line does not record a distance.
The points representing places are called vertices; the lines recording direct connections are called edges. Two places without an edge between them cannot be reached in one step, although a route through other places may still connect them. We have kept the information about direct connections on its own.
First select a place and look at its connected edges. Then use the edge buttons to add or remove one connection. The places keep their positions, so you can see exactly which relationship changes.
Each pressed button below means that direct road exists. Change one and inspect the diagram.
Count the connections
Now return to the original seven roads. Let us ask a small question: how many roads can we take directly from one place? The number of edges meeting a vertex is its degree. This example has no edge from a place back to itself, and no multiple edges between the same two places.
, for example, meets and , so its degree is . Each press of Next step below adds one edge and records one count at each end. Gold marks the edge just added; the table keeps all the counts already recorded.
Why must the total count be twice the number of edges? Every edge has two ends. Wherever those ends are, we count it once at one end and once at the other. Each added edge therefore adds to the total. After every edge has been processed, we get the equation below.
| A | B | C | D | E | F |
|---|---|---|---|---|---|
This is often called the handshaking lemma: count each handshake once for each of its two people. We derived it by counting edge ends, so it applies to any finite undirected graph of the kind described here, not just our seven-road example.
Follow a route
Direct connections alone do not answer our original question. Home has no direct road to school, but we can go through the shop and library. Following connected edges in order gives a walk. Here its length means the number of edges used: to uses one, then to uses one, then to uses one, giving edges.
We call a walk with no repeated vertex a simple path. In this simple undirected graph, a cycle starts and ends at the same vertex, repeats no other vertex, and uses at least three edges. For example, returns to the first after visiting each other place once.
Below, you may choose only a place directly connected to your current position. Try reaching school, then reset and return home. The numbered labels show the order of visits; if you visit a place again, the complete sequence remains below.
An undirected graph is connected when there is a path between every pair of vertices. Remove both roads touching school and school becomes separated from the other places. Even if the remaining five places can still reach one another, the whole graph is no longer connected.
A one-way road needs another piece of information: being able to go from to does not let us go back from to . An arrow records the permitted direction, giving a directed graph. Our search can follow arrows too, but reachability must then specify a starting place and a direction.
This drawing uses no arrows: every edge permits both directions. The route record holds up to 19 edges; reset to try another walk.
Search in an organised order
Trying routes without a record can bring us back to the same place repeatedly. We can put discovered places whose roads we have not yet examined into a waiting line. We process the earliest entry first. This record is called a queue: its front is on the left below, and new entries join on the right.
At first we know only the starting place. Mark it as steps away and put it in the queue. Next, remove the front place and inspect its outgoing connections one at a time. When we find a place not yet discovered, record where we found it from and how many steps reach it, then put it at the back of the queue. Do not add an already discovered place again.
Why add to the current distance? We already have a route to the current place. Following the edge just inspected extends that route by one edge. If the current place was reached in steps, the new one is recorded at steps.
This method processes a nearer layer before the next layer. It is called breadth-first search, or BFS. Use Next step to inspect individual edges and queue changes; the search never advances on its own. Grey means undiscovered, green means discovered, and gold marks the connection being inspected.
Available edges: A—B, A—C, B—D, C—D, D—E, E—F, C—F
Queue: front on the left, new entries at the right
| Place | A | B | C | D | E | F |
|---|---|---|---|---|---|---|
| Edges from start | — | — | — | — | — | |
| Found from | Start | — | — | — | — | — |
Finished checking: none
Why the number of steps is smallest
Now we need to explain why the recorded number of steps is the smallest possible. The starting place needs steps. Every neighbour of the start is discovered while we process the start, and needs step. The queue places all these one-step places ahead of any two-step place discovered later.
Continue this reasoning. When we process layer , the places whose shortest routes use edges, all closer layers have already been processed. An undiscovered neighbour can be reached in steps: use the known -step route, then follow one more edge.
Could a shorter route reach this new place? If it could, the place just before it on that shorter route would have a shortest distance smaller than . That place would already have been processed, including its connection to our new place. Our new place would therefore already have been discovered, contradicting its being undiscovered now. So the new record of is the smallest possible.
Starting with layer , this argument justifies every following layer. Each discovered place enters the queue only once, and there are finitely many places, so the queue eventually becomes empty. Any place still undiscovered is unreachable: on a supposed route to it, the first undiscovered place would have been discovered when its discovered predecessor was processed.
The quantity minimised here is the number of edges. If every road takes the same time, that also minimises time. Different times need different reasoning. A direct road might take minutes, while two roads through another stop each take minutes, giving minutes in total. A number such as travel time assigned to an edge is called its weight. BFS layers do not add these weights.
Check your understanding
Try answering before opening the worked explanations. Use the original seven edges, and return to the diagrams to check them one at a time.
Why is the degree of equal to , rather than the number of all places reachable from ?
Degree counts edges directly meeting the vertex. meets , and : three edges. Continuing along roads can reach other places, but that does not add direct connections to .
What is the fewest number of edges from home to school?
From , the first layer is . The second adds . One further edge from or reaches in layer three. Thus the minimum is , for example along . The absence of in the first two layers rules out a shorter route.
Add the previously absent edge . How does the sum of degrees change?
gains one edge and gains one edge, so each degree increases by . The others do not change. The old sum was ; now it is . There are now edges instead of , and still holds.
If the only arrow is , can we also go from to ?
No. The arrow permits travel only from its tail to its head. Going back requires another edge permitting that direction, or a directed route through other vertices.
