拓扑排序

拓扑排序

大家好,又见面了,我是全栈君,祝每个程序员都可以多学几门语言。

拓扑排序是对有向无环图的一种排序。表示了顶点按边的方向出现的先后顺序。假设有环,则无法表示两个顶点的先后顺序。

在现实生活中,也会有不少应用样例,比方学校课程布置图,要先修完一些基础课,才干够继续修专业课。
一个简单的求拓扑排序的算法:首先要找到随意入度为0的一个顶点,删除它及全部相邻的边,再找入度为0的顶点,以此类推,直到删除全部顶点。顶点的删除顺序即为拓扑排序。
    非常easy得到拓扑排序的伪代码:
    void TopSort(Graph g)
    {
        for (int i=0; i<vertexnum; i++)
        {
            vertex v = FindZeroIndegree(g);
            if (v is not vertex)        
                cout <<“the graph has cycle”<<endl;
            cout << v ;
            foreach vertex w adjacent to v
                w.indegree–;
        }
    }
拓扑排序   
    相同以上图为例,对于该图进行拓扑排序会得到:v1 v2 v5 v4 v3 v7 v6 或者v1 v2 v5 v4 v7 v3 v6 。
    仍然利用上一贴图的构建方法,进行验证。
    代码实现:
 

 
#include <iostream>

using
namespace
std;

#define MAX_VERTEX_NUM    20

struct
adjVertexNode

{

   
int
adjVertexPosition;

   
adjVertexNode
*
next;

};

struct
VertexNode

{

   
char
data
[
2
];

   
adjVertexNode
*
list;

   
int
indegree;

};

struct
Graph

{

   
VertexNode
VertexNode
[
MAX_VERTEX_NUM
];

   
int
vertexNum;

   
int
edgeNum;

};

void
CreateGraph (
Graph
&
g)

{

    
int
i
,
j
,
edgeStart
,
edgeEnd;

    
adjVertexNode
*
adjNode;

    
cout
<<
“Please input vertex and edge num (vnum enum):”
<<
endl;

    
cin
>>
g
.
vertexNum
>>
g
.
edgeNum;

    
cout
<<
“Please input vertex information (v1)
/n
note: every vertex info end with Enter”
<<
endl;

    
for (
i
=
0;
i
<
g
.
vertexNum;
i
++)

    
{

        
cin
>>
g
.
VertexNode
[
i
].
data;
// vertex data info.

        
g
.
VertexNode
[
i
].
list
=
NULL;

        
g
.
VertexNode
[
i
].
indegree
=
0;

    
}

    
cout
<<
“input edge information(start end):”
<<
endl;

    
for (
j
=
0;
j
<
g
.
edgeNum;
j
++)

    
{

        
cin
>>
edgeStart
>>
edgeEnd;

        
adjNode
=
new
adjVertexNode;

        
adjNode
->
adjVertexPosition
=
edgeEnd

1;
// because array begin from 0, so it is j-1

        
adjNode
->
next
=
g
.
VertexNode
[
edgeStart

1
].
list;

        
g
.
VertexNode
[
edgeStart

1
].
list
=
adjNode;

        
//每添加�一条边,则边的End顶点的入度加1

        
g
.
VertexNode
[
edgeEnd

1
].
indegree
++;

    
}

}

void
PrintAdjList(
const
Graph
&
g)

{

   
cout
<<
“The adjacent list for graph is:”
<<
endl;

   
for (
int
i
=
0;
i
<
g
.
vertexNum;
i
++)

   
{

       
cout
<<
g
.
VertexNode
[
i
].
data
<<
“->”;

       
adjVertexNode
*
head
=
g
.
VertexNode
[
i
].
list;

       
if (
head
==
NULL)

           
cout
<<
“NULL”;

       
while (
head
!=
NULL)

       
{

           
cout
<<
head
->
adjVertexPosition
+
1
<<
” “;

           
head
=
head
->
next;

       
}

       
cout
<<
endl;

   
}

}

VertexNode
&
FindZeroIndegree(
Graph
&
g)

{

   
for (
int
i
=
0;
i
<
g
.
vertexNum;
i
++)

   
{

       
if (
g
.
VertexNode
[
i
].
indegree
==
0)

           
return
g
.
VertexNode
[
i
];

   
}

   
return
g
.
VertexNode
[
0
];

}

void
TopSort(
Graph
&
g)

{

   
cout
<<
“The topsort is:”
<<
endl;

   
for (
int
i
=
0;
i
<
g
.
vertexNum;
i
++)

   
{

       
VertexNode
&
v
=
FindZeroIndegree(
g);

       
if (
v
.
indegree
!=
NULL)

           
cout
<<
“The graph has cycle, can not do topsort”
<<
endl;

       
// print graph as topsort.

       
cout
<<
v
.
data
<<
” “;

       
// for each vertex w adjacent to v, –indegree

       
adjVertexNode
*
padjv
=
v
.
list;

       
while (
padjv
!=
NULL)

       
{
//!!这个算法这里破坏了原图中的入度信息。最后入度均为1

           
g
.
VertexNode
[
padjv
->
adjVertexPosition
].
indegree
;

           
padjv
=
padjv
->
next;

       
}

       
//避免入度信息均为零FindZeroIndegree找到删除的顶点,将删除的顶点入度置为1

       
v
.
indegree
++;

   
}

   
cout
<<
endl;

}

void
DeleteGraph(
Graph
&
g)

