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:
- In every DAG G, there is a node v with no incoming edges.
- 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:
- Choose an arbitrary vertex $v$ and run DFS starting from that vertex (at this stage we will not assign directions to the edges).
- 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.
- 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.
- 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$.
- 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