Prim与Kruskal算法

  1. Prime算法
View Code
 

#include <stdio.h>
#include <stdlib.h>
#define MAX 100
/********ÁÚ½Ó¾ØÕó½á¹¹***************/
typedef struct graph
{
 char vers[MAX];
 int numVertex,numEdge;
 int arc[MAX][MAX];
}Graph;

void CreateGraph(Graph *G)
{
 int i,j;
 G->numEdge=10;
 G->numVertex=8;
 G->vers[0]='B';
 G->vers[1]='I';
 G->vers[2]='U';
 G->vers[3]='Y';
 G->vers[4]='T';
 G->vers[5]='G';
 G->vers[6]='C';
 G->vers[7]='D';

 for (i=0; i<8; ++i)
 {
  for (j=0; j<8; ++j)
  {
   G->arc[i][j]=MAX;
  }
 }
 G->arc[1][2]=1;
 G->arc[1][4]=2;
 G->arc[1][5]=3;
 G->arc[2][5]=4;
 G->arc[2][7]=5;
 G->arc[0][2]=6;
 G->arc[0][5]=7;
 G->arc[3][4]=8;
 G->arc[3][5]=9;
 G->arc[4][6]=10;
 for (i=0; i<8; ++i)
 {
  for (j=0; j<8; ++j)
  {
   G->arc[j][i]=G->arc[i][j];
  }
 }
}

void PrintGraph(Graph *G)
{
 int i, j;
 for (i=0; i<8; ++i)
 {
  for (j=0; j<8; ++j)
  {
   printf("%d ",G->arc[i][j]);
  }
  printf("\n");
 }
}

void Prim(Graph *G)
{
 int min,k,i,j;
 int lowcost[8];
 int vers[8];
 for (i=0; i<G->numVertex; i++)
 {
  lowcost[i]=G->arc[0][i];
  vers[i]=0;
 }
    printf("%c\n", G->vers[0]);
 for (i=1; i<G->numVertex; ++i)
 {
  min=MAX;
  j=1;
  k=0;
  while(j<G->numVertex)
  {
   if (lowcost[j] && lowcost[j]<min)
   {
    min=lowcost[j];
    k=j;
   }
   j++;
  }
  printf("(%c, %c)\n", G->vers[vers[k]], G->vers[k]);
  //printf("%c\n", G->vers[k]);
  lowcost[k]=0;
  for(j=1; j<G->numVertex; j++)
  {
   if (lowcost[j] && G->arc[k][j] < lowcost[j])
   {
    lowcost[j]=G->arc[k][j];
    vers[j]=k;
   }
  }
 }

}

int main()
{
 Graph G;
 CreateGraph(&G);
 Prim(&G);
 return 0;
}


Kruskal算法

View Code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX 100
/********邻接矩阵结构***************/
typedef struct graph
{
    char vers[MAX];
    int numVertex,numEdge;
    int arc[MAX][MAX];
}Graph;
/*******定义边******/
typedef struct
{
    int begin;
    int end;
    int weight;
}Edge;

void CreateGraph(Graph *G)
{
    int i,j;
    G->numEdge=10;
    G->numVertex=8;
    G->vers[0]='B';
    G->vers[1]='I';
    G->vers[2]='U';
    G->vers[3]='Y';
    G->vers[4]='T';
    G->vers[5]='G';
    G->vers[6]='C';
    G->vers[7]='D';

    for (i=0; i<8; ++i)
    {
        for (j=0; j<8; ++j)
        {
            if (i==j)
            {
                G->arc[i][j]=0;
            }
            else
            {
                G->arc[i][j]=MAX;
            }            
        }
    }
    G->arc[1][2]=1;
    G->arc[1][4]=2;
    G->arc[1][5]=3;
    G->arc[2][5]=4;
    G->arc[2][7]=5;
    G->arc[0][2]=6;
    G->arc[0][5]=7;
    G->arc[3][4]=8;
    G->arc[3][5]=9;
    G->arc[4][6]=10;
    for (i=0; i<8; ++i)
    {
        for (j=0; j<8; ++j)
        {
            G->arc[j][i]=G->arc[i][j];
        }
    }
}

void PrintGraph(Graph *G)
{
    int i, j;
    for (i=0; i<8; ++i)
    {
        for (j=0; j<8; ++j)
        {
            printf("%d ",G->arc[i][j]);
        }
        printf("\n");
    }
}

int Find(int *parent, int f)
{
    while ( parent[f] > 0)
    {
        f = parent[f];
    }
    return f;
}

