Python networkx.weakly_connected_component_subgraphs() Examples
The following are 23
code examples of networkx.weakly_connected_component_subgraphs().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
networkx
, or try the search function
.
Example #1
Source File: startupplanner.py From minemeld-core with Apache License 2.0 | 6 votes |
def plan(config, state_info): """Defines a startup plan for the MineMeld graph. Args: config (MineMeldConfig): config state_info (dict): state_info for each node Returns a dictionary where keys are node names and values the satrtup command for the node. """ plan = {} graph = _build_graph(config) for subgraph in nx.weakly_connected_component_subgraphs(graph, copy=True): plan.update(_plan_subgraph(subgraph, config, state_info)) return plan
Example #2
Source File: test_subgraph_copies.py From aws-kube-codesuite with Apache License 2.0 | 6 votes |
def setUp(self): self.undirected = [ nx.connected_component_subgraphs, nx.biconnected_component_subgraphs, ] self.directed = [ nx.weakly_connected_component_subgraphs, nx.strongly_connected_component_subgraphs, nx.attracting_component_subgraphs, ] self.subgraph_funcs = self.undirected + self.directed self.D = nx.DiGraph() self.D.add_edge(1, 2, eattr='red') self.D.add_edge(2, 1, eattr='red') self.D.nodes[1]['nattr'] = 'blue' self.D.graph['gattr'] = 'green' self.G = nx.Graph() self.G.add_edge(1, 2, eattr='red') self.G.nodes[1]['nattr'] = 'blue' self.G.graph['gattr'] = 'green'
Example #3
Source File: test_subgraph_copies.py From Carnets with BSD 3-Clause "New" or "Revised" License | 6 votes |
def setUp(self): self.undirected = [ nx.connected_component_subgraphs, nx.biconnected_component_subgraphs, ] self.directed = [ nx.weakly_connected_component_subgraphs, nx.strongly_connected_component_subgraphs, nx.attracting_component_subgraphs, ] self.subgraph_funcs = self.undirected + self.directed self.D = nx.DiGraph() self.D.add_edge(1, 2, eattr='red') self.D.add_edge(2, 1, eattr='red') self.D.nodes[1]['nattr'] = 'blue' self.D.graph['gattr'] = 'green' self.G = nx.Graph() self.G.add_edge(1, 2, eattr='red') self.G.nodes[1]['nattr'] = 'blue' self.G.graph['gattr'] = 'green'
Example #4
Source File: test_subgraph_copies.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 6 votes |
def setUp(self): self.undirected = [ nx.connected_component_subgraphs, nx.biconnected_component_subgraphs, ] self.directed = [ nx.weakly_connected_component_subgraphs, nx.strongly_connected_component_subgraphs, nx.attracting_component_subgraphs, ] self.subgraph_funcs = self.undirected + self.directed self.D = nx.DiGraph() self.D.add_edge(1, 2, eattr='red') self.D.add_edge(2, 1, eattr='red') self.D.node[1]['nattr'] = 'blue' self.D.graph['gattr'] = 'green' self.G = nx.Graph() self.G.add_edge(1, 2, eattr='red') self.G.node[1]['nattr'] = 'blue' self.G.graph['gattr'] = 'green'
Example #5
Source File: test_weakly_connected.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_weakly_connected_component_subgraphs(self): wcc = nx.weakly_connected_component_subgraphs cc = nx.connected_component_subgraphs for G, C in self.gc: U = G.to_undirected() w = {frozenset(g) for g in wcc(G)} c = {frozenset(g) for g in cc(U)} assert_equal(w, c)
Example #6
Source File: test_connected.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_connected_component_subgraphs(self): wcc = nx.weakly_connected_component_subgraphs cc = nx.connected_component_subgraphs for G, C in self.gc: U = G.to_undirected() w = {frozenset(g) for g in wcc(G)} c = {frozenset(g) for g in cc(U)} assert_equal(w, c)
Example #7
Source File: recognition.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def is_forest(G): """ Returns True if `G` is a forest. A forest is a graph with no undirected cycles. For directed graphs, `G` is a forest if the underlying graph is a forest. The underlying graph is obtained by treating each directed edge as a single undirected edge in a multigraph. Parameters ---------- G : graph The graph to test. Returns ------- b : bool A boolean that is True if `G` is a forest. Notes ----- In another convention, a directed forest is known as a *polyforest* and then *forest* corresponds to a *branching*. See Also -------- is_branching """ if len(G) == 0: raise nx.exception.NetworkXPointlessConcept('G has no nodes.') if G.is_directed(): components = nx.weakly_connected_component_subgraphs else: components = nx.connected_component_subgraphs return all(len(c) - 1 == c.number_of_edges() for c in components(G))
Example #8
Source File: test_weakly_connected.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_connected_raise(self): G = nx.Graph() assert_raises(NetworkXNotImplemented, nx.weakly_connected_components, G) assert_raises(NetworkXNotImplemented, nx.number_weakly_connected_components, G) assert_raises(NetworkXNotImplemented, nx.is_weakly_connected, G) # deprecated assert_raises(NetworkXNotImplemented, nx.weakly_connected_component_subgraphs, G)
Example #9
Source File: test_weakly_connected.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_weakly_connected_component_subgraphs(self): wcc = nx.weakly_connected_component_subgraphs cc = nx.connected_component_subgraphs for G, C in self.gc: U = G.to_undirected() w = {frozenset(g) for g in wcc(G)} c = {frozenset(g) for g in cc(U)} assert_equal(w, c)
Example #10
Source File: test_connected.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_connected_component_subgraphs(self): wcc = nx.weakly_connected_component_subgraphs cc = nx.connected_component_subgraphs for G, C in self.gc: U = G.to_undirected() w = {frozenset(g) for g in wcc(G)} c = {frozenset(g) for g in cc(U)} assert_equal(w, c)
Example #11
Source File: recognition.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def is_forest(G): """ Returns True if `G` is a forest. A forest is a graph with no undirected cycles. For directed graphs, `G` is a forest if the underlying graph is a forest. The underlying graph is obtained by treating each directed edge as a single undirected edge in a multigraph. Parameters ---------- G : graph The graph to test. Returns ------- b : bool A boolean that is True if `G` is a forest. Notes ----- In another convention, a directed forest is known as a *polyforest* and then *forest* corresponds to a *branching*. See Also -------- is_branching """ if len(G) == 0: raise nx.exception.NetworkXPointlessConcept('G has no nodes.') if G.is_directed(): components = nx.weakly_connected_component_subgraphs else: components = nx.connected_component_subgraphs return all(len(c) - 1 == c.number_of_edges() for c in components(G))
Example #12
Source File: test_weakly_connected.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 5 votes |
def test_weakly_connected_component_subgraphs(self): wcc = nx.weakly_connected_component_subgraphs cc = nx.connected_component_subgraphs for G, C in self.gc: U = G.to_undirected() w = {frozenset(g) for g in wcc(G)} c = {frozenset(g) for g in cc(U)} assert_equal(w, c)
Example #13
Source File: test_connected.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 5 votes |
def test_connected_component_subgraphs(self): wcc = nx.weakly_connected_component_subgraphs cc = nx.connected_component_subgraphs for G, C in self.gc: U = G.to_undirected() w = {frozenset(g) for g in wcc(G)} c = {frozenset(g) for g in cc(U)} assert_equal(w, c)
Example #14
Source File: graph_util.py From GEM with BSD 3-Clause "New" or "Revised" License | 5 votes |
def get_lcc(di_graph): di_graph = max(nx.weakly_connected_component_subgraphs(di_graph), key=len) tdl_nodes = list(di_graph.nodes()) nodeListMap = dict(zip(tdl_nodes, range(len(tdl_nodes)))) nx.relabel_nodes(di_graph, nodeListMap, copy=False) return di_graph, nodeListMap
Example #15
Source File: dagcircuit.py From quantumflow with Apache License 2.0 | 5 votes |
def components(self) -> List['DAGCircuit']: """Split DAGCircuit into independent components""" comps = nx.weakly_connected_component_subgraphs(self.graph) return [DAGCircuit(comp) for comp in comps]
Example #16
Source File: rel_graph.py From cloudify-dsl-parser with Apache License 2.0 | 5 votes |
def _handle_contained_in(ctx): # for each 'contained' tree, recursively build new trees based on # scaling groups with generated ids for contained_tree in nx.weakly_connected_component_subgraphs( ctx.plan_contained_graph.reverse(copy=True)): # extract tree root node id node_id = nx.topological_sort(contained_tree)[0] _build_multi_instance_node_tree_rec( node_id=node_id, contained_tree=contained_tree, ctx=ctx) ctx.deployment_contained_graph = ctx.deployment_node_graph.copy()
Example #17
Source File: utils_sig.py From fiber with BSD 2-Clause "Simplified" License | 5 votes |
def cluster_padding_nodes(cfg,addrs): nodes = [get_node_by_addr(cfg,x) for x in addrs] sg = cfg.subgraph(nodes) gs = [g for g in networkx.weakly_connected_component_subgraphs(sg)] return sorted(gs,key=lambda x:len(x))
Example #18
Source File: utils_sig.py From fiber with BSD 2-Clause "Simplified" License | 5 votes |
def ok_to_remove_node(g,n): g0 = copy.deepcopy(g) n0 = get_node_by_addr(g0,n.addr) g0.remove_node(n0) return len([x for x in networkx.weakly_connected_component_subgraphs(g0)]) <= 1
Example #19
Source File: graph_util.py From GEM-Benchmark with BSD 3-Clause "New" or "Revised" License | 5 votes |
def get_lcc(di_graph): di_graph = di_graph.to_undirected().to_directed() di_graph = max(nx.weakly_connected_component_subgraphs(di_graph), key=len) tdl_nodes = di_graph.nodes() nodeListMap = dict(zip(tdl_nodes, range(len(tdl_nodes)))) di_graph = nx.relabel_nodes(di_graph, nodeListMap, copy=True) return di_graph, nodeListMap
Example #20
Source File: weakly_connected.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 4 votes |
def weakly_connected_component_subgraphs(G, copy=True): """Generate weakly connected components as subgraphs. Parameters ---------- G : NetworkX graph A directed graph. copy: bool (default=True) If True make a copy of the graph attributes Returns ------- comp : generator A generator of graphs, one for each weakly connected component of G. Examples -------- Generate a sorted list of weakly connected components, largest first. >>> G = nx.path_graph(4, create_using=nx.DiGraph()) >>> G.add_path([10, 11, 12]) >>> [len(c) for c in sorted(nx.weakly_connected_component_subgraphs(G), ... key=len, reverse=True)] [4, 3] If you only want the largest component, it's more efficient to use max instead of sort. >>> Gc = max(nx.weakly_connected_component_subgraphs(G), key=len) See Also -------- strongly_connected_components connected_components Notes ----- For directed graphs only. Graph, node, and edge attributes are copied to the subgraphs by default. """ for comp in weakly_connected_components(G): if copy: yield G.subgraph(comp).copy() else: yield G.subgraph(comp)
Example #21
Source File: recognition.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 4 votes |
def is_forest(G): """ Returns ``True`` if ``G`` is a forest. A forest is a graph with no undirected cycles. For directed graphs, ``G`` is a forest if the underlying graph is a forest. The underlying graph is obtained by treating each directed edge as a single undirected edge in a multigraph. Parameters ---------- G : graph The graph to test. Returns ------- b : bool A boolean that is ``True`` if ``G`` is a forest. Notes ----- In another convention, a directed forest is known as a *polyforest* and then *forest* corresponds to a *branching*. See Also -------- is_branching """ if len(G) == 0: raise nx.exception.NetworkXPointlessConcept('G has no nodes.') if G.is_directed(): components = nx.weakly_connected_component_subgraphs else: components = nx.connected_component_subgraphs for component in components(G): # Make sure the component is a tree. if component.number_of_edges() != component.number_of_nodes() - 1: return False return True
Example #22
Source File: weakly_connected.py From aws-kube-codesuite with Apache License 2.0 | 4 votes |
def weakly_connected_component_subgraphs(G, copy=True): """Generate weakly connected components as subgraphs. Parameters ---------- G : NetworkX graph A directed graph. copy: bool (default=True) If True make a copy of the graph attributes Returns ------- comp : generator A generator of graphs, one for each weakly connected component of G. Raises ------ NetworkXNotImplemented: If G is undirected. Examples -------- Generate a sorted list of weakly connected components, largest first. >>> G = nx.path_graph(4, create_using=nx.DiGraph()) >>> nx.add_path(G, [10, 11, 12]) >>> [len(c) for c in sorted(nx.weakly_connected_component_subgraphs(G), ... key=len, reverse=True)] [4, 3] If you only want the largest component, it's more efficient to use max instead of sort: >>> Gc = max(nx.weakly_connected_component_subgraphs(G), key=len) See Also -------- weakly_connected_components strongly_connected_component_subgraphs connected_component_subgraphs Notes ----- For directed graphs only. Graph, node, and edge attributes are copied to the subgraphs by default. """ for comp in weakly_connected_components(G): if copy: yield G.subgraph(comp).copy() else: yield G.subgraph(comp)
Example #23
Source File: preprocess.py From EvalNE with MIT License | 4 votes |
def prep_graph(G, relabel=True, del_self_loops=True, maincc=True): r""" Preprocess a graphs according to the parameters provided. By default the (digraphs) graphs are restricted to their main (weakly) connected component. Trying to embed graphs with several CCs may cause some algorithms to put them infinitely far away. Parameters ---------- G : graph A NetworkX graph relabel : bool, optional Determines if the nodes are relabeled with consecutive integers 0..N del_self_loops : bool, optional Determines if self loops should be deleted from the graph. Default is True. maincc : bool, optional Determines if the graphs should be restricted to the main connected component or not. Default is True. Returns ------- G : graph A preprocessed NetworkX graph Ids : list of tuples A list of (OldNodeID, NewNodeID). Returns None if relabel=False. """ # Remove self loops if del_self_loops: G.remove_edges_from(G.selfloop_edges()) # Restrict graph to its main connected component if maincc: if G.is_directed(): Gcc = max(nx.weakly_connected_component_subgraphs(G), key=len) else: Gcc = max(nx.connected_component_subgraphs(G), key=len) else: Gcc = G # Relabel graph nodes in 0...N if relabel: Grl = nx.convert_node_labels_to_integers(Gcc, first_label=0, ordering='sorted') # A list of (oldNodeID, newNodeID) ids = list(zip(sorted(Gcc.nodes), sorted(Grl.nodes))) return Grl, ids else: return Gcc, None