python 在networkx中查找所有有向路径并将其保存为 Dataframe

xwbd5t1u  于 2023-03-28  发布在  Python
关注(0)|答案(1)|浏览(172)

我需要找到网络中的所有有向路径,如示例所示,并将有向路径保存在新的 Dataframe 中。
样品:

import pandas as pd
import networkx as nx
import matplotlib.pyplot as plt

sample_dict = {
    'target': ['A', 'A', 'B', 'B', 'F'],
    'source': ['B', 'E', 'C', 'D', 'G'],
}

sample_data = pd.DataFrame(sample_dict)

G = nx.from_pandas_edgelist(sample_data,
                         source='source',
                         target='target',
                         create_using=nx.DiGraph())

pos = nx.spring_layout(G)
nx.draw(G, pos, with_labels=True)
plt.show()

我已经厌倦了nx.weakly_connected_components,但是我不知道如何解释方向。

d = {}
for c in nx.weakly_connected_components(G):
    path= ','.join(sorted(c))
    for n in c:
        d[n] = path
attempt_data = pd.DataFrame(d.items())

    0   1
0   A   A,B,C,D,E
1   C   A,B,C,D,E
2   D   A,B,C,D,E
3   E   A,B,C,D,E
4   B   A,B,C,D,E
5   G   F,G
6   F   F,G

预期输出:

desired_dict = {
    'unit': ['A', 'A', 'A', 'B', 'B', 'C', 'D', 'E', 'F', 'G'],
    'group': ['A,B,C', 'A,B,D', 'A,E', 'A,B,C', 'A,B,D', 'A,B,C', 'A,B,D', 'A,E', 'F,G', 'F,G']
}

desired_data = pd.DataFrame(desired_dict)
print(desired_data)

  unit  group
0   A   A,B,C
1   A   A,B,D
2   A   A,E
3   B   A,B,C
4   B   A,B,D
5   C   A,B,C
6   D   A,B,D
7   E   A,E
8   F   F,G
9   G   F,G
1cosmwyk

1cosmwyk1#

我会尝试给予一个“丑陋”的解决方案。步骤是为了解释代码而注解的。但我已经使用了很多for循环,如果有人能改进这一点,我将不胜感激。

# Find the sources and targets nodes with degrees
sources = [x for x in G.nodes() if G.out_degree(x)==1 and G.in_degree(x)==0]
targets = [x for x in G.nodes() if G.out_degree(x)==0 and G.in_degree(x)>=1]

# Generate all the paths with the sources and targets
paths = []
for source_node in sources:
    for target_node in targets:
        path = list(nx.all_simple_paths(G, source=source_node, target=target_node))
        if len(path) > 0:
            paths.append(path[0])
            

# Find the corresponding path to the node             
unit_list = []
group_list = []

for node in G.nodes():
    for path in paths: 
        if node in path:
            unit_list.append(node)
            group_list.append(','.join(reversed(path)))

# Sort the output with the order of the nodes

sorted_list = list(zip(*sorted(zip(unit_list, group_list))))

desired_dict  = {'unit' : sorted_list[0],
                 'group' : sorted_list[1]}

desired_data = pd.DataFrame(desired_dict)

print(desired_data)

结果如你所愿:

unit  group
0    A  A,B,C
1    A  A,B,D
2    A    A,E
3    B  A,B,C
4    B  A,B,D
5    C  A,B,C
6    D  A,B,D
7    E    A,E
8    F    F,G
9    G    F,G

相关问题