-
最小生成树--prime
-
- Vertex FindMinDist( MGraph Graph, WeightType dist[] )
- {
- Vertex MinV, V;
- WeightType MinDist = INFINITY;
-
- for (V=0; V<Graph->Nv; V++) {
- if ( dist[V]!=0 && dist[V]<MinDist) {
-
- MinDist = dist[V];
- MinV = V;
- }
- }
- if (MinDist < INFINITY)
- return MinV;
- else return ERROR;
- }
-
- int Prim( MGraph Graph, LGraph MST )
- {
- WeightType dist[MaxVertexNum], TotalWeight;
- Vertex parent[MaxVertexNum], V, W;
- int VCount;
- Edge E;
-
-
- for (V=0; V<Graph->Nv; V++) {
-
- dist[V] = Graph->G[0][V];
- parent[V] = 0;
- }
- TotalWeight = 0;
- VCount = 0;
-
- MST = CreateGraph(Graph->Nv);
- E = (Edge)malloc( sizeof(struct ENode) );
-
-
- dist[0] = 0;
- VCount ++;
- parent[0] = -1;
-
- while (1) {
- V = FindMinDist( Graph, dist );
-
- if ( V==ERROR )
- break;
-
-
- E->V1 = parent[V];
- E->V2 = V;
- E->Weight = dist[V];
- InsertEdge( MST, E );
- TotalWeight += dist[V];
- dist[V] = 0;
- VCount++;
-
- for( W=0; W<Graph->Nv; W++ )
- if ( dist[W]!=0 && Graph->G[V][W]<INFINITY ) {
-
- if ( Graph->G[V][W] < dist[W] ) {
-
- dist[W] = Graph->G[V][W];
- parent[W] = V;
- }
- }
- }
- if ( VCount < Graph->Nv )
- TotalWeight = ERROR;
- return TotalWeight;
- }
-
- typedef Vertex ElementType;
- typedef Vertex SetName;
- typedef ElementType SetType[MaxVertexNum];
-
- void InitializeVSet( SetType S, int N )
- {
- ElementType X;
-
- for ( X=0; X<N; X++ ) S[X] = -1;
- }
-
- void Union( SetType S, SetName Root1, SetName Root2 )
- {
-
- if ( S[Root2] < S[Root1] ) {
- S[Root2] += S[Root1];
- S[Root1] = Root2;
- }
- else {
- S[Root1] += S[Root2];
- S[Root2] = Root1;
- }
- }
-
- SetName Find( SetType S, ElementType X )
- {
- if ( S[X] < 0 )
- return X;
- else
- return S[X] = Find( S, S[X] );
- }
-
- bool CheckCycle( SetType VSet, Vertex V1, Vertex V2 )
- {
- Vertex Root1, Root2;
-
- Root1 = Find( VSet, V1 );
- Root2 = Find( VSet, V2 );
-
- if( Root1==Root2 )
- return false;
- else {
- Union( VSet, Root1, Root2 );
- return true;
- }
- }
-
- void PercDown( Edge ESet, int p, int N )
- {
-
- int Parent, Child;
- struct ENode X;
-
- X = ESet[p];
- for( Parent=p; (Parent*2+1)<N; Parent=Child ) {
- Child = Parent * 2 + 1;
- if( (Child!=N-1) && (ESet[Child].Weight>ESet[Child+1].Weight) )
- Child++;
- if( X.Weight <= ESet[Child].Weight ) break;
- else
- ESet[Parent] = ESet[Child];
- }
- ESet[Parent] = X;
- }
-
- void InitializeESet( LGraph Graph, Edge ESet )
- {
- Vertex V;
- PtrToAdjVNode W;
- int ECount;
-
-
- ECount = 0;
- for ( V=0; V<Graph->Nv; V++ )
- for ( W=Graph->G[V].FirstEdge; W; W=W->Next )
- if ( V < W->AdjV ) {
- ESet[ECount].V1 = V;
- ESet[ECount].V2 = W->AdjV;
- ESet[ECount++].Weight = W->Weight;
- }
-
- for ( ECount=Graph->Ne/2; ECount>=0; ECount-- )
- PercDown( ESet, ECount, Graph->Ne );
- }
-
- int GetEdge( Edge ESet, int CurrentSize )
- {
-
-
- Swap( &ESet[0], &ESet[CurrentSize-1]);
-
- PercDown( ESet, 0, CurrentSize-1 );
-
- return CurrentSize-1;
- }
-
-
- int Kruskal( LGraph Graph, LGraph MST )
- {
- WeightType TotalWeight;
- int ECount, NextEdge;
- SetType VSet;
- Edge ESet;
-
- InitializeVSet( VSet, Graph->Nv );
- ESet = (Edge)malloc( sizeof(struct ENode)*Graph->Ne );
- InitializeESet( Graph, ESet );
-
- MST = CreateGraph(Graph->Nv);
- TotalWeight = 0;
- ECount = 0;
-
- NextEdge = Graph->Ne;
- while ( ECount < Graph->Nv-1 ) {
- NextEdge = GetEdge( ESet, NextEdge );
- if (NextEdge < 0)
- break;
-
- if ( CheckCycle( VSet, ESet[NextEdge].V1, ESet[NextEdge].V2 )==true ) {
-
- InsertEdge( MST, ESet+NextEdge );
- TotalWeight += ESet[NextEdge].Weight;
- ECount++;
- }
- }
- if ( ECount < Graph->Nv-1 )
- TotalWeight = -1;
-
- return TotalWeight;
- }
-
相关阅读:
c++笔试题3
C++笔试题
C++编程指南续(10-11)
C++详解(8-9)
C++编程指南(6-7)
C++编程指南续(4-5)
C++编程指南续
C++的编程指南
HPSocket介绍与使用
WinForm中TreeView控件实现鼠标拖动节点(可实现同级节点位置互换,或拖到目标子节点)
-
原文地址:https://www.cnblogs.com/jundima/p/10186158.html
Copyright © 2020-2023
润新知