AlgoFrames

Condensation graph

Standard · Write code · about 20 min

Shrink every strongly connected component of a directed graph to one node. Name each component by its smallest node number.

Return the edges of this condensation graph as [a, b] pairs of component names. List each pair once, in any order. Edges inside one component disappear.

Example 1

Input:

[{"n":6,"edges":[[0,1],[1,2],[2,0],[1,3],[3,4],[4,3],[4,5]],"directed":true}]

Output:

[[0,3],[3,5]]

Edge 1 → 3 joins components 0 and 3. Edge 4 → 5 joins 3 and 5.

Example 2

Input:

[{"n":4,"edges":[[0,1],[1,2],[2,3]],"directed":true}]

Output:

[[0,1],[1,2],[2,3]]

Every node is its own component, so every edge stays.

Constraints

  • 1 ≤ n ≤ 100
  • 0 ≤ number of edges ≤ 300
  • 0 ≤ u, v < n

Learn the idea first: Strongly connected components: Kosaraju and Tarjan, a visual lesson.

More Strongly connected components: Kosaraju and Tarjan problems