图解拓扑排序(Topologicalsort)

更新时间:2023-07-28 01:00:36 阅读: 评论:0

图解拓扑排序(Topologicalsort)
⼀、什么是拓扑排序
下图就是拓扑排序
拓扑排序其实是⼀个线性排序。——若图中存在⼀条有向边从u指向v,则在拓扑排序中u⼀定出现在v前⾯。
a topological sort or topological ordering of a  is a  of its  such that for every directed edge uv from vertex u to vertex v, u comes before v in the ordering.
⼆、图解拓扑排序
算法思路:
1. 从图中选择⼀个没有前驱(即⼊度为0)的顶点并输出。
2. 从图中删除该顶点和所有以它为起点的有向边。
重复 1 和 2 直到图为空或当前图中不存在⽆前驱的顶点为⽌。
伪码:
例⼦:
. 1.  选⼊度为0的节点A,  输出A。  删除AB AC的边(B⼊度为1-1=0,C⼊度为2-1=1)
2.  选⼊度为0的节点B, 输出B。删除BC,BE的边(C⼊度为1-1=0,E⼊度-1)
3.  选⼊度为0的节点C, 输出C。删以C开始的边(对应节点⼊度-1)。。。。。。。。。。。继续重复。。。。。。。。。。
注: 拓扑排序结果不唯⼀(同时有多个⼊度为0)。
三、Code in Java
import java.util.*;
// data structure to store graph edges
class Edge
{
int source, dest;
public Edge(int source, int dest) {
this.source = source;
this.dest = dest;
}
};
// class to reprent a graph object
英文的自我介绍
class Graphothers
{
英语陪同翻译
// An array of Lists to reprent adjacency list视频剪辑培训
List<List<Integer>> adjList = null;
// stores indegree of a vertex
List<Integer> indegree = null;
// Constructor
Graph(List<Edge> edges, int N) {
adjList = new ArrayList<>(N);
for (int i = 0; i < N; i++) {
adjList.add(i, new ArrayList<>());
}
// initialize indegree of each vertex by 0
简报怎么写
indegree = new ArrayList<>(Collections.nCopies(N, 0));
// add edges to the undirected graph
for (int i = 0; i < edges.size(); i++)
{hide
kill switch{
int src = (i).source;
int dest = (i).dest;
// add an edge from source to destination
<(src).add(dest);
/
/ increment in-degree of destination vertex by 1
indegree.t(dest, (dest) + 1);
}
}
}
class Main
{
// performs Topological Sort on a given DAG
public static List<Integer> doTopologicalSort(Graph graph, int N) {
// list to store the sorted elements
List<Integer> L = new ArrayList<>();
/
/ get indegree information of the graph
List<Integer> indegree = graph.indegree;
// Set of all nodes with no incoming edges
Stack<Integer> S = new Stack<>();
for (int i = 0; i < N; i++) {
if ((i) == 0) {
料理鼠王英文版S.add(i);
}
}
while (!S.isEmpty())
{
/
/ remove a node n from S
int n = S.pop();
// add n to tail of L
L.add(n);
zincoxidefor (int m : (n))
{
// remove edge from n to m from the graph
indegree.t(m, (m) - 1);
// if m has no other incoming edges then
// inrt m into S
if ((m) == 0) {
S.add(m);
}
}
}
// if graph has edges then graph has at least one cycle
for (int i = 0; i < N; i++) {
if ((i) != 0) {
return null;
}
}
return L;
}
public static void main(String[] args)
{
// vector of graph edges as per above diagram
// vector of graph edges as per above diagram
List<Edge> edges = Arrays.asList(
new Edge(0, 6), new Edge(1, 2), new Edge(1, 4),
new Edge(1, 6), new Edge(3, 0), new Edge(3, 4),
new Edge(5, 1), new Edge(7, 0), new Edge(7, 1)
);
// Set number of vertices in the graph
final int N = 8;
儿童英语教育机构// create a graph from edges
Graph graph = new Graph(edges, N);
// Perform Topological Sort
List<Integer> L = doTopologicalSort(graph, N);
if (L != null) {
System.out.print(L); // print topological order
} el {
System.out.println("Graph has at least one cycle. " +
"Topological sorting is not possible");
}
}
}
四、算法分析
时间复杂度: O(n + e),其中n为图中的结点数⽬,e为图中的边的数⽬          空间复杂度:O(n)
也可以⽤深度优先遍历DFS和 ⼴度优先遍历BFS 实现拓扑排序。

本文发布于:2023-07-28 01:00:36,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/78/1120908.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:排序   拓扑   节点
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图