拓扑排序(Topological)
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<stack>
using namespace std;
#define MAX 20
typedef struct node
{
struct node * nextarc;
char Info;
}ArcNode, *pArcNode;
typedef struct
{
int VerCount;//顶点数目
int ArcCount; //弧的数目
pArcNode Adj;
}AdjList, *pAdjList;
void CreatAdj(pAdjList myAdjList,int *sign)
{
int i, j;
char tail;
pArcNode p, q; printf("Please input the num of Vertex:");//顶点个数
scanf("%d", &myAdjList->VerCount); printf("Pleaase input the num of Arc");//弧的个数
scanf("%d", &myAdjList->ArcCount); myAdjList->Adj = (pArcNode)malloc(myAdjList->VerCount *sizeof(ArcNode));
for (i = ; i < myAdjList->VerCount; i++)
myAdjList->Adj[i].nextarc = NULL; printf("Please input the Graph: \n");
printf("Please input all of the vertex:\n");
fflush(stdin);
for (i = ; i < myAdjList->VerCount; i++)
{
scanf("%c", &myAdjList->Adj[i].Info);
fflush(stdin);
}
printf("Please input the Ver:\n");
for (i = ; i < myAdjList->ArcCount; i++)
{
printf("Please input tail of Arc%d :", i);
scanf("%c", &tail);
p = (pArcNode)malloc(sizeof(ArcNode));
p->nextarc = NULL;
fflush(stdin);
printf("Please input head of Arc%d :", i);
scanf("%c", &p->Info);
getchar(); for (j = ; j < myAdjList->VerCount; j++)
if (myAdjList->Adj[j].Info == p->Info)
sign[j]++; for (j = ; j < myAdjList->VerCount; j++)
{
if (myAdjList->Adj[j].Info == tail)
{
q = &myAdjList->Adj[j];
while (q->nextarc != NULL)
q = q->nextarc; q->nextarc = p;
}
} } }
void TopologicalSort(pAdjList myAdjList, int *sign)
{
stack <ArcNode> S;
ArcNode *temp; int s[];
int i, j;
for (i = ; i < myAdjList->VerCount; i++)
s[i] = sign[i]; for (i = ; i < myAdjList->VerCount; i++)
{
if (s[i] == )
{
S.push(myAdjList->Adj[i]);
s[i] = -;
}
}
while (!S.empty())
{ for (i = ; i < myAdjList->VerCount; i++)
if (myAdjList->Adj[i].Info == S.top().Info)
temp = &myAdjList->Adj[i];
S.pop();
printf("%c", temp->Info);
while (temp->nextarc != NULL)
{
for ( j = ; j < myAdjList->VerCount; j++)
{
if (myAdjList->Adj[j].Info == temp->nextarc->Info)
{
s[j]--;
if (s[j] == )
{
S.push(myAdjList->Adj[j]);
s[j] = -;
} } }
temp = temp->nextarc;
} }
}
int main()
{
AdjList myAdjList;
int sign[];
for (int i = ; i < ; i++)
sign[i] = ;
CreatAdj(&myAdjList,sign);
TopologicalSort(&myAdjList, sign); system("pause");
return ;
}
栈操作。
拓扑排序(Topological)的更多相关文章
- 拓扑排序 (Topological Sorting)
拓扑排序(Topological Sorting) 一.拓扑排序 含义 构造AOV网络全部顶点的拓扑有序序列的运算称为拓扑排序(Topological Sorting). 在图论中,拓扑排序(Topo ...
- LeetCode编程训练 - 拓扑排序(Topological Sort)
拓扑排序基础 拓扑排序用于解决有向无环图(DAG,Directed Acyclic Graph)按依赖关系排线性序列问题,直白地说解决这样的问题:有一组数据,其中一些数据依赖其他,问能否按依赖关系排序 ...
- 拓扑排序 Topological Sort
2018-05-02 16:26:07 在计算机科学领域,有向图的拓扑排序或拓扑排序是其顶点的线性排序,使得对于从顶点u到顶点v的每个有向边uv,u在排序中都在v前.例如,图形的顶点可以表示要执行的任 ...
- 算法与数据结构基础 - 拓扑排序(Topological Sort)
拓扑排序基础 拓扑排序用于解决有向无环图(DAG,Directed Acyclic Graph)按依赖关系排线性序列问题,直白地说解决这样的问题:有一组数据,其中一些数据依赖其他,问能否按依赖关系排序 ...
- 【数据结构与算法Python版学习笔记】图——拓扑排序 Topological Sort
概念 很多问题都可转化为图, 利用图算法解决 例如早餐吃薄煎饼的过程 制作松饼的难点在于知道先做哪一步.从图7-18可知,可以首先加热平底锅或者混合原材料.我们借助拓扑排序这种图算法来确定制作松饼的步 ...
- [MIT6.006] 14. Depth-First Search (DFS), Topological Sort 深度优先搜索,拓扑排序
一.深度优先搜索 它的定义是:递归探索图,必要时要回溯,同时避免重复. 关于深度优先搜索的伪代码如下: 左边DFS-Visit(V, Adj.s)是只实现visit所有连接某个特定点(例如s)的其他点 ...
- 拓扑排序(三)之 Java详解
前面分别介绍了拓扑排序的C和C++实现,本文通过Java实现拓扑排序. 目录 1. 拓扑排序介绍 2. 拓扑排序的算法图解 3. 拓扑排序的代码说明 4. 拓扑排序的完整源码和测试程序 转载请注明出处 ...
- 拓扑排序(二)之 C++详解
本章是通过C++实现拓扑排序. 目录 1. 拓扑排序介绍 2. 拓扑排序的算法图解 3. 拓扑排序的代码说明 4. 拓扑排序的完整源码和测试程序 转载请注明出处:http://www.cnblogs. ...
- 拓扑排序(一)之 C语言详解
本章介绍图的拓扑排序.和以往一样,本文会先对拓扑排序的理论知识进行介绍,然后给出C语言的实现.后续再分别给出C++和Java版本的实现. 目录 1. 拓扑排序介绍 2. 拓扑排序的算法图解 3. 拓扑 ...
- HDOJ 1285 确定比赛名次(拓扑排序)
Problem Description 有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委 ...
随机推荐
- RedHat Enterprise Linux AS4&5 安装gcc过程
三.Gcc安装方法(redhat 4): 一.安装步骤 1.使用which gcc命令查看gcc是否安装安装 2.如若没有安装则下载如下安装包,所需安装包如下 一共需要拷贝以下五个安装包: binut ...
- css单独设定样式
<style type="text/css"> #footer { position: fixed; bottom: 0; } .td-right{ border-ri ...
- zf-关于注册码过期
Webroot-index.jsp下 少写了个函数 导致登陆进去不能弹出注册码过期的对话框,函数如下 window.onload = function() { <ww:iterator valu ...
- POJ 3984 迷宫问题 记录路径的广搜
主要是学一下如何在广搜中记录路径:每找到一个点我就记录下这个点是由那个点得来的,这样我找到最后个点后,我就可以通过回溯得到我走过的路径,具体看代码吧~ #include<cstdio> # ...
- (转)Hadoop的InputFormats和OutputFormats
Data Mining Hadoop的InputFormats和OutputFormats InputFormat InputFormat类用来产生InputSplit,并把它切分成record. p ...
- 在IE6里面当元素浮动后再设置margin那么就会产生双倍边距
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 转:检查点(web_reg_find函数详解)
LR检查点 设置检查点的目的不只是为了验证我们的脚本没有错误,而更重要的是一个规范问题,如何使得测试结果更具有说服力,因此建议所有的测试脚本中都添加检查点设置 一.设置检查点的方法 1.将脚本切换到树 ...
- The 2014 ACMICPC Asia Regional Guangzhou Online
[A]-_-/// [B]线段树+位运算(感觉可出) [C]地图BFS,找最长线 [D]地图BFS,加上各种复杂情况的最短路-_- [E]-_-/// [F]三分+圆与线段的交点,计算几何 [G]-_ ...
- javascript svg 页面 loading
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 使用axis2开发webservices并打包到tomcat
1. 写service类 package com.datatrans.demo; public class HelloServiceNew { public String sayHelloNew(){ ...