最小生成树kruskal算法适合稀疏图(网络整理)8.1.3

上一篇实现了prim算法,适合稠密图,这次来实现另一种算法KRUSKAL算法

最小生成树kruskal算法适合稀疏图(网络整理)8.1.3_第1张图片

最小生成树kruskal算法适合稀疏图(网络整理)8.1.3_第2张图片

#include
#include
#include
#define MAXSIZE 100
typedef int Weight;
typedef char Datatype; 
typedef struct AdjVNode{
Weight weight;
int adjv;
struct AdjVNode *Next;
}*PtrToAdjVNode;
typedef struct Vnode{
Datatype data;
PtrToAdjVNode FirstEdge;/* 边表头指针 */  
}AdjList[MAXSIZE];
typedef struct Lnode{
int Nv;
int Ne;
AdjList G;
}*LGraph;
typedef struct Enode{
int v1;
int v2;
Weight weight;
//bool flag;//true -> false ->
}Edge;




LGraph  CreateGraph(int n){
LGraph Graph=(LGraph)malloc(sizeof(struct Lnode));
Graph->Nv=n;
Graph->Ne=0;
for(int i=0;iG[i].FirstEdge=NULL;

}

return Graph;
}

void InsertEdge(LGraph Graph,const Edge *ptre){
PtrToAdjVNode Newnode=(PtrToAdjVNode)malloc(sizeof(struct AdjVNode));
Newnode->weight=ptre->weight;
Newnode->adjv=ptre->v2;
Newnode->Next=Graph->G[ptre->v1].FirstEdge;
Graph->G[ptre->v1].FirstEdge=Newnode;
//若是无向边

Newnode=(PtrToAdjVNode)malloc(sizeof(struct AdjVNode));
Newnode->weight=ptre->weight;
Newnode->adjv=ptre->v1;
Newnode->Next=Graph->G[ptre->v2].FirstEdge;
Graph->G[ptre->v2].FirstEdge=Newnode;



}

LGraph  buildGraph(){
LGraph Graph;  
    Edge E;  
    int V;  
    int Nv, i;  
       
    scanf("%d", &Nv);   /* 读入顶点个数 */  
    Graph = CreateGraph(Nv); /* 初始化有Nv个顶点但没有边的图 */   
       
    scanf("%d", &(Graph->Ne));   /* 读入边数 */  
    if ( Graph->Ne != 0 ) { /* 如果有边 */   
         
        /* 读入边,格式为"起点 终点 权重",插入邻接矩阵 */  
        for (i=0; iNe; i++) {  
            scanf("%d %d %d", &E.v1, &E.v2, &E.weight);   
            /* 注意:如果权重不是整型,Weight的读入格式要改 */  
            InsertEdge( Graph, &E );  
        } 
    } 

/* 如果顶点有数据的话,读入数据 */  
    for (V=0; VNv; V++)   
        scanf(" %c", &(Graph->G[V].data));  
   
    return Graph;  


}

/*-------------------- 边的最小堆定义 --------------------*/
typedef Edge Ele;
const Ele ERROR={-1,-1,-1};
typedef struct heap{
Ele *data;
int size;
int maxsize;
}*MinHeap;
MinHeap createMinHeap(int n){
MinHeap minheap=(MinHeap)malloc(sizeof(struct heap));
minheap->data=(Ele*)malloc(sizeof(Ele)*(n+1));
minheap->size=0;
minheap->maxsize=n;
return minheap;

}
bool isfull(MinHeap heap){


    return heap->size>=heap->maxsize;
}
bool isempty(MinHeap heap){
if(heap==NULL)
heap->size =5;
    return heap->size<=0;
}

void InsertMinHeap(MinHeap heap,Ele e){
if(isfull(heap))
    return;
heap->size++;
int i;
for(i=heap->size;i>1&&e.weightdata[i/2].weight;i/=2)
heap->data[i]=heap->data[i/2];
heap->data[i]=e;

}

Ele DelMinHeap(MinHeap heap){
if(isempty(heap))
return ERROR;
Ele min=heap->data[1];
Ele tmp=heap->data[heap->size--];
int i=0,childmin;
for(i=1;2*i<=heap->size;i=childmin)
{
childmin=2*i;
if(childmin!=heap->size&&heap->data[childmin].weight>heap->data[childmin+1].weight)
childmin++;
if(tmp.weight<=heap->data[childmin].weight)break;
 heap->data[i]=heap->data[childmin];   
}
heap->data[i]=tmp;

return min;
}

