[英]networkX: ego_graph without sibling?
I have a directed with no-cycles.networkX graph.我有一个定向的 no-cycles.networkX 图。 I would like to create a subgraph with only all direct or direct predecessors of a given node n.我想创建一个仅包含给定节点 n 的所有直接或直接前辈的子图。 For instance, if n has 3 predecessors, a, b and c, I will also search for predecessors for each of those 3 nodes.例如,如果 n 有 3 个前任节点 a、b 和 c,我还将为这 3 个节点中的每一个节点搜索前任节点。
I am currently using the ego_graph method of.networkX, this works perfectly but the output also keeps sibling nodes with no direct access to my target node since it's an directed graph.我目前正在使用 .networkX 的 ego_graph 方法,这非常有效,但 output 也保留了无法直接访问我的目标节点的兄弟节点,因为它是一个有向图。
def draw(graph_path: Path, target: str, radius: int)
graph = nx.read_graphml(graphml)
subgraph = nx.ego_graph(graph, target, undirected=True, radius=radius)
draw_graph(subgraph, table)
My undirected
is set to False
because when I set it True
, it is only retuning my target
only, does not matter what the radius
value is.我的undirected
设置为False
因为当我将它设置为True
时,它只是重新调整我的target
,与radius
值是多少无关。
Target node is called CORE - SUPPLY CHAIN [DEV].20220128 AS_IS stock_V1norm.DIM calendar
and with a radius
of 1
:目标节点称为CORE - SUPPLY CHAIN [DEV].20220128 AS_IS stock_V1norm.DIM calendar
, radius
为1
:
The result is what I am expecting.结果是我所期待的。 Now, same target but with a radius
of 2
:现在,相同的目标,但radius
为2
:
The result is not what I was expecting since I am getting sibling and I only wants to get predecessors nodes such as:结果不是我所期望的,因为我正在获得兄弟姐妹并且我只想获得前任节点,例如:
graphML sample: graphML 示例:
<?xml version='1.0' encoding='utf-8'?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
<key id="d1" for="node" attr.name="kind" attr.type="string" />
<key id="d0" for="node" attr.name="zone" attr.type="string" />
<graph edgedefault="directed">
<node id="gold_core.customers">
<data key="d0">gold</data>
<data key="d1">core</data>>
</node>
<node id="rt.F0116">
<data key="d0">silver</data>>
</node>
<node id="hy.F4211">
<data key="d0">silver</data>
</node>
<edge
source="hy.F4211"
target="gold_core.customers"
/>
You can get the predecessors for a node using the DiGraph.predecessors
method.您可以使用DiGraph.predecessors
方法获取节点的前辈。
#!/usr/bin/env python
"""
Find predecessors to a given node.
"""
import matplotlib.pyplot as plt
import networkx as nx
from netgraph import Graph # pip install netgraph
# create a test graph
edges = [
('parent a', 'target'),
('parent b', 'target'),
('parent c', 'target'),
('grandparent aa', 'parent a'),
('grandparent bb', 'parent b'),
('grandparent cc', 'parent c'),
('parent a', 'sibling'),
('target', 'child')
]
g = nx.from_edgelist(edges, create_using=nx.DiGraph)
# get predecessors
parents = list(g.predecessors('target'))
grandparents = []
for parent in parents:
for grandparent in list(g.predecessors(parent)):
grandparents.append(grandparent)
predecessors = parents + grandparents
# give predecessors a red color
node_color = dict()
for node in g:
if node in predecessors:
node_color[node] = 'red'
else:
node_color[node] = 'white'
# plot
fig, (ax1, ax2) = plt.subplots(1, 2)
Graph(g,
node_layout='dot',
arrows=True,
node_color=node_color,
node_labels=True,
node_label_fontdict=dict(size=10),
node_label_offset=0.1,
ax=ax1
)
# plot subgraph
subgraph = g.subgraph(predecessors + ['target'])
Graph(subgraph,
node_layout='dot',
arrows=True,
node_labels=True,
node_label_fontdict=dict(size=10),
node_label_offset=0.1,
ax=ax2,
)
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.