Part I · Chapter 3

Ripples

How far apart are two things in a network? Not in metres, but in links. Drop a pebble on one node and let the ripple spread outward ring by ring: that ripple is one of the most useful ideas in computing, and it finds the shortest way to everything at once.

Someone asks: “Do you know anyone who knows the mayor?” Without realising it, you start a search. First you think of everyone you know directly. If the mayor isn’t among them, you think of the people they know. Then the people those people know. Your search spreads outward from you in rings, nearest first.

That habit of mind has a precise version, and it starts with two words. A path is a chain of links you can follow from one node to another, like stepping stones. The length of a path is its number of links. The distance between two nodes is the length of the shortest path between them: your friend is at distance 1, a friend of a friend (whom you don’t know yourself) at distance 2, and so on.

A search shaped like a ripple

To find every distance from one starting node, you can do exactly what the pond does. The start is ring 0. Everything linked to it is ring 1. Everything linked to ring 1 that you haven’t met yet is ring 2. Keep going until the ripple reaches nothing new. Computer scientists call this breadth-first search, or BFS: it explores the whole breadth of one ring before going one step deeper.

Interactive 3.1

The BFS ripple

Click a node to drop the pebble there, then press Step or Play. Switch to “Destination” and click a node to see its shortest path home.

the ripple’s front (current ring)reached, labelled with distancenot reached yet
Clicking a node sets the
Each ring holds exactly the nodes at that distance, so the label on a node is its distance from the start. The little cluster on the right is never reached: it is a separate piece of the network. Turn on the tree to see the link by which the ripple first reached each node; following it backwards always gives a shortest path.

Notice what the ripple gives you for free. The first time it touches a node, it has arrived by a shortest route, because every shorter route would have been explored in an earlier ring. So one ripple from a start node computes its distance to every other node at once, and the links along which it first arrived form a tree of shortest paths. Ask for any destination and you can read the route straight off that tree.

Key idea

Breadth-first search visits nodes in order of their distance from the start, so the first time it reaches a node, it has found a shortest path to it.

A closer look: the waiting line

A computer runs the ripple with a queue, a first-in, first-out waiting line. Put the start in the line. Then repeatedly take the person at the front, and add each of their not-yet-seen neighbours to the back. Because everybody from ring 1 joins the line before anyone from ring 2, the rings come out in order automatically. Swap the queue for a stack (last in, first out) and you get a different search, depth-first search, that plunges down one corridor before trying the next. It still visits everything, but its first arrival is not always by a shortest route.

A maze is a network

Here is the same idea wearing a disguise. Every square of a maze is a node. Two neighbouring squares are linked if there is no wall between them. Finding the way out is now a question about distance in a network, and the ripple solves it: start at the entrance, spread ring by ring, and stop when the ripple touches the exit.

Interactive 3.2

Maze as a network

Press Solve to send a ripple from the entrance. Click between two squares to add or remove a wall (that cuts or adds a link).

A freshly made maze here has exactly one route between any two squares (it is a tree, more on those in Chapter 4). Knock down a few walls and loops appear; the ripple still finds the shortest route, often a very different one. Wall off the exit completely and the ripple reports, correctly, that no way out exists.

How big is a network, in steps?

Once you can measure distance, you can describe a whole network by it. Two numbers are especially useful:

If some nodes cannot reach each other at all, the network falls into separate pieces called components, like the small cluster in the first figure. Whether a network holds together in one big piece is a surprisingly dramatic question, the subject of Chapter 5. And the average path length of real social networks turns out to be strikingly small, the subject of Chapter 6.

Key idea

Distance in a network counts links, not metres: two nodes far apart on the page can be one link apart, and two neighbours on the page can be unreachable from each other.

Intuition check

A breadth-first search from node A first reaches node B in ring 4. What can you conclude for certain?

What you now see

  • A path is a chain of links; the distance between two nodes is the number of links in the shortest one.
  • Breadth-first search spreads like a ripple, ring by ring, and finds the distance to every node at once.
  • The links the ripple first arrives by form a tree of shortest paths.
  • Mazes, social circles and computer networks are all searched the same way; when the ripple cannot reach a node, the network is in separate components.