I am trying to convert a DAG to a binary tree. Consider the following graph
I want to have the following output for above tree.
Since A,B,C,E forms a diamond, to convert it into a tree, I need to move B and C into a line.
I have tried following:
Topological Path gives us a straight path. But, I want to preserve the sequence if possible i.e. A -> D. However, if there is a diamond, I want a node to only have one parent and sequence these parents as well.
Is there a way to generate a tree from a DAG for above cases?
Run a topological sort on the graph
For every node B, in reverse order of the topological sort:
If B has more than one parent:
Order its parents A1, A2, ..., An in the order of the topological sort
For every i in 1..n-1:
Add an arc from Ai to A(i+1)
Remove the arc from Ai to B
O(N^2)
where N
is the number of nodesThis proves that the algorithm terminates and that every node has at most one parent after executing the algorithm. Since we only remove parents from nodes which had more than one parent, I think it also satisfies your question.