拓扑排序(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进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委 ...
随机推荐
- springmvc传递json数据到前台显示
需要两个包 jackson-core-asl, jackson-mapper-asl controller @RequestMapping(value="/findEduList" ...
- C# 中的内存管理,摘自网络
C#编程的一个优点是程序员不需要关心具体的内存管理,尤其是垃圾收集器会处理所有的内存清理工作.虽然不必手工管理内存,但如果要编写高质量的代码,还是要理解后台发生的事情,理解C#的内存管理.本文主要介绍 ...
- POJ 3254 Corn Fields(状态压缩)
一道状态压缩的题,错了好多次....应该先把满足的情况预处理出来 #include<iostream> #include<cstdio> #include<cstring ...
- hihoCoder 1252 Kejin Game
2015 ACM / ICPC 北京站 D题 网络最大流 和同学讨论了一会儿,还是Xiang578机智... ... /* ************************************** ...
- Commons Codec基本使用(转载)
在实际的应用中,我们经常需要对字符串进行编解码,Apache Commons家族中的Commons Codec就提供了一些公共的编解码实现,比如Base64, Hex, MD5,Phonetic an ...
- HTML中为何P标签内不可包含DIV标签? (转)
起因:在做项目时发现原本在DW中无误的代码到了MyEclipse6.0里面却提示N多错误,甚是诧异.于是究其原因,发现块级元素P内是不能嵌套DIV的. 深究:我们先来认识in-line内联元素和blo ...
- openstack controller ha测试环境搭建记录(五)——配置rabbitmq集群
配置rabbitmq集群的步骤非常简单,因为其本身含集群功能,参考openstack官网文档:http://docs.openstack.org/ha-guide/controller-ha-rabb ...
- CodeForces 618C CodeForces 618C
第一反应是在凸包上随便找一条边,然后找剩下n-2个点里面距离这条边最短的一个点,这三点就构成了符合要求的三角形..然而..精度被卡死. 换种思路,随便找两个点P1,P2,找剩下n-2个点中哪一个点与P ...
- Ubuntu Apache2 配置解析
转自:http://www.cnblogs.com/ylan2009/archive/2012/02/25/2368028.html Ubuntu的Apache 2.4 之后的版本的配置文件是 / ...
- (简单) POJ 1278 Catch That Cow,回溯。
Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...