I have a graph with directed and undirected edges, now I want to get rid of the undirected edges by replacing them with directed edges (each undirected edge becomes one directed edge). There are two possibilies for each undirected edge (replace it with a directed edge in one direction or the other direction).
How to determine the direction of the undirected edges so that my graph stays acyclic (a DAG)?
My approach:
Do a topological sort on the graph without the undirected edges, add the undirected edges one by one (make them point from smaller topo-vlalue to larger).
This doesn't seem to work since topological sort creates a partial order (not all vertices are comparable to each other), what I need is a total order (all vertices are comparable). How to extend the partial order to a total order?
An example graph where my approach fails:
- n = 8 (number of vertices, numbered from 1 to n)
- m = 5 (number of directed edges)
- l = 6 (number of undirected edges)
Directed edges:
- 2 -> 1
- 3 -> 2
- 2 -> 6
- 4 -> 5
- 5 -> 8
Undirected edges:
- 1 <-> 4
- 2 <-> 5
- 3 <-> 7
- 4 <-> 8
- 6 <-> 7
- 6 <-> 8
The graph with only directed edges and topological order values beside the vertices:
Now as I start adding undirected edges (and directing them according to the topological order values) I get a cycle after adding the edge 8 -> 4: