1. 程式人生 > >09拓撲排序_TopologicalSort

09拓撲排序_TopologicalSort

eno malloc true sizeof ror 當前 next time lib

#include "stdio.h"
#include "stdlib.h"
#include "io.h"
#include "math.h"
#include "time.h"

#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXEDGE 20
#define MAXVEX 14
#define INFINITY 65535

typedef int Status; /* Status是函數的類型,其值是函數結果狀態代碼,如OK等 */

/* 鄰接矩陣結構 */
typedef struct
{
int vexs[MAXVEX];
int arc[MAXVEX][MAXVEX];
int numVertexes, numEdges;
}MGraph;

/* 鄰接表結構****************** */
typedef struct EdgeNode /* 邊表結點 */
{
int adjvex; /* 鄰接點域,存儲該頂點對應的下標 */
int weight; /* 用於存儲權值,對於非網圖可以不需要 */
struct EdgeNode *next; /* 鏈域,指向下一個鄰接點 */
}EdgeNode;

typedef struct VertexNode /* 頂點表結點 */
{
int in; /* 頂點入度 */
int data; /* 頂點域,存儲頂點信息 */
EdgeNode *firstedge;/* 邊表頭指針 */
}VertexNode, AdjList[MAXVEX];

typedef struct
{
AdjList adjList;
int numVertexes,numEdges; /* 圖中當前頂點數和邊數 */
}graphAdjList,*GraphAdjList;
/* **************************** */


void CreateMGraph(MGraph *G)/* 構件圖 */
{
int i, j;

/* printf("請輸入邊數和頂點數:"); */
G->numEdges=MAXEDGE;
G->numVertexes=MAXVEX;

for (i = 0; i < G->numVertexes; i++)/* 初始化圖 */
{
G->vexs[i]=i;
}

for (i = 0; i < G->numVertexes; i++)/* 初始化圖 */
{
for ( j = 0; j < G->numVertexes; 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 CreateALGraph(MGraph G,GraphAdjList *GL)
{
int i,j;
EdgeNode *e;

*GL = (GraphAdjList)malloc(sizeof(graphAdjList));

(*GL)->numVertexes=G.numVertexes;
(*GL)->numEdges=G.numEdges;
for(i= 0;i <G.numVertexes;i++) /* 讀入頂點信息,建立頂點表 */
{
(*GL)->adjList[i].in=0;
(*GL)->adjList[i].data=G.vexs[i];
(*GL)->adjList[i].firstedge=NULL; /* 將邊表置為空表 */
}

for(i=0;i<G.numVertexes;i++) /* 建立邊表 */
{
for(j=0;j<G.numVertexes;j++)
{
if (G.arc[i][j]==1)
{
e=(EdgeNode *)malloc(sizeof(EdgeNode));
e->adjvex=j; /* 鄰接序號為j */
e->next=(*GL)->adjList[i].firstedge; /* 將當前頂點上的指向的結點指針賦值給e */
(*GL)->adjList[i].firstedge=e; /* 將當前頂點的指針指向e */
(*GL)->adjList[j].in++;

}
}
}

}


/* 拓撲排序,若GL無回路,則輸出拓撲排序序列並返回1,若有回路返回0。 */
Status TopologicalSort(GraphAdjList GL)
{
EdgeNode *e;
int i,k,gettop;
int top=0; /* 用於棧指針下標 */
int count=0;/* 用於統計輸出頂點的個數 */
int *stack; /* 建棧將入度為0的頂點入棧 */
stack=(int *)malloc(GL->numVertexes * sizeof(int) );

for(i = 0; i<GL->numVertexes; i++)
if(0 == GL->adjList[i].in) /* 將入度為0的頂點入棧 */
stack[++top]=i;
while(top!=0)
{
gettop=stack[top--];
printf("%d -> ",GL->adjList[gettop].data);
count++; /* 輸出i號頂點,並計數 */
for(e = GL->adjList[gettop].firstedge; e; e = e->next)
{
k=e->adjvex;
if( !(--GL->adjList[k].in) ) /* 將i號頂點的鄰接點的入度減1,如果減1後為0,則入棧 */
stack[++top]=k;
}
}
printf("\n");
if(count < GL->numVertexes)
return ERROR;
else
return OK;
}


int main(void)
{
MGraph G;
GraphAdjList GL;
int result;
CreateMGraph(&G);
CreateALGraph(G,&GL);
result=TopologicalSort(GL);
printf("result:%d",result);

return 0;
}

09拓撲排序_TopologicalSort