{

   
for (
int
i
=
0;
i
<
g
.
vertexNum;
i
++)

   
{

       
adjVertexNode
*
tmp
=
NULL;

       
while(
g
.
VertexNode
[
i
].
list
!=
NULL)

       
{

           
tmp
=
g
.
VertexNode
[
i
].
list;

           
g
.
VertexNode
[
i
].
list
=
g
.
VertexNode
[
i
].
list
->
next;

           
delete
tmp;

           
tmp
=
NULL;

       
}

   
}

}

int
main(
int
argc
,
const
char
**
argv)

{

   
Graph
g;

   
CreateGraph(
g);

   
PrintAdjList(
g);

   
TopSort(
g);

   
DeleteGraph(
g);

   
return
0;

}

 执行结果:
拓扑排序
 

     从上面的代码能发现
FindZeroIndegree的时间复杂度为O(|V|),TopSort的时间复杂度为O(|V|2)
     原因在于,每次删除顶点,仅仅有邻接点须要调整入度,但
FindZeroIndegree却是遍历了全部顶点,甚至已经删除的顶点。
     更为合理的方法是将每次遍历得出的入度为0的顶点放入一个队列。
void
TopSort2(
Graph
&
g)

{

   
queue
<
VertexNode
>
q;

   
for (
int
i
=
0;
i
<
g
.
vertexNum;
i
++)

   
{

       
if (
g
.
VertexNode
[
i
].
indegree
==
0)

           
q
.
push(
g
.
VertexNode
[
i
]);

   
}

   
int
count
=
0;

   
cout
<<
“The topsort is:”
<<
endl;

   
while (
!
q
.
empty())

   
{

       
VertexNode
v
=
q
.
front();

       
q
.
pop();

       
cout
<<
v
.
data
<<
” “;

       
count
++;

       
adjVertexNode
*
padjv
=
v
.
list;

       
while (
padjv
!=
NULL)

       
{
//!!这个算法这里破坏了原图中的入度信息。最后入度均为1

           
if (
(
g
.
VertexNode
[
padjv
->
adjVertexPosition
].
indegree)
==
0)

               
q
.
push(
g
.
VertexNode
[
padjv
->
adjVertexPosition
]);

           
padjv
=
padjv
->
next;

       
}

   
}

   
if (
count
!=
g
.
vertexNum)

       
cout
<<
“The graph has cycle, can not do topsort”
<<
endl;

}
     内部的while循环最多运行|E|次,即每条边运行一次。队列对每一个顶点最多运行一次操作,所以新算法的时间复杂度为O(|E|+|V|). 优于O(|V|2)由于拓扑图边数最多有n(n-1)/2,即O(|E|+|V|)<=O(|V|2)

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/118755.html原文链接:https://javaforall.net

(0)
上一篇 2021年12月1日 下午6:00
下一篇 2021年12月1日 下午7:00


相关推荐

  • IIS设置ISAPI筛选器Rewrite组件防盗链(防盗链可以节省流量,提高性能)

    IIS设置ISAPI筛选器Rewrite组件防盗链(防盗链可以节省流量,提高性能)如何在IIS的设置下添加ISAPI筛选器里的Rewrite组件,防止图片被盗用链接。首先笔者要说的是“盗链”很常见的现象,虽然没有采集那么“流行”,但是对于被盗者来说,碰到这事还真的及时解决,要不资源的消耗很可能会影响自身网站的正常运营。那究竟什么是盗链,怎样防止网站的信息被盗链呢?下面简单的说下:“盗链”的定义是:此内容不在自己服务器上,而通过技术手段,绕过别人放广告有利益的最终页,直接…

    2022年7月23日
    9
  • virsh命令详解_lsattr命令详解

    virsh命令详解_lsattr命令详解virshhelp分组的命令:域管理(虚拟机实例管理)DomainManagement(helpkeyword’domain’):attach-device从一个XML文件附加装置attach-disk附加磁盘设备attach-interface获得网络界面autostart自动开始一个域

    2022年8月12日
    5
  • 目标检測的图像特征提取之(一)HOG特征

    目标检測的图像特征提取之(一)HOG特征

    2021年12月3日
    46
  • Cacls命令详解

    Cacls命令详解在复杂的入侵过程中我们必须掌握很多的命令 当我们获得 webshell 后 对于提权来讲这些命令就显示得尤为重要了 首先磁盘分区格式得是 NTFS FAT32 是不行的 一 Cacls exe 命令的使用这是一个在 Windows2000 XP Server2003 操作系统下都可以使用的命令 作用是显示或者修改文件的访问控制表 在命令中可以使用通配符指定多个文件 也可以在命令中指定多个用户 命令

    2026年3月17日
    2
  • 小白入门智能体agent — 畅读《A SURVEY OF SELF-EVOLVING AGENTS: ON PATH TOARTIFICIAL SUPER INTELLIGENCE》分篇(一)

    小白入门智能体agent — 畅读《A SURVEY OF SELF-EVOLVING AGENTS: ON PATH TOARTIFICIAL SUPER INTELLIGENCE》分篇(一)

    2026年3月16日
    2
  • 阿里云轻量服务器开放指定端口吗_阿里云服务器怎么远程连接

    阿里云轻量服务器开放指定端口吗_阿里云服务器怎么远程连接第一次使用云服务暴露端口,踩了一些坑总结一下整个过程首先要在防火墙中添加对应端口,然后,进入安全组添加指定的端口,才算完成整个设置一、防火墙设置1、进入服务器选择防火墙,点击“添加规则”2、输入指定端口号,协议选择TCP就行,完成后点击确定二、在安全组中添加端口1、如果没有安全组时点击“创建安全组”,有则点击“配置规则”2、点击“手动添加”3、输入对应的端口号、源地址,点击保存整个设置过程就是这样,整个设置很简单,但是要是漏掉一个环节对新手来也是麻烦的…

    2022年10月2日
    4

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号