Make undirected graph directed

I've got a undirected, complete graph and would like to convert it to a directed acyclic graph with a (unidirectional) path between each node. To start off, I want to add random edges and stop once all nodes are connected. What would be an algorithm to look at (using Python, but any language will do).

So for instance this graph, does not to be connected any further:

A  ---- B            A ---> B
      /      =>           /
     /                   v
    C                    C

, but in this case, all undirected edges turn into a directed edge

A  ---- B            A ---> B
      /      =>      ^     ^
     /                   /
    C                    C

Update

Note that the aim is to convert an undirected graph into a directed graph as per the above constraints. Like for a spanning tree, there are more than 1 solutions to this conversion process (as shown in above example).


You need a directed spanning tree.

It's easy to find a directed spanning tree in an undirected graph. Just do depth-first-search, ignoring back and cross edges. The edges you actually traverse form a directed tree that touches every node in each connected component.

However you have added the restriction that you want edge choice to be random.

So for this you can use a Minimum Spanning Tree algorithm like Kruskal's algorithm with random edge weights.

Note there is no reason to actually store and compute random edge weights. The first step of the algorithm is to sort by weight. So replace this sort by a random permutation of the edges, and you're in business.

链接地址: http://www.djcxy.com/p/52380.html

上一篇: 它可以交叉边缘吗?

下一篇: 指示无向图