#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)的更多相关文章

  1. 拓扑排序 (Topological Sorting)

    拓扑排序(Topological Sorting) 一.拓扑排序 含义 构造AOV网络全部顶点的拓扑有序序列的运算称为拓扑排序(Topological Sorting). 在图论中,拓扑排序(Topo ...

  2. LeetCode编程训练 - 拓扑排序(Topological Sort)

    拓扑排序基础 拓扑排序用于解决有向无环图(DAG,Directed Acyclic Graph)按依赖关系排线性序列问题,直白地说解决这样的问题:有一组数据,其中一些数据依赖其他,问能否按依赖关系排序 ...

  3. 拓扑排序 Topological Sort

    2018-05-02 16:26:07 在计算机科学领域,有向图的拓扑排序或拓扑排序是其顶点的线性排序,使得对于从顶点u到顶点v的每个有向边uv,u在排序中都在v前.例如,图形的顶点可以表示要执行的任 ...

  4. 算法与数据结构基础 - 拓扑排序(Topological Sort)

    拓扑排序基础 拓扑排序用于解决有向无环图(DAG,Directed Acyclic Graph)按依赖关系排线性序列问题,直白地说解决这样的问题:有一组数据,其中一些数据依赖其他,问能否按依赖关系排序 ...

  5. 【数据结构与算法Python版学习笔记】图——拓扑排序 Topological Sort

    概念 很多问题都可转化为图, 利用图算法解决 例如早餐吃薄煎饼的过程 制作松饼的难点在于知道先做哪一步.从图7-18可知,可以首先加热平底锅或者混合原材料.我们借助拓扑排序这种图算法来确定制作松饼的步 ...

  6. [MIT6.006] 14. Depth-First Search (DFS), Topological Sort 深度优先搜索,拓扑排序

    一.深度优先搜索 它的定义是:递归探索图,必要时要回溯,同时避免重复. 关于深度优先搜索的伪代码如下: 左边DFS-Visit(V, Adj.s)是只实现visit所有连接某个特定点(例如s)的其他点 ...

  7. 拓扑排序(三)之 Java详解

    前面分别介绍了拓扑排序的C和C++实现,本文通过Java实现拓扑排序. 目录 1. 拓扑排序介绍 2. 拓扑排序的算法图解 3. 拓扑排序的代码说明 4. 拓扑排序的完整源码和测试程序 转载请注明出处 ...

  8. 拓扑排序(二)之 C++详解

    本章是通过C++实现拓扑排序. 目录 1. 拓扑排序介绍 2. 拓扑排序的算法图解 3. 拓扑排序的代码说明 4. 拓扑排序的完整源码和测试程序 转载请注明出处:http://www.cnblogs. ...

  9. 拓扑排序(一)之 C语言详解

    本章介绍图的拓扑排序.和以往一样,本文会先对拓扑排序的理论知识进行介绍,然后给出C语言的实现.后续再分别给出C++和Java版本的实现. 目录 1. 拓扑排序介绍 2. 拓扑排序的算法图解 3. 拓扑 ...

  10. HDOJ 1285 确定比赛名次(拓扑排序)

    Problem Description 有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委 ...

随机推荐

  1. AndroidStudio使用注意事项

    今天在引入GitHUb上的开源框架时,写好依赖后编译时,报以下错误: Error:Execution failed for task ':app:processDebugResources'.> ...

  2. HDU 1204 基础DP 非连续字段的最大和

    其实这个感觉是一眼题,只不过我真心太菜了. 题目已经告诉你了,有m段,n个数字,那么我们就只需要dp[m][n]即可,然后最后的答案肯定是dp[m][]的那一行,所以其他行都是没有用的,因为我们可以把 ...

  3. android的m、mm、mmm编译命令的使用

    android的m.mm.mmm编译命令的使用   android源码目录下的build/envsetup.sh文件,描述编译的命令 - m:       Makes from the top of ...

  4. centos dmesg

    linux dmesg命令详解   功能说明:显示开机信息. 语 法:dmesg [-cn][-s ] 补充说明:kernel会将开机信息存储在ring buffer,若是开机时来不及查看信息,可利用 ...

  5. docker rmi all

    docker stop $(docker ps -a -q) docker rm $(docker ps -a -q)

  6. 转 oraenv

    代码如下: lines, characters #!/bin/sh # # Get the machine type and then set up ORATAB and TNS_ADMIN vari ...

  7. egret GUI 文本混排+文本链接的聊天解决方案【取巧法】

    ui方面: <e:Scroller verticalScrollPolicy="auto" width="468" height="620&qu ...

  8. PAT (Advanced Level) 1016. Phone Bills (25)

    简单模拟题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm& ...

  9. 搭建git服务器及利用git hook自动布署代码

    注意:服务器:Ubuntu Server 14.04,我的项目比较小,所有操作都使用root,建议最好新建一个用户(需要有管理员权限,否则在后面使用hooks自动部署代码时会出现各种权限问题,很蛋疼的 ...

  10. 跳舞链 Dancing Links

    作为搜索里面的一个大头,终于刷了一部分题目了,跳舞链一般都有现成的模板来套...... 至于跳舞链的学习的话,我觉得http://www.cnblogs.com/grenet/p/3163550.ht ...