- 最小生成树(Minimum Span Tree):对于带权无向连通图。所有节点都连通且总权值最小。应用:电缆布线、网络、电路设计
- 找V-1条边,连接V个顶点,总权值最小
- 切分定理(Cut Property):给定任意切分,横切边中权值最小的边必属于最小生成树
- 切分:把图中节点分为两部分
- 横切边:边的两个端点属于切分的不同两边
- 证明:反证法,假设横切边中一条权值不是最小的边属于最小生成树,给生成树添加横切边中权值最小的边形成环,删掉权值不是最小的边打破环得到新的权值更小的生成树,与假设矛盾
- 实现:从一个点开始,不断扩散,求出最小生成树
main.cpp(测试有权图)
1 #include <iostream> 2 #include <iomanip> 3 #include "SparseGraph.h" 4 #include "DenseGraph.h" 5 #include "ReadGraph.h" 6 #include "Component.h" 7 #include "Path.h" 8 #include "ShortestPath.h" 9 10 using namespace std; 11 12 int main(){ 13 14 string filename = "testG1.txt"; 15 int V = 8; 16 cout<<fixed<<setprecision(2); 17 18 // Test Weighted Dense Graph 19 DenseGraph<double> g1 = DenseGraph<double>(V,false); 20 ReadGraph<DenseGraph<double>,double> readGraph(g1, filename); 21 g1.show(); 22 cout<<endl; 23 24 // Test Weighted Dense Graph 25 SparseGraph<double> g2 = SparseGraph<double>(V,false); 26 ReadGraph<SparseGraph<double>,double> SparseGraph(g2, filename); 27 g2.show(); 28 cout<<endl; 29 30 return 0; 31 }