Home> Java> javaTutorial> body text

Introduction to task scheduling topology graph of directed graph

零下一度
Release: 2017-06-25 10:59:34
Original
2683 people have browsed it

1. Data type of directed graph

Use Bag to represent the directed graph, where edge v->w is represented as vertex v The corresponding adjacency linked list contains a w vertex. Unlike the undirected graph, each edge here only appears once. The data structure type of the directed graph is as follows:

public class Digraph {private final int V;private int E;private Bag[] adj;public Digraph(int V) {this.V=V;this.E=0; adj=(Bag[])new Bag[V];for(int v=0;v(); } }public int V() {return V; }public int E() {return E; }//添加一条边v->w,由于是有向图只要添加一条边就可以了public void addEdge(int v,int w) { adj[v].add(w); E++; }public Iterable adj(int v) {return adj[v]; }//返回当前图的一个反向的图public Digraph reverse() { Digraph R=new Digraph(V);for(int v=0;v
         
Copy after login

2. Reachability in directed graph

Connectivity in undirected graphSimilarly, depth-first search can be used to solve the

single-point reachability problem in directed graphs: that is: given a directed graph and A starting point s, answer the question of whether there is a directed path from s to a given vertex v.

Multi-point reachability problem: Given a Given a directed graph and a set of vertices, answer whether there is a directed path from any vertex in the set to a given vertex v?

public class DirectedDFS {private boolean[] marked;//从G中找出所有s可达的点public DirectedDFS(Digraph G,int s) { marked=new boolean[G.V()]; dfs(G,s); }//G中找出一系列点可达的点public DirectedDFS(Digraph G,Iterable sources) { marked=new boolean[G.V()];for(int s:sources) {if(!marked[s]) dfs(G,s); } }//深度优先搜素判断.private void dfs(Digraph G, int v) { marked[v]=true;for(int w:G.adj(v)) {if(!marked[w]) dfs(G,w); } }//v是可达的吗public boolean marked(int v) {return marked[v]; } }
Copy after login

An important application of the multi-point reachability problem is in typical memory management systems, including many Java implementations. In a directed graph, a vertex represents an object, and an edge represents a reference from one object to another.

This model well represents the memory usage of running java programs. There are certain objects that can be directly accessed at any time during program execution, and all objects that cannot be accessed through these objects should be recycled to

free up memory. It periodically runs a directed graph reachability algorithm similar to DirectedDFS to mark all accessible objects.

3. Pathfinding for directed graphs

is similar to undirected graphs. Common problems in directed graphs:

#Single-point directed path. Given a directed graph and a starting point, answer "Is there a directed path from s to the given destination vertex v? If so, find this path"

Single point shortest directed path.Given a directed graph and a starting point, answer "Is there a directed path from s to the given destination vertex v? If so, find the shortest one (containing the least number of edges)"

4. Scheduling problem—topological sorting

4.1 Finding directed cycles

If a directed cycle exists in a problem with priority constraints, then the problem must be unsolvable. Therefore, directed ring detection is required.

The following code can be used to detect whether a given directed graph contains a directed cycle. If so, return all vertices on the cycle in the direction of the path.

When executing dfs, what is searched is the directed path from the starting point to v. The onStack array marks all the vertices on the stack of the recursive call, and the edgeTo array is also added. After finding the When moving to the ring, return all the vertices in the ring.

/** * 有向图G是否含有有向环 * 获取有向环中的所有顶点 * @author Administrator * */public class DirectedCycle {private boolean[] marked; private int[] edgeTo;private Stack cycle; //有向环中的所有顶点private boolean[] onStack; //递归调用的栈上的所有顶点public DirectedCycle(Digraph G) { edgeTo=new int[G.V()]; onStack=new boolean[G.V()]; marked=new boolean[G.V()];for(int v=0;v();for(int x=v;x!=w;x=edgeTo[x]) cycle.push(x); cycle.push(w); cycle.push(v); } }//dfs方法结束,对于该点的递归调用结束.该点指向的所有点已经遍历完毕onStack[v]=false; }private boolean hasCycle() {return cycle!=null; }public Iterable cycle() {return cycle; } }
Copy after login

4.2 Topological sorting

Topological sorting: Given a directed graph, sort all the vertices so that all directed edges point from the elements in the front to the elements in the back. If there is a directed cycle If so, then topological sorting cannot be completed.

To implement topological sorting of directed graphs, the task can be completed using the standard depth-first search order. There will be three arrangements of vertices here:

1. Pre-order: Add the vertices to the queue before the recursive call

2. Post-order: Add the vertices to the queue after the recursive call

3. Reverse sequence: push the vertices onto the stack after the recursive call.

See the code below for specific operations:

//有向图中基于深度优先搜索的拓补排序public class DepthFirstOrder {private boolean[] marked;private Queue pre; //所有顶点的前序排列private Queue post; //所有顶点的后序排列private Stack reversePost;//所有顶点的逆后序排列public DepthFirstOrder(Digraph G) { pre=new Queue<>(); post=new Queue<>(); reversePost=new Stack<>(); marked=new boolean[G.V()]; for(int v=0;v pre() {return pre; }public Iterable post() {return post; }public Iterable reversePost() {return reversePost; } }
Copy after login

遍历的顺序取决于这个数据结构的性质以及是在递归调用之前还是之后进行保存。

前序:在递归调用之前将顶点加入队列。

后序:在递归调用之后将顶点加入队列。

逆后序:在递归调用之后将顶点压入栈。

前序就时dfs()的调用顺序;后序就是顶点遍历完成的顺序;逆后序就是顶点遍历完成顺序的逆。

拓补排序的实现依赖于上面的API,实际上拓补排序即为所有顶点的逆后序排列

拓补排序的代码如下:

public class Topological {private Iterable order; //顶点的拓补排序public Topological(Digraph G) { DirectedCycle cyclefinder=new DirectedCycle(G);if(!cyclefinder.hasCycle()) {//只有无环才能进行拓补排序DepthFirstOrder dfs=new DepthFirstOrder(G); order=dfs.reversePost(); } }public Iterable order() {return order; }public boolean isDAG() {return order!=null; } }
Copy after login

5.有向图的强连通性

定义:如果两个顶点v和w是互相可达的,则称它们为强连通的.也就是说既存在一条从v到w的有向路径也存在一条从w到v的有向路径.

如果一幅有向图中的任意两个顶点都是强连通的,则称这副有向图也是强连通的.任意顶点和自己都是强连通的.

下面的代码采用如下步骤来计算强连通分量以及两个点是否是强连通的:

  1.在给定的有向图中,使用DepthFirsetOrder来计算它的反向图GR的逆后序排列

  2.按照第一步计算得到的顺序采用深度优先搜索来访问所有未被标记的点

  3.在构造函数中,所有在同一个递归dfs()调用中被访问到的顶点都是在同一个强连通分量中.

  下面的代码实现遵循了上面的思路:

/** * 该算法实现的关键: * 使用深度优先搜索查找给定有向图的反向图GR.根据由此得到的所有顶点的逆后序 * 再次用深度优先搜索处理有向图G.其构造函数的每一次递归调用所标记的顶点都在 * 同一个强连通分量中. * 解决问题: * 判断两个点是否是强连通的 * 判断总共有多少个连通分量 * @author Administrator * */public class KosarajuSCC {private boolean[] marked;//已经访问过的顶点private int[] id; //强连通分量的标识符private int count; //强联通分量的数量public KosarajuSCC(Digraph G) { marked=new boolean[G.V()]; id=new int[G.V()]; DepthFirstOrder order=new DepthFirstOrder(G.reverse());for(int s:order.reversePost()) {if(!marked[s]) { dfs(G,s); count++; } } }private void dfs(Digraph G, int v) { marked[v]=true; id[v]=count;for(int w:G.adj(v)) {if(!marked[w]) { dfs(G,w); } } }public boolean stronglyConnected(int v,int w) {return id[v]==id[w]; }public int id(int v) {return id[v]; }public int count() {return count; } }
Copy after login

The above is the detailed content of Introduction to task scheduling topology graph of directed graph. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!