如何阻止Networkx在边缘将头和尾节点(u,v)更改为(v,u)的顺序? [英] How to stop Networkx from changing the order of head and tail nodes(u,v) to (v,u) in an edge?

查看:128
本文介绍了如何阻止Networkx在边缘将头和尾节点(u,v)更改为(v,u)的顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用networkx创建的简单图形.

I've got a simple graph created using networkx.

import networkx as nx
import matplotlib.pyplot as plt
from pprint import pprint

G = nx.Graph()
head_nodes = range(0, 9)
tail_nodes = range(1, 10)
edge_ls = list(zip(head_nodes, tail_nodes))
G.add_nodes_from(range(0, 10))
G.add_edges_from(edge_ls)
pprint(G.nodes())
nx.draw(G)
plt.show()

我想删除节点0和1之间的边缘,并添加三个新节点(例如,节点10、11、12).然后,边缘有 在节点0和10、10和11、11和2之间创建.

I want to remove the edge between node 0 and 1 and add three new nodes (say node 10,11,12). Then, edges have to be created between node 0 and 10, 10 and 11, 11 and 2.

我正在使用G.remove_edge(0,1)删除节点0和1之间的边.

I'm using G.remove_edge(0,1) to remove the edge between node 0 and 1.

有人可以建议可以使用哪个功能来添加n个新节点吗?

Could someone suggest which function can be used to add n new nodes?

此外,如果添加了n个新节点,这些节点是否会自动编号?

Also, if n new nodes are added, will these nodes be numbered automatically?

我打算循环执行此操作,删除两个节点之间已经存在的边,并添加n个新节点和连接这些节点的边.

I intend to do this in a loop, delete an edge that already exists between two nodes and add n new nodes and edges connecting these nodes.

我尝试了以下方法来添加n新边缘

I tried the following to add n new edges

G = nx.Graph()
head_nodes = range(0, 9)
tail_nodes = range(1, 10)
edge_ls = list(zip(head_nodes, tail_nodes))
G.add_nodes_from(range(0, 10))
G.add_edges_from(edge_ls)

head = 0
tail = 1
G.remove_edge(head, tail)
Nnodes = G.number_of_nodes()
newnodes = [head, Nnodes+1, Nnodes+2, Nnodes+3, tail] # head and tail already exists
newedges = [(x, y) for x, y in zip(newnodes[0:len(newnodes)-1], newnodes[1:len(newnodes)])]
G.add_edges_from(newedges)
pprint(G.edges())

输出:

EdgeView([(0, 11), (1, 2), (1, 13), (2, 3), (3, 4), (4, 5), (5, 6), (6, 7), (7, 8), (8, 9), (11, 12), (12, 13)])

Expected Output:
EdgeView([(0, 11), (1, 2), (13, 1), (2, 3), (3, 4), (4, 5), (5, 6), (6, 7), (7, 8), (8, 9), (11, 12), (12, 13)])

我不确定为什么将按(13,1)(头,尾)顺序添加的边存储为(1,13).关于在添加新边时如何保留头和尾节点的顺序有什么建议吗?

I'm not sure why the edge that was added in the order (13,1)(head, tail) is stored as (1,13). Any suggestion on how to preserve the order of head and tail node while adding a new edge?

用nx.OrderedGraph()替换nx.Graph()也无济于事.

replacing nx.Graph() with nx.OrderedGraph() also doesn't help.

推荐答案

Graph是无向图,其中(1, 13)(13, 1)表示同一事物,边缘没有箭头".

A Graph is an undirected graph, where (1, 13) and (13, 1) mean the same thing, the edges have no 'arrows'.

您想要的是DiGraph,表示有向图.参见 https://networkx.github.io/documentation/stable/reference /classes/index.html

What you want is a DiGraph, meaning a directed graph. See https://networkx.github.io/documentation/stable/reference/classes/index.html

OrderedGraph是另一回事-它只是意味着当您遍历节点和边缘时,它们以特定的顺序出现(类似于列表与集合).

An OrderedGraph is something else - it just means that when you iterate over nodes and edges, they come out in a particular order (similar to lists vs sets).

这篇关于如何阻止Networkx在边缘将头和尾节点(u,v)更改为(v,u)的顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