Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to efficiently generate all possible spanning trees from a graph

First please note that this question is NOT asking about MST, instead, just all possible spanning trees.

So this is NOT the same as finding all minimal spanning trees or All minimum spanning trees implementation


I just need to generate all possible spanning trees from a graph.

I think the brute-force way is straight:

Suppose we have V nodes and E edges.

  1. Get all edges of the graph
  2. Get all possible combinations of V-1 out of E edges.
  3. Filter out non-spanning-tree out of the combinations (for a spanning tree, all nodes inside one set of V-1 edges should appear exactly once)

But I think it is too slow when facing big graph.

Do we have a better way?

like image 429
Jackson Tale Avatar asked Mar 02 '14 13:03

Jackson Tale


People also ask

How do you find all possible spanning trees on a graph?

If a graph is a complete graph with n vertices, then total number of spanning trees is n(n-2) where n is the number of nodes in the graph. In complete graph, the task is equal to counting different labeled trees with n nodes for which have Cayley's formula.

How many spanning trees are possible from graph?

Spanning tree has n-1 edges, where n is the number of nodes (vertices). From a complete graph, by removing maximum e - n + 1 edges, we can construct a spanning tree. A complete graph can have maximum nn-2 number of spanning trees.

How many spanning trees can be generated from a graph with 4 nodes?

Figure 1: A four-vertex complete graph K4. The answer is 16.

Which algorithm is used to find a spanning tree from a graph?

Kruskal's algorithm - This algorithm is also used to find the minimum spanning tree for a connected weighted graph.


2 Answers

Set the weight of all edges to the same value, then use an algorithm to find all minimum spanning trees. Since all spanning trees have |V|-1 edges and all edge weights are equal, all spanning trees will be minimum spanning trees.

like image 71
G. Bach Avatar answered Oct 10 '22 00:10

G. Bach


I've become interested in this question, and have yet to find a really satisfactory answer. However, I have found a number of references: Knuth's Algorithms S and S' in TAOCP Volume 4 Fascicle 4, a paper by Sorensen and Janssens, and GRAYSPAN, SPSPAN, and GRAYSPSPAN by Knuth. It's too bad none of them are implementations in a language I could use ... I guess I'll have to spend some time coding these ...

like image 27
Edward Doolittle Avatar answered Oct 10 '22 01:10

Edward Doolittle