Condensation

Replaces each strongly connected component with one vertex. What is left is a DAG, so it can be sorted and scheduled.

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

How it works

A strongly connected component is a group of vertices that can all reach each other. The condensation replaces each one with a single vertex, and all the edges from one component to another with a single edge. A cycle never leaves its component, so the condensed graph has none.

connectivity.Condense runs Tarjan once and numbers the components so that every edge between two of them goes from a lower number to a higher one, which is a topological order. The members of each component keep the graph's vertex order, and Members and ComponentOf map between the two graphs. It costs O(V+E).

WithComponentWeight gives each component a weight from its members, such as the sum of their weights, and WithCrossingWeight gives each condensed edge a weight from the edges it stands for. A build tool does this with packages that import each other: each loop is built as one unit, and the units are sorted like any other dependency graph.

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 services call each other in a loop and have to be released together, and which way do the calls between those groups go?

The 9 vertices condense into 5 components joined by 7 edges, with no cycle left. The largest component has 3 vertices: Orders, Payments and Shipping. The components are listed in topological order: every edge between two of them goes from an earlier one to a later one, so in reverse, each component comes after every component it has an edge into.

Step by step

  1. Condense runs Tarjan once to find the strongly connected components, the groups of vertices that can all reach each other. It numbers them so that every edge between two components goes from a lower number to a higher one. Then each component becomes one vertex, and the edges from one component to another become one edge.
  2. Component 1: Gateway reaches no vertex that reaches it back, so it is a component on its own.
  3. Component 2: Orders, Payments and Shipping can all reach each other, so they become one vertex. The 3 edges among them drop out.
  4. Component 3: Inventory and Search reach each other, so they become one vertex. The 2 edges among them drop out.
  5. Component 4: Auth and Users reach each other, so they become one vertex. The 2 edges among them drop out.
  6. Component 5: Notify reaches no vertex that reaches it back, so it is a component on its own.

3 more steps lead to the last one:

  1. Done. The 9 vertices condense into 5 components and 7 edges between them. The condensed graph has no cycle, and its vertices are listed in topological order.

Use it in Go

sum := func(members []*gograph.Vertex[string]) float64 {
	var total float64
	for _, v := range members {
		total += v.Weight()
	}
	return total
}
c, err := connectivity.Condense(g, connectivity.WithComponentWeight(sum))
if err != nil {
	return err
}
for i, members := range c.Members {
	var names []string
	for _, v := range members {
		names = append(names, v.Label())
	}
	fmt.Println(i, strings.Join(names, ", "), c.Graph.GetVertexByID(i).Weight())
}
for _, e := range c.Graph.AllEdges() {
	fmt.Println(e.Source().Label(), "->", e.Destination().Label())
}

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.
  • 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.

All algorithms