編一C程序
(注
#include
typedef enum {False
int G[
int n;
void CreatG() /*建立圖的鄰接矩陣G[][]*/
{int i
printf(
scanf(
printf(
for (i= for (j=0;j G[i][j]=0; do { scanf("%d %d",&i,&j); G[i][j]=1; }while ((i!=-1)&&(j!=-1)); } void TopSort() /*拓撲排序,輸出拓撲序列*/ { int i,j; int degree[100]; /*按照無前驅頂點優先思想,degree[]存放個節點的入度.*/ Boolean visited[100],flag=True; printf("The Topolgical Order as follow:"); for (i=0;i { degree[i]=0; visited[i]=False; } printf("\n"); while(flag==True) { for (i=0;i for (j=0;j degree[i]=G[j][i]+degree[i]; i=0; while ((i if (i {printf(" %d",i); visited[i]=True; for(j=0;j {G[i][j]=0; degree[j]=0;} } else flag=False; } } main() { CreatG(); TopSort(); }
From:http://tw.wingwit.com/Article/program/sjjg/201311/23922.html