2
$\begingroup$

I was thinking what if you just started with the node with lowest non-zero degree $u$ (only count undirected edges) and picked random edge that is connected to that and direct that inwards. EX: undirected edge $uv \to (v,u)$. Then repeat, ignoring nodes that have reached the at-most-one limit. At the end, you check to see if there are any undirected edges left. If yes then output impossible;if no then you have a valid solution.

I'm not sure if this works though.

$\endgroup$
4
  • $\begingroup$ With this idea, what would you do if after converting an edge of vertex $u$, it is still the vertex with the fewest undirected edge? $\endgroup$
    – Russel
    Commented Feb 24, 2023 at 1:45
  • $\begingroup$ ah yea so you would ignore nodes that have reached the at most one limit. Then at the end after you are done you check to see if there are any undirected edges left. If yes then output impossible if no then you have a valid solution. $\endgroup$
    – CHTM
    Commented Feb 24, 2023 at 2:01
  • 2
    $\begingroup$ I think your algorithm might work, although perhaps it could be simplified. In order to see whether it is correct consider: When can a tree be directed like this? When can a connected graph with at least one cycle be directed like this? $\endgroup$
    – Discrete lizard
    Commented Feb 24, 2023 at 9:21
  • 1
    $\begingroup$ @CHTM Was my answer helpful? Have you considered upvoting and accepting my answer? Please comment if my answer can be improved. (This comment will be deleted upon feedback.) $\endgroup$
    – John L.
    Commented Mar 4, 2023 at 10:13

1 Answer 1

1
$\begingroup$

Your algorithm works. The main concept here are tree and cycle.

it is possible iff no cycles or one cycle

Claim: Let $G$ be an undirected connected graph. $G$ can be turned into a directed graph with in-degree at most $1$ iff $G$ has no cycles or exactly one cycle.
Proof: "$\Leftarrow$":
If $G$ has no cycle, it is a tree. Pick any node as the root, it is easy to change an undirected tree into a directed rooted tree.

If $G$ has one cycle, change that cycle into a directed cycle. Contract this cycle into one vertex so that $G$ becomes an undirected tree. Using that vertex as the root, change $G$ into a directed rooted tree. We have, in fact, assigned a direction for each edge in the original $G$ so that it becomes a directed graph with in-degree at most $1$.

"$\Rightarrow$":
Suppose $G$ is turned into directed graph $G'$ with in-degree at most $1$.

Consider $G'$. Since different edges must have different tails, the number of edges is no more than the number of nodes.

Since $G$ and $G'$ have the same number of edges and the same number of nodes, the number of edges in $G$ is no more than the number of nodes in $G$. As a connected graph, $G$ has at least $|V|-1$ edges.

  • $G$ has $|V|-1$ edges.
    Then $G$ must be a tree, which has no cycles.
  • Otherwise $G$ has $|V|$ edges.
    Let $T$ be a spanning tree of $G$, which has $|V|-1$ edges. $G$ is $T$ plus one edge. So $G$ has exactly one cycle.

Your algorithm works

It is enough to show your algorithm works on each connected component.

Assume the given graph, $G$ is connected.

If $G$ has more than one cycle, then it is impossible to change $G$ as wanted. Your algorithm must return "impossible" as well.

If $G$ has no cycle, your algorithm will pick a leaf every time. In the end, your algorithm will produce a directed rooted tree.

If $G$ has one cycle, your algorithm will pick a leaf every time until every node that is incident to an undirected edge is incident to more than one undirected edge. Upon that time, all those nodes together with all undirected edges form the unique cycle in $G$. Your algorithm will assign a direction to one of the undirected edges. Then spread the direction either way along the cycle one edge at a time. In the end, every node will have in-degree $1$.

Another algorithm

Split the given graph $G$ into connected components.

For each connected component $C$, check the number of edges in it.

  • If it is more than the number of nodes, return "impossible" (and end the entire algorithm).
  • If it is less than the number of nodes, $C$ is a tree. Pick any node as the root. Make $C$ a directed rooted tree.
  • Otherwise, there is a unique cycle in $C$. Find that cycle. Change that cycle into a directed cycle. Imagine the nodes in the cycle as one (super-)node. Picking that node as root, make $C$ a directed root tree.

This algorithm is not necessarily faster or easier to implement than your algorithm. It is, however, clearer what is going on with this algorithm.

$\endgroup$
1
  • $\begingroup$ Nice explanation. One very minor suggestion: Change "tails" to "heads" in "Since different edges must have different tails". $\endgroup$ Commented Jul 26, 2023 at 4:54

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.