拓扑排序
来源:互联网 发布:贝克汉姆 帅 知乎 编辑:程序博客网 时间:2024/05/29 06:57
//Stack.h#ifndef STACK_H#define STACK_Hclass Stack{public: Stack(int n = 10); bool push(int a); int pop(); bool isfull();//判断是否满 bool isempty();private: int size;//表示最大容量 int *base, *top;//top指向栈顶前一个元素};#endif
//Topo.h#ifndef TUOPU_H#define TUOPU_Hclass Node{public: int vexNum; Node* next; Node(int vex = 0, Node* a=NULL);};class MGraph{//图的储存用邻接表,要求入度public: int Vex; Node* vertices; MGraph(int num = 0); bool Inition();};class ToPo{ /*算法: 1.计算每个顶点的入度(初始化) 初始化栈 2.将入度为0的点压栈 3.弹栈,并将以该顶点为弧尾的顶点入度-1 4.重复2,3,直到栈空(有可能顶点都遍历,有可能存在环) */public: bool TopologicalSort(const MGraph g);};#endif
//Stack.cpp#include"Stack.h"#include<iostream>using namespace std;Stack::Stack(int n){ size = n; top = base = new int[size];}bool Stack::push(int a){ *top = a; top++; return true;}int Stack::pop(){ /*if (top == base){ exit(0); }*/ return *(--top);}bool Stack::isfull(){ if (top - base >= size) return true; else return false;}bool Stack::isempty(){ if (top == base) return true; else return false;}
//ToPo.cpp#include<iostream>#include"Topo.h"#include"Stack.h"using namespace std;//NodeNode::Node(int vex , Node* a ){ vexNum = vex; next = a;}//MGraphMGraph::MGraph(int num){ Vex = num; vertices = new Node[Vex](); /*for (int i = 0; i < Vex; i++){ cout << vertices[i].vexNum; } cout << endl;*/}bool MGraph::Inition(){ if (!Vex) return false; int b, e, value; while (cin >> b&&b){ cin >> e ; Node* p = vertices[b-1].next; vertices[b-1].next = new Node(e-1,p);//前插 vertices[e-1].vexNum++;//记录入度 } /*for (int i = 0; i < Vex; i++){ for (Node* p = vertices[i].next; p; p = p->next){ cout << p->vexNum << " "; } cout << endl; }*/ return true;}//Topo /*算法: 1.计算每个顶点的入度(初始化) 初始化栈 2.将入度为0的点压栈 3.弹栈,并将以该顶点为弧尾的顶点入度-1 4.重复2,3,直到栈空(有可能顶点都遍历,有可能存在环) */bool ToPo::TopologicalSort(const MGraph g){ //如果最后有环,输出false,如果全部顶点都输出,则返回true Stack S(g.Vex); int *count = new int[g.Vex]; for (int i = 0; i < g.Vex; i++){ count[i] = g.vertices[i].vexNum; if (!count[i]) S.push(i); } int flag = 0; while (!S.isempty()){ int i = S.pop(); flag++; for (Node* p = g.vertices[i].next; p; p = p->next){ if (--count[p->vexNum] == 0){ S.push(p->vexNum); } } cout << i+1<< " "; } if (flag == g.Vex) return true; else return false;}
//main.cpp#include<iostream>#include"Topo.h"using namespace std;int main(){ int n; cout << "Please input the Vex-Number...." << endl; cin >> n; MGraph g(n); g.Inition(); ToPo p; if(!p.TopologicalSort(g)) cout<<"with cycle....."<<endl; system("pause"); return 0;}/*测试数据131 21 61 73 13 44 66 57 57 108 79 810 1110 1210 1312 13*/
阅读全文
0 0
- 拓扑排序
- 拓扑排序
- 拓扑排序
- 拓扑排序
- 拓扑排序
- 拓扑排序
- 拓扑排序
- 拓扑排序
- 拓扑排序
- 拓扑排序
- 拓扑排序
- 拓扑排序
- 拓扑排序
- 【拓扑排序】
- 拓扑排序
- 拓扑排序
- 拓扑排序
- 拓扑排序
- Java小面试题(4)
- PHP留言板提供功能
- airflow worker报错raised unexpected:airflowexception('celery command failed')
- 洪小文:以科学的方式赤裸裸地剖析AI(四)| 未来是人工智能+人类智能
- adminLTE 教程 -1 基础
- 拓扑排序
- Linux关机时执行指定脚本(亲测有效)
- 关于antd form this.props.form.getFormValue(): undefined.的bug解决
- TCP协议--复位报文段
- BZOJ2257(Jsoi2009)[瓶子和燃料]--最大公因数(裴蜀定理)
- 值传递和引用传递
- Linux nginx+node+supervisor+mysql+redis 环境部署
- NYOJ 部分和问题(入门题)
- 镜像