void Kruskal(Graph *G)
{
    int i,j,k=0;
    Edge edge[MAX];
        int parent[MAX];
    for(i=0; i<G->numVertex; i++)
        {
            for (j=i+1; j<G->numVertex; j++)
            {
                edge[k].begin=i;
                edge[k].end=j;
                edge[k].weight=G->arc[i][j];
                k++;
            }
        }

        for (i=0; i<k; i++)
        {
            for (j=i+1; j<k; j++)
            {
                if (edge[i].weight > edge[j].weight)
                {
                    int temp;
                    temp = edge[i].begin;
                    edge[i].begin = edge[j].begin;
                    edge[j].begin = temp;
                    temp = edge[i].end;
                    edge[i].end = edge[j].end;
                    edge[j].end = temp;
                    temp = edge[i].weight;
                    edge[i].weight = edge[j].weight;
                    edge[j].weight = temp;
                }
            }
        }

        for (i=0; i<G->numVertex; i++)
        {
            parent[i]=0;
        }

        printf("打印最小生成树:\n");
        int n,m;
        for (i = 0; i < G->numEdge; i++)
        {
            /******判断遍历的边是存在环*****/
            n = Find(parent,edge[i].begin);
            m = Find(parent,edge[i].end);
            if (n != m) 
            {
                parent[n] = m;    
                printf("(%d, %d) %d\n", edge[i].begin, edge[i].end, edge[i].weight);
            }
        }
}


int main()
{
    Graph G;
    CreateGraph(&G);
    Kruskal(&G);
    return 0;
}

 

 Floyd算法

View Code
#include <stdio.h>    
#include <stdlib.h>
#define  MAX 100

typedef struct
{
    int vexs[MAX];
    int arc[MAX][MAX];
    int numVertex;
    int numEdge;
}Graph;

typedef int Path[MAX][MAX];//arc[i][j]从vi到vj的最短路径
typedef int Distance[MAX][MAX];

void CreateGraph(Graph *G)
{
    int i, j;
    G->numEdge=16;
    G->numVertex=9;
    for (i = 0; i < G->numVertex; i++)
    {
        G->vexs[i]=i;
    }

    for (i = 0; i < G->numVertex; i++)
    {
        for ( j = 0; j < G->numVertex; j++)
        {
            if (i==j)
                G->arc[i][j]=0;
            else
                G->arc[i][j] = G->arc[j][i] =MAX;
        }
    }
    G->arc[0][1]=1;
    G->arc[0][2]=5; 
    G->arc[1][2]=3; 
    G->arc[1][3]=7; 
    G->arc[1][4]=5; 
    G->arc[2][4]=1; 
    G->arc[2][5]=7; 
    G->arc[3][4]=2; 
    G->arc[3][6]=3; 
    G->arc[4][5]=3;
    G->arc[4][6]=6;
    G->arc[4][7]=9; 
    G->arc[5][7]=5; 
    G->arc[6][7]=2; 
    G->arc[6][8]=7;
    G->arc[7][8]=4;
    for(i = 0; i < G->numVertex; i++)
    {
        for(j = i; j < G->numVertex; j++)
        {
            G->arc[j][i] =G->arc[i][j];
        }
    }

}
  
void Floyd(Graph *G, Path *P, Distance *D)
{    
    int v,w,k;    
    for(v=0; v<G->numVertex; ++v)
    {        
        for(w=0; w<G->numVertex; ++w)  
        {
            (*D)[v][w]=G->arc[v][w];    
            (*P)[v][w]=w;
        }
    }
    for(k=0; k<G->numVertex; ++k)   
    {
        for(v=0; v<G->numVertex; ++v)  
        {        
            for(w=0; w<G->numVertex; ++w)    
            {
                if ((*D)[v][w]>(*D)[v][k]+(*D)[k][w])
                {
                    (*D)[v][w]=(*D)[v][k]+(*D)[k][w];
                    (*P)[v][w]=(*P)[v][k];
                }
            }
        }
    }
}

int main(void)
{    
    int v,w,k;  
    Graph G;        
    Path P;    
    Distance D;    
    CreateGraph(&G);
    Floyd(&G,&P,&D);  
    printf("各顶点间最短路径如下:\n");    
    for(v=0; v<G.numVertex; ++v)   
    {        
        for(w=v+1; w<G.numVertex; w++)  
        {
            printf("v%d-v%d weight: %d ",v,w,D[v][w]);
            k=P[v][w];
            printf(" path: %d",v);    
            while(k!=w)    
            {
                printf("--->%d",k);    
                k=P[k][w];
            }
            printf("--->%d\n",w);    
        }
        printf("\n");
    }
    printf("最短距离D\n");
    for(v=0; v<G.numVertex; ++v)  
    {        
        for(w=0; w<G.numVertex; ++w)    
        {
            printf("%d\t",D[v][w]);
        }
        printf("\n");
    }
    printf("最短路径P\n");
    for(v=0; v<G.numVertex; ++v)  
    {        
        for(w=0; w<G.numVertex; ++w)    
        {
            printf("%d ",P[v][w]);
        }
        printf("\n");
    }
    return 0;
}

 

