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.

Import cycles

Eighteen packages of a Python web app. Each edge is an import, and some packages import each other in a loop.

A directed graph with 18 vertices and 30 edges.

Which packages have to be built together because they import each other, and in what order can the groups be built, each after the groups it imports?

The 18 vertices condense into 10 components joined by 14 edges, with no cycle left. The largest component has 5 vertices: auth, billing, invoices, session and users. 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. Components 1 to 4: main, api, middleware and handlers each reach no vertex that reaches them back, so each is a component on its own.
  3. Component 5: auth, session, users, billing and invoices can all reach each other, so they become one vertex. The 6 edges among them drop out.
  4. Component 6: email and templates reach each other, so they become one vertex. The 2 edges among them drop out.
  5. Component 7: cli reaches no vertex that reaches it back, so it is a component on its own.
  6. Component 8: models, db and cache can all reach each other, so they become one vertex. The 3 edges among them drop out.

9 more steps lead to the last one:

  1. Done. The 18 vertices condense into 10 components and 14 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