def test_breadth_first_spanning_forest(self): sff = GraphTraversalSpanningForestFinder() f = tuple(sff.spanning_forest(self.iberia)) self.assertEqual(len(f), 1) self.assertEqual(count(f[0]), len(cities) - 1) f = tuple(sff.spanning_forest(self.unconnected)) self.assertEqual(len(f), 2) self.assertEqual(count(f[0]) + count(f[1]), 7) forest = tuple(sff.spanning_forest(self.g)) self.assertEqual(len(forest), 1) self.assertEqual(count(forest[0]), 9)
def test_Kruskal(self): sff = KruskalsMinimumSpanningForestFinder() self.assertEqual(count(sff.minimum_spanning_forest(self.iberia, km)), len(cities) - 1) self.assertEqual( count( sff.minimum_spanning_forest(self.unconnected, self.unconnected_weight)), 7) w = sum( self.d(u, v) for (u, v) in sff.minimum_spanning_forest(self.g, self.d)) self.assertEqual(w, 45)
def test_Prim(self): stf = PrimsMinimumSpanningFinder() self.assertEqual( count(stf.minimum_spanning_tree(self.iberia, km, "Gandia")), len(cities) - 1) self.assertEqual( count( stf.minimum_spanning_tree(self.unconnected, self.unconnected_weight, 0)), 5) w = sum( self.d(u, v) for (u, v) in stf.minimum_spanning_tree(self.g, self.d, 0)) self.assertEqual(w, 45)
def test_Prim_with_priority_dict(self): stf = PrimsMinimumSpanningFinder() self.assertEqual( count(stf.minimum_spanning_tree(self.iberia, km, "Gandia")), len(cities) - 1) self.assertEqual( count( stf.minimum_spanning_tree(self.unconnected, self.unconnected_weight, 0)), 5) pstf = PrimsWithPriorityQueueMinimumSpanningTreeFinder() w = sum( self.d(u, v) for (u, v) in pstf.minimum_spanning_tree(self.g, self.d, 0)) self.assertEqual(w, 45)
def test_count(self): self.assertEqual(10, count(range(10))) self.assertEqual(10, count(list(range(10))))
def is_connected(self, G: "undirected IDigraph<T>") -> "bool": return count(self.connected_components(G)) == 1 #]interface
def test_breadth_first_spanning_tree(self): spf = GraphTraversalSpanningTreeFinder() self.assertEqual(count(spf.spanning_tree(self.iberia, 'Madrid')), len(cities) - 1) self.assertEqual(count(spf.spanning_tree(self.unconnected, 0)), 5) self.assertEqual(count(spf.spanning_tree(self.g, 0)), 9)