拓扑排序(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进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委 ...
随机推荐
- O(n)线性时间找第K大,中位数
运用快速排序的思想,可以达到线性时间找到一串数的第K大 #include<cstdio> #define F(i,a,b) for(int i=a;i<=b;i++) ],n; vo ...
- ASP.NET中的Response
Response.BufferOutput=true.false 是否设置缓存 Response.Write("") 输出字符串 Response.IsClientConne ...
- pop动画使用示例
// 弹簧动画 POPSpringAnimation *anim = [POPSpringAnimation animationWithPropertyNamed:kPOPViewFrame]; an ...
- Ubuntu 下开发 Android 环境变量设置
-----------------------------------------------------ANDROID_SDK_HOME:/home/cmm/avds PATH:/home/cmm/ ...
- jQuery判断当前元素是第几个元素&获取第N个元素
假设有下面这样一段HTML代码: <ul> <li>jQuery判断当前元素是第几个元素示例</li> <li>jQuery获取第N个元素示例</ ...
- 基于Annotation与SpringAOP的缓存简单解决方案
前言: 由于项目的原因,需要对项目中大量访问多修改少的数据进行缓存并管理,为达到开发过程中通过Annotation简单的配置既可以完成对缓存的设置与更新的需求,故而设计的该简易的解决方案. 涉及技术: ...
- HTML基本
a标签的四种状态 a:link; /* 未访问的链接 */ a:visited; /* 已访问的链接 */ a:hover; /* 当有鼠标悬停在链接上 */ a:hover 必须位于 a:lin ...
- Android 各层中日志打印功能的应用
1. HAL层 头文件:#include <utils/Log.h> 对应的级别 打印方法 VERBOSE LOGV()DEBUG LOGD()INFO LOGI()WARN LOGW() ...
- C#读取word文件
第一步:添加对在项目引用里添加上对Microsoft Word 11.0 object library的引用.右击--引用---在com标签下添加.
- Oracle case 关键字的使用
select e.salary, --case 语句开始 case then salary else salary end new_salary --case 语句结束,可见也和存储过程等结束方式一样 ...