在Networkx中重新编号图中的顶点

huangapple go评论70阅读模式
英文:

Renumbering vertices in a graph in Networkx

问题

我使用以下代码生成了一个网络,N=2。然而,我想将顶点的编号从1改为0。我展示了当前和期望的输出。

import networkx as nx
N=2

def pos():
    x, y = 1, N + 3 - 1
    for _ in range(2 * N * (N + 1)):
        yield (x, y)
        y -= (x + 2) // (N + 3)
        x = (x + 2) % (N + 3)

G = nx.Graph()
it_pos = pos()
for u in range(2 * N * (N + 1)):
    G.add_node(u, pos=next(it_pos))
    if u % (2 * N + 1) < N:
        for v in (u - 2 * N - 1, u - N - 1, u - N):
            if G.has_node(v):
                G.add_edge(u, v)
    elif u % (2 * N + 1) == N:
        G.add_edge(u, u - N)
    elif u % (2 * N + 1) < 2 * N:
        for v in (u - 1, u - N - 1, u - N):
            G.add_edge(u, v)
    else:
        for v in (u - 1, u - N - 1):
            G.add_edge(u, v)

nx.draw(G, nx.get_node_attributes(G, 'pos'), with_labels=True, font_weight='bold')

当前输出为:

在Networkx中重新编号图中的顶点

期望的输出是:

在Networkx中重新编号图中的顶点

英文:

I am generating a network using the code below for N=2. However, I would like to renumber the vertices starting from 0 and not 1. I present the current and expected outputs.

import networkx as nx
N=2

def pos():
    x, y = 1, N + 3 - 1
    for _ in range(2 * N * (N + 1)):
        yield (x, y)
        y -= (x + 2) // (N + 3)
        x = (x + 2) % (N + 3)

G = nx.Graph()
it_pos = pos()
for u in range(2 * N * (N + 1)):
    G.add_node(u+1, pos=next(it_pos))
    if u % (2 * N + 1) &lt; N:
        for v in (u - 2 * N - 1, u - N - 1, u - N):
            if G.has_node(v + 1):
                G.add_edge(u + 1, v + 1)
    elif u % (2 * N + 1) == N:
        G.add_edge(u + 1, u - N + 1)
    elif u % (2 * N + 1) &lt; 2 * N:
        for v in (u - 1, u - N - 1, u - N):
            G.add_edge(u + 1, v + 1)
    else:
        for v in (u - 1, u - N - 1):
            G.add_edge(u + 1, v + 1)

nx.draw(G, nx.get_node_attributes(G, &#39;pos&#39;), with_labels=True, font_weight=&#39;bold&#39;)

The current output is

在Networkx中重新编号图中的顶点

The expected output is

在Networkx中重新编号图中的顶点

答案1

得分: 1

你可以使用nx.relabel_nodes函数(文档在这里):

import networkx as nx
N=2

def pos():
    x, y = 1, N + 3 - 1
    for _ in range(2 * N * (N + 1)):
        yield (x, y)
        y -= (x + 2) // (N + 3)
        x = (x + 2) % (N + 3)

G = nx.Graph()
it_pos = pos()
for u in range(2 * N * (N + 1)):
    G.add_node(u+1, pos=next(it_pos))
    if u % (2 * N + 1) &lt; N:
        for v in (u - 2 * N - 1, u - N - 1, u - N):
            if G.has_node(v + 1):
                G.add_edge(u + 1, v + 1)
    elif u % (2 * N + 1) == N:
        G.add_edge(u + 1, u - N + 1)
    elif u % (2 * N + 1) &lt; 2 * N:
        for v in (u - 1, u - N - 1, u - N):
            G.add_edge(u + 1, v + 1)
    else:
        for v in (u - 1, u - N - 1):
            G.add_edge(u + 1, v + 1)

#重新标记节点
G=nx.relabel_nodes(G,{i:i-1 for i in range(1,G.number_of_nodes()+1)},copy=False)

nx.draw(G, nx.get_node_attributes(G, 'pos'), with_labels=True, font_weight='bold') 
英文:

You could use the nx.relabel_nodes function (doc here):

import networkx as nx
N=2

def pos():
    x, y = 1, N + 3 - 1
    for _ in range(2 * N * (N + 1)):
        yield (x, y)
        y -= (x + 2) // (N + 3)
        x = (x + 2) % (N + 3)

G = nx.Graph()
it_pos = pos()
for u in range(2 * N * (N + 1)):
    G.add_node(u+1, pos=next(it_pos))
    if u % (2 * N + 1) &lt; N:
        for v in (u - 2 * N - 1, u - N - 1, u - N):
            if G.has_node(v + 1):
                G.add_edge(u + 1, v + 1)
    elif u % (2 * N + 1) == N:
        G.add_edge(u + 1, u - N + 1)
    elif u % (2 * N + 1) &lt; 2 * N:
        for v in (u - 1, u - N - 1, u - N):
            G.add_edge(u + 1, v + 1)
    else:
        for v in (u - 1, u - N - 1):
            G.add_edge(u + 1, v + 1)

#relabelling nodes
G=nx.relabel_nodes(G,{i:i-1 for i in range(1,G.number_of_nodes()+1)},copy=False)

nx.draw(G, nx.get_node_attributes(G, &#39;pos&#39;), with_labels=True, font_weight=&#39;bold&#39;) 

在Networkx中重新编号图中的顶点

huangapple
  • 本文由 发表于 2023年2月19日 18:39:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75499523.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定