拓扑排序

View Code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define  MAX 100
typedef struct graph
{
int vers[MAX];
int numVertex;
int numEdge;
int arc[MAX][MAX];
}Grpah;

typedef struct edgenode
{
int nodevalue;
int weight;
struct edgenode* next;
}Edgenode;

typedef struct adjlist
{
int in;  //表示节点的度
int data;
Edgenode* firstnode;
}Adjlist[MAX];

typedef struct
{
Adjlist adjlist;
int numVertex;
int numEdge;
}GraphList;

void CreateGraph(Grpah *G)
{
int i,j;
G->numEdge=20;
G->numVertex=14;
for (i = 0; i < G->numVertex; i++)
{
G->vers[i]=i;
}

for (i = 0; i < G->numVertex; i++)
{
for ( j = 0; j < G->numVertex; j++)
{
G->arc[i][j]=0;
}
} 
G->arc[0][4]=1;
G->arc[0][5]=1; 
G->arc[0][11]=1; 
G->arc[1][2]=1; 
G->arc[1][4]=1; 
G->arc[1][8]=1; 
G->arc[2][5]=1; 
G->arc[2][6]=1;
G->arc[2][9]=1;
G->arc[3][2]=1; 
G->arc[3][13]=1;
G->arc[4][7]=1;
G->arc[5][8]=1;
G->arc[5][12]=1; 
G->arc[6][5]=1; 
G->arc[8][7]=1;
G->arc[9][10]=1;
G->arc[9][11]=1;
G->arc[10][13]=1;
G->arc[12][9]=1;

}

void CreateGraphList(Grpah *G, GraphList *GL)
{
int i,j;
Edgenode *e;
GL->numEdge=G->numEdge;
GL->numVertex=G->numVertex;
for (i=0; i<GL->numVertex; ++i)
{
GL->adjlist[i].firstnode=NULL;
GL->adjlist[i].in=0;
GL->adjlist[i].data=G->vers[i];
}
for (i=0; i<GL->numVertex; ++i)
{
for (j=0; j<GL->numVertex; ++j)
{
if (G->arc[i][j]==1)
{
e=(Edgenode* )malloc(sizeof(Edgenode));
e->nodevalue=j;
e->weight=G->arc[i][j];
e->next=GL->adjlist[i].firstnode;
GL->adjlist[i].firstnode=e;
GL->adjlist[j].in++;
}
}
}
}

void PrintGraph(Grpah *G)
{
int i,j;
for (i=0; i<G->numVertex; i++)
{
for (j=0; j<G->numVertex; ++j)
{
printf("%d ",G->arc[i][j]);
}
printf("\n");
}
}

void PrintGraphList(GraphList *GL)
{
int i;
Edgenode *p;
for (i=0; i<GL->numVertex; ++i)
{
p=GL->adjlist[i].firstnode;
printf("%d", GL->adjlist[i].data);
while(p)
{
printf("<--->%d", p->nodevalue);
p=p->next;
}
printf("\n");
}
};

void TopSort(GraphList *GL)
{
    Edgenode *e;    
    int i,k,gettop;   
    int top=0;  
    int *stack;   
    stack=(int *)malloc(GL->numVertex * sizeof(int));     
    for(i = 0; i<GL->numVertex; i++)   
    {
        if(0 == GL->adjlist[i].in) 
        {
            stack[++top]=i;  
        }
    }   
    while(top!=0)    
    {        
        gettop=stack[top--];        
        printf("%d<--->",GL->adjlist[gettop].data);              
        for(e = GL->adjlist[gettop].firstnode; e; e = e->next)        
        {            
            k=e->nodevalue;            
            if(!(--GL->adjlist[k].in))
            {
                stack[++top]=k;        
            }
        } 
    }
}

int main()
{
    Grpah G;
    GraphList GL;
    CreateGraph(&G);
    PrintGraph(&G);
    CreateGraphList(&G,&GL);
    PrintGraphList(&GL);
    TopSort(&GL);
    return 0;
}

最佳路径

View Code
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define  MAX 100
int *etv;  //事件最早发生时间
int *ltv; //事件最迟发生时间
int *stack2;
int top2=0;

typedef struct graph
{
int vers[MAX];
int numVertex;
int numEdge;
int arc[MAX][MAX];
}Grpah;

typedef struct edgenode
{
int nodevalue;
int weight;
struct edgenode* next;
}Edgenode;

typedef struct adjlist
{
int in;  //表示节点的度
int data;
Edgenode* firstnode;
}Adjlist[MAX];

typedef struct
{
Adjlist adjlist;
int numVertex;
int numEdge;
}GraphList;

