How to convert an undirected graph to a DAG?

前端 未结 4 1487
情深已故
情深已故 2021-02-08 07:40

The Wiki page says

Any undirected graph may be made into a DAG by choosing a total order for its vertices and orienting every edge from the earlier endpo

4条回答
  •  清酒与你
    2021-02-08 08:21

    The problem is that after we change our undirected edges into directed ones we don't want any cycles left.

    For example, suppose we have the complete triangle graph

    A -- B
      \  |
       \ |
         C
    

    We could choose orientations for the edges as A -> B, B -> C and C -> A

    A -> B
     \\  |
       \ v
         C
    

    But then we'd get a cycle and that is not a Directed Acyclic Graph.

    The trick suggested in the Wikipedia page is choose an ordering of the vertices, any ordering, actually, and use that to decide what directions to point the edges.

    Since the edges point upwards in the ordering we can never "fall back down" again to complete a cycle so the resulting graph is guarantted to be acyclic.

提交回复
热议问题