-
Notifications
You must be signed in to change notification settings - Fork 430
/
拓扑排序.c
158 lines (143 loc) · 3.15 KB
/
拓扑排序.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <stdio.h>
#include <stdlib.h>
#define MaxVertexNum 100
#define ERROR 0
#define OK 1
#define FALSE 0
#define TRUE 1
typedef int Boolean;
typedef int VertexType;
Boolean visit[MaxVertexNum];
typedef struct node
{
int adjvex;
struct node *next;
}EdgeNode;
typedef struct
{
VertexType vertex;
EdgeNode *firstedge;
}VertexNode;
typedef VertexNode AdjList[MaxVertexNum];
typedef struct
{
AdjList adjlist;
int n,e;
}ALGraph;
int FindVertex(ALGraph *G ,int e,int n)
{
int i;
for(i=0;i<n;i++)
{
if(G->adjlist[i].vertex==e)
{
return i;
}
}
return -1;
}
void create(ALGraph *G) //创建邻接表
{
int i,j,k,w,v;
EdgeNode *s;
printf("读入顶点和边数");
scanf("%d %d",&G->n,&G->e);
for(i=0;i<G->n;i++)
{
printf("建立顶点表");
fflush(stdin);
scanf("%d",&G->adjlist[i].vertex);
G->adjlist[i].firstedge=NULL;
}
printf("建立边表\n");
for(k=0;k<G->e;k++)
{
printf("读入(vi-vj)的顶点对序号");
scanf("%d %d",&i,&j);
i=FindVertex(G,i,G->n);
j=FindVertex(G,j,G->n);
if(i==-1||j==-1)
{
printf("找不到顶点,请重新输入!\n");
printf("读入(vi-vj)的顶点对序号");
scanf("%d %d",&i,&j);
i=FindVertex(G,i,G->n);
j=FindVertex(G,j,G->n);
}
s=(EdgeNode*)malloc(sizeof(EdgeNode));
s->adjvex=(j);
s->next=G->adjlist[i].firstedge;
G->adjlist[i].firstedge=s;
}
}
void TopoSort(ALGraph *G,int n)
{
int i,j,k,top,m=0;
EdgeNode *p;
int *d=(int *)malloc(n*sizeof(int));
for(i=0;i<n;i++) //初始化数组
{
d[i]=0;
}
for(i=0;i<n;i++) //统计各个顶点的入度情况,并把他们填入数组里面
{
p=G->adjlist[i].firstedge;
while(p!=NULL)
{
j=p->adjvex;
d[j]++;
p=p->next;
}
}
top=-1;
for(i=0;i<n;i++) //先找出里面入度是0的顶点
{
if(d[i]==0)
{
d[i]=top;
top=i;
}
}
while(top!=-1)
{
j=top;
top=d[top];
printf("%d ",j);
m++; //统计顶点
p=G->adjlist[j].firstedge;
while(p)
{
k=p->adjvex; //相l连接的顶点
d[k]--; //相连接的顶点入度减1
if(d[k]==0) //如果发现入度为0的新顶点,从该顶点出发
{
d[k]=top;
top=k;
}
p=p->next;
}
}
if(m<n) printf("\n有回路!\n");
free(d);
}
void main()
{
ALGraph *G=(ALGraph *)malloc(sizeof(ALGraph));
EdgeNode *p;
create(G);
int i;
printf("其邻接表为('->'表示两个之间有连接):\n");
for(i=0;i<G->n;i++)
{
p=G->adjlist[i].firstedge;
printf("%d->",G->adjlist[i].vertex);
while(p!=NULL)
{
printf("%d->",G->adjlist[p->adjvex].vertex);
p=p->next;
}
printf("\n");
}
printf("拓扑排序为:\n");
TopoSort(G,G->n);
}