Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用拓扑排序查找母顶点?_Java_Algorithm_Graph_Depth First Search - Fatal编程技术网

Java 使用拓扑排序查找母顶点?

Java 使用拓扑排序查找母顶点?,java,algorithm,graph,depth-first-search,Java,Algorithm,Graph,Depth First Search,我正在浏览拓扑排序的代码,然后我意识到我们可以使用拓扑排序找到图的母顶点 package com.bharat; import java.util.*; public class GraphsTS { private int V; private LinkedList adj[]; public GraphsTS(int v) { V = v; this.adj = new LinkedList[v]; for

我正在浏览拓扑排序的代码,然后我意识到我们可以使用拓扑排序找到图的母顶点


    package com.bharat;
import java.util.*;
public class GraphsTS {
    private int V;
    private LinkedList adj[];

    public GraphsTS(int v) {
        V = v;
        this.adj = new LinkedList[v];
        for (int i =0;i<v;i++){
            adj[i]= new LinkedList();
        }
    }
    public void addEdge(int v,int w){
        adj[v].add(w);
    }
    /*
    Now, what is topological sorting?
    As it says one task always comes after the second task as if the second one is dependent on
    the first
    Now how do we choose the first most task
    To choose such task we need to visualize that if that node does not have any incoming
    edge
    for ex:

Connections in the graph 
5---->0
4---->0
5---->2
2---->3
3---->1
4---->1

                    5                            4
                 /        \                /        \
                /             \         /            \
               /                 \   /                \
              2                    0                   1
                \                                   /
                   \                              /
                      \                         /
                         \                    /
                           \                /
                              \           /
                                 \      /
                                   \  /
                                     3
              In this we can choose 4 or 5 as the parent vertex as there is no
              edge coming to these nodes and all the edges coming out
              Now here we can not directly sketch the DFS as before printing
              Zero we need to print his parents which are 4 and 5;

      Now what should be the algorithm for topological sorting?
      Let' see/
      We are gonna create a stack and then Do dfs here but not gonna pop
      and print the stack elements immediately.
      First we will take all the adjacent vertices in the stack and then after
      we are gonna pop out and print the elements
      So the code implementation will be
                   */
    //Utility function of topological sorting
    public void topologicalSortUtil(int v,boolean[] visited,Stack<Integer> stack){
        visited[v]=true;
        Iterator<Integer> i = adj[v].listIterator();
        while (i.hasNext()){
            int n = i.next();
            if (!visited[n]){
                topologicalSortUtil(n,visited,stack);
            }
        }
        stack.push(v);
    }
    //Main function of topological Sorting
    public void topology(){
        Stack<Integer> stack = new Stack<>();
        boolean[] visited = new boolean[V];
        for (int i =0;i<V;i++){
            visited[i]=false;
        }
        for (int i =0;i<V;i++){
            if (!visited[i]){
                topologicalSortUtil(i,visited,stack);
            }
        }
        while (!stack.isEmpty()){
            System.out.print(stack.pop()+" ");
        }
    }
}
/*
Here was the whole theory and code for the
 topological sorting of a graph
 */


包com.bharat;
导入java.util.*;
公共类图形{
私人INTV;
私有链接列表adj[];
公共图形(int v){
V=V;
this.adj=新链接列表[v];
对于(int i=0;i0
4---->0
5---->2
2---->3
3---->1
4---->1
5                            4
/        \                /        \
/             \         /            \
/                 \   /                \
2                    0                   1
\                                   /
\                              /
\                         /
\                    /
\                /
\           /
\      /
\  /
3.
在这种情况下,我们可以选择4或5作为父顶点,因为没有
到这些节点的边和所有出来的边
现在我们不能像打印之前那样直接绘制DFS
我们需要打印他的父母,他们分别是4岁和5岁;
现在,拓扑排序的算法应该是什么?
让我看看/
我们将创建一个堆栈,然后在这里执行dfs,但不会弹出
并立即打印堆栈元素。
首先,我们将获取堆栈中的所有相邻顶点,然后
我们将弹出并打印元素
因此,代码实现将是
*/
//拓扑排序的效用函数
public void topologicalSortUtil(int v,boolean[]已访问,堆栈){
访问[v]=正确;
迭代器i=adj[v].listIterator();
while(i.hasNext()){
int n=i.next();
如果(!已访问[n]){
topologicalSortUtil(n,已访问,堆栈);
}
}
栈推(v);
}
//拓扑排序的主要功能
公共void拓扑(){
堆栈=新堆栈();
布尔[]访问=新布尔[V];

对于(int i=0;i是,但如果图的母顶点存在,则拓扑排序的第一个元素基于Kosaraju的强连通组件算法。在强连通组件图中,母顶点始终是组件图中源组件的顶点。该思想基于以下事实

如果存在母顶点(或多个顶点),则其中一个母顶点是DFS中最后完成的顶点。(或母顶点在DFS遍历中具有最长完成时间)

如果对其DFS的递归调用已结束,即顶点的所有子体都已访问,则称该顶点在DFS中已完成

上述想法是如何运作的? 假设最后一个完成的顶点是v。基本上,我们需要证明,如果u不是另一个母顶点,那么从另一个顶点u到v不存在边(或者不存在非母顶点u,使得u-→v是一条边)。有两种可能

在v之前对u进行递归DFS调用-→如果v存在,则v必须在u之前完成,因为v可以通过u到达,并且顶点在其所有子体之后完成。 递归DFS调用是在u之前对v进行的。在这种情况下,如果边u-→v存在,那么要么v必须在u之前完成(这与我们关于v在末尾完成的假设相矛盾),要么u应该可以从v到达(这意味着u是另一个母顶点)

算法: 对给定的图进行DFS遍历。遍历时跟踪最后完成的顶点“v”。此步骤需要O(v+E)时间。
如果存在母顶点(或vetices),则v必须是一个(或其中一个)。通过从v执行DFS/BFS来检查v是否为母顶点。此步骤还需要O(v+E)时间。

你说的“母顶点”是什么意思顶点?它是拓扑排序顶点的起点吗?如果是,你只需要找到没有传入边的节点。你不需要做整个排序。没有母顶点意味着我们可以从图的顶点到达图的每个节点。