void PercDown(MinHeap heap, int p){
 
 Ele tmp = heap->data[p]; /* 取出根结点存放的值 */
    int i=p,childmin;
for(;2*i<=heap->size;i=childmin)
{
childmin=2*i;
if(childmin!=heap->size&&heap->data[childmin].weight>heap->data[childmin+1].weight)
childmin++;
if(tmp.weight<=heap->data[childmin].weight)break;
 heap->data[i]=heap->data[childmin];   
}
heap->data[i]=tmp;


}
MinHeap buildMinHeap(Ele e[],int n){
MinHeap heap=createMinHeap(n);
heap->size=n;
for(int i=0;idata[i]=e[i];

for(int i=n/2;i>0;i--)
    PercDown(heap,i);

return heap;

}
/*-------------------- 边的最小堆定义end --------------------*/
/*-------------------- 顶点并查集定义 --------------------*/
typedef int ElementType; /* 默认元素可以用非负整数表示 */
typedef int SetName;     /* 默认用根结点的下标作为集合名称 */
typedef ElementType SetType[MAXSIZE]; /* 假设集合元素下标从0开始 */
 
void InitializeVSet( SetType S, int N )
{ /* 初始化并查集 */
    int X;
 
    for ( X=0; XNe);
for (int V=0; VNv; V++ )
        for ( W=Graph->G[V].FirstEdge; W; W=W->Next )
            if ( Vadjv) { /* 避免重复录入无向图的边,只收V1adjv;
                edge.weight=W->weight;
                //printf("abc %d\n",edge.weight );
                InsertMinHeap(edgeheap,edge);
            }
return edgeheap;
}

Edge GetEdge( MinHeap edgeheap){

    return DelMinHeap(edgeheap);
}


/*-------------------- 并查集定义结束 --------------------*/
    int Kruskal( LGraph Graph, LGraph MST )
    { /* 将最小生成树保存为邻接表存储的图MST,返回最小权重和 */
        Weight TotalWeight;
        int ECount;
        SetType VSet; /* 顶点并查集 */
        MinHeap edgeheap=NULL;    /* 边的最小堆 */
       // printf("%p\n",edgeheap );exit(0);
       Edge NextEdge;
        InitializeVSet( VSet, Graph->Nv ); /* 初始化顶点并查集 */
       
        edgeheap=InitializeESet( Graph ); /* 初始化边的最小堆 */

        /* 创建包含所有顶点但没有边的图。注意用邻接表版本 */
        MST = CreateGraph(Graph->Nv);
        TotalWeight = 0; /* 初始化权重和     */
        ECount = 0;      /* 初始化收录的边数 */
     
       
        while ( ECount < Graph->Nv-1 ) {  /* 当收集的边不足以构成树时 */
            NextEdge = GetEdge( edgeheap ); /* 从边的最小堆de到最小边 */
        //printf("%d\n", NextEdge.weight);
            if (NextEdge.v1==-1) /* 边集已空 */
                break;

            /* 如果该边的加入不构成回路,即两端结点不属于同一连通集 */
            if ( CheckCycle( VSet, NextEdge.v1, NextEdge.v2 )==true ) {
                /* 将该边插入MST */
                InsertEdge( MST, &NextEdge);
                printf("%d<->%d weight:%d collected\n",NextEdge.v1,NextEdge.v2,NextEdge.weight);
                TotalWeight += NextEdge.weight; /* 累计权重 */
                ECount++; /* 生成树中边数加1 */
            }
        }
        if ( ECount < Graph->Nv-1 )
            TotalWeight = -1; /* 设置错误标记,表示生成树不存在 */
     
        return TotalWeight;
    }







int main(){

LGraph Graph=buildGraph();
LGraph MST;
printf("-------------------------------------------\n");
printf("TotalWeight %d",Kruskal(Graph,MST));

    return 0;
}

最小生成树kruskal算法适合稀疏图(网络整理)8.1.3_第3张图片

你可能感兴趣的:(数据结构)