def test_undirected_graph_with_2_unequal_components_larger_is_returned( self): G = nx.Graph([('a', 'b'), ('b', 'c'), ('d', 'e')]) lcc = largest_connected_component(G) lcc_expected = nx.Graph([('a', 'b'), ('b', 'c')]) self.assertEqual(lcc_expected.adj, lcc.adj, 'LCC should be largest component')
def test_directed_graph_with_2_unequal_components_weakly_connected_returned( self): G = nx.DiGraph([('a', 'b'), ('b', 'c'), ('c', 'a'), ('d', 'a')]) lcc = largest_connected_component(G) lcc_expected = nx.DiGraph([('a', 'b'), ('b', 'c'), ('c', 'a'), ('d', 'a')]) self.assertEqual(lcc_expected.adj, lcc.adj, 'LCC should have all edges')
def test_undirected_largest_component(self): graph = nx.Graph() graph.add_edge("a", "b", weight=2) graph.add_edge("b", "c", weight=3) graph.add_edge("e", "f", weight=5) expected = nx.Graph() expected.add_edge("a", "b", weight=2) expected.add_edge("b", "c", weight=3) self.assertEqual(expected.edges, largest_connected_component(graph).edges)
def test_directed_largest_component(self): graph = nx.DiGraph() graph.add_edge("a", "b") graph.add_edge("b", "c") graph.add_edge("a", "c") graph.add_edge("c", "a") graph.add_edge("e", "f") expected = nx.DiGraph() expected.add_edge("a", "b") expected.add_edge("b", "c") expected.add_edge("a", "c") expected.add_edge("c", "a") self.assertEqual( expected.edges, largest_connected_component(graph, weakly=False).edges)
def test_undirected_single_connected_component(self): graph = nx.Graph() graph.add_edge("a", "b") graph.add_edge("b", "c") graph.add_edge("c", "d") self.assertEqual(graph.edges, largest_connected_component(graph).edges)
def test_has_largest_fails_on_nongraph(self): with self.assertRaises(TypeError): largest_connected_component("sandwich")
def test_connected_directed_graph_lcc_equals_graph(self): G = nx.DiGraph([('a', 'b'), ('b', 'c'), ('c', 'a')]) lcc = largest_connected_component(G) self.assertEqual(G.adj, lcc.adj, 'LCC should be the entire graph')