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.

Linked web pages

Forty pages of a product website, P1 to P40, where P stands for page: the main site (P1 to P13), the blog (P14 to P19), landing pages (P20 to P24), the help center (P25 to P28), the docs (P29 to P38) and the status pages (P39 and P40). Each edge counts the links from one page to another. Only ads and emails lead to the landing pages, and the help center, docs and status pages never link back to the main site.

A directed, weighted graph with 40 vertices and 114 edges.

Once each group of pages that link to each other in a loop counts as one page, which way do the links between the groups go?

The 40 vertices condense into 10 components joined by 11 edges, with no cycle left. The largest component has 18 vertices: P1, P2, P3, P4, P5, P6, P7, P8, P9, P10 and 8 more. 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 6: P24, P23, P22, P21, P20 and P19 each reach no vertex that reaches them back, so each is a component on its own.
  3. Component 7: P1, P2, P3, P4, P5, P6, P7, P8, P9, P10 and 8 more can all reach each other, so they become one vertex. The 53 edges among them drop out.
  4. Component 8: P39 and P40 reach each other, so they become one vertex. The 2 edges among them drop out.
  5. Component 9: P25, P26, P27 and P28 can all reach each other, so they become one vertex. The 9 edges among them drop out.
  6. Component 10: P29, P30, P31, P32, P33, P34, P35, P36, P37 and P38 can all reach each other, so they become one vertex. The 26 edges among them drop out.

8 more steps lead to the last one:

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