Esempio n. 1
0
    def test_union(self):
        treesetA = TreeSet()
        for i in [5, 19, 21, 25]:
            treesetA.add(i)

        treesetB = TreeSet()
        for i in [19, 6, 1, 5]:
            treesetB.add(i)

        treesetC = TreeSet()
        for i in [19, 6, 1, 5, 21, 25]:
            treesetC.add(i)

        self.assertEqual(treesetC, treesetA.union(treesetB))
        self.assertEqual(treesetA, treesetA.union(TreeSet()))
Esempio n. 2
0
    exit(0)

#create priority (d-heap) from edges
p_queue.make_heap(edges)

#create nodes with rooted tree set
treeset = TreeSet()
for i in vertices:
    treeset.make_set(i)

#compute min spanning tree starting from smallest edge weight
min_cost = 0
while p_queue.size() != 0:
    edge_i = p_queue.find_min()
    if treeset.find(edge_i.u) != treeset.find(edge_i.v):
        treeset.union(treeset.find(edge_i.u),treeset.find(edge_i.v))
        blue_edges.append(edge_i)
        min_cost = edge_i.weight + min_cost
    p_queue.delete_min()

#write to file
if(args.output == None): #no output file. Write to stdout
    print "c Total cost of tree ", min_cost
    for i in blue_edges:
        print i
else:
    with open(args.output,"w") as f:
        f.write("c Total cost of tree "+ str(min_cost)+"\n")
        for i in blue_edges:
            f.write(str(i)+"\n")
            f.flush()