Exemplo n.º 1
0
 def test_prim_matrix_with_edges(self):
     algorithm = PrimMatrixMSTWithEdges(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))
Exemplo n.º 2
0
 def test_prim_matrix_with_edges(self):
     algorithm = PrimMatrixMSTWithEdges(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 = 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))
Exemplo n.º 3
0
 def run(self, source=None):
     """Executable pseudocode."""
     if source is None:
         source = self.graph.iternodes().next()
     self.source = source
     algorithm = PrimMatrixMSTWithEdges(self.graph)  # O(V**2) time
     algorithm.run(self.source)
     self.mst = algorithm.to_tree()
     # Obchodzenie MST w kolejnosci preorder z uzyciem DFS.
     # Hamiltonian cycle as a list of nodes.
     order = list()
     algorithm = SimpleDFS(self.mst)  # O(V) time
     algorithm.run(self.source, pre_action=lambda node: order.append(node))
     order.append(self.source)  # close cycle, length V+1
     # Finding edges for the Hamiltonian cycle, O(V**2) time.
     for i in xrange(self.graph.v()):
         source = order[i]
         target = order[i + 1]
         for edge in self.graph.iteroutedges(source):
             if edge.target == target:
                 self.hamiltonian_cycle.add_edge(edge)
                 break
Exemplo n.º 4
0
 def run(self, source=None):
     """Executable pseudocode."""
     if source is None:
         source = next(self.graph.iternodes())
     self.source = source
     algorithm = PrimMatrixMSTWithEdges(self.graph)   # O(V**2) time
     algorithm.run(self.source)
     self.mst = algorithm.to_tree()
     # Obchodzenie MST w kolejnosci preorder z uzyciem DFS.
     # Hamiltonian cycle as a list of nodes.
     order = list()
     algorithm = SimpleDFS(self.mst)   # O(V) time
     algorithm.run(self.source, pre_action=lambda node: order.append(node))
     order.append(self.source)   # close cycle, length V+1
     # Finding edges for the Hamiltonian cycle, O(V**2) time.
     for i in xrange(self.graph.v()):
         source = order[i]
         target = order[i+1]
         for edge in self.graph.iteroutedges(source):
             if edge.target == target:
                 self.hamiltonian_cycle.add_edge(edge)
                 break
Exemplo n.º 5
0
 def test_prim_matrix_with_edges(self):
     algorithm = PrimMatrixMSTWithEdges(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))
Exemplo n.º 6
0
 def test_prim_matrix_with_edges(self):
     algorithm = PrimMatrixMSTWithEdges(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 = 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))
Exemplo n.º 7
0
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 ..."
t1 = timeit.Timer(lambda: PrimConnectedMST(G).run())
print V, E, t1.timeit(1)  # single run

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

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

print "Testing KruskalMSTSorted ..."