def test_connected_raise(): DG = nx.DiGraph() with pytest.raises(NetworkXNotImplemented): next(nx.biconnected_components(DG)) with pytest.raises(NetworkXNotImplemented): next(nx.biconnected_component_edges(DG)) with pytest.raises(NetworkXNotImplemented): next(nx.articulation_points(DG)) pytest.raises(NetworkXNotImplemented, nx.is_biconnected, DG)
def test_biconnected_components2(): G=nx.Graph() nx.add_cycle(G, 'ABC') nx.add_cycle(G, 'CDE') nx.add_cycle(G, 'FIJHG') nx.add_cycle(G, 'GIJ') G.add_edge('E','G') comps = list(nx.biconnected_component_edges(G)) answer = [ [tuple('GF'), tuple('FI'), tuple('IG'), tuple('IJ'), tuple('JG'), tuple('JH'), tuple('HG')], [tuple('EG')], [tuple('CD'), tuple('DE'), tuple('CE')], [tuple('AB'), tuple('BC'), tuple('AC')] ] assert_components_edges_equal(comps, answer)
def test_biconnected_components2(): G = nx.Graph() nx.add_cycle(G, 'ABC') nx.add_cycle(G, 'CDE') nx.add_cycle(G, 'FIJHG') nx.add_cycle(G, 'GIJ') G.add_edge('E', 'G') comps = list(nx.biconnected_component_edges(G)) answer = [ [tuple('GF'), tuple('FI'), tuple('IG'), tuple('IJ'), tuple('JG'), tuple('JH'), tuple('HG')], [tuple('EG')], [tuple('CD'), tuple('DE'), tuple('CE')], [tuple('AB'), tuple('BC'), tuple('AC')] ] assert_components_edges_equal(comps, answer)
def _compute_biconnected_components_edges( G: nx.Graph) -> List[List[Tuple[int], Set[int]]]: """ Biconnected components are maximal subgraphs such that the removal of a node (and all edges incident on that node) will not disconnect the subgraph. Source: - https://networkx.github.io/documentation/stable/_modules/networkx/algorithms/components/biconnected.html#biconnected_components - https://networkx.github.io/documentation/stable/_modules/networkx/algorithms/components/biconnected.html#biconnected_component_edges ------------------ Returns a list of lists of length 2: [Set, List of tuple pairs (edges)] """ biconnected_components = list(nx.biconnected_components(G)) biconnected_edges = list(nx.biconnected_component_edges(G)) components_and_edges = [[ biconnected_components[idx], biconnected_edges[idx] ] for idx in range(len(biconnected_components))] return components_and_edges
def complex_triangle_check(graph): for compedges in nx.biconnected_component_edges(graph): comp = nx.Graph() comp.add_edges_from(compedges) # print("hi") # print(comp.edges) H = comp.to_directed() # H = G.copy() # Get all triangles all_cycles = list(nx.simple_cycles(H)) all_triangles = [] for cycle in all_cycles: if len(cycle) == 3: all_triangles.append(cycle) # Get edges on outer boundary # print(f"all triangles {all_triangles}") outer_boundary = [] for edge in H.edges: count = 0 for triangle in all_triangles: if edge[0] in triangle and edge[1] in triangle: count += 1 if count == 2: outer_boundary.append(edge) # Get Vertex-Set of outerboundary outer_vertices = [] for edge in outer_boundary: if edge[0] not in outer_vertices: outer_vertices.append(edge[0]) if edge[1] not in outer_vertices: outer_vertices.append(edge[1]) # print(f"outer vertices {outer_vertices}") if all_triangles: if 2 * len(comp) - 5 + 3 - len( outer_vertices) - len(all_triangles) / 2: # print("complex") # print(2*len(comp)-5+3-len(outer_vertices)-len(all_triangles)) # print(2*len(comp)) # print(len(outer_vertices)) # print(len(all_triangles)) return False return True
def test_biconnected_components1(): # graph example from # https://web.archive.org/web/20121229123447/http://www.ibluemojo.com/school/articul_algorithm.html edges = [ (0, 1), (0, 5), (0, 6), (0, 14), (1, 5), (1, 6), (1, 14), (2, 4), (2, 10), (3, 4), (3, 15), (4, 6), (4, 7), (4, 10), (5, 14), (6, 14), (7, 9), (8, 9), (8, 12), (8, 13), (10, 15), (11, 12), (11, 13), (12, 13), ] G = nx.Graph(edges) pts = set(nx.articulation_points(G)) assert pts == {4, 6, 7, 8, 9} comps = list(nx.biconnected_component_edges(G)) answer = [ [(3, 4), (15, 3), (10, 15), (10, 4), (2, 10), (4, 2)], [(13, 12), (13, 8), (11, 13), (12, 11), (8, 12)], [(9, 8)], [(7, 9)], [(4, 7)], [(6, 4)], [(14, 0), (5, 1), (5, 0), (14, 5), (14, 1), (6, 14), (6, 0), (1, 6), (0, 1)], ] assert_components_edges_equal(comps, answer)
def test_biconnected_components1(): # graph example from # http://www.ibluemojo.com/school/articul_algorithm.html edges=[ (0, 1), (0, 5), (0, 6), (0, 14), (1, 5), (1, 6), (1, 14), (2, 4), (2, 10), (3, 4), (3, 15), (4, 6), (4, 7), (4, 10), (5, 14), (6, 14), (7, 9), (8, 9), (8, 12), (8, 13), (10, 15), (11, 12), (11, 13), (12, 13) ] G=nx.Graph(edges) pts = set(nx.articulation_points(G)) assert_equal(pts, {4, 6, 7, 8, 9}) comps = list(nx.biconnected_component_edges(G)) answer = [ [(3, 4), (15, 3), (10, 15), (10, 4), (2, 10), (4, 2)], [(13, 12), (13, 8), (11, 13), (12, 11), (8, 12)], [(9, 8)], [(7, 9)], [(4, 7)], [(6, 4)], [(14, 0), (5, 1), (5, 0), (14, 5), (14, 1), (6, 14), (6, 0), (1, 6), (0, 1)], ] assert_components_edges_equal(comps, answer)
def test_biconnected_components2(): G = nx.Graph() nx.add_cycle(G, "ABC") nx.add_cycle(G, "CDE") nx.add_cycle(G, "FIJHG") nx.add_cycle(G, "GIJ") G.add_edge("E", "G") comps = list(nx.biconnected_component_edges(G)) answer = [ [ tuple("GF"), tuple("FI"), tuple("IG"), tuple("IJ"), tuple("JG"), tuple("JH"), tuple("HG"), ], [tuple("EG")], [tuple("CD"), tuple("DE"), tuple("CE")], [tuple("AB"), tuple("BC"), tuple("AC")], ] assert_components_edges_equal(comps, answer)
def component_break(given_graph): """ Given a graph, returns [list of the 2 outer components(1 articulation point) with 2cip], [list of other inner components(2 articulation points) with 0 cip] """ test_graph = given_graph.copy() cutvertices = list(nx.articulation_points(test_graph)) # O(biconnected_components * cutvertices) inner_components = [] outer_components = [] if len(cutvertices) == 0: single_component = test_graph return 0, 0, single_component for peice_edges in nx.biconnected_component_edges(test_graph): # Find num cutvertices # num_cutverts = nx.intersection(peice, cutvertices) peice = nx.Graph() peice.add_edges_from(list(peice_edges)) num_cutverts = 0 for cutvert in cutvertices: if cutvert in peice.nodes(): num_cutverts += 1 if num_cutverts == 2: inner_components.append(peice) elif num_cutverts == 1: outer_components.append(peice) else: print("Illegal Case 1") print(test_graph.edges()) if len(outer_components) > 2: print("Illegal Case 2") print(test_graph) return outer_components, inner_components, 0
# colors_to_select.remove(color) # colors_of_edges.append((color)) # nodes_color_alpha.append(0.4) # edges_color_alpha.append(0.6) # edge_width_l.append(4.0) # print str(" ") # Gcc=sorted(nx.biconnected_connected_component_subgraphs(G), key = len, reverse=True) # Ggc=Gcc[0] # graph='Ggc' # print 'Nodes of giant connected component', graph+':', Ggc.nodes() # print 'Edges of giant connected component', graph+':', Ggc.edges() # print str(" ") print 'The biconnected component edges of G are:', list( nx.biconnected_component_edges(G)) print str(" ") print 'The articulation points of G are:', set(nx.articulation_points(G)) print str(" ") print 'Isolated nodes:', nx.isolates(G) print str(" ") # n1=list(set(G.nodes()) - set(Ggc.nodes()) - set(nx.isolates(G))) # print 'Non-isolated nodes outside the giant connected component:', n1 # N1=G.subgraph(n1) # print 'Edges among non-isolated nodes outside the giant connected component:', N1.edges() plt.figure()
def find_max_biconnected_component(G): gr = maximum_connected_component(G) bnodes = nx.biconnected_component_edges(gr) return list(bnodes).pop()
def test_null_graph(): G = nx.Graph() assert not nx.is_biconnected(G) assert list(nx.biconnected_components(G)) == [] assert list(nx.biconnected_component_edges(G)) == [] assert list(nx.articulation_points(G)) == []
def biconnected_components_gen(new_graph): return [ edge for list in nx.biconnected_component_edges(new_graph) for edge in list ]
# color=random.choice(colors_to_select) # colors_to_select.remove(color) # colors_of_edges.append((color)) # nodes_color_alpha.append(0.4) # edges_color_alpha.append(0.6) # edge_width_l.append(4.0) # print str(" ") # Gcc=sorted(nx.biconnected_connected_component_subgraphs(G), key = len, reverse=True) # Ggc=Gcc[0] # graph='Ggc' # print 'Nodes of giant connected component', graph+':', Ggc.nodes() # print 'Edges of giant connected component', graph+':', Ggc.edges() # print str(" ") print 'The biconnected component edges of G are:', list(nx.biconnected_component_edges(G)) print str(" ") print 'The articulation points of G are:', set(nx.articulation_points(G)) print str(" ") print 'Isolated nodes:', nx.isolates(G) print str(" ") # n1=list(set(G.nodes()) - set(Ggc.nodes()) - set(nx.isolates(G))) # print 'Non-isolated nodes outside the giant connected component:', n1 # N1=G.subgraph(n1) # print 'Edges among non-isolated nodes outside the giant connected component:', N1.edges()
def test_null_graph(): G = nx.Graph() assert_false(nx.is_biconnected(G)) assert_equal(list(nx.biconnected_components(G)), []) assert_equal(list(nx.biconnected_component_edges(G)), []) assert_equal(list(nx.articulation_points(G)), [])
def label_edges(self, dungeon): biconnected_component_edges = nx.biconnected_component_edges(dungeon) for edge, edge_data in dungeon.edges(data=True): if edge in biconnected_component_edges: edge_data['important'] = True