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.

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

How it works

Gabow's algorithm finds the strongly connected components in one depth-first search, as Tarjan's does, but without lowlink values. It keeps two stacks. The first holds the vertices the search has reached that aren't in a component yet. The second holds the vertices on the current path that could still be the first vertex of a component.

When an edge leads to a vertex still on the first stack, every vertex on the path after it is in the same component, so the second stack is popped back to it. When the search finishes a vertex that is still on top of the second stack, that vertex starts a component. The first stack is popped down to it, and those vertices form the component.

It costs O(V+E). On the same graph, Tarjan's and Gabow's algorithms find the same components in the same order. They differ only in how they tell that the search has finished one.

Microservice calls

Nine services of an online shop. Each edge is a call from one service to another, and some services call each other back.

A directed graph with 9 vertices and 15 edges.

Which groups of services call each other in a loop, so that each service in a group depends on all the others?

The graph has 5 strongly connected components. The ones with more than one vertex are Auth and Users; Inventory and Search; Orders, Payments and Shipping. The other 2 components have one vertex each: Notify and Gateway.

Step by step

  1. Gabow's path-based algorithm searches depth-first from each vertex no earlier search has reached, in the graph's vertex order, and numbers the vertices in the order it reaches them. Stack S holds every vertex reached but not yet placed in a component. Stack P holds the vertices on the search path that can still be the root of a component. An edge to a vertex still on S closes a cycle, so P loses every vertex numbered above that vertex. When a vertex finishes on top of P, it is the root of a component: it and the vertices above it on S form one strongly connected component.
  2. Start at Gateway, the first vertex. Gateway gets number 0 and goes on S and P.
  3. Follow Gateway to Auth. Auth gets number 1 and goes on S and P.
  4. Follow Auth to Users. Users gets number 2 and goes on S and P.
  5. Users has an edge to Auth, which is still on S with number 1. That closes a cycle through Auth, so every vertex on P numbered above 1 is in the same component as Auth and can't be its root. Pop Users off P.
  6. Follow Users to Notify. Notify gets number 3 and goes on S and P.

16 more steps lead to the last one:

  1. Done. Gabow found 5 strongly connected components, 2 of them with one vertex. Each component is listed after every component it has an edge into. Every vertex now shows the component it belongs to.

Use it in Go

for _, component := range connectivity.Gabow(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.
  • Condensation: Replaces each strongly connected component with one vertex. What is left is a DAG, so it can be sorted and scheduled.
  • 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.
  • 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.
  • Find a cycle: Returns the vertices of one cycle in order: the first one a depth-first search closes with an edge back to its search path.
  • Bridges and articulation points: Finds the edges and the vertices whose loss splits an undirected graph into more pieces, its single points of failure, from one depth-first search.

All algorithms