Beispiel #1
0
 def test_mst_wiki(self):
     self.assertEqual(self.G.v(), self.N)
     algorithm = BoruvkaMST(self.G)
     algorithm.run()
     self.assertEqual(algorithm.mst.v(), self.N)
     self.assertEqual(algorithm.mst.e(), self.N - 1)
     mst_weight_expected = 40
     mst_weight = sum(edge.weight for edge in algorithm.mst.iteredges())
     self.assertEqual(mst_weight, mst_weight_expected)
     mst_edges_expected = [Edge(0, 1, 7), Edge(0, 3, 4), Edge(2, 4, 5), Edge(1, 4, 10), Edge(4, 6, 8), Edge(3, 5, 6)]
     for edge in mst_edges_expected:
         self.assertTrue(algorithm.mst.has_edge(edge))
Beispiel #2
0
 def test_boruvka(self):
     self.assertEqual(self.G.v(), self.N)
     algorithm = BoruvkaMST(self.G)
     algorithm.run()
     self.assertEqual(algorithm.mst.v(), self.N)
     self.assertEqual(algorithm.mst.e(), self.N-2) # 2 components
     mst_weight_expected = 40
     mst_weight = sum(edge.weight for edge in algorithm.mst.iteredges())
     self.assertEqual(mst_weight, mst_weight_expected)
     mst_edges_expected = [
         Edge(0, 1, 4), Edge(0, 7, 8), Edge(8, 2, 2), Edge(7, 6, 1), 
         Edge(6, 8, 6), Edge(3, 4, 9), Edge(5, 4, 10)]
     for edge in mst_edges_expected:
         self.assertTrue(algorithm.mst.has_edge(edge))
Beispiel #3
0
 def test_boruvka(self):
     self.assertEqual(self.G.v(), self.N)
     algorithm = BoruvkaMST(self.G)
     algorithm.run()
     self.assertEqual(algorithm.mst.v(), self.N)
     self.assertEqual(algorithm.mst.e(), self.N-1)
     mst_weight_expected = 40
     mst_weight = sum(edge.weight for edge in algorithm.mst.iteredges())
     self.assertEqual(mst_weight, mst_weight_expected)
     mst_edges_expected = [
         Edge(0, 1, 7), Edge(0, 3, 4), Edge(2, 4, 5), Edge(1, 4, 10), 
         Edge(4, 6, 8), Edge(3, 5, 6)]
     for edge in mst_edges_expected:
         self.assertTrue(algorithm.mst.has_edge(edge))
Beispiel #4
0
 def test_mst_cormen(self):
     self.assertEqual(self.G.v(), self.N)
     algorithm = BoruvkaMST(self.G)
     algorithm.run()
     self.assertEqual(algorithm.mst.v(), self.N)
     self.assertEqual(algorithm.mst.e(), self.N - 1)
     mst_weight_expected = 42
     mst_weight = sum(edge.weight for edge in algorithm.mst.iteredges())
     self.assertEqual(mst_weight, mst_weight_expected)
     mst_edges_expected = [
         Edge(0, 1, 4),
         Edge(0, 7, 8),
         Edge(8, 2, 2),
         Edge(7, 6, 1),
         Edge(2, 5, 5),
         Edge(6, 5, 3),
         Edge(3, 4, 9),
         Edge(5, 4, 10)
     ]
     for edge in mst_edges_expected:
         self.assertTrue(algorithm.mst.has_edge(edge))
Beispiel #5
0
 def test_mst_cormen(self):
     self.assertEqual(self.G.v(), self.N)
     algorithm = BoruvkaMST(self.G)
     algorithm.run()
     self.assertEqual(algorithm.mst.v(), self.N)
     self.assertEqual(algorithm.mst.e(), self.N - 1)
     mst_weight_expected = 42
     mst_weight = sum(edge.weight for edge in algorithm.mst.iteredges())
     self.assertEqual(mst_weight, mst_weight_expected)
     mst_edges_expected = [
         Edge(0, 1, 4),
         Edge(0, 7, 8),
         Edge(8, 2, 2),
         Edge(7, 6, 1),
         Edge(2, 5, 5),
         Edge(6, 5, 3),
         Edge(3, 4, 9),
         Edge(5, 4, 10),
     ]
     for edge in mst_edges_expected:
         self.assertTrue(algorithm.mst.has_edge(edge))
Beispiel #6
0
E = G.e()
#G.show()

print "Calculate parameters ..."
print "Nodes:", G.v(), V
print "Edges:", G.e(), E
print "Directed:", G.is_directed()
print "Connected:", is_connected(G)
print "Biconnected:", is_biconnected(G)
print "Acyclic:", is_acyclic(G)
print "Bipartite:", is_bipartite(G)
Delta = max(G.degree(node) for node in G.iternodes())
print "Delta:", Delta

print "Testing BoruvkaMST ..."
t1 = timeit.Timer(lambda: BoruvkaMST(G).run())
print V, E, t1.timeit(1)  # single run

print "Testing PrimMST ..."
t1 = timeit.Timer(lambda: PrimMST(G).run())
print V, E, t1.timeit(1)  # single run

print "Testing PrimMSTWithEdges ..."
t1 = timeit.Timer(lambda: PrimMSTWithEdges(G).run())
print V, E, t1.timeit(1)  # single run

print "Testing PrimMatrixMST ..."
t1 = timeit.Timer(lambda: PrimMatrixMST(G).run())
print V, E, t1.timeit(1)  # single run

print "Testing PrimMatrixMSTWithEdges ..."