# Synopse

iamstp_and - Find a minimum spanning tree in a weighted graph.

• g = iamstp_and(W, A)
• Output
• g: tuple, (T, weights). T, with shape=(n,2), is a list of the MST edges, and weights is an array of weights for each MST edge.
• Input
• W: ndarray, 2D, square. Weighted graph matrix. If A is not specified, W must have value :eq:+Infty where there are no relations.
• A: ndarray, 2D, square, boolean (optional). Adjacency matrix with 1/True where there is an edge/arc, and 0/False where there is not.

# Description

Function iamstp_and() find a minimum spanning tree in a weighted graph. This implementation is based on Prim's MST algorithm.

# Function Code

1 def iamstp_and(W, A=None):
2     from numpy import arange, argsort, array, uint32
3     from heapq import heappush, heappop
4
5     if A is None:
6         A = W!=float('inf')
7
8     n,_ = A.shape
9     labels = arange(n)
10     n -= 1
11     T = []
12     Q = []
13     w, u, v = (0, 0, 0)
14     keep = True
15     while len(T)<n:
16         neighbors = A[u,:].nonzero()[0]
17         neighbors = neighbors[labels[neighbors]!=0]
18         weights = W[u,neighbors]
19         for i in arange(weights.shape[0]):
20             heappush(Q, (weights[i], (u,neighbors[i])))
21
22         keep = False
23         while len(Q)>0:
24             w, (u,v) = heappop(Q)
25             if labels[v]!=0:
26                 keep = True
27                 break
28
29         if not keep:
30             break
31
32         T.append([u,v,w])
33         labels[v] = 0
34         u = v
35     T = array(T)
36     return (T[:,:2].astype(uint32), T[:,2])

# Examples

In a small weighted graph.

1 from iamstp_and import iamstp_and
2 from time import time
3 from numpy import array, zeros
4
5 A = array( [[0,1,1,0,0,0,0,0,0],
6             [1,0,0,0,0,0,0,1,0],
7             [1,0,0,1,1,0,0,1,0],
8             [0,0,1,0,0,0,0,1,0],
9             [0,0,1,0,0,1,1,0,0],
10             [0,0,0,0,1,0,1,0,1],
11             [0,0,0,0,1,1,0,0,1],
12             [0,1,1,1,0,0,0,0,0],
13             [0,0,0,0,0,1,1,0,0]], dtype=byte)
14
15 W = array( [[0,1,2,0,0,0,0,0,0],
16             [1,0,0,0,0,0,0,3,0],
17             [2,0,0,4,5,0,0,6,0],
18             [0,0,4,0,0,0,0,7,0],
19             [0,0,5,0,0,8,9,0,0],
20             [0,0,0,0,8,0,1,0,2],
21             [0,0,0,0,9,1,0,0,3],
22             [0,3,6,7,0,0,0,0,0],
23             [0,0,0,0,0,2,3,0,0]], dtype=byte)
24
25 t = time()
26 T, weights = iamstp_and(W,A)
27 print 'MST edges:\n', T
28 print 'MST weight: ', sum(weights)
29 print 'Computational time: ', time() - t
MST edges:
[[0 1]
[0 2]
[1 7]
[2 3]
[2 4]
[4 5]
[5 6]
[5 8]]
MST weight:  26
Computational time:  0.00161409378052

# Performance Tests

This function is tested by running the tests defined at the page MST Test.

/usr/local/lib/python2.6/dist-packages/scikits/__init__.py:1: UserWarning: Module dateutil was already imported from /usr/local/lib/python2.6/dist-packages/matplotlib-1.1.0-py2.6-linux-x86_64.egg/dateutil/__init__.pyc, but /usr/lib/pymodules/python2.6 is being added to sys.path
__import__('pkg_resources').declare_namespace(__name__)

System Message: WARNING/2 (<string>, line 127)

Definition list ends without a blank line; unexpected unindent.

<type 'numpy.float64'>

gravando arquivo: /home/rubens/www/media/Attachments/iaOPF/iamstp_and/testperf.pkl

Autor Funcao A simple   A: rand(6,6)   100 nodes, complete   100 nodes, 0.5   Grafo MST
Andre iamstp_and 0.223 ms 9.0 0.263 ms 21.0 16.45 ms 318.055294457 9.583 ms 624.679654926 0.325 ms 139.0

# References

 [Feof2009] Paulo Feofiloff. Algoritmos para Grafos em C via Sedgewick. IME-USP. http://www.ime.usp.br/~pf/algoritmos_para_grafos