For a graph, after finding the strongly connected components, how do find the number of SCCs that have a path to each other? I want to find if there is a path to SCC2 from SCC1.
-
1what is your attempt and why do you think it is problematic? actually, the answer seems obvious to me. – ram Nov 04 '17 at 22:29
-
My approach is to run a DFS on a vertex in SCC1. I found it problematic because , not all SCC might have a path to one another. And I want to know how I can reach to a maximum number of SCC, if I start in one. – AryaStark Nov 04 '17 at 22:49
-
Just to make sure: is it a directed graph? And if so, what do you mean by two SCCs are connected? Do they have to be reachable from each other? – ram Nov 04 '17 at 22:53
-
Yes, it's a directed graph. I meant if I can reach one SCC from another. They don't have to reachable, but how do find if they are or are not? – AryaStark Nov 04 '17 at 23:07
-
Is it "I want to find if there is a path to SCC2 from SCC1" or "I want to find if there is a path to SCC2 from SCC1 AND a path to SCC1 from SCC2"? – ram Nov 04 '17 at 23:09
-
The first, if there is a path from one SCC to another SCC – AryaStark Nov 04 '17 at 23:51
3 Answers
You asked two things:
how do find the number of SCCs that have a path to each other?
You can run dfs from every SCC and save what are the SCC that you can reach.
For example: You run dfs from SCC A and you can reach SCC B and C. (Just check what is the SCC of the node that you are visiting) Then you run dfs from another SCC D and you reach SCC A. At this time you can stop your dfs because you have already calculate what are the others.
So the time complexity is O(n+m)

- 51
- 6
Assuming that you only need know to whether a SCC is reachable from another or not (true/false), you can assign an identity to each SCC while you discovered one and create a mapping for each node to which SCC_ID it belongs to. Then traverse the original graph again. If there exists an edge (u,v)
where SCC_ID(u) != SCC_ID(v)
then a SCC is reachable from another.
A similar type of question : Kosaraju's Algorithm for finding SCCs but keep track of edge between SCCs?

- 71
- 3
While finding SCC using Kosaraju (Because it's easy to implement) assign each SCC discoverer also known as representative a unique id and for each SCC, assign its id to all its members. This will help remember which member belongs to which SCC. Now this information can help us to compress this graph into a graph whose nodes are each SCC (representative ID). While building this compress graph you will know which SCC is connected to which one.
You can refer to this link for more detail along with implementation: https://cp-algorithms.com/graph/strongly-connected-components.html.

- 75
- 9