AOV 拓扑排序
2013-03-27 19:22
323 查看
/* * AOV * TopSort * Date: 2013/3/27 * NolanJian */ #include <stdio.h> #include <string.h> #include <stdlib.h> #define MAX_VERTEX 100 struct node; struct hnode; typedef struct node Node; typedef struct hnode Hnode; typedef Node *NodePointer; void TopSort(void); void Input(void); void Add(int x, int y); void InitialiseGraph(void); struct node { int Vertex; NodePointer Next; }; struct hnode { int Count; NodePointer Next; }; int N, M; Hnode Graph[MAX_VERTEX]; int main() { while(1) { Input(); TopSort(); } } void TopSort(void) { int i, j, k, top = -1, flag = 1; NodePointer p; for(i = 0; i < N; i++) { if(Graph[i].Count == 0) { Graph[i].Count = top; top = i; } } for(i = 0; i < N; i++) { if(top == -1) { printf("\nNetwork has a cycle. TopSort terminated.\n"); flag = 0; exit(1); } else { j = top; top = Graph[top].Count; printf("V%d ", j); for(p = Graph[j].Next; p; p = p->Next) { k = p->Vertex; Graph[k].Count--; if(Graph[k].Count == 0) { Graph[k].Count = top; top = k; } } } } if(flag) printf("\n"); } void Input(void) { int i, x, y; scanf("%d%d", &N, &M); InitialiseGraph(); for(i = 0; i < M; i++) { scanf("%d%d", &x, &y); Add(x, y); } return ; } void Add(int x, int y) { NodePointer tmp = (NodePointer)malloc(sizeof(Node)); tmp->Next = NULL; tmp->Vertex = y; Graph[y].Count++; if(Graph[x].Next == NULL) { Graph[x].Next = tmp; return ; } tmp->Next = Graph[x].Next; Graph[x].Next = tmp; return ; } void InitialiseGraph(void) { int i; for(i = 0; i < N; i++) { if(Graph[i].Next) free(Graph[i].Next); Graph[i].Next = NULL; Graph[i].Count = 0; } return ; }
相关文章推荐
- 对AOV(DAG的一种,Activity on vertex)拓扑排序得到chain(人为添加边构成全序)来判断AOV是否有环
- Python实现AOV网络——拓扑排序,AOE网络——关键路径
- 拓扑排序与AOV
- 拓扑排序AOV和AOE
- 拓扑排序---AOV图
- 数据结构 学习笔记(九):图(下):最小生成树(Prim,Kruskal 算法),拓扑排序 AOV,关键路径 AOE
- 图--AOV网络与拓扑排序
- 关键路径(AOE)—基于拓扑排序(AOV)
- AOV 拓扑排序
- AOV网络及拓扑排序
- 数据结构之---C语言实现拓扑排序AOV图
- 拓扑排序,AOV网络,AOE网络
- 拓扑排序---AOV图
- 【数据结构】算法7.12 AOV-网 拓扑排序
- 拓扑排序(AOV)---判断图中是否有环---《数据结构》严蔚敏
- 数据结构之---C语言实现拓扑排序AOV图
- 有向图的拓扑排序
- hdu 1285 拓扑排序起步
- [poj1094]拓扑排序经典例题
- 拓扑排序小记