Example #1
0
            i += 1

    def merge(self, ucomp, vcomp):
        self.C = [ucomp[0] + vcomp[0]] + [i for j, i in enumerate(self.C) if j not in [ucomp[1], vcomp[1]]]


# My experiment

timings_vertices2 = []
theorotical2 = []
vertices = 50
import numpy

for nt in numpy.linspace(0.1, 0.95, 20):
    start = time.time()
    A = gen.topology(vertices, nt)
    N = list(xrange(len(A)))

    p = []
    for i in xrange(1, len(A)):
        for j in xrange(1, i):
            if A[i][j] != 0:
                p.append((i, j, A[i][j]))

    myExperiment = Kruskal(N, p)
    myExperiment.execute()
    end = time.time()
    t = end - start
    timings_vertices2.append(t)
    theorotical2.append(len(p) * math.log(vertices))
    print t, len(p), vertices
Example #2
0
    mst = []
    used = set( nodes[ 0 ] )
    usable_edges = conn[ nodes[0] ][:]
    heapify( usable_edges )


    while usable_edges:
        cost, n1, n2 = heappop( usable_edges )
        if n2 not in used:
            used.add( n2 )
            mst.append( ( n1, n2, cost ) )


            for e in conn[ n2 ]:
                if e[ 2 ] not in used:
                    heappush( usable_edges, e )
    return mst

A = gen.topology(1000, 0.4)        
N = [str(x) for x in list(xrange(len(A)))]

p = []

for i in xrange(1,len(A)):
  for j in xrange(1,i):
    if A[i][j] != 0:
      p.append((str(i),str(j),A[i][j]))

print len(p)

prim(N, p)