def test_prim(self): self.assertEqual(self.G.v(), self.N) algorithm = PrimMST(self.G) algorithm.run() algorithm.to_tree() 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))
def test_prim(self): self.assertEqual(self.G.v(), self.N) algorithm = PrimMST(self.G) algorithm.run() algorithm.to_tree() 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))
def test_prim(self): self.assertEqual(self.G.v(), self.N) algorithm = PrimMST(self.G) algorithm.run() algorithm.to_tree() 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))
def _find_td(self): """Find a tree decomposition for a Halin graph.""" H = self.graph.__class__(self.graph.v()) # graf przeciec klik maksymalnych bag_dict = dict() # Budowanie workow. for c in self.cliques: bag = tuple(sorted(c)) bag_dict[bag] = c H.add_node(bag) # Budowanie krawedzi grafu przeciec klik. for bag1 in bag_dict: for bag2 in bag_dict: if bag1 < bag2: inter = bag_dict[bag1].intersection(bag_dict[bag2]) if inter: H.add_edge(Edge(bag1, bag2, -len(inter))) algorithm = PrimMST(H) algorithm.run() algorithm.to_tree() self.td = algorithm.mst
def test_prim(self): self.assertEqual(self.G.v(), self.N) algorithm = PrimMST(self.G) algorithm.run() algorithm.to_tree() 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))
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 ..." t1 = timeit.Timer(lambda: PrimMatrixMSTWithEdges(G).run()) print V, E, t1.timeit(1) # single run print "Testing PrimConnectedMST ..."