def test_general_k_edge_subgraph_quick_return(): # tests quick return optimization G = nx.Graph() G.add_node(0) subgraphs = list(general_k_edge_subgraphs(G, k=1)) assert len(subgraphs) == 1 for subgraph in subgraphs: assert subgraph.number_of_nodes() == 1 G.add_node(1) subgraphs = list(general_k_edge_subgraphs(G, k=1)) assert len(subgraphs) == 2 for subgraph in subgraphs: assert subgraph.number_of_nodes() == 1
def test_general_k_edge_subgraph_quick_return(): # tests quick return optimization G = nx.Graph() G.add_node(0) subgraphs = list(general_k_edge_subgraphs(G, k=1)) assert_equal(len(subgraphs), 1) for subgraph in subgraphs: assert_equal(subgraph.number_of_nodes(), 1) G.add_node(1) subgraphs = list(general_k_edge_subgraphs(G, k=1)) assert_equal(len(subgraphs), 2) for subgraph in subgraphs: assert_equal(subgraph.number_of_nodes(), 1)
def _check_edge_connectivity(G): """ Helper - generates all k-edge-components using the aux graph. Checks the both local and subgraph edge connectivity of each cc. Also checks that alternate methods of computing the k-edge-ccs generate the same result. """ # Construct the auxillary graph that can be used to make each k-cc or k-sub aux_graph = EdgeComponentAuxGraph.construct(G) # memoize the local connectivity in this graph memo = {} for k in it.count(1): # Test "local" k-edge-components and k-edge-subgraphs ccs_local = fset(aux_graph.k_edge_components(k)) ccs_subgraph = fset(aux_graph.k_edge_subgraphs(k)) # Check connectivity properties that should be garuenteed by the # algorithms. _assert_local_cc_edge_connectivity(G, ccs_local, k, memo) _assert_subgraph_edge_connectivity(G, ccs_subgraph, k) if k == 1 or k == 2 and not G.is_directed(): assert_equal( ccs_local, ccs_subgraph, 'Subgraphs and components should be the same ' 'when k == 1 or (k == 2 and not G.directed())') if G.is_directed(): # Test special case methods are the same as the aux graph if k == 1: alt_sccs = fset(nx.strongly_connected_components(G)) assert_equal(alt_sccs, ccs_local, 'k=1 failed alt') assert_equal(alt_sccs, ccs_subgraph, 'k=1 failed alt') else: # Test special case methods are the same as the aux graph if k == 1: alt_ccs = fset(nx.connected_components(G)) assert_equal(alt_ccs, ccs_local, 'k=1 failed alt') assert_equal(alt_ccs, ccs_subgraph, 'k=1 failed alt') elif k == 2: alt_bridge_ccs = fset(bridge_components(G)) assert_equal(alt_bridge_ccs, ccs_local, 'k=2 failed alt') assert_equal(alt_bridge_ccs, ccs_subgraph, 'k=2 failed alt') # if new methods for k == 3 or k == 4 are implemented add them here # Check the general subgraph method works by itself alt_subgraph_ccs = fset( [set(C.nodes()) for C in general_k_edge_subgraphs(G, k=k)]) assert_equal(alt_subgraph_ccs, ccs_subgraph, 'alt subgraph method failed') # Stop once k is larger than all special case methods # and we cannot break down ccs any further. if k > 2 and all(len(cc) == 1 for cc in ccs_local): break
def _check_edge_connectivity(G): """ Helper - generates all k-edge-components using the aux graph. Checks the both local and subgraph edge connectivity of each cc. Also checks that alternate methods of computing the k-edge-ccs generate the same result. """ # Construct the auxiliary graph that can be used to make each k-cc or k-sub aux_graph = EdgeComponentAuxGraph.construct(G) # memoize the local connectivity in this graph memo = {} for k in it.count(1): # Test "local" k-edge-components and k-edge-subgraphs ccs_local = fset(aux_graph.k_edge_components(k)) ccs_subgraph = fset(aux_graph.k_edge_subgraphs(k)) # Check connectivity properties that should be garuenteed by the # algorithms. _assert_local_cc_edge_connectivity(G, ccs_local, k, memo) _assert_subgraph_edge_connectivity(G, ccs_subgraph, k) if k == 1 or k == 2 and not G.is_directed(): assert_equal(ccs_local, ccs_subgraph, 'Subgraphs and components should be the same ' 'when k == 1 or (k == 2 and not G.directed())') if G.is_directed(): # Test special case methods are the same as the aux graph if k == 1: alt_sccs = fset(nx.strongly_connected_components(G)) assert_equal(alt_sccs, ccs_local, 'k=1 failed alt') assert_equal(alt_sccs, ccs_subgraph, 'k=1 failed alt') else: # Test special case methods are the same as the aux graph if k == 1: alt_ccs = fset(nx.connected_components(G)) assert_equal(alt_ccs, ccs_local, 'k=1 failed alt') assert_equal(alt_ccs, ccs_subgraph, 'k=1 failed alt') elif k == 2: alt_bridge_ccs = fset(bridge_components(G)) assert_equal(alt_bridge_ccs, ccs_local, 'k=2 failed alt') assert_equal(alt_bridge_ccs, ccs_subgraph, 'k=2 failed alt') # if new methods for k == 3 or k == 4 are implemented add them here # Check the general subgraph method works by itself alt_subgraph_ccs = fset([set(C.nodes()) for C in general_k_edge_subgraphs(G, k=k)]) assert_equal(alt_subgraph_ccs, ccs_subgraph, 'alt subgraph method failed') # Stop once k is larger than all special case methods # and we cannot break down ccs any further. if k > 2 and all(len(cc) == 1 for cc in ccs_local): break
def test_zero_k_exception(): G = nx.Graph() # functions that return generators error immediately pytest.raises(ValueError, nx.k_edge_components, G, k=0) pytest.raises(ValueError, nx.k_edge_subgraphs, G, k=0) # actual generators only error when you get the first item aux_graph = EdgeComponentAuxGraph.construct(G) pytest.raises(ValueError, list, aux_graph.k_edge_components(k=0)) pytest.raises(ValueError, list, aux_graph.k_edge_subgraphs(k=0)) pytest.raises(ValueError, list, general_k_edge_subgraphs(G, k=0))
def test_zero_k_exception(): G = nx.Graph() # functions that return generators error immediately assert_raises(ValueError, nx.k_edge_components, G, k=0) assert_raises(ValueError, nx.k_edge_subgraphs, G, k=0) # actual generators only error when you get the first item aux_graph = EdgeComponentAuxGraph.construct(G) assert_raises(ValueError, list, aux_graph.k_edge_components(k=0)) assert_raises(ValueError, list, aux_graph.k_edge_subgraphs(k=0)) assert_raises(ValueError, list, general_k_edge_subgraphs(G, k=0))