我再次更新了代码以确保一般情况下有效。我希望我没有让这变得比必要的更复杂,我觉得必须有一些更简单的实现,也许是依赖递归的。
无论如何,我已经产生了我认为可以接受的结果。虽然它不是您的直接代码,但我相信我已经按照您想要的基本解决方案实现了一些东西:
import matplotlib.pyplot as plt
import networkx as nx
from networkx import Graph
#We make a node class to track which node to modify (modify here means add children to.)
class Node:
def __init__(self, node_id, has_children, not_connected):
self.node_id = node_id
self.has_children = has_children
self.not_connected = not_connected
def get_min_not_connected(nodes_tracker):
smallest = float('inf')
for node in nodes_tracker:
#print(f"Is the node {node.node_id} not connected: {node.not_connected}")
if node.node_id < smallest and node.not_connected:
smallest = node.node_id
return smallest-1
def construction_step(G, node_id, num_children, nodes_tracker):
#print(f"The range is {len(nodes_tracker)+1} to {len(nodes_tracker)+num_children+1}")
#I am just creating new Node objects to track which connections have been made here. Note how the third parameter of not connected is True.
nodes_tracker = nodes_tracker + [Node(i,False,True) for i in range(len(nodes_tracker)+1, len(nodes_tracker)+num_children+1)]
for i in range(1, num_children+1):
print(f'adding edge relation ({node_id}, {get_min_not_connected(nodes_tracker)+i})')
#Here I am adding the child nodes to the parent ones.
G.add_edge(node_id, get_min_not_connected(nodes_tracker)+i)
for i in range(1, num_children+1):
#print(get_min_not_connected(nodes_tracker))
nodes_tracker[get_min_not_connected(nodes_tracker)].not_connected = False
return nodes_tracker
#Hardcode inputs for your specific example.
#I am using num_children in place of your D variable.
num_children=3
L=2
G=nx.Graph()
#Create the central (initial) node and setup
total_nodes = 0
#correct formula is like 2^0+2^1+...+2^L
for i in range(0,L):
total_nodes += num_children**i
print(total_nodes)
nodes_tracker = [Node(1,False,False)]
#Create the actual d-ary graph here.
for i in range(1, total_nodes+1):
nodes_tracker = construction_step(G, i, num_children, nodes_tracker)
#print(len(nodes_tracker))
nx.draw(G);
plt.show()
对于参数 D=2、L=3 的输出,我得到:
为了测试更一般的情况,我使用了 D=4,L=2,得到:
为了好玩,D=5,L=3:
它也适用于更大的 D 和 L,但图表自然看起来很丑。
感谢您对此回答的耐心等待,希望对您有所帮助。