17

I have a set of nodes and set of directed edges between them. The edges have no weight.

How can I found minimal number of edges which has to be added to make the graph strongly connected (ie. there should be a path from every node to all others)? Does this problem have a name?

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
Jan Langer
  • 657
  • 1
  • 6
  • 17

3 Answers3

33

It's a really classical graph problem.

  1. Run algorithm like Tarjan-SCC algorithm to find all SCCs. Consider each SCC as a new vertice, link a edge between these new vertices according to the origin graph, we can get a new graph. Obviously, the new graph is a Directed Acyclic Graph(DAG).
  2. In the DAG, find all vertices whose in-degree is 0, we define them {X}; find all vertices whose out-degree is 0, we define them {Y}.
  3. If DAG has only one vertice, the answer is 0; otherwise, the answer is max(|X|, |Y|).
Jun HU
  • 3,176
  • 6
  • 18
  • 21
  • I am not sure I understand the solution. Consider the graph with edges `(a,b),(b,e),(e,b),(a,c),(c,f),(f,c),(b,d),(c,d)`. The resulting DAG is `(A,B),(A,C),(B,D),(C,D)`, with `B={b,e}` and `C={c,f}`. Hence `X=Y={B,C}` so `|X|=|Y|=2`. However, clearly, we can just add the single edge `(d,a)` to the original graph to make it strongly connected. – mitchus Jan 14 '13 at 22:54
  • With in-degree set to 0, the algorithm fails for the following graph: `(a,b),(b,d),(d,b),(a,c),(c,e),(e,c)` which results in DAG `(A,B),(A,C)`. There `max(|X|,|Y|)=1` but we need two edges to make the graph strongly connected. – mitchus Jan 15 '13 at 07:38
  • @mitchus in the new DAG, A's in-degree is 0, B and C has out-degree which is 0. So |X| = 1, |Y| = 2, and answer is 2. – Jun HU Jan 15 '13 at 09:40
  • How can we find required minimum edges (any if possible)? – Sanjit Prasad Aug 23 '19 at 08:13
  • Can someone explain why max(|x|, |y|) gives the answer? – router Dec 21 '22 at 07:07
  • One answer to my comment above is: Try to see DAG as a topological sort with a beginning nodes having indegrees==0 and last nodes having outdegrees=0. Now try to connect them – router Dec 21 '22 at 07:48
1

Off the top of my head, it seems the simplest (fewest edges) way to make a directed graph strongly connected would be to just have a cycle involving all nodes; so the minimum number of edges would just be N where N is the number of nodes. If there are already edges, just do something like connect longest existing directed path to the next longest path that doesn't overlap with your current path, until you form a complete cycle (once your path contains all nodes, connect the ends to form the cycle.)

Not sure if there is a more formal definition of any of this, but is seems logical to me.

Josh Buell
  • 606
  • 5
  • 12
1

I would find all weakly connected components, and tie them up in a cycle.

EDIT:

To be more explicit, the idea is if you have WCCs W(1),...,W(n), make all of W(i%n + 1) reachable from any node in W(i), for i=1 to n.

mitchus
  • 4,677
  • 3
  • 35
  • 70
  • This is incorrect. Consider the digraph `a -> b`. That has two SCCs, but one WCC, so your algorithm wouldn't do anything. – Fred Foo Jan 14 '13 at 13:22
  • @larsmans In this case it would tie the single WCC up in a cycle, i.e. add the edge `b -> a`. I will add more detail to make it more explicit. – mitchus Jan 14 '13 at 15:53
  • I'm sorry, but I don't really understand what you mean by this. – Fred Foo Jan 14 '13 at 16:01
  • @larsmans Do you disagree with some specific part of the answer? The answer by Jun HU is much better anyway :) – mitchus Jan 14 '13 at 16:19