我想了很多个小时这个问题,但我找不到任何解决方案
问题是:
给定图表有多少个MST (MST:=最小生成树)
图G是无向连通图
保证没有顶点的度数超过3:)
更喜欢C / C ++解决方案(也可以理解类似代码形成的算法)
请低位订购:)(运行时间)
更新
首先找到所有的MST :) O(| E | log | E |)
其他更糟糕的事情:(
答案 0 :(得分:0)
您可以尝试回溯。在“叶子”顶点之一的每个步骤中,决定要使用多少“出”边。
function add_some_vertex_edges(leaves, edge):
if |leaves| == |V|:
num_of_MSTs += 1
return
v = leaves.pop() # takes one vertex and removes it from leaves
let v_edge be set of incident edges to v that are not already in edges and that don't make cycle if added to edges
for each combination of edges from v_edge:
add_edges(leaves + incident_vertices, edges + edge_combination)
add_some_vertex_edges({v}, {})
由于顶点度数<= 3,因此对于每个叶子,一个边缘用于“输入”它,因此| v_edge | &lt; = 2,因为搜索树很窄。
最坏情况下的运行时间为O(2 ^ n),但在实际情况下可能很快。有最坏情况下的MST数量的例子。我能想到的例子是两条平行的顶点线和它们之间的连接。
O---O---O---O---O---O---O---O---O---
\ / \ / \ / \ / \ /
X X X X X ...
/ \ / \ / \ / \ / \
O---O---O---O---O---O---O---O---O---
在这个例子中,恰好有2 ^(n / 2)个MST。要计算这个,请取两个最左边的顶点。它们可以通过4种方式连接到图表的其余部分。
O---O O O O O O---O
\ / \ / \ /
X X X
/ \ / \ / \
O---O , O O , O---O , O O
对于每组互连的4个顶点,有4 = 2 ^ 2种可供选择的可能性。