LazyPrim,Prim,Kruscal三个算法对同一个稀疏有权无向图进行最小生成树
0 [(0-5,wt:0.21)] 1 [(1-4,wt:0.32), (1-2,wt:0.32)] 2 [(2-3,wt:0.63), (2-4,wt:0.02), (2-1,wt:0.32)] 3 [(3-2,wt:0.63)] 4 [(4-1,wt:0.32), (4-2,wt:0.02)] 5 [(5-0,wt:0.21)] components: 2 no path no path no path no path 0 -> 5 -> ok ==================== lazyprim: mst: [(0-5,wt:0.21)] mst weight: 0.21 prim: mst: [(0-5,wt:0.21)] mst weight: 0.21 kruscal mst: [(2-4,wt:0.02), (0-5,wt:0.21), (4-1,wt:0.32), (2-3,wt:0.63)] mst weight: 1.18
可以看到,0和5联通,与其他定点不联通。
lazyprim 和prim 从0开始计算,最小生成树就只有一个edge
而kruscal直接把所有边都加到minHeap里,然后生成一个残缺的最小生成树