简体   繁体   English

如何在python中从有向非循环图中找到边层次结构图

[英]How to find edges hierarchy graph from directed acyclic graph in python

I have data as following: 我有以下数据:
在此输入图像描述

I want a new graph as follows: 我想要一个新的图表如下:
在此输入图像描述
Which represents edges hierarchy of the previous graph. 表示上一个图的边缘层次结构。

You want to construct the graph with edges between nodes in original graph as nodes and nodes in original graphs as edges, as I understand. 您想构建原始图中节点之间边缘的图形作为节点,原始图形中的节点作为边缘,我理解。 In this case, your graph in post is incorrect. 在这种情况下,帖子中的图表不正确。 For example: you have node be , which is a successor of ae , but in original graph it is not true, ae and be are parallel. 例如:你有节点be ,这是一个继任者ae ,但在原图这是不正确的, aebe是平行的。 I constructed a code to solve this problem: 我构造了一个代码来解决这个问题:

import networkx as nx
import random
from itertools import groupby

# Create a random DAG
G = nx.gnp_random_graph(10,0.3,directed=True)
DAG = nx.DiGraph([(u,v) for (u,v) in G.edges() if u<v])

res = nx.DiGraph()
# Create nodes in new DAG as edges in original DAG
res.add_nodes_from(list(DAG.edges()))
sorted_res_nodes = sorted(res.nodes, key=lambda x: x[1])

# Connect all nodes with end of node1 is equal to start of node2
for n1 in sorted_res_nodes:
    for n2 in sorted_res_nodes:
        if n1[1] == n2[0]:
            res.add_edge(n1, n2)

# Draw graphs
nx.draw(
    DAG,
    with_labels=True,
    pos=nx.drawing.nx_agraph.graphviz_layout(
        DAG, prog='dot'
    )
)
nx.draw(
    res,
    with_labels=True,
    pos=nx.drawing.nx_agraph.graphviz_layout(
        res, prog='dot'
    )
)

For this graph: 对于此图:

在此输入图像描述

The new graph will be constructed: 将构建新图:

在此输入图像描述

Note, that all edges from roots in original DAG transformed into roots in new DAG. 请注意,原始DAG中根的所有边都转换为新DAG中的根。 Also note that for every edge the end of the start node is equal to the start of the end node. 另请注意,对于每个边,起始节点的末尾等于结束节点的起点。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM