Graphs · Exercise 49 of 100

Count strongly connected components

In a directed graph, a strongly connected component is a group where every vertex can reach every other vertex by following arrows. Make each group as large as possible while keeping that two-way reachability. A vertex can form a component on its own.

Try it yourself

A points to B, B to C, C to A, C to D, D to E, E to D and E to F. These are all the arrows. How many strongly connected components are there?

One-step hint

Group vertices that can make a round trip to each other. Check the arrows out of C and E for possible return routes.

Worked solution
  1. A, B and C can reach each other around their loop. D and E can also reach each other, but neither can return to the first loop.
  2. F has no outgoing arrow and forms a group by itself. The two loops and the singleton give 3 components.
DSA compiler