AOV网络与拓扑(二)——实现
2014-04-05 17:04
302 查看
例:对于输入的有向图进行拓扑排序,并输出一个拓扑有序序列;如果存在有向环,则给出提示信息。
首先输入顶点个数n和边数m;然后输入每条边<u,v>,输入0 0结束;顶点序号从1开始记起。
样例输入:
6 8
1 2
1 4
2 6
3 2
3 6
5 1
5 2
5 6
6 8
1 3
1 2
2 5
3 4
4 2
4 6
5 4
5 6
0 0
样例输出:
5 1 4 3 2 6
Network has a cycle!
分析:
样例中第一组测试数据描述的有向图:
样例中第二组测试数据描述的有向图:
存在有向回路的AOV网络。
下面奉上具体实现~
Code:
运行结果:
Ps:如有Bug,欢迎拍砖~
首先输入顶点个数n和边数m;然后输入每条边<u,v>,输入0 0结束;顶点序号从1开始记起。
样例输入:
6 8
1 2
1 4
2 6
3 2
3 6
5 1
5 2
5 6
6 8
1 3
1 2
2 5
3 4
4 2
4 6
5 4
5 6
0 0
样例输出:
5 1 4 3 2 6
Network has a cycle!
分析:
样例中第一组测试数据描述的有向图:
样例中第二组测试数据描述的有向图:
存在有向回路的AOV网络。
下面奉上具体实现~
Code:
#include<iostream> using namespace std; #define MAX 10 //顶点个数的最大值 struct ArcNode { int to; struct ArcNode *next; }; int n, m; //顶点个数、边数 ArcNode *List[MAX]; //每个顶点的边链表表头指针 int count[MAX]; //各顶点的入度 char output[100]; //输出内容 void TopSort() { int i, top = -1; ArcNode *temp; bool bcycle = false; //是否存在有向环的标志 int pos = 0; //写入output数组的位置 for(i = 0; i < n; i++) //入度为0的顶点入栈 { if(count[i] == 0) { count[i] = top; top = i; } } for(i = 0; i < n; i++) { if(top == -1) //栈为空,存在有向回路 { bcycle = true; break; } else { int j = top; top = count[top]; //栈顶顶点j出栈 pos += sprintf(output+pos, "%d ", j+1); temp = List[j]; //遍历顶点j的边链表,每条出边的终点的入度减1 while(temp != NULL) { int k = temp->to; if(--count[k] == 0) { count[k] = top; top = k; } temp = temp->next; } } } if(bcycle) printf("Network has a cycle!\n"); else { output[pos-1] = '\n'; //去掉最后的空格 printf(output); } } int main() { int i, u, v; //循环变量、边的起点和终点 while(1) { scanf("%d%d", &n, &m); //读入顶点个数、边数 if(n == 0 && m == 0) break; memset(List, 0, sizeof(List)); memset(count, 0, sizeof(count)); memset(output, 0, sizeof(output)); ArcNode *temp; for(i = 0; i < m; i++) //边链表 { scanf("%d%d", &u, &v); u--; v--; count[v]++; temp = new ArcNode; temp->to = v; temp->next = NULL; //构造邻接表 if(List[u] == NULL) List[u] = temp; else { temp->next = List[u]; List[u] = temp; } } TopSort(); for(i = 0; i < n; i++) //释放边链表上各边结点所占用的存储空间 { temp = List[i]; while(temp != NULL) { List[i] = temp->next; delete temp; temp = List[i]; } } } return 0; }
运行结果:
Ps:如有Bug,欢迎拍砖~
相关文章推荐
- EXT 结合jtopo实现网络拓扑刷新 节点删除 告警
- java实现基于snmp的网络拓扑发现
- Python实现AOV网络——拓扑排序,AOE网络——关键路径
- mininet实现自定义拓扑结构--基于两个数据中心的网络拓扑--fattree.py
- SDN 下用ODL实现QoS保障(2)-创建网络拓扑
- Cytoscape Web 实现网络拓扑结构图
- LVS之——Lvs Net网络拓扑实现负载均衡
- AOV网络与拓扑(一)
- java实现基于snmp的网络拓扑发现
- 【原创】锐捷交换机实现双星型拓扑网络(链路冗余与以太网通道)
- Aov网络与拓补排序的实现
- 详解网络数字电视的实现方法与关键技术
- 网络断开后再连接的断点续传是怎么实现的?
- 利用Windows 2008NPS和cisco交换机实现802.1X网络验证
- boost::asio::ip::tcp实现网络通信的小例子
- php实现远程网络文件下载到服务器指定目录(方法二)
- 7种网络编程I/O模型代码实现实例
- c语言中如何实现网络通信
- 《tensorflow实战》之实现复杂的卷积网络(五)
- Linux套接字实现简单的客户/服务器网络通信示例