Python networkx.induced_subgraph() Examples
The following are 19
code examples of networkx.induced_subgraph().
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: loopfinder.py From angr with BSD 2-Clause "Simplified" License | 6 votes |
def _parse_loops_from_graph(self, graph): """ Return all Loop instances that can be extracted from a graph. :param graph: The graph to analyze. :return: A list of all the Loop instances that were found in the graph. """ outtop = [] outall = [] for subg in ( networkx.induced_subgraph(graph, nodes).copy() for nodes in networkx.strongly_connected_components(graph)): if len(subg.nodes()) == 1: if len(list(subg.successors(list(subg.nodes())[0]))) == 0: continue thisloop, allloops = self._parse_loop_graph(subg, graph) if thisloop is not None: outall += allloops outtop.append(thisloop) return outtop, outall
Example #2
Source File: print_util.py From mac-graph with The Unlicense | 5 votes |
def measure_paths(row, vocab, node_from, node_to, avoiding, avoiding_property=1): graph = nx.Graph() def l(j): return vocab.inverse_lookup(j) only_using_nodes = [l(i[0]) for i in row["kb_nodes"] if l(i[avoiding_property]) != avoiding] + [node_from, node_to] for i in row["kb_nodes"]: graph.add_node( l(i[0]), attr_dict={"body": [l(j) for j in i]} ) for id_a, connections in enumerate(row["kb_adjacency"][:row["kb_nodes_len"]]): for id_b, connected in enumerate(connections[:row["kb_nodes_len"]]): if connected: node_a = row["kb_nodes"][id_a] node_b = row["kb_nodes"][id_b] edge = (l(node_a[0]), l(node_b[0])) graph.add_edge(*edge) induced_subgraph = nx.induced_subgraph(graph, only_using_nodes) try: shortest_path_avoiding = len(nx.shortest_path(induced_subgraph, node_from, node_to))-2 except: shortest_path_avoiding = None pass try: shortest_path = len(nx.shortest_path(graph, node_from, node_to)) -2 except: shortest_path = None pass return { "shortest_path": shortest_path, "shortest_path_avoiding": shortest_path_avoiding, }
Example #3
Source File: functional.py From clevr-graph with The Unlicense | 5 votes |
def op(self, graph, a:NodeSpec, b:NodeSpec, only_using_nodes:List[NodeSpec], fallback): try: induced_subgraph = nx.induced_subgraph(graph.gnx, [i["id"] for i in only_using_nodes + [a,b]]) return ids_to_nodes(graph, nx.shortest_path(induced_subgraph, a["id"], b["id"])) except nx.exception.NetworkXNoPath: return fallback
Example #4
Source File: test_ordered.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_subgraph_order(self): G = self.G G_sub = G.subgraph([1, 2, 3]) assert_equals(list(G.nodes), list(G_sub.nodes)) assert_equals(list(G.edges), list(G_sub.edges)) assert_equals(list(G.pred[3]), list(G_sub.pred[3])) assert_equals([2, 1], list(G_sub.pred[3])) assert_equals([], list(G_sub.succ[3])) G_sub = nx.induced_subgraph(G, [1, 2, 3]) assert_equals(list(G.nodes), list(G_sub.nodes)) assert_equals(list(G.edges), list(G_sub.edges)) assert_equals(list(G.pred[3]), list(G_sub.pred[3])) assert_equals([2, 1], list(G_sub.pred[3])) assert_equals([], list(G_sub.succ[3]))
Example #5
Source File: test_subgraphviews.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_partial_subgraph(self): G = self.K3 H = nx.induced_subgraph(G, 0) assert_equal(dict(H.adj), {0: {}}) assert_not_equal(dict(G.adj), {0: {}}) H = nx.induced_subgraph(G, [0, 1]) assert_equal(dict(H.adj), {0: {1: {}}, 1: {0: {}}})
Example #6
Source File: test_subgraphviews.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_full_graph(self): G = self.K3 H = nx.induced_subgraph(G, [0, 1, 2, 5]) assert_equal(H.name, G.name) self.graphs_equal(H, G) self.same_attrdict(H, G)
Example #7
Source File: test_function.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_subgraph(self): assert_equal(self.G.subgraph([0, 1, 2, 4]).adj, nx.subgraph(self.G, [0, 1, 2, 4]).adj) assert_equal(self.DG.subgraph([0, 1, 2, 4]).adj, nx.subgraph(self.DG, [0, 1, 2, 4]).adj) assert_equal(self.G.subgraph([0, 1, 2, 4]).adj, nx.induced_subgraph(self.G, [0, 1, 2, 4]).adj) assert_equal(self.DG.subgraph([0, 1, 2, 4]).adj, nx.induced_subgraph(self.DG, [0, 1, 2, 4]).adj) # subgraph-subgraph chain is allowed in function interface H = nx.induced_subgraph(self.G.subgraph([0, 1, 2, 4]), [0, 1, 4]) assert_is_not(H._graph, self.G) assert_equal(H.adj, self.G.subgraph([0, 1, 4]).adj)
Example #8
Source File: test_graphviews.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_subgraph_toundirected(self): SG = nx.induced_subgraph(self.G, [4, 5, 6]) SSG = SG.to_undirected() assert_equal(list(SSG), [4, 5, 6]) assert_equal(sorted(SSG.edges), [(4, 5), (5, 6)])
Example #9
Source File: test_graphviews.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_subgraph_todirected(self): SG = nx.induced_subgraph(self.G, [4, 5, 6]) SSG = SG.to_directed() assert_equal(sorted(SSG), [4, 5, 6]) assert_equal(sorted(SSG.edges), [(4, 5), (5, 4), (5, 6), (6, 5)])
Example #10
Source File: test_graphviews.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_restricted_induced_subgraph_chains(self): """ Test subgraph chains that both restrict and show nodes/edges. A restricted_view subgraph should allow induced subgraphs using G.subgraph that automagically without a chain (meaning the result is a subgraph view of the original graph not a subgraph-of-subgraph. """ hide_nodes = [3, 4, 5] hide_edges = [(6, 7)] RG = nx.restricted_view(self.G, hide_nodes, hide_edges) nodes = [4, 5, 6, 7, 8] SG = nx.induced_subgraph(RG, nodes) SSG = RG.subgraph(nodes) assert_is(SSG.root_graph, SSG._graph) assert_is_not(SG.root_graph, SG._graph) assert_edges_equal(SG.edges, SSG.edges) # should be same as morphing the graph CG = self.G.copy() CG.remove_nodes_from(hide_nodes) CG.remove_edges_from(hide_edges) assert_edges_equal(CG.edges(nodes), SSG.edges) CG.remove_nodes_from([0, 1, 2, 3]) assert_edges_equal(CG.edges, SSG.edges) # switch order: subgraph first, then restricted view SSSG = self.G.subgraph(nodes) RSG = nx.restricted_view(SSSG, hide_nodes, hide_edges) assert_is_not(RSG.root_graph, RSG._graph) assert_edges_equal(RSG.edges, CG.edges)
Example #11
Source File: test_graphviews.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_subgraph_of_subgraph(self): SGv = nx.subgraph(self.G, range(3, 7)) SDGv = nx.subgraph(self.DG, range(3, 7)) SMGv = nx.subgraph(self.MG, range(3, 7)) SMDGv = nx.subgraph(self.MDG, range(3, 7)) for G in self.graphs + [SGv, SDGv, SMGv, SMDGv]: SG = nx.induced_subgraph(G, [4, 5, 6]) assert_equal(list(SG), [4, 5, 6]) SSG = SG.subgraph([6, 7]) assert_equal(list(SSG), [6]) # subgraph-subgraph chain is short-cut in base class method assert_is(SSG._graph, G)
Example #12
Source File: test_ordered.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_subgraph_order(self): G = self.G G_sub = G.subgraph([1, 2, 3]) assert_equals(list(G.nodes), list(G_sub.nodes)) assert_equals(list(G.edges), list(G_sub.edges)) assert_equals(list(G.pred[3]), list(G_sub.pred[3])) assert_equals([2, 1], list(G_sub.pred[3])) assert_equals([], list(G_sub.succ[3])) G_sub = nx.induced_subgraph(G, [1, 2, 3]) assert_equals(list(G.nodes), list(G_sub.nodes)) assert_equals(list(G.edges), list(G_sub.edges)) assert_equals(list(G.pred[3]), list(G_sub.pred[3])) assert_equals([2, 1], list(G_sub.pred[3])) assert_equals([], list(G_sub.succ[3]))
Example #13
Source File: test_subgraphviews.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_partial_subgraph(self): G = self.K3 H = nx.induced_subgraph(G, 0) assert_equal(dict(H.adj), {0: {}}) assert_not_equal(dict(G.adj), {0: {}}) H = nx.induced_subgraph(G, [0, 1]) assert_equal(dict(H.adj), {0: {1: {}}, 1: {0: {}}})
Example #14
Source File: test_subgraphviews.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_full_graph(self): G = self.K3 H = nx.induced_subgraph(G, [0, 1, 2, 5]) assert_equal(H.name, G.name) self.graphs_equal(H, G) self.same_attrdict(H, G)
Example #15
Source File: test_function.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_subgraph(self): assert_equal(self.G.subgraph([0, 1, 2, 4]).adj, nx.subgraph(self.G, [0, 1, 2, 4]).adj) assert_equal(self.DG.subgraph([0, 1, 2, 4]).adj, nx.subgraph(self.DG, [0, 1, 2, 4]).adj) assert_equal(self.G.subgraph([0, 1, 2, 4]).adj, nx.induced_subgraph(self.G, [0, 1, 2, 4]).adj) assert_equal(self.DG.subgraph([0, 1, 2, 4]).adj, nx.induced_subgraph(self.DG, [0, 1, 2, 4]).adj) # subgraph-subgraph chain is allowed in function interface H = nx.induced_subgraph(self.G.subgraph([0, 1, 2, 4]), [0, 1, 4]) assert_is_not(H._graph, self.G) assert_equal(H.adj, self.G.subgraph([0, 1, 4]).adj)
Example #16
Source File: test_graphviews.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_subgraph_toundirected(self): SG = nx.induced_subgraph(self.G, [4, 5, 6]) SSG = SG.to_undirected() assert_equal(list(SSG), [4, 5, 6]) assert_equal(sorted(SSG.edges), [(4, 5), (5, 6)])
Example #17
Source File: test_graphviews.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_subgraph_todirected(self): SG = nx.induced_subgraph(self.G, [4, 5, 6]) SSG = SG.to_directed() assert_equal(sorted(SSG), [4, 5, 6]) assert_equal(sorted(SSG.edges), [(4, 5), (5, 4), (5, 6), (6, 5)])
Example #18
Source File: test_graphviews.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_restricted_induced_subgraph_chains(self): """ Test subgraph chains that both restrict and show nodes/edges. A restricted_view subgraph should allow induced subgraphs using G.subgraph that automagically without a chain (meaning the result is a subgraph view of the original graph not a subgraph-of-subgraph. """ hide_nodes = [3, 4, 5] hide_edges = [(6, 7)] RG = nx.restricted_view(self.G, hide_nodes, hide_edges) nodes = [4, 5, 6, 7, 8] SG = nx.induced_subgraph(RG, nodes) SSG = RG.subgraph(nodes) assert_is(RG._graph, self.G) assert_is(SSG._graph, self.G) assert_is(SG._graph, RG) assert_edges_equal(SG.edges, SSG.edges) # should be same as morphing the graph CG = self.G.copy() CG.remove_nodes_from(hide_nodes) CG.remove_edges_from(hide_edges) assert_edges_equal(CG.edges(nodes), SSG.edges) CG.remove_nodes_from([0, 1, 2, 3]) assert_edges_equal(CG.edges, SSG.edges) # switch order: subgraph first, then restricted view SSSG = self.G.subgraph(nodes) RSG = nx.restricted_view(SSSG, hide_nodes, hide_edges) assert_is_not(RSG._graph, self.G) assert_edges_equal(RSG.edges, CG.edges)
Example #19
Source File: test_graphviews.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_subgraph_of_subgraph(self): SGv = nx.subgraph(self.G, range(3, 7)) SDGv = nx.subgraph(self.DG, range(3, 7)) SMGv = nx.subgraph(self.MG, range(3, 7)) SMDGv = nx.subgraph(self.MDG, range(3, 7)) for G in self.graphs + [SGv, SDGv, SMGv, SMDGv]: SG = nx.induced_subgraph(G, [4, 5, 6]) assert_equal(list(SG), [4, 5, 6]) SSG = SG.subgraph([6, 7]) assert_equal(list(SSG), [6]) # subgraph-subgraph chain is short-cut in base class method assert_is(SSG._graph, G)