Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Algorithm to generate spanning set

Given this input: [1,2,3,4]

I'd like to generate the set of spanning sets:

[1] [2] [3] [4]
[1] [2] [3,4]
[1] [2,3] [4]
[1] [3] [2,4]
[1,2] [3] [4]
[1,3] [2] [4]
[1,4] [2] [3]
[1,2] [3,4]
[1,3] [2,4]
[1,4] [2,3]
[1,2,3] [4]
[1,2,4] [3]
[1,3,4] [2]
[2,3,4] [1]
[1,2,3,4]

Every set has all the elements of the original set, permuted to appear in unique subsets. What is the algorithm that produces these sets? I've tried Python generator functions using choose, permutation, combination, power set, and so on, but can't get the right combination.

20 Jan 2009

This is not a homework question. This is an improved answer I was working on for www.projecteuler.net problem # 118. I already had a slow solution but came up with a better way -- except I could not figure out how to do the spanning set.

I'll post my code when I get back from an Inauguration Party.

21 Jan 2009

This is the eventual algorithm I used:

def spanningsets(items):
    if len(items) == 1:
        yield [items]
    else:
        left_set, last = items[:-1], [items[-1]]
        for cc in spanningsets(left_set):
            yield cc + [last]
            for i,elem in enumerate(cc):
                yield cc[:i] + [elem + last] + cc[i+1:]

@Yuval F: I know how to do a powerset. Here's a straightforward implementation:

def powerset(s) :
    length = len(s)
    for i in xrange(0, 2**length) :
        yield [c for j, c in enumerate(s) if (1 << j) & i]
    return
like image 517
hughdbrown Avatar asked Jan 20 '09 08:01

hughdbrown


People also ask

What is a spanning tree?

A spanning tree is defined as a tree which is a subset of the graph that have the same vertices as graph and edges same as a graph, but one less edge than the given graph makes the graph a spanning tree where all the vertices are covered with one less than edges of the given graph which makes it cycle free graph.

How do you find the minimum spanning tree?

There are different algorithms used to find the minimum spanning trees, such as Kruskal’s and Prim’s algorithm where it also helps to find running time with an efficient way of using spanning trees in any networking such as spanning tree protocols. This is a guide to Spanning Tree Algorithm.

How many spanning trees can a single graph have?

A single graph can have many different spanning trees. A minimum spanning tree (MST) or minimum weight spanning tree for a weighted, connected, undirected graph is a spanning tree with a weight less than or equal to the weight of every other spanning tree. The weight of a spanning tree is the sum of weights given to each edge of the spanning tree.

How to find (V-1) edges of a spanning tree?

1 Sort all the edges in non-decreasing order of their weight. 2 Pick the smallest edge. Check if it forms a cycle with the spanning tree formed so far. ... 3 Repeat step#2 until there are (V-1) edges in the spanning tree.


1 Answers

This should work, though I haven't tested it enough.

def spanningsets(items):
    if not items: return
    if len(items) == 1:
        yield [[items[-1]]]
    else:
        for cc in spanningsets(items[:-1]):
            yield cc + [[items[-1]]]
            for i in range(len(cc)):
                yield cc[:i] + [cc[i] + [items[-1]]] + cc[i+1:]

for sset in spanningsets([1, 2, 3, 4]):
    print ' '.join(map(str, sset))

Output:

[1] [2] [3] [4]
[1, 4] [2] [3]
[1] [2, 4] [3]
[1] [2] [3, 4]
[1, 3] [2] [4]
[1, 3, 4] [2]
[1, 3] [2, 4]
[1] [2, 3] [4]
[1, 4] [2, 3]
[1] [2, 3, 4]
[1, 2] [3] [4]
[1, 2, 4] [3]
[1, 2] [3, 4]
[1, 2, 3] [4]
[1, 2, 3, 4]
like image 173
dasony Avatar answered Oct 26 '22 06:10

dasony