0

A directed graph is said to be uniquely connected if there exists exactly one path between every pair of vertices. How to identify whether a graph has this property or not? This needs to be done in order O(n+m), where n are the number of vertices of the graph and m are the edges.

It is quite clear that there shouldn't be any cross-edges or forward-edges in the graph. But what about back-edges?

Ranveer
  • 6,683
  • 8
  • 45
  • 92
  • Running depth-first search, once you find the first path between every pair of vertices, you could push into a queue those said edges in the order you traversed them, and then check subsequent depth-first searches by the queue. Something like that could potentially work. – David Nov 12 '13 at 17:23
  • @David I didn't get what you mean by "check subsequent dfs's by the queue". Can you please clarify? – Ranveer Nov 12 '13 at 17:45
  • duplicate: http://stackoverflow.com/questions/19931851/singly-connected-graph – Tom Swifty Nov 12 '13 at 20:36
  • "exactly one path between every pair of vertices": For x and y, does this mean exactly one path either from x to y OR y to x, or exactly one path from x to y AND exactly one path from y to x? – Chris Okasaki Nov 13 '13 at 00:42
  • @ChrisOkasaki Since it is a directed graph, a path from X to Y wouldn't be the same as Y to X. So there needs to be a path between both. – Ranveer Nov 13 '13 at 06:01
  • Are you sure about the terminology here? There is something called a [singly connected network](http://en.wikipedia.org/wiki/Polytree), but it would be more accurately defined as having no pair of vertices for which there are two or more paths between them. – Michael J. Barber Nov 14 '13 at 11:29
  • This is different from singly connected as it requires exactly two paths between each pair of vertices. Say the vertices are u and v. In singly connected, we are fine with an edge from u to v but no edge from v to u. But in this case, we need both the edges to be present. – Ranveer Nov 14 '13 at 13:23

1 Answers1

3

If there is exactly one directed path between every pair of nodes, then

  • every node must have at least one out-edge (else no paths from that node to other nodes)
  • no node can have have more than one out-edge (if there is an edge from X to Y and an edge from X to Z, and there are paths from Y to T and from Z to T, then there are multiple paths from X to T)

But now, with every node having exactly one out-edge, and every node being reachable from every other node, the graph must be a single directed cycle.

That is trivial to check in O(n) time.

Edit: As Erik P notes in the comments, this argument only applies if the paths in question are simple paths. In the same spirit, a graph of size 3 may need special treatment, because the X-Y-Z-T reasoning above doesn't apply, which means a graph with nodes X,Y,Z and edges from X to Y and Z, and from Y and Z to X would be legal.

Chris Okasaki
  • 4,875
  • 1
  • 20
  • 19
  • Every node having one out edge is a necessary condition. But is it sufficient? How to you prove that the graph is connected? Remember, the whole thing has to be done in O(n+m). – Ranveer Nov 13 '13 at 12:26
  • No, every node having one out-edge is not sufficient. But all you need to check is if the graph is one giant directed cycle, which can be done with DFS, although you could customize and simplify the DFS in this specific case. – Chris Okasaki Nov 13 '13 at 16:48
  • So will that be O(n+m)? – Ranveer Nov 13 '13 at 17:44
  • One could argue that, if there is any cycle in the graph, then traversing the cycle multiple times gives different paths from any point in the cycle to any other point in the cycle. This would reduce the burden to checking whether the graph has at most one vertex, which is O(1). – Erik P. Nov 13 '13 at 20:56
  • @Ranveer: Yes, but m must be equal to n, so that simplifies to O(n). (Of course, you could get a graph where m was not equal to n, but then you could immediately return no.) – Chris Okasaki Nov 13 '13 at 20:57