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.
Time complexity O(V+E), gograph function connectivity.Tarjan.
How it works
A strongly connected component is a group of vertices in a directed graph where each one can reach every other. Tarjan's algorithm finds them all in one depth-first search. It numbers the vertices in the order it reaches them and keeps them on a stack. Each vertex also tracks its lowlink, the smallest number it can reach through the vertices below it in the search and one more edge to a vertex still on the stack.
When the search finishes a vertex whose lowlink is still its own number, nothing it leads to can reach back to an earlier vertex on the stack. That vertex and every vertex above it on the stack form one component, and they leave the stack together. So a component comes out before any component that can reach it.
gograph keeps the search path in a slice instead of recursing, so a long chain doesn't grow the goroutine stack. It costs O(V+E). Use it to find services that call each other in a loop, modules that require each other, or the states of a machine that can all reach each other.
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
- Tarjan searches depth-first from each vertex no earlier search has reached, in the graph's vertex order. It gives each vertex an index, the order the search reached it in, and a lowlink, the lowest index the vertex is known to reach among the vertices still on the stack. Every vertex goes on the stack when the search reaches it. When a vertex finishes with a lowlink equal to its own index, it is the root of a component: it and the vertices above it on the stack form one strongly connected component.
- Start at Gateway, the first vertex. Gateway gets index 0 and lowlink 0 and goes on the stack.
- Follow Gateway to Auth. Auth gets index 1 and lowlink 1 and goes on the stack.
- Follow Auth to Users. Users gets index 2 and lowlink 2 and goes on the stack.
- Users has an edge to Auth, which is still on the stack with index 1. The lowlink of Users drops from 2 to 1.
- Follow Users to Notify. Notify gets index 3 and lowlink 3 and goes on the stack.
16 more steps lead to the last one:
- Done. Tarjan 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.Tarjan(g) {
var names []string
for _, v := range component {
names = append(names, v.Label())
}
fmt.Println(strings.Join(names, ", "))
}
Example graphs
- Microservice calls, 9 vertices and 15 edges
- Import cycles, 18 vertices and 30 edges
- Linked web pages, 40 vertices and 114 edges
- Web crawl, 100 vertices and 274 edges
More in Connectivity
- 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.
- 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.