5

Let's say that there is one vertex with the following property in a DAG:

  1. All vertices are connected to it

  2. It is not connected to any vertex

This is usually called a sink vertex.

Is it possible to detect this vertex in O(n), where n is number of vertices in graph?

nbro
  • 15,395
  • 32
  • 113
  • 196
Rohan Monga
  • 1,759
  • 1
  • 19
  • 31

3 Answers3

5

As there are no cycles in the graph, and all vertex connect with the sink, just select any starting node and start walking randomly. When you can't continue walking, you are at the sink, in at most n steps.

Once you walked n steps (or less and you can't continue), as the problem does not guarantee that there is a sink, you should check if you are at one. That adds another O(n). So the problem is O(2 n) = O(n)

nbro
  • 15,395
  • 32
  • 113
  • 196
Dr. belisarius
  • 60,527
  • 15
  • 115
  • 190
  • As Jason said its O(n+m) not n. – Saeed Amiri Nov 09 '10 at 19:42
  • It's not guaranteed that a unique sink exists--the DAG might be disconnected. This method won't tell you whether all vertices are connected to the node you find. – Heatsink Nov 09 '10 at 19:43
  • If disconnected then all vertices can't be connected to it, can they ? – Loïc Février Nov 09 '10 at 19:44
  • EVERY vertex in connected to the sink. This arrives sooner to the sink if the graph has less edges. You can't stop at any vertex that is not the sink, because that vertex IS connected to the sink by definition. – Dr. belisarius Nov 09 '10 at 19:45
  • @SaeedAlg I need to traverse at most n edges, selecting any (the first) edge at each vertex I visit. If you assume that selecting the first edge on a vertex is costless, it's O(n) – Dr. belisarius Nov 09 '10 at 19:51
  • @Loïc Février The problem description says there is _at most_ one vertex satisfying the conditions. It's possible that no such vertex exists. – Heatsink Nov 09 '10 at 19:51
  • @Heatsink So if you walked n steps and you are no at the sink, it doesn't exist (there are no cycles) – Dr. belisarius Nov 09 '10 at 19:53
  • @belisarius Counterexample: a <- b <- c -> d -> e. A walk would stop at a or e, but these are not sinks because they're not reachable from every vertex. – Heatsink Nov 09 '10 at 19:57
  • @Loïc Février After you stop, you can check if you are at a sink in O(n), so the problem is still O(n). See edit please – Dr. belisarius Nov 09 '10 at 20:01
  • @belisarius How to check it in O(n). I can think of a BFS with inverted adjacency list. Do you have a better solution ? – Loïc Février Nov 09 '10 at 20:02
  • I agree with the new solution. – Heatsink Nov 09 '10 at 20:03
  • @Loïc Février I think you only need to check the IAL for that node, no searching ... but perhaps I'm answering too quickly. Do you see any problem with that? – Dr. belisarius Nov 09 '10 at 20:16
  • @Loïc Février inverted adjacency list. – Dr. belisarius Nov 09 '10 at 20:25
  • @belisarius If fact with a DAG the adjacency list is often not complete : think a->b, b->c c is a sink. Then in inverted c->b, b->a. The IAL is not sufficient you need BFS/DFS. – Loïc Février Nov 09 '10 at 20:27
  • @Loïc Février Not in this case. The sink is connected to ALL the nodes – Dr. belisarius Nov 09 '10 at 20:29
  • By connected I read "there is a path", not "there is an edge". Otherwise one would just check all nodes connected with nothing (can find them in O(n)) and then check in the IAL it has n-1 ancestors. – Loïc Février Nov 09 '10 at 20:34
  • @Loïc Février You are right. I am thinking whether a better way to check that exists. – Dr. belisarius Nov 09 '10 at 20:51
2

The best I can think of is O(n + m) which is O(n) if m is O(n).

Assuming a sink exists, do a topological sort of the graph. The minimal node in the sort is a sink. Note that topological sort is O(n + m).

I have previously provided an implementation here which can easily be modified for this problem.

Community
  • 1
  • 1
jason
  • 236,483
  • 35
  • 423
  • 525
  • topological sort in this case is O(n+m) if the graph representation is by Adjacency list. – Saeed Amiri Nov 09 '10 at 19:32
  • You can update your answer : to check if it's a sink (there might be no sink in this graph), invert the adjacency list and not a DFS and count the number of nodes reached. – Loïc Février Nov 09 '10 at 20:01
  • You need first to know and how to do (implement) a "find topological sort algorithm" – nbro Jul 27 '15 at 19:01
0

Provided you can count the number of edges in/out of a node in linear time, it's possible. First, find the vertices that have no outgoing edges (O(n) to scan all nodes). Your conditions are satisfied only if there's exactly one such vertex. Then, count its incoming edges (O(n) to scan all input edges). Your conditions are satisfied if there are exactly n-1 incoming edges. If either test fails, there's no sink vertex.

I'm assuming by "connected" you mean "connected by an edge", not "reachable by a path".

Heatsink
  • 504
  • 2
  • 3