简体   繁体   English

在有向图中查找所有节点前驱

[英]Find all node predecessors in a directed graph

I need to find all the connected elements in column ID.我需要在列 ID 中找到所有连接的元素。

for eg My main Element is 4120882840 in ID Column.例如,我的主要元素是ID列中的4120882840

4120882840 is connected to 4120874920, 4120874720 (refer column ID2 ) likewise, 4120874920 is connected to 4121482000 which is further connected to 4121480930 and so on 4120882840连接到4120874920, 4120874720 (参见列ID2 )同样, 4120874920连接到4121482000进一步连接到4121480930等等

finally, all the elements that are connected to 4120882840 are [4120882840, 4120874920, 4121482000, 4121480930, 4121480780, 4120874720, 4120871840, 4120871830] total 8 in the list最后,所有连接到4120882840的元素是[4120882840, 4120874920, 4121482000, 4121480930, 4121480780, 4120874720, 4120871840, 4120871830]列表中共有 8 个

But I am only getting first 7 ie [4120882840, 4120874920, 4121482000, 4121480930, 4121480780, 4120874720, 4120871840]但我只得到前 7 个,即[4120882840, 4120874920, 4121482000, 4121480930, 4121480780, 4120874720, 4120871840]

file link https://drive.google.com/file/d/1E5_cbGjtKoB6RDSsC7ned-X2RFoF6Rad/view?usp=sharing文件链接https://drive.google.com/file/d/1E5_cbGjtKoB6RDSsC7ned-X2RFoF6Rad/view?usp=sharing

This is my Code这是我的代码

import pandas as pd
df = pd.read_csv("Test.csv")
ID = df.iloc[:,1] 
ID2 = df.iloc[:,2] 

x = [4120882840]
for i in range (len(ID)):
    for element in x:
        if element == ID2[i]:
            newID = ID[i]
            #print (newID)
            x.append (newID)
print (x)

It looks like you want to find all predecessors of the node in question.看起来您想要找到相关节点的所有前辈 This becomes clearer by inspecting the corresponding component subgraph:通过检查相应的组件子图,这一点变得更加清晰:

G = nx.from_pandas_edgelist(df, source='ID', target='ID2',
                                create_using=nx.DiGraph)

comps = nx.weakly_connected_components(G)
comp = next(comp for comp in comps if 4120882840 in comp)
H = nx.subgraph(G, comp)
plt.figure(figsize=(10,4))
nx.draw(H, node_color='lightgreen', with_labels=True, node_size=500)

在此处输入图像描述

We can use to find the node's predecessors.我们可以用它来查找节点的前辈。 NetworkX has nx.edge_dfs , where we can set orientation='reverse' to traverse every predecessor edge in reverse order ( upstream ). NetworkX 有nx.edge_dfs ,我们可以在其中设置orientation='reverse'以逆序(上游)遍历每个前驱边。 Then we can just flatten the returned list of tuples to obtain the corresponding nodes:然后我们可以将返回的元组列表展平以获得相应的节点:

from itertools import chain 
source = 4120882840

*n, _ = zip(*(nx.edge_dfs(G, source, orientation='reverse')))
print(set(chain.from_iterable(n)))
{4120874720, 4120871840, 4121480930, 4120874920, 4121480780, 
 4121482000, 4120871830, 4120882840}

Is this what you're looking for?这是你要找的吗?

import networkx as nx
graph = nx.from_pandas_edgelist(df, source='ID', target='ID2',
                                create_using=nx.DiGraph)
visited = set()
to_visit = [4120882840]
while to_visit:
    dst = to_visit.pop()
    visited.add(dst)
    for parent in graph.predecessors(dst):
        if parent in visited:
            continue
        to_visit.append(parent)
print(visited)

Output Output

{4120874720, 4120871840, 4121480930, 4120874920, 4121480780, 4121482000, 4120871830, 4120882840}

You can use mask to check if the element in ID2 and you can use list(set(x)) to remove duplicate您可以使用掩码检查ID2中的元素,您可以使用list(set(x))删除重复项

Here is an algorithm that work:这是一个有效的算法:

x = [4120882840]
lastLen = 0
while lastLen != len(x):
    lastLen = len(x)
    for i in x:
        x += list(df['ID'][df['ID2'] == i])
        x = list(set(x))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有向图节点:跟踪后继者和前任者 - Directed graph nodes: Keep track of successors and predecessors 如何从 python 中的循环有向图中获取节点的前辈/祖先列表? - How to get a predecessors/ancestor list of a node from cyclic directed graph in python? "如何在有向无环图中找到每个节点的所有父节点?" - How to find all the parent nodes of each node in a directed acyclic graph? 在有向图中查找结束节点 - Find end node in directed graph 在有向图和无向图中查找所有循环 - Find all cycles in directed and undirected graph NetworkX 在有向图中查找特定节点的 root_node - NetworkX find root_node for a particular node in a directed graph Python NetworkX从节点作为根在有向图中找到子图 - Python NetworkX find a subgraph in a Directed Graph from a node as root 使用 .networkX 查找节点前辈的最优雅方式 - Most elegant way to find node's predecessors with networkX 在有向加权图中,有效地找到两个节点 A 和 B 之间穿过节点 X 的最短路径的成本 - In a directed, weighted graph, efficiently find the cost of the shortest path between two nodes A and B that traverses through a node X 在定向加权图中查找最短路径,该路径访问每个节点,对重新访问节点和边缘没有限制 - Find shortest path in directed, weighted graph that visits every node with no restrictions on revisiting nodes and edges
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM