拓扑排序是求一个AOV网(顶点代表活动, 各条边表示活动之间的率先关系的有向图)中各活动的一个拓扑序列的运算, 可用于測试AOV

网络的可行性.

整个算法包含三步:

1.计算每一个顶点的入度, 存入InDegree数组中.

2.检查InDegree数组中顶点的入度, 将入度为零的顶点进栈.

3.不断从栈中弹出入度为0的顶点并输出, 并将该顶点为尾的全部邻接点的入度减1, 若此时某个邻接点的入度为0, 便领其进栈. 反复步骤

3, 直到栈为空时为止. 此时, 或者所有顶点都已列出, 或者因图中包括有向回路, 顶点未能所有列出.

实现代码:

#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
#include "queue"
#include "stack"
#include "cmath"
#include "utility"
#include "map"
#include "set"
#include "vector"
#include "list"
#include "string"
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
enum ResultCode { Underflow, Overflow, Success, Duplicate, NotPresent, Failure, HasCycle };
template <class T>
struct ENode
{
ENode() { nxtArc = NULL; }
ENode(int vertex, T weight, ENode *nxt) {
adjVex = vertex;
w = weight;
nxtArc = nxt;
}
int adjVex;
T w;
ENode *nxtArc;
/* data */
};
template <class T>
class Graph
{
public:
virtual ~Graph() {}
virtual ResultCode Insert(int u, int v, T &w) = 0;
virtual ResultCode Remove(int u, int v) = 0;
virtual bool Exist(int u, int v) const = 0;
/* data */
};
template <class T>
class LGraph: public Graph<T>
{
public:
LGraph(int mSize);
~LGraph();
ResultCode Insert(int u, int v, T &w);
ResultCode Remove(int u, int v);
bool Exist(int u, int v) const;
int Vertices() const { return n; }
void Output();
protected:
ENode<T> **a;
int n, e;
/* data */
};
template <class T>
void LGraph<T>::Output()
{
ENode<T> *q;
for(int i = 0; i < n; ++i) {
q = a[i];
while(q) {
cout << '(' << i << ' ' << q -> adjVex << ' ' << q -> w << ')';
q = q -> nxtArc;
}
cout << endl;
}
cout << endl << endl;
}
template <class T>
LGraph<T>::LGraph(int mSize)
{
n = mSize;
e = 0;
a = new ENode<T>*[n];
for(int i = 0; i < n; ++i)
a[i] = NULL;
}
template <class T>
LGraph<T>::~LGraph()
{
ENode<T> *p, *q;
for(int i = 0; i < n; ++i) {
p = a[i];
q = p;
while(p) {
p = p -> nxtArc;
delete q;
q = p;
}
}
delete []a;
}
template <class T>
bool LGraph<T>::Exist(int u, int v) const
{
if(u < 0 || v < 0 || u > n - 1 || v > n - 1 || u == v) return false;
ENode<T> *p = a[u];
while(p && p -> adjVex != v) p = p -> nxtArc;
if(!p) return false;
return true;
}
template <class T>
ResultCode LGraph<T>::Insert(int u, int v, T &w)
{
if(u < 0 || v < 0 || u > n - 1 || v > n - 1 || u == v) return Failure;
if(Exist(u, v)) return Duplicate;
ENode<T> *p = new ENode<T>(v, w, a[u]);
a[u] = p;
e++;
return Success;
}
template <class T>
ResultCode LGraph<T>::Remove(int u, int v)
{
if(u < 0 || v < 0 || u > n - 1 || v > n - 1 || u == v) return Failure;
ENode<T> *p = a[u], *q = NULL;
while(p && p -> adjVex != v) {
q = p;
p = p -> nxtArc;
}
if(!p) return NotPresent;
if(q) q -> nxtArc = p -> nxtArc;
else a[u] = p -> nxtArc;
delete p;
e--;
return Success;
}
template <class T>
class ExtLgraph: public LGraph<T>
{
public:
ExtLgraph(int mSize): LGraph<T>(mSize) {}
void TopoSort(int *order);
private:
void CallInDegree(int *InDegree);
/* data */
};
template <class T>
void ExtLgraph<T>::TopoSort(int *order)
{
int *InDegree = new int[LGraph<T>::n];
int top = -1; // 置栈顶指针为-1, 代表空栈
ENode<T> *p;
CallInDegree(InDegree); // 计算每一个顶点的入度
for(int i = 0; i < LGraph<T>::n; ++i)
if(!InDegree[i]) { // 图中入度为零的顶点进栈
InDegree[i] = top;
top = i;
}
for(int i = 0; i < LGraph<T>::n; ++i) { // 生成拓扑排序
if(top == -1) throw HasCycle; // 若堆栈为空, 说明图中存在有向环
else {
int j = top;
top = InDegree[top]; // 入度为0的顶点出栈
order[i] = j;
cout << j << ' ';
for(p = LGraph<T>::a[j]; p; p = p -> nxtArc) { // 检查以顶点j为尾的全部邻接点
int k = p -> adjVex; // 将j的出邻接点入度减1
InDegree[k]--;
if(!InDegree[k]) { // 顶点k入度为0时进栈
InDegree[k] = top;
top = k;
}
}
}
}
}
template <class T>
void ExtLgraph<T>::CallInDegree(int *InDegree)
{
for(int i = 0; i < LGraph<T>::n; ++i)
InDegree[i] = 0; // 初始化InDegree数组
for(int i = 0; i < LGraph<T>::n; ++i)
for(ENode<T> *p = LGraph<T>::a[i]; p; p = p -> nxtArc) // 检查以顶点i为尾的全部邻接点
InDegree[p -> adjVex]++; // 将顶点i的邻接点p -> adjVex的入度加1
}
int main(int argc, char const *argv[])
{
ExtLgraph<int> lg(9);
int w = 10; lg.Insert(0, 2, w); lg.Insert(0, 7, w);
lg.Insert(2, 3, w); lg.Insert(3, 5, w);
lg.Insert(3, 6, w); lg.Insert(4, 5, w);
lg.Insert(7, 8, w); lg.Insert(8, 6, w);
int *order = new int[9];
lg.TopoSort(order);
cout << endl;
delete []order;
return 0;
}

