Python Networkx对无向图的边只计数一次

Python Networkx对无向图的边只计数一次,python,graph,networkx,Python,Graph,Networkx,我有以下图表: full_graph = nx.Graph() tgt_nodes = ['B','F'] full_graph.add_edge('A','B') full_graph.add_edge('B','C') full_graph.add_edge('B','D') full_graph.add_edge('B','E') full_graph.add_edge('E','F') #display and save as img p = nx.drawing.nx_pydot

我有以下图表:

full_graph = nx.Graph()

tgt_nodes = ['B','F']

full_graph.add_edge('A','B')
full_graph.add_edge('B','C')
full_graph.add_edge('B','D')
full_graph.add_edge('B','E')
full_graph.add_edge('E','F')

#display and save as img
p = nx.drawing.nx_pydot.to_pydot(full_graph)
p.layout = 'spring'

#p.write_png(outputDir+ version+ '//' + 'cluster_no' + str(clusterNo) + '.png')
display.Image(p.create_png())

我试图找到所有的节点,它们正好是两个连接,并根据重量来处理它们

out_graph = nx.Graph()

for curr_node in tgt_nodes:

        #find all paths for curr_node that are <=2
        pot_paths = nx.single_source_dijkstra_path_length(full_graph, curr_node,2)
        print(pot_paths)

        #iterate over all potential paths. If length ==2 either increment weight or add with weight = 1
        for pot_node, dist in pot_paths.items():

            if dist == 2:
                print(pot_node)

                if out_graph.has_edge(curr_node, pot_node):

                    # we added this one before, just increase the weight by one. NEED TO LIMIT SO THAT THIS DOESN't TRIGGER ON INVERSES
                    out_graph[curr_node][pot_node]['weight'] += 1
                    print('incremented edge for '+ curr_node)
                else:
                    # new edge. add with weight=1
                    out_graph.add_edge(curr_node, pot_node, weight=1)
                    print('added edge for '+ pot_node)
如何在同一中间节点上修改so(F,B)和(B,F)计数一次,而不是两次

谢谢

编辑

实际上,这里有一个不起作用的例子:

full_graph = nx.Graph()

tgt_nodes = ['B','F']

full_graph.add_edge('A','B')
full_graph.add_edge('B','C')
full_graph.add_edge('B','D')
full_graph.add_edge('B','E')
full_graph.add_edge('E','F')
full_graph.add_edge('G','F')
full_graph.add_edge('B','G')
full_graph.add_edge('F','H')
full_graph.add_edge('B','H')


#display and save as img
p = nx.drawing.nx_pydot.to_pydot(full_graph)
p.layout = 'spring'

#p.write_png(outputDir+ version+ '//' + 'cluster_no' + str(clusterNo) + '.png')
display.Image(p.create_png())

当它应该是3时,它输出一条2的边(B&F通过G、E和H连接)

您根据
full\u graph
确定路径长度,然后将边添加到
out\u graph
。将(B,F)边添加到
out\u图
后,它仍然不存在于
full\u图
中。毕竟,您的
out\u图
被初始化为空。这也是为什么末端的
out\u图
只包含一条边(B,F)


如果您的目标是更新原始图形,则应将
out\u graph
初始化为
full\u graph
的副本,并在此基础上进行所有计算。根据您的需要,networkx的内置功能可能会起作用,否则您应该研究Python。

因此,是的,如果
G
是无向的,那么
G.has_edge(a,b)
G.has_edge(b,a)
都是
True
如果边存在,则
False

因此,给定代码的设置方式,您将两次查看每对节点,并两次执行相同的计算


再添加一个条件怎么样:
如果G.根据@Energya的请求有_边(a,b)和a——下面是简单的代码:

out_graph = nx.bipartite.weighted_projected_graph(full_graph, ['B','F'])

在你的代码中,
tgt_节点的值是多少?第二行代码:tgt_节点=['B','F']注:在你的编辑中,
pot_路径={'B':0,'A':1,'C':1,'D':1,'E':1,'G':1,'H':1,'F':2}
。没有任何东西能说明在最小距离下有多少种不同的方式。它只知道最小距离。完整图中存在不正确的-(B>E>F),因此应将B>F添加到外部图中。问题是,它也在查找(F>E>B)并重新递增B>F。我需要一种方法来跟踪我在到达F>E>B时已经穿过B>E>F的节点。将这些节点视为医生和患者。医生只能治疗病人,病人的病情只有医生才能看到。我想知道哪些医生在他们的实践中有重叠,所以我看了看谁在两个节点之外,并将它们添加到out_图中。你是对的,我最初的答案是不正确的,我现在知道你在做什么了。那么,您的问题在性质上似乎有所不同:只有在
out\u图
中的两个边之间的距离为2 in
full\u图
时,才能生成边in
out\u图。那么,从定义上讲,这是否意味着只要它存在于
out_graph
中,就可以保证看到前面找到的一对的倒数?如果你的图是显式的二分图(即,由两组从未直接连接的节点组成),你可能真的想看看networkx对它的一些支持,比如函数。你是对的-我需要走二分路线。谢谢-这应该行得通-阅读@energya关于二分的评论-这是一个明确的二分图,所以“正确”的方法可能是使用二分相关算法,但我会选择这个,因为它更容易。我正要添加一组排序的节点并与之进行比较,但这更有效。tgt_节点列表都可能相互匹配,因此两者都会出现在那里。实际上这不起作用。它没有考虑到可能存在多个中间节点这一事实。我在上面编辑了一个不起作用的例子,它显示的权重是2,而它应该是3。问题是,如果你看这个例子,我在你的代码中看不到任何东西,告诉它去看两者之间所有不同的路径。一旦您选择了
当前节点
,它只会看到每个
节点
一次。因此,我在这里描述的内容解决了从
a
b
以及从
b
a
的边计数问题,但如果要计算不同的路径,则需要做一些不同的事情。
out_graph = nx.bipartite.weighted_projected_graph(full_graph, ['B','F'])