拓撲排序 TopologicalSort c

2021-07-02 04:10:13 字數 1702 閱讀 4217

#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 inf 65535

typedef

int status; /* status是函式的型別,其值是函式結果狀態**,如ok等 */

/* 鄰接矩陣結構 */

typedef

struct

mgraph;

/* 鄰接表結構****************** */

typedef

struct edgenode /* 邊表結點 */

edgenode;

typedef

struct vertexnode /* 頂點表結點 */

vertexnode, adjlist[maxvex];

typedef

struct

graphadjlist,*graphadjlist;

/* **************************** */

void createmgraph(mgraph *g)/* 構件圖 */

for (i = 0; i < g->numvertexes; i++)/* 初始化圖 */

}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)

for(i=0;i/* 建立邊表 */}}

}/* 拓撲排序,若gl無迴路,則輸出拓撲排序序列並返回1,若有迴路返回0。 */

status topologicalsort(graphadjlist gl)

}

printf("\n");

if(count < gl->numvertexes)

return error;

else

return ok;

}int main(void)

python 拓撲排序 Python 拓撲排序

python 拓撲排序 在圖論中,由乙個有向無環圖的頂點組成的序列,當且僅當滿足下列條件時,稱為該圖的乙個拓撲排序 英語 topological sorting 每個頂點出現且只出現一次 若a在序列中排在b的前面,則在圖中不存在從b到a的路徑。print 拓撲排序結果 g.topologicalso...

python 排序 拓撲排序

在電腦科學領域中,有向圖的拓撲排序是其頂點的先行排序,對於每個從頂點u到頂點v的有向邊uv,在排序的結果中u都在v之前。如果圖是有向無環圖,則拓撲排序是可能的 為什麼不說一定呢?圖論 是組合數學的乙個分支,它和其他分支比如 群論 拓撲學 矩陣論有著密切的關係。圖是圖論的主要研究物件。圖是由若干給定的...

拓撲排序演算法

對許多資料結構教材實在不滿意,至少我是看不懂 至於拓撲排序演算法,教材上那些偽 真真教人頭暈。只寫了幾個struct結構,我根本看不出這是鄰接表。如果給出乙個清晰明了的圖,一切不就簡單了?總之,關鍵就是建立乙個鄰接表。然後利用這個表進行拓撲排序。邊表結點宣告 typedef struct edgen...