当前位置:网站首页>拓扑排序
拓扑排序
2022-06-22 18:28:00 【单单一个越字】
B站小甲鱼视频
https://www.bilibili.com/video/BV1jW411K7yg?p=67
有向无环图(DAG)才有拓扑排序
拓扑序列:设G=(V,E)是一个具有n个顶点的有向图,V中的顶点序列V1,V2,……,Vn满足若从顶点Vi到Vj有一条路径,则在顶点序列中顶点Vi必在顶点Vj之前。则我们称这样的顶点序列为一个拓扑序列。
拓扑排序:所谓的拓扑排序,其实就是对一个有向图构造拓扑序列的过程。
• 对AOV网进行拓扑排序的方法和步骤如下:
– 从AOV网中选择一个没有前趋的顶点(该顶点的入度为0)并且输出它;
– 从网中删去该顶点,并且删去从该顶点发出的全部有向边;
– 重复上述两步,直到剩余网中不再存在没有前趋的顶点为止。
• 算法时间复杂度:
– 对一个具有n个顶点,e条边的网来说,初始建立入度为零的顶点栈,要检查所有顶点一次,执行时间为O(n)。
– 排序中,若AOV网无回路,则每个顶点入、出栈各一次,每个表结点被检查一次,因而执行时间是 O(n+e)。
– 所以,整个算法的时间复杂度是 O(n+e)。
// 边表结点声明
typedef struct EdgeNode
{
int adjvex;
struct EdgeNode *next;
}EdgeNode;
// 顶点表结点声明
typedef struct VertexNode
{
int in; // 顶点入度
int data;
EdgeNode *firstedge;
}VertexNode, AdjList[MAXVEX];
typedef struct
{
AdjList adjList;
int numVertexes, numEdges;
}graphAdjList, *GraphAdjList;
// 拓扑排序算法
// 若GL无回路,则输出拓扑排序序列并返回OK,否则返回ERROR
Status TopologicalSort(GraphAdjList GL)
{
EdgeNode *e;
int i, k, gettop;
int top = 0; // 用于栈指针下标索引
int count = 0; // 用于统计输出顶点的个数
int *stack; // 用于存储入度为0的顶点
stack = (int *)malloc(GL->numVertexes * sizeof(int));
for( i=0; i < GL->numVertexes; i++ )
{
if( 0 == GL->adjList[i].in )
{
stack[++top] = i; // 将度为0的顶点下标入栈
}
}
while( 0 != top )
{
gettop = stack[top--]; // 出栈
printf("%d -> ", GL->adjList[gettop].data);
count++;
for( e=GL->adjList[gettop].firstedge; e; e=e->next )
{
k = e->adjvex;
// 注意:下边这个if条件是分析整个程序的要点!
// 将k号顶点邻接点的入度-1,因为他的前驱已经消除
// 接着判断-1后入度是否为0,如果为0则也入栈
if( !(--GL->adjList[k].in) )
{
stack[++top] = k;
}
}
}
if( count < GL->numVertexes ) // 如果count小于顶点数,说明存在环
{
return ERROR;
}
else
{
return OK;
}
}
边栏推荐
- 08_一句话让你人间清醒
- antd tree 树状选择器 子类必填
- 再谈SQL profile : 到底能不能固定执行计划?
- 0816 shortcomings of Feida (improvement direction)
- [nfs failed to mount problem] mount nfs: access denied by server while mounting localhost:/data/dev/mysql
- Comparison of NAND flash particles SLC, MLC, TLC and QLC
- 3D打印机耗材受潮
- Antd tree tree tree selector subclass required
- Service practice: use service to complete a download task
- Human pose estimation
猜你喜欢

Nrf51822 peripheral learning

ABAQUS 使用RSG绘制插件初体验

510000 prize pool invites you to join the war! The second Alibaba cloud ECS cloudbuild developer competition is coming

新唐NUC980使用记录:开发环境准备与编译配置基础说明

安装Office的一些工具

YARN笔记

NAND闪存(NAND Flash)颗粒SLC,MLC,TLC,QLC的对比

2.什么是机械设计?

将一维数据(序列)转化为二维数据(图像)的方法汇总GAFS, MTF, Recurrence plot,STFT

51万奖池邀你参战!第二届阿里云ECS CloudBuild开发者大赛来袭
随机推荐
Compilation error: /usr/bin/ld: /usr/local/lib/libgflags a(gflags.cc.o): relocation R_ X86_ 64_ 32S against `. rodata‘
1.4----- PCB design? (circuit design) determination scheme
.Net 5.0 通过IdentityServer4实现单点登录之oidc认证部分源码解析
Experiment 7 trigger
给对象赋值
MySQL多表操作
优化了一半的SQL
0.1----- process of drawing PCB with AD
Altium Designer中off grid pin解决方法
lua--数据类型、变量、循环、函数、运算符的使用
1.3-----Simplify 3D切片软件简单设置
Calendar control programming
如何在 FlowUs和Notion 等笔记软件中进行任务管理?
what? Homekit, Micah, aqara and other ecosystems can also be linked with tmall elf ecology through zhiting?
自定义控件AutoScaleMode为Font造成宽度增加的问题
生产系统SQL执行计划突然变差怎么办?
Solution of off grid pin in Altium Designer
Focal and global knowledge distillation for detectors
c# winform 嵌入flash
AttributeError: ‘KeyedVectors‘ object has no attribute ‘wv‘