0
$\begingroup$

I got the following question from my academic instructor and since I am new to graph algorithms I would be happy if someone could review my solution.

Given undirected graph $G = (V,E)$, suggest an algorithm that determines whether or not it is possible to assign directions to edges in a way that the indegree of each vertex will be greater than zero.
If the answer is positive the algorithm should also return the orientation of the edges for making indegrees greater than zero.

First of all, we assume that the graph G is connected; otherwise, we work with the connected components separately. My solution is based on the following 2 theorems:

  1. In every DAG G, there is a node v with no incoming edges.
  2. Let G be a connected undirected graph, If G contains a cycle then we can assign directions to the edges such that the indegree of each vertex is greater than 0.

Proof for 1st theorem:
Let G be a directed graph in which every node has at least one incoming edge. We show how to find a cycle in G; this will prove the claim. We pick any node v, and begin following edges backward from v: since v has at least one incoming edge (u, v), we can walk backward to u; then, since u has at least one incoming edge (x, u), we can walk backward to x; and so on. We can continue this process indefinitely, since every node we encounter has an incoming edge. But after n + 1 steps, we will have visited some node w twice. If we let C denote the sequence of nodes encountered between successive visits to w, then clearly C forms a cycle. (Source: Algorithm Design by Jon Kleinberg, Éva Tardos)

Proof for 2nd theorem:
Let G be a connected undirected graph, suppose G has a cycle $C = (v_1,v_2,...,v_k)$, therefore for each vertex, starting from $v_1$ we will assign the direction of the edges to be from the current vertex to its proceeding vertex. (i.e: $<v_1,v_2>, <v_2,v_3> ,...<v_{k-1},v_k>)$.
In case that there is more than one cycle in the graph G we will act on it in the same way.
Since C is now a directed cycle we can claim that the indegree of each vertex in C is at least one.
Let $v_i \in C$ be an arbitrary vertex in C, from the assumption that G is a connected undirected graph, we can say that for each vertex $u \in V \setminus C$ there is a path from $v_i$ to $u$ we will assign the directions for the edges on this path so they create a directed path from $v_i$ to $u$, we will act like that for each one of the remaining vertices and in the end, we will obtain a directed graph which satisfies the condition that indegree of each vertex is greater than zero.

Now for the algorithm:

  1. Choose an arbitrary vertex $v$ and run DFS starting from that vertex (at this stage we will not assign directions to the edges).
  2. If the DFS from step 1 finds a vertex $u$ which is already marked as "explored" in a previous iteration (i.e DFS is traveling on a back edge $e$), we can conclude that G contains a cycle and therefore can be directed as we want (and we are saving that vertex $u$ and the edge $e$), otherwise G is a tree and by theorem 1 cannot be directed as we want.
  3. Now we are running DFS again but this time from vertex $u$ (which is part of a cycle) that we found previously and this time with each iteration of the DFS we are assigning directions to the graph inside-out as we are going deeper.
  4. At the end of the DFS from step 3, each vertex in G will have an indegree of 1 except for vertex $u$, luckily we know that edge $e$ we found earlier can be directed in a way that will contribute 1 to the indegree of $u$, we will assign direction of $e$ so it will contribute 1 to the indegree of $u$.
  5. All other undirected edges can be directed arbitrarily.

My main concern is that the 2nd theorem might be wrong (?) and therefore the algorithm might be not true as well. Any insights about it will be greatly appreciated. Thanks

$\endgroup$
5
  • 1
    $\begingroup$ We discourage "please check whether my answer is correct" questions, as only "yes/no" answers are possible, which won't help you or future visitors. See here and here. Can you edit your post to ask about a specific conceptual issue you're uncertain about? As a rule of thumb, a good conceptual question should be useful even to someone who isn't looking at the problem you happen to be working on. I've offered this feedback before: cs.stackexchange.com/q/150521/755 $\endgroup$
    – D.W.
    Commented Nov 9, 2022 at 19:07
  • $\begingroup$ @D.W. Yes, you're right, it's my bad. Thanks for the clarification. I will edit my question and emphasize the things that I'm uncertain about. $\endgroup$
    – Yarin
    Commented Nov 9, 2022 at 20:29
  • 1
    $\begingroup$ Take any spanning tree of the graph. If there are no non-tree edges then the number of edges is $n-1$ and hence it's impossible to find the sought orientation. If there is at least one non-tree edge $(u,v)$ direct all tree edges away from $u$, and direct $(u,v)$ towards $u$. The direction of the remaining edges does not matter. $\endgroup$
    – Steven
    Commented Nov 9, 2022 at 21:32
  • 1
    $\begingroup$ Theorem 2 is correct, although the proof is a bit sloppy. My previous comment also provides a proof of Theorem 2: a connected cyclic graph has at least $n$ edges, therefore a non-tree edge always exists. $\endgroup$
    – Steven
    Commented Nov 9, 2022 at 21:38
  • $\begingroup$ @Steven Thank you Steven $\endgroup$
    – Yarin
    Commented Nov 10, 2022 at 6:06

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.