0

Give a graph:

Input:

0 -> 1
2 -> 1
3 -> 1

Representation:

0 -> 1 <- 2
     ^
     |
     3

This graph is not strongly connected because not every vertex u can reach vertex v and vice versa (path u to v and v to u)

The algorithm I am currently using for checking if the directed graph is strongly connected is applying DFS from each vertex O(n3), if I can find N-1 vertices from the N vertices, then the digraph is strongly connected.
Alternative algorithm is Tarjan's algorithgm.

But is this graph considered connected (not strongly) ? If yes, what would be a good algorithm to apply.

Thank you

Dominique Fortin
  • 2,212
  • 15
  • 20
CMPS
  • 7,733
  • 4
  • 28
  • 53
  • Why the question has been downvoted ? – CMPS Dec 16 '14 at 06:04
  • As it says it is "put on hold as too broad" in someones opinion so they down vote you, but this point are nothing(2 vote down is less that 1/1000 of your reps.) It would be least from you to read mine and @Khanna111's answers and leave a comment, no one expect you to upvote or mark anyone of them as you answer if they are not what you are looking for, but least you could do was to tell us why you are not satisfied with them. So we update our answer or do some other action, if you just have 1 rep, I would say you are not familiar with this site, but really I'm disappointed with people like you. – Lrrr Dec 17 '14 at 07:17

2 Answers2

2

If you want to figure out your digraph is strongly connected there are several algorithm for that and in wiki you can find this three:

If you want to check if your digraph is just connected or not, you can simply assume that it is a graph not a digraph then apply connected component algorithm with O(|V|+|E|). if it just have one connected component then your graph is connected.

Lrrr
  • 4,755
  • 5
  • 41
  • 63
1

The term "Connected (not strongly connected" is usually made out for undirected graphs. In your case the directed graph is not connected (strongly).

One of the algorithms that can evaluate if a digraph (directed graph) is strongly connected in O (V + E) time is named after Kosaraju who discovered it.

The wiki: http://en.wikipedia.org/wiki/Kosaraju%27s_algorithm is pretty instructive. I have implemented the algorithm and it is available here. Runs in O (v + E ). We are assuming that the graph is backed as an adjacency list.

Link for Kosaraju Implementation in Java

Try it out, hopefully you would find no bugs :-)

Khanna111
  • 3,627
  • 1
  • 23
  • 25
  • Thank you for sharing your code, I'll take a look at it. The best answer I found, which is also suggested by Ali is assuming the graph is not directed and run a DFS or BFS. – CMPS Dec 18 '14 at 15:31