Basis Lab

Answers · Making it fast enough

How do I find out if a graph is really in two separate pieces?

In shortWalk from one point, mark everything you can reach, then start again at any unmarked point; the number of walks is the number of separate pieces.

A graph is in two separate pieces exactly when one walk from one point cannot reach every point. Walk from any point until nothing new is reachable, mark every point it reached, then start a fresh walk at any still unmarked point; each walk discovers one piece, so the number of walks is the number of pieces.

A six point graph

Take six points a,b,c,d,e,fa,b,c,d,e,f. In the first three, link aa to bb, bb to cc, and cc to aa. In the second three, link dd to ee and ee to ff. There is no link between any of a,b,ca,b,c and any of d,e,fd,e,f. Start at aa with the pile holding just aa. Pop aa, mark it seen, and push its neighbours bb and cc, so the pile is [b,c][b,c]. Pop cc, mark it seen, and push its unseen neighbour bb, so the pile becomes [b,b][b,b]. Pop bb, mark it seen, find no unseen neighbours, leaving pile [b][b]. Pop the remaining bb, see that it is already seen, and skip it. The pile is empty. This walk reached a,c,ba,c,b, three points. The point dd was never seen, so start a second walk at dd. Pop dd, mark it, push ee. Pop ee, mark it, push ff. Pop ff, mark it, and stop with the pile empty. This walk reached d,e,fd,e,f, another three points. No unvisited point remains. There were two starts, so there are two pieces.

FIG 1
piecepointslinks insidelinks to the other piece
firsta, b, ca-b, b-c, c-a0
secondd, e, fd-e, e-f0
\[ \text{first walk from } a = \{a,b,c\},\quad \text{second walk from } d = \{d,e,f\},\quad 3+3=6 \text{ points, } 2 \text{ starts.} \]

The arithmetic of the count is the whole test. The first walk processed three points, the second processed three points, and 3+3=63+3=6 is every point in the graph. No point was processed twice. The five links were looked at when their endpoints were processed, so the total work is one pass over six points and five links, not one full search for each piece. If the first walk had reached all six points, the fresh-start loop would have found no unvisited point afterwards and the number of starts would be one. That is what it means for the graph to be in one piece.

Why looking is not enough

The natural guess is that separate pieces are obvious: a lonely point, a gap in a drawing, or a cluster that looks detached. That is not a test. Two large, busy clusters can each have many links inside them and share no link at all. In the example there are six points and five links, and the graph is in two pieces; a connected graph on six points can also have five links, namely a tree. So neither the number of links nor the busyness of a region tells you the number of pieces. A split is a reachability fact. The only reliable method is to exhaust one walk until it stops, then look for a point it never marked, and start again there.

Directed links change the question

If the links are directed, a walk from aa reaches only the points that aa can reach. Two points can sit in one undirected piece and still fail to reach each other, so 'same piece' needs a direction. The fresh-start procedure then counts out-reachable regions from the chosen starts, not a symmetric partition. For strongly connected pieces you need a forward walk and a backward walk, and you count the points that both reach. With undirected links the pieces are symmetric, and the fresh-start count gives a true partition.

A graph with no unvisited point after the first walk is in one piece. A graph with one unvisited point after the first walk is in two pieces, because the second walk will reach that point and everything reachable from it. If a third unvisited point remains after the second walk, the count goes to three. The number of fresh starts is the number of pieces, and each start is forced by a point that no earlier walk could reach.

Worth keeping

  • A graph is in two pieces exactly when no walk from one point can reach the other piece.
  • Starting a new walk at every unvisited point counts the pieces in one pass over the graph.
  • Looking for a lonely point or counting links cannot prove a graph is connected; only exhausting a walk can.

Where this goes next

This question is one step of Graphs and Finding a Path, a course that works through its subject from the beginning. The page that teaches this part of it is Following One Path to the End, and it reads the whole way through whether or not you have an account.

What an account changes is the practice: problems standing under every idea, marked with a reason rather than a tick, in a copy of the course that is yours.

Read that pageThe whole course

Other questions people ask about this

  1. Why doesn't breadth-first search give the cheapest path once the links have different costs?Breadth-first search counts links rather than costs, so it can return a route with fewer links that is more expensive than a longer route.
  2. Why does breadth-first search always find the path with the fewest links?Breadth-first search meets points in order of how many links they are from the start, so the first time it reaches a point it has used the fewest possible links.
  3. How do I know when there is no order that satisfies all the dependencies?If you repeatedly remove anything with nothing still waiting on it and cannot remove everything, the dependencies contain a loop, so no valid order exists.
  4. Why is it safe to keep taking the cheapest link that doesn't create a loop?Because the cheapest link joining two separate pieces is the cheapest way across a split, and swapping it into any rival solution cannot raise the total cost.
  5. Why is O(2n) treated the same as O(n)?Because it describes how cost grows rather than exact cost, doubling every cost does not change the growth class.
  6. How does a hash function turn a word into an array index?A hash function reads the word as bytes, mixes them into one large number, then takes the remainder after dividing by the table size to get an index from zero to size minus one.

Read alongside