拓扑排序的实现_TopoSort的更多相关文章

  1. 算法与数据结构(七) AOV网的拓扑排序

    今天博客的内容依然与图有关,今天博客的主题是关于拓扑排序的.拓扑排序是基于AOV网的,关于AOV网的概念,我想引用下方这句话来介绍: AOV网:在现代化管理中,人们常用有向图来描述和分析一项工程的计划 ...

  2. 有向无环图的应用—AOV网 和 拓扑排序

    有向无环图:无环的有向图,简称 DAG (Directed Acycline Graph) 图. 一个有向图的生成树是一个有向树,一个非连通有向图的若干强连通分量生成若干有向树,这些有向数形成生成森林 ...

  3. 【BZOJ-2938】病毒 Trie图 + 拓扑排序

    2938: [Poi2000]病毒 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 609  Solved: 318[Submit][Status][Di ...

  4. BZOJ1565 [NOI2009]植物大战僵尸(拓扑排序 + 最大权闭合子图)

    题目 Source http://www.lydsy.com/JudgeOnline/problem.php?id=1565 Description Input Output 仅包含一个整数,表示可以 ...

  5. 图——拓扑排序(uva10305)

    John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...

  6. Java排序算法——拓扑排序

    package graph; import java.util.LinkedList; import java.util.Queue; import thinkinjava.net.mindview. ...

  7. poj 3687(拓扑排序)

    http://poj.org/problem?id=3687 题意:有一些球他们都有各自的重量,而且每个球的重量都不相同,现在,要给这些球贴标签.如果这些球没有限定条件说是哪个比哪个轻的话,那么默认的 ...

  8. 拓扑排序 - 并查集 - Rank of Tetris

    Description 自从Lele开发了Rating系统,他的Tetris事业更是如虎添翼,不久他遍把这个游戏推向了全球. 为了更好的符合那些爱好者的喜好,Lele又想了一个新点子:他将制作一个全球 ...

  9. *HDU1285 拓扑排序

    确定比赛名次 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

随机推荐

  1. Nmap误报1720端口开放的原因

    在使用Nmap扫描服务器开放端口(全连接扫描)时,一直会发现误报1720端口开放,telnet也有时会连接成功.而实际上服务器并未开启此端口.经过查阅资料,确定原因如下: H.323协议在负载中放入了 ...

  2. CodeForces 723E One-Way Reform

    构造. 有一种十分巧妙的方法可以使图中所有度数为偶数的节点,经过每条边定向后,出度和入度都相等. 首先统计每个节点的度数,将度数为奇数的节点与编号为$n+1$的节点连边,这样一来,这张新图变成了每个节 ...

  3. Python3 文件基本修改替换

    现有原文件: Somehow, it seems the love I knew was always the most destructive kind 不知为何,我经历的爱情总是最具毁灭性的的那种 ...

  4. 读书笔记(高性能javascript)(一)

    1.加载与执行: (1)将脚本放在底部:(否则会阻塞) (2)由于每个<script>标签初始下载时都会阻塞页面渲染,所以减少页面包含的<script>标签数量有助于改善这一情 ...

  5. If you want to allow applications containing errors to be published on the server

    If you want to allow applications containing errors to be published on the server, enable the Allow ...

  6. 【POJ 2154】 Color (置换、burnside引理)

    Color Description Beads of N colors are connected together into a circular necklace of N beads (N< ...

  7. eclipse汉化 adt汉化

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha

  8. React的思想

    react是什么 react是开发出来用来促进UI交互的,创建带有状态的.可复用的UI组件的IU库 react不仅可以在浏览器端使用,还可以在服务器端使用,还可以两端一起使用. react的底层概念: ...

  9. PHP5.3魔术方法 __invoke

    这个魔幻方法被调用的时机是: 当一个对象当做函数调用的时候, 如果对象定义了__invoke魔幻方法则这个函数会被调用, class Callme { public function __invoke ...

  10. 将HTML表格的每行每列转为数组,采集表格数据

    将HTML表格的每行每列转为数组,采集表格数据 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 //将HTML ...