AOV网:活动在顶点上的网(AOV)activity on vertex network
拓扑排序核心算法
返回值为1,说明是无环图,反之为有环图。
1 #include <iostream>
2 using namespace std;
3
4 //拓扑排序
5 //(AOV)activity on vertex network
6 typedef struct ArcNode {
7 int adjvex;
8 int info;
9 struct ArcNode *nextarc;
10 };
11 typedef struct VNode {
12 int data;
13 int count;//统计顶点当前的入度
14 ArcNode *firstarc;
15 };
16 typedef struct AGraph {
17 int e,n;
18 VNode *adjlist[maxsize];
19 };
20 int Top(AGraph *G) {
21 int *stack[maxsize];
22 int top=-1,n=0;
23 for(int i=0; i<G->n; i++) {
24 if(G->adjlist[i].count==0) {
25 stack[++top]=i;
26 }
27 }
28 while(top!=-1) {
29 int j=stack[top--];
30 ++n;
31 ArcNode *p=G->adjlist[j].firstarc;
32 while(p!=NULL) {
33 --G->adjlist[p->adjvex];
34 if(G->adjlist[p->adjvex].count==0) {
35 stack[++top]=p->adjvex;
36 }
37 p=p->nextarc;
38 }
39 }
40 if(n==G->n)return 1;
41 else return 0;
42 }