简体   繁体   English

从有向图网络x上删除可逆边

[英]Remove reversible edges from directed graph networkx

Is there a way of removing reversible edges in a graph. 有没有一种方法可以消除图中的可逆边缘。 For instance, let's say the following graph 例如,假设下图

import networkx as nx
G=nx.DiGraph()
G.add_edge(1,2)
G.add_edge(2,3)
G.add_edge(2,1)
G.add_edge(3,1)
print (G.edges())

[(1, 2), (2, 3), (2,1), (3,1)]

I want to remove (2,1) and (3,1), since I want the graph to be directed in just one direction. 我想删除(2,1)和(3,1),因为我希望图形仅在一个方向上定向。 I know you can remove self-loops by using G.remove_edges_from(G.selfloop_edges()) but that's not the case here. 我知道您可以通过使用G.remove_edges_from(G.selfloop_edges())删除自循环,但事实并非如此。 The output I am looking for would be [(1, 2), (2, 3)] . 我正在寻找的输出将是[(1, 2), (2, 3)] Is there a way to remove this edges once the graph is created either by networkx or by other graph tool such as cytoscape?. 一旦通过networkx或其他图工具(例如cytoscape)创建了图,是否有办法消除这些边缘?

Method 1: 方法1:

remove duplicate entries in edgelist -> remove everything from graph -> add back single edges => graph with single edges 删除边列表中的重复条目->从图形中删除所有内容->添加单个边=>具有单个边的图

Edges are stored as tuples. 边存储为元组。 You could lose index information via temporary conversion to sets. 您可能会通过临时转换为集合而丢失索引信息。 You could then lose duplicate tuples, again, through temporary conversion to a set. 然后,通过临时转换为集合,您可能会丢失重复的元组。 After conversion back to a list, you have your list of edges, with duplicate entries removed, like so: 转换回列表后,您将获得边列表,并删除了重复的条目,如下所示:

stripped_list = list(set([tuple(set(edge)) for edge in G.edges()]))

Then remove all edges from the graph that are currently there, and add back those that are in the list just created: 然后从图中移除所有当前存在的边,然后重新添加刚创建的列表中的边:

G.remove_edges_from([e for e in G.edges()])
G.add_edges_from(stripped_list)

Method 2: 方法2:

find duplicate edges -> remove only those from graph => graph with single edges 查找重复的边->仅从图上删除那些边=>单边图

again, losing positional information via conversion to sets: 再次,通过转换为集合而丢失位置信息:

set_list = [set(a) for a in G.edges()] # collect all edges, lose positional information
remove_list = [] # initialise

for i in range(len(set_list)):
    edge = set_list.pop(0) # look at zeroth element in list:

    # if there is still an edge like the current one in the list, 
    # add the current edge to the remove list:
    if set_list.count(edge) > 0:
        u,v = edge 

        # add the reversed edge
        remove_list.append((v, u))

        # alternatively, add the original edge: 
        # remove_list.append((u, v))

G.remove_edges_from(remove_list) # remove all edges collected above

As far as I know, networkx does not store the order that edges were added in, so unless you want to write further logic, you either remove all duplicate edges going from nodes with lower number to nodes with higher number, or the other way round. 据我所知,networkx不会存储添加边的顺序,因此除非您要编写进一步的逻辑,否则您将删除所有重复的边(从编号较小的节点到编号较大的节点),或者反过来。

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

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