void CreateGraph(Grpah *G)
{
int i,j;
G->numEdge=13;
G->numVertex=10;
for (i = 0; i < G->numVertex; i++)
{
G->vers[i]=i;
}

for (i = 0; i < G->numVertex; i++)
{
for ( j = 0; j < G->numVertex; j++)
{
G->arc[i][j]=0;
}
} 
G->arc[0][1]=3;
G->arc[0][2]=4;
G->arc[1][3]=5;
G->arc[1][4]=6;
G->arc[2][3]=8;
G->arc[2][5]=7;
G->arc[3][4]=3;
G->arc[4][6]=9;
G->arc[4][7]=4;
G->arc[5][7]=6;
G->arc[6][9]=2;
G->arc[7][8]=5;
G->arc[8][9]=3;

}

void CreateGraphList(Grpah *G, GraphList *GL)
{
int i,j;
Edgenode *e;
GL->numEdge=G->numEdge;
GL->numVertex=G->numVertex;
for (i=0; i<GL->numVertex; ++i)
{
GL->adjlist[i].firstnode=NULL;
GL->adjlist[i].in=0;
GL->adjlist[i].data=G->vers[i];
}
for (i=0; i<GL->numVertex; ++i)
{
for (j=0; j<GL->numVertex; ++j)
{
if (G->arc[i][j])
{
e=(Edgenode* )malloc(sizeof(Edgenode));
e->nodevalue=j;
e->weight=G->arc[i][j];
e->next=GL->adjlist[i].firstnode;
GL->adjlist[i].firstnode=e;
GL->adjlist[j].in++;
}
}
}
}

void PrintGraph(Grpah *G)
{
int i,j;
for (i=0; i<G->numVertex; i++)
{
for (j=0; j<G->numVertex; ++j)
{
printf("%d ",G->arc[i][j]);
}
printf("\n");
}
}

void PrintGraphList(GraphList *GL)
{
int i;
Edgenode *p;
for (i=0; i<GL->numVertex; ++i)
{
p=GL->adjlist[i].firstnode;
printf("%d", GL->adjlist[i].data);
while(p)
{
printf("<--->%d", p->nodevalue);
p=p->next;
}
printf("\n");
}
};

void TopSort(GraphList *GL)
{
Edgenode *e;    
int i,k,gettop;   
int top=0; 
int *stack;   
stack=(int *)malloc(GL->numVertex * sizeof(int));  
stack2=(int *)malloc(GL->numVertex * sizeof(int)); 
etv=(int* )malloc(GL->numVertex*sizeof(int));
for (i=0; i<GL->numVertex; i++)
{
etv[i]=0;
}
for(i = 0; i<GL->numVertex; i++)   
{
if(0 == GL->adjlist[i].in) 
{
stack[++top]=i;  
}
} 
printf("拓扑排序:\n");
while(top!=0)    
{        
gettop=stack[top--];      
stack2[++top2]=gettop;
printf("%d<--->",GL->adjlist[gettop].data);              
for(e = GL->adjlist[gettop].firstnode; e; e = e->next)        
{            
k=e->nodevalue;            
if(!(--GL->adjlist[k].in))
{
stack[++top]=k;        
}
if (etv[gettop]+e->weight > etv[k])
{
etv[k]=etv[gettop]+e->weight;
}
} 
}
printf("\n");
}

void CriticalPath(GraphList *GL)
{
Edgenode *p;
int i,j,k;
TopSort(GL);
ltv=(int *)malloc(sizeof(int)*GL->numVertex);
for (i=0; i<GL->numVertex; i++)
{
ltv[i]=etv[GL->numVertex-1];
}
printf("事件最早发生时间:\n");
for (i=0; i<GL->numVertex; ++i)
{
printf("%d<--->",etv[i]);
}
printf("\n");
while(top2)
{
int gettop=stack2[top2--];
p=GL->adjlist[gettop].firstnode;
while(p)
{
k=p->nodevalue;
if (ltv[k]-p->weight<ltv[gettop])
{
ltv[gettop]=ltv[k]-p->weight;
}
p=p->next;
}
}
printf("事件最迟发生时间:\n");
for (i=0; i<GL->numVertex; ++i)
{
printf("%d<--->",ltv[i]);
}
printf("\n");

int e,l;
for(i=0; i<GL->numVertex; i++)      
{            
for(p = GL->adjlist[i].firstnode; p; p = p->next)            
{                
k=p->nodevalue;                
e= etv[i];                 
l= ltv[k]-p->weight;                
if(e == l)                   
printf("v%d <---> v%d length: %d \n",GL->adjlist[i].data,GL->adjlist[k].data, p->weight);
}        
}
}

int main()
{
Grpah G;
GraphList GL;
CreateGraph(&G);
PrintGraph(&G);
CreateGraphList(&G,&GL);
PrintGraphList(&GL);
CriticalPath(&GL);
return 0;
}
 

 

 

posted @ 2012-05-11 09:44  徐露  阅读(199)  评论(0编辑  收藏  举报