Connected components

Splits a graph into its connected pieces, the groups of vertices joined to each other by some path, with one search per piece.

Time complexity O(V+E), gograph function gograph.ConnectedComponents.

connectivity.ConnectedComponents returns the same components, because it calls gograph.ConnectedComponents.

How it works

Two vertices are in the same connected component when some path joins them. The components are the separate pieces of a graph: no edge runs between two of them, so nothing in one piece can reach anything in another.

gograph searches breadth-first from the first vertex and gives every vertex it reaches the same component. When that search runs out, it starts the next one at the first vertex no search has reached, and so on, so the components come out in the order of their first vertex. Every vertex and edge is looked at once, so it costs O(V+E).

On a directed graph, gograph ignores the edge directions and returns the weakly connected components. It joins the two ends of each edge with union-find instead of searching, since a vertex doesn't keep a list of the edges that end at it. To find the groups that can reach each other along the edge directions, use Tarjan, Kosaraju or Gabow. Use connected components to find the islands of a network after an outage, or to handle each piece of a graph on its own.

Locked maze

The maze with four doors locked: between B4 and C4, B5 and C5, B6 and C6, and C2 and D2. Each edge is a passage that is still open, so a walker can only reach the cells on their own side of the locked doors.

An undirected graph with 30 vertices and 27 edges.

With four doors locked, which cells of the maze can a walker still get between?

The graph falls into 4 connected components, with 8, 6, 10 and 6 vertices. No edge joins two of them, so each vertex can reach only the vertices in its own component.

Step by step

  1. ConnectedComponents searches breadth-first from each vertex no earlier search has reached, in the graph's vertex order. Every vertex a search reaches is in the same component as the vertex it started from. When the queue runs out, the component is complete.
  2. Start component 1 at A1, the first vertex. Put it in the queue.
  3. Take A1 from the front of the queue. Its neighbor A2 is new, so it joins the back of the queue.
  4. Take A2 from the front of the queue. Its neighbor A3 is new, so it joins the back of the queue.
  5. Take A3 from the front of the queue. Its neighbor B3 is new, so it joins the back of the queue.
  6. Take B3 from the front of the queue. Its neighbor B2 is new, so it joins the back of the queue.

33 more steps lead to the last one:

  1. Done. ConnectedComponents found 4 components. They come out in the order of their first vertex. Every vertex now shows the component it belongs to.

Use it in Go

for _, component := range gograph.ConnectedComponents(g) {
	var names []string
	for _, v := range component {
		names = append(names, v.Label())
	}
	fmt.Println(strings.Join(names, ", "))
}

Example graphs

More in Connectivity

  • Tarjan: Finds the strongly connected components of a directed graph, the groups of vertices that can all reach each other, with one depth-first search.
  • Kosaraju: Finds the strongly connected components of a directed graph with two depth-first searches, the second on the graph with every edge reversed.
  • Gabow: Finds the strongly connected components of a directed graph with one depth-first search and two stacks, without the lowlink values of Tarjan's algorithm.
  • Condensation: Replaces each strongly connected component with one vertex. What is left is a DAG, so it can be sorted and scheduled.
  • Cycle detection: Says whether a graph has a cycle, a way to follow its edges from a vertex back to the same vertex, and stops at the first one it finds.

All algorithms