数据结构 — 图 之 拓扑排序 (AOV网)
来源:互联网 发布:mac 装google chrome 编辑:程序博客网 时间:2024/05/21 11:17
【度娘说】:
对一个有向无环图(Directed Acyclic Graph简称DAG)G进行拓扑排序,是将G中所有顶点排成一个线性序列,使得图中任意一对顶点u和v,若边(u,v)∈E(G),则u在线性序列中出现在v之前。通常,这样的线性序列称为满足拓扑次序(Topological Order)的序列,简称拓扑序列。简单的说,由某个集合上的一个偏序得到该集合上的一个全序,这个操作称之为拓扑排序。
【实现】:
/*input:6 80 10 20 31 42 42 53 43 5output:V0 V3 V2 V5 V1 V4 */#include<iostream>#include<stdlib.h>using namespace std;#define EleType int#define MAX_NUM 100typedef struct node { EleType v; struct node *next;}NodeType,*NodePointer;typedef struct { int idegree; NodePointer next;}GNode,*GPointer;GNode graph[MAX_NUM];int vn,en; //顶点数和边数void CreatG() { EleType et1,et2; NodePointer tail; cin>>vn>>en; for(int i = 0; i<vn; i++) { graph[i].idegree = 0; graph[i].next = NULL; } for(int i = 0; i<en; i++) { cin>>et1>>et2;NodePointer np = new NodeType; np->v = et2; np->next = NULL; if(graph[et1].next == NULL) { graph[et1].next = np; }else { tail = graph[et1].next; while(tail->next != NULL ) { tail = tail->next; } tail->next = np; } graph[et2].idegree++; }}void TopSort() { int n,m; int top; NodePointer np; top = -1; for(int i = 0; i<vn; i++) { //入栈 if(graph[i].idegree == 0) { graph[i].idegree = top; top = i; } } //topsort for(int i = 0; i<vn; i++) { //如果已经到了栈底说明图中包含环路 if (top == -1) { cout<<"图中有环路,工程不可行"<<endl; exit(1); }else { //出栈 n = top; top = graph[n].idegree; cout<<"V"<<n<<" "; //删除以出栈顶点为 尾 的边 的 头顶点 的 入度 for(np = graph[n].next; np; np = np->next) { m = np->v; graph[m].idegree--; if(graph[m].idegree == 0) { graph[m].idegree = top; top = m; } } } } cout<<endl;}int main() { CreatG(); TopSort(); return 0;}
0 0
- 数据结构 — 图 之 拓扑排序 (AOV网)
- 数据结构之AOV网与拓扑排序
- 图之AOV拓扑排序
- 数据结构之---C语言实现拓扑排序AOV图
- 【数据结构】算法7.12 AOV-网 拓扑排序
- 拓扑排序---AOV图
- 基本图算法之拓扑排序 AOV网
- 图——AOV拓扑排序
- 拓扑排序之aov模板
- 数据结构--图--拓扑排序/关键路径/AOE/AOV
- AOV网拓扑排序算法
- AOV网与拓扑排序
- AOV网与拓扑排序
- AOV网的拓扑排序
- 拓扑排序(AOV)---判断图中是否有环---《数据结构》严蔚敏
- 关键路径(AOE)—基于拓扑排序(AOV)
- 【算法】基于AOV网的拓扑排序
- 【笔记】AOV网与拓扑排序
- iOS 利用mask layer 使view中扣掉一块露出下边的view
- CSS边距详解
- 解释执行的语言相比编译执行的语言优缺点思考
- 将一个数m分解为n个不同的数之和(积)
- Python中简单的正则表达式
- 数据结构 — 图 之 拓扑排序 (AOV网)
- 兔子-查看当前php的版本
- [Javascript] XHTML VS HTML , Document type
- linux .o .a .so文件解析
- Android单元测试设置
- AngularJS进阶(十)AngularJS改变元素显示状态
- 内存
- NSMotification通知使用方法
- 用纯Java代码根据IP获取windows和linux的MAC物理地址