本文整理汇总了Python中networkx.induced_subgraph方法的典型用法代码示例。如果您正苦于以下问题:Python networkx.induced_subgraph方法的具体用法?Python networkx.induced_subgraph怎么用?Python networkx.induced_subgraph使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类networkx
的用法示例。
在下文中一共展示了networkx.induced_subgraph方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _parse_loops_from_graph
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import induced_subgraph [as 别名]
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
示例2: op
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import induced_subgraph [as 别名]
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
示例3: test_subgraph_of_subgraph
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import induced_subgraph [as 别名]
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)
示例4: test_restricted_induced_subgraph_chains
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import induced_subgraph [as 别名]
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)
示例5: test_subgraph_todirected
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import induced_subgraph [as 别名]
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)])
示例6: test_subgraph_toundirected
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import induced_subgraph [as 别名]
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)])
示例7: test_subgraph
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import induced_subgraph [as 别名]
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)
示例8: test_full_graph
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import induced_subgraph [as 别名]
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)
示例9: test_partial_subgraph
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import induced_subgraph [as 别名]
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: {}}})
示例10: test_subgraph_order
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import induced_subgraph [as 别名]
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]))
示例11: test_restricted_induced_subgraph_chains
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import induced_subgraph [as 别名]
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)
示例12: measure_paths
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import induced_subgraph [as 别名]
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,
}