def test_nan_weight(self): invalid_graph = retworkx.PyGraph() invalid_graph.extend_from_weighted_edge_list([(0, 1, 0.5), (0, 2, float("nan"))]) with self.assertRaises(ValueError): retworkx.minimum_spanning_tree(invalid_graph, lambda x: x)
def test_tree(self): mst_graph = retworkx.minimum_spanning_tree(self.graph, weight_fn=lambda x: x) self.assertEqual(self.graph.nodes(), mst_graph.nodes()) self.assertEqual( len(self.graph.nodes()) - 1, len(mst_graph.edge_list())) self.assertEqualEdgeList(self.expected_edges, mst_graph.weighted_edge_list())
def test_multigraph(self): mutligraph = retworkx.PyGraph(multigraph=True) mutligraph.extend_from_weighted_edge_list([(0, 1, 1), (0, 2, 3), (1, 2, 2), (0, 0, -10), (1, 2, 1)]) mst_graph = retworkx.minimum_spanning_tree(mutligraph, weight_fn=lambda x: x) self.assertEqualEdgeList([(0, 1, 1), (1, 2, 1)], mst_graph.weighted_edge_list())
def test_isolated(self): s = self.graph.add_node("S") msf_graph = retworkx.minimum_spanning_tree(self.graph, weight_fn=lambda x: x) self.assertEqual("S", msf_graph.nodes()[s]) self.assertEqual(self.graph.nodes(), msf_graph.nodes()) self.assertEqual( len(self.graph.nodes()) - 2, len(msf_graph.edge_list())) self.assertEqualEdgeList(self.expected_edges, msf_graph.weighted_edge_list())
def test_default_weight(self): weightless_graph = retworkx.PyGraph() weightless_graph.extend_from_edge_list([ (0, 1), (0, 2), (0, 3), (0, 4), (1, 5), (2, 6), (3, 7), (4, 8) ]) # MST of the graph is itself mst_graph_default_weight = retworkx.minimum_spanning_tree( weightless_graph) mst_graph_weight_2 = retworkx.minimum_spanning_tree(weightless_graph, default_weight=2.0) self.assertTrue( retworkx.is_isomorphic( weightless_graph, mst_graph_default_weight, )) self.assertTrue( retworkx.is_isomorphic( weightless_graph, mst_graph_weight_2, ))
def test_forest(self): s = self.graph.add_node("S") t = self.graph.add_node("T") u = self.graph.add_node("U") self.graph.add_edges_from([(s, t, 10), (t, u, 9), (s, u, 8)]) forest_expected_edges = self.expected_edges + [(s, u, 8), (t, u, 9)] msf_graph = retworkx.minimum_spanning_tree(self.graph, weight_fn=lambda x: x) self.assertEqual(self.graph.nodes(), msf_graph.nodes()) self.assertEqual( len(self.graph.nodes()) - 2, len(msf_graph.edge_list())) self.assertEqualEdgeList(forest_expected_edges, msf_graph.weighted_edge_list())