AOV網絡拓撲排序

這個算法,主要是爲輸出一個無環圖的拓撲序列node

算法思想:

主要依賴一個棧,用來存放沒有入度的節點,每次讀取棧頂元素,並將棧頂元素的後繼節點入度減一,若是再次出現入度爲零的節點,就加入到棧中。參考《大話數據結構》,寫下下面完整代碼,並發現,其中程序的進行,出現錯誤。v6執行完,應該執行v9,由於此時v9是站頂元素,並非v0.算法

算法流程:

int topGraph(graph g){ EdgeNode *e; int i,k,gettop; int top = 0 ; int count = 0; int *stack; stack = (int *)malloc(g->numVertexes * sizeof(int)); for(i=0;i<g->numVertexes;i++){ if(g->headlist[i].in == 0) //把入度爲0的,即沒有入度的點入棧
            stack[++top] = i; } while(top){ gettop = stack[top--]; printf("%d ",gettop); count++; for(e = g->headlist[gettop].fnode; e ; e=e->next){ //一次遍歷鏈表,減小各個子節點的入度
            k = e->data; if(!(--g->headlist[k].in)) stack[++top] = k; } } if(count < g->numVertexes) return ERROR; else
        return OK; }

所有代碼:

#include <stdio.h> #include <stdlib.h>
#define MAX 14
#define ERROR 1
#define OK 0 typedef struct edgeNode{ int data; struct edgeNode *next; }EdgeNode; typedef struct headNode{ int in; int data; EdgeNode *fnode; }HeadNode,HeadList[MAX]; typedef struct{ HeadList headlist; int numEdges,numVertexes; }Graph,*graph; void initGraph(graph g); int inputInfo(graph g,int tar,int in,int data,int first); void printGraph(graph g); int topGraph(graph g); int main(){ Graph *g = (Graph *)malloc(sizeof(Graph)); initGraph(g); printGraph(g); if(topGraph(g) == ERROR) printf("有環路!\n"); else printf("沒有環路!\n"); free(g); getchar(); return 0; } int topGraph(graph g){ EdgeNode *e; int i,k,gettop; int top = 0 ; int count = 0; int *stack; stack = (int *)malloc(g->numVertexes * sizeof(int)); for(i=0;i<g->numVertexes;i++){ if(g->headlist[i].in == 0) //把入度爲0的,即沒有入度的點入棧
            stack[++top] = i; } while(top){ gettop = stack[top--]; printf("%d ",gettop); count++; for(e = g->headlist[gettop].fnode; e ; e=e->next){ //一次遍歷鏈表,減小各個子節點的入度
            k = e->data; if(!(--g->headlist[k].in)) stack[++top] = k; } } if(count < g->numVertexes) return ERROR; else
        return OK; } void printGraph(graph g){ int i; printf("vertex:%d,edges:%d\n",g->numVertexes,g->numEdges); EdgeNode *e = (EdgeNode *)malloc(sizeof(EdgeNode)); for(i=0;i<MAX;i++){ printf("[in:%d]%d",g->headlist[i].in,g->headlist[i].data); e = g->headlist[i].fnode; while(e != NULL){ printf("->%d",e->data); e = e->next; } printf("\n"); } free(e); } void initGraph(graph g){ g->numVertexes = MAX; g->numEdges = 0; int i; for(i=0;i<MAX;i++){ g->headlist[i].fnode = NULL; } inputInfo(g,0,0,0,4); inputInfo(g,0,0,0,5); inputInfo(g,0,0,0,11); inputInfo(g,1,0,1,2); inputInfo(g,1,0,1,4); inputInfo(g,1,0,1,8); inputInfo(g,2,2,2,5); inputInfo(g,2,2,2,6); inputInfo(g,2,2,2,9); inputInfo(g,3,0,3,2); inputInfo(g,3,0,3,13); inputInfo(g,4,2,4,7); inputInfo(g,5,3,5,8); inputInfo(g,5,3,5,12); inputInfo(g,6,1,6,5); inputInfo(g,7,2,7,-1); inputInfo(g,8,2,8,7); inputInfo(g,9,1,9,10); inputInfo(g,9,1,9,11); inputInfo(g,10,1,10,13); inputInfo(g,11,2,11,-1); inputInfo(g,12,1,12,9); inputInfo(g,13,2,13,-1); } int inputInfo(graph g,int tar,int in,int data,int first){ g->numEdges++; if(first == -1){ //沒有後繼的邊節點
        g->headlist[tar].in = in; g->headlist[tar].data = data; return 1; } if(!g->headlist[tar].fnode){ //觀察是否已經初始化
        g->headlist[tar].in = in; g->headlist[tar].data = data; } EdgeNode *e = (EdgeNode *)malloc(sizeof(EdgeNode)); e->data = first; e->next = g->headlist[tar].fnode; g->headlist[tar].fnode = e; return 0; }

執行示例:

相關文章
相關標籤/搜索