第
// 有點未加判斷
#include
#include
#include
#define MAX
typedef struct {
int arcs[MAX][MAX];
int vexnum;
}Mgraph;
void input(Mgraph *mgraph
{
int a
for(int j=
for(int k=0;k
mgraph->arcs[j][k]=0;
while(1){
printf("輸入(i j),(-1 -1)結束:");
scanf("%d%d",&a,&b);
if(a==-1 && b==-1)
break;
else
{
mgraph->arcs[a][b]=1;continue;
}
}
for (int k=0;k
int c=0;
for(int m=0;m
if(mgraph->arcs[m][k]==1)
c++;
}
count1[k]=c;
}
printf("\n矩陣為:\n");
for(int l=0;l
for(int m=0;m
printf("%3d",mgraph->arcs[l][m]);
c1=c1+1;
if(c1==i){
printf("\n");
c1=0;
}
}
}
//拓撲排序
void topsort(Mgraph * mgraph,int i,int *count){
int a,b,c=0,s=-1;
for(a=0;a
if(count[a]==0){
count[a]=s;
s=a;
}
}
while(s!=-1){
printf("V(%d) ",s);
c++;
a=s;
s=count[a];
for(b=0;b
if(mgraph->arcs[a][b]){
count[b]--;
if (count[b]==0){
count[b]=s;
s=b;
}
}
}
}
}
void main(){
Mgraph mgraph;
int i;
printf("輸入頂點個數:");
scanf("%d",&i);
int count1[MAX];
input(&mgraph,i,count1);
topsort(&mgraph,i,count1);
}
From:http://tw.wingwit.com/Article/program/sjjg/201311/23535.html