如何在m个节点之间分别生成n条边

时间:2018-07-01 17:05:03

标签: graph nodes edges

我在概念化问题时遇到了麻烦。但实际上,如果我有m个节点,并且希望为每个节点生成不超过n个连接。我还想确保从每个节点到任何其他节点始终存在一条路径。我不在乎周期。

我没有合适的词汇来发现这个问题,尽管我确信它必须存在于某个地方。

有人知道这个问题的解释地点吗,还是自己知道答案?

1 个答案:

答案 0 :(得分:0)

最简单的方法是构造生成树以确保所有节点都已连接,然后添加不违反每个节点的最大边缘数的边,直到获得目标数目。用伪代码:

// nodes[] is a list of all m nodes in our graph
connected_nodes = nodes[0];
// add nodes one by one until all are in the spanning tree
for next_node = 1 to (m-1)
   repeat
      select node connected_nodes[k]   // randomly, nearest, smallest degree, whatever
   until degree(k) < n   // make sure node to connect to does not violate max degree
   add edge between nodes[next_node] and new node connected_nodes[k]
   add nodes[next_node] to connected_nodes[]
end for

// the graph is now connected, add the desired number of edges
for e = m+1 to desired_edge_count
   select 2 nodes i,j from connected_nodes, each with degree < n
   add edge between nodes i and j
end for