1. compile a C program, you can digraph G, and outputs the DFS traversal sequence G (starting from V0) The configuration of the read data,
input in the form of FIG n V0 Vi0 V1 Vi1 V2 Vi2 ... Vi Vin -1 -1 (-1, -1 input end mark, the remaining values are> = 0, and
they are all integers, and 100> n> 0. (Note: the executable program file name must be e2.exe, stored under your account or debug directory).
input: 9. 1 0 0 2 0 7,121,423,353,643,457,886 -1 -1
#include<stdio.h>
#define MAX 100
typedef enum{False,True} Boolean;
int G[MAX][MAX];
int n;
//建立图的邻接矩阵G[][]
void GreateG(){
int i,j;
printf("Input the number of the node:");
scanf("%d", &n);
printf("\n");
for(i=0;i<n;i++){
for(j=0;j<n;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;
//按照无前驱顶点优先思想,degree[]存放个节点的入度
int degree[100];
Boolean visited[MAX],flag=True;
printf("The Topolgical Order as follow:");
for(i=0;i<n;i++){
degree[i]=0;
visited[i]=False;
}
printf("\n");
while(flag==True){
for(i=0;i<n;i++){
for(j=0;j<n;j++){
degree[i]=G[j][i]+degree[i];
}
}
i=0;
//最先输出入度为0的顶点
while((i<n) && (degree[i]!=0) || visited[i]==True){
i++;
}
//所有节点均已输出结束,否则说明存在环,无拓扑序列
if(i<n){
printf("%d",i);
visited[i]=True;
for(j=0;j<n;j++){
G[i][j]=0;
degree[j]=0;
}
}else{
flag = False;
}
}
}
void main(){
GreateG();
TopSort();
printf("\n");
}