![](/img/trans.png)
[英]Easy method to find minimum number of nodes that connect to each node in a graph
[英]Given the number of total nodes and degrees of each node, is it possible to construct a graph?
以立方體為例,有8個節點, 12條邊,每個節點連接3個節點。
使用networkx ,我必須手動輸入所有邊緣。 例如,下面的代碼是構建一個包含二十面體所有邊的圖(12 個節點,30 個邊,每個節點 5 個相鄰節點)。
import networkx as nx
G = nx.Graph()
nodes = list(range(12))
edges = [
[0, 1], [0, 2], [0, 3], [0, 4], [0, 5],
[1, 2], [1, 6], [1, 10], [1, 5],
[2, 3], [2, 6], [2, 7],
[3, 4], [3, 7], [3, 8],
[4, 5], [4, 8], [4, 11],
[5, 11], [5, 10],
[6, 7], [6, 9], [6, 10],
[7, 8], [7, 9],
[8, 9], [8, 11],
[9, 10], [9, 11],
[10, 11],
]
G.add_nodes_from(nodes)
G.add_edges_from(edges)
我的問題是如何在不手動編寫的情況下獲得所有可能的邊緣。 每個節點的名稱可以隨機初始化。
據我所知,igraph 中的 Erdős–Rényi model無法約束相鄰節點。
from igraph import *
g = Graph.Erdos_Renyi(12, m=30, directed=False)
g.get_edgelist()
"""
[(0, 1),
(0, 2),
(1, 3),
(2, 3),
(0, 4),
(1, 4),
(3, 5),
(4, 6),
(5, 6),
(0, 7),
(2, 7),
(3, 7),
(6, 7),
(0, 8),
(1, 8),
(3, 8),
(0, 9),
(3, 9),
(4, 9),
(6, 9),
(0, 10), node10 has more than 5 edges.
(2, 10),
(3, 10),
(5, 10),
(7, 10),
(8, 10),
(1, 11),
(2, 11),
(4, 11),
(9, 11)]
"""
您不能僅知道頂點數、邊數和每個頂點的邊數來創建唯一的平面圖。 一個簡單的反例:
以您的立方體示例為例,每個頂點有 8 個頂點、12 條邊和 3 條邊,那么您可以擁有:
[
(A,B),(B,C),(C,D),(D,A), # top face of the cube.
(E,F),(F,G),(G,H),(H,E), # bottom face of the cube.
(A,E),(B,F),(C,G),(D,H) # sides of the cube connecting top and bottom.
]
這將創建一個具有 6 個面的平面圖,其中每個面都有 4 條邊。
您同樣可以擁有:
[
(A,B),(B,C),(C,D),(D,E),(E,F),(F,G),(G,H),(H,A), # cycle containing all vertices
(A,C), # connect vertices 2 apart on the cycle.
(B,D), # overlap previous and also connect vertices 2 apart on the cycle.
(E,G), # connect vertices 2 apart on the cycle.
(F,H) # overlap previous and also connect vertices 2 apart on the cycle.
]
這將創建一個平面圖,其中包含 4 個三角形面和 2 個以 6 條邊為界的面。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.