简体   繁体   English

如何在来自特定顶点的有向图中执行 BFS 或 DFS,某些顶点的出度为 0?

[英]How to do a BFS or DFS in directed graph from particular vertex with some vertex having outdegree 0?

What is the order of DFS or BFS traversals for graphs when there may be vertices which have outdegrees of 0 if we want to do DFS/BFS from a given vertex {0..n-1} .如果我们想从给定的顶点{0..n-1}执行 DFS/BFS,当可能存在出度为 0 的顶点时,图的 DFS 或 BFS 遍历的顺序是什么。

在此处输入图片说明

Below is one BFS implementation for a graph下面是一个图的 BFS 实现

import java.util.LinkedList;
import java.util.ListIterator;
import java.util.Queue;

public class BreadthFirstSearch {

    static class Graph {
        int V;
        LinkedList[] vertexList;

        Graph(int v) {
            this.V = v;
            vertexList = new LinkedList[v];

            for (int i = 0; i < v; i++) {
                vertexList[i] = new LinkedList<>();
            }
        }
    }

    private void addEdge(Graph graph, int src, int dest) {
        graph.vertexList[src].add(dest); // Directed Graph Only
    }

    private void bfs(Graph graph, int vertex) {

        if (vertex > graph.V) {
            throw new IllegalArgumentException("Value not defined");
        }
        boolean visited[] = new boolean[graph.V];

        Queue<Integer> queue = new LinkedList<>();

        queue.add(vertex);
        while (!queue.isEmpty()) {
            int a = queue.poll();
            if (!visited[a]) {
                System.out.print(a + " ");
                visited[a] = true;

            }
            for (Object o : graph.vertexList[a]) {
                int n = (int) o;
                if (!visited[n]) {
                    queue.add(n);
                }
            }
        }
    }

    private void traverseGraphList(Graph G) {
        int i = 0;
        for (LinkedList list : G.vertexList) {
            ListIterator itr = list.listIterator();
            System.out.print("src: " + i++ + " ");
            while (itr.hasNext()) {
                System.out.print(" -> " + itr.next());
            }
            System.out.println();
        }
    }

    public static void main(String args[]) {
        BreadthFirstSearch g = new BreadthFirstSearch();
        int n = 8;
        Graph graph = new Graph(n);
        g.addEdge(graph, 0, 1);
        g.addEdge(graph, 0, 2);
        g.addEdge(graph, 1, 2);
        g.addEdge(graph, 2, 0);
        g.addEdge(graph, 2, 3);
        g.addEdge(graph, 3, 3);
        g.addEdge(graph, 3, 4);
        g.addEdge(graph, 5, 4);
        g.addEdge(graph, 5, 6);
        g.addEdge(graph, 7, 6);
        System.out.println("BFS starting from node 2");
        g.bfs(graph, 2);

        System.out.println();
        g.traverseGraphList(graph);
        //2 0 3 1 4

    }
}

The output is 2 0 3 1 4输出为2 0 3 1 4

If you want to traverse all the nodes of the graph, then you should be aware that since there are nodes that are not reachable from your selected root node, you won't be able to traverse them while using a single BFS/DFS.如果您想遍历图中的所有节点,那么您应该注意,由于存在无法从您选择的根节点访问的节点,您将无法在使用单个 BFS/DFS 时遍历它们。

The order on which the nodes are traversed is dependent on the implementation of your BFS/DFS.遍历节点的顺序取决于您的 BFS/DFS 的实现。 In your case, it depends on the order you insert each node to the adjacency list.在您的情况下,这取决于您将每个节点插入邻接列表的顺序。

If you would like to traverse the whole graph, then you should maintain which nodes are visited and then run BFS/DFS for each non-visited node (eg by using a loop).如果你想遍历整个图,那么你应该维护哪些节点被访问过,然后为每个未访问的节点运行 BFS/DFS(例如,通过使用循环)。 Particularly, the order of appearance of the traversed nodes again depend on the ordering of the items on which you will actually loop.特别是,遍历节点的出现顺序再次取决于您将实际循环的项目的顺序。

So from what I can see, the implementation of BFS is fine.所以就我所见,BFS的实现是没问题的。 IMO, its output is what I would expect for a standard implementation of the algorithm when the input to the initial call has 2 set for the vertex parameter. IMO,当初始调用的输入为vertex参数设置2时,它的输出是我对算法标准实现的期望。

That being said, not all directed graphs are completely traversable from any given node.话虽如此,并非所有有向图都可以从任何给定节点完全遍历。 Based on the directions of the arrows, when I start traversing at node 2 , I can really only get to nodes 0 , 1 , 3 , and 4 .根据箭头的方向,当我从节点2开始遍历时,我实际上只能到达节点0134 Nodes 5 , 6 , and 7 aren't reachable from node 2 because 5 points to 4 , but 4 doesn't point to 5 .节点567无法从节点2到达,因为5指向4 ,但4不指向5

If you want to truly traverse the entire graph you will need to amend your BFS algorithm such that if at the end of the current implementation, the length of the visited array is less than the length of the vertexList array, then you will need to find the next element in vertexList that isn't present in visited until the lengths of the two arrays match.如果你想真正遍历整个图,你需要修改你的 BFS 算法,如果在当前实现的末尾, visited数组的长度小于vertexList数组的长度,那么你需要找到在两个数组的长度匹配之前, vertexList中的下一个元素不存在于visited中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Java查找图的中心(顶点,该顶点与其他每个顶点相连,但边指向图的中心) - how to Find the center of graph (vertex, that is connected with every other vertex, but edges are directed to the center of graph) with java 如何在图中找到特定顶点的度数? - How to find the degree of a particular vertex in graph? 如何在 PHP 有向非循环图(二维数组)上执行 DFS 和 BFS 遍历 - How to perform DFS and BFS traversal on a PHP directed non-cyclic graph (2D array) 给定图中无向图BFS的“邻接矩阵”表示并输出顶点 - Given an “Adjacency Matrix” representation of a an undirected graph BFS on the graph and output the vertex 如何遍历图的顶点,而不是每个顶点都有单独的add.vertex? - How can I loop through a graph's vertices instead of having a separate add.vertex for each vertex? 荣:如何根据顶点的某些属性进行图聚类? - JUNG : How can we do Graph Clustering based on some properties of vertex? 从orienttb顶点检索特定属性 - retrieve particular properties from orienttb vertex java中的BFS和DFS并制作图形 - BFS and DFS in java and making the graph 如何找到入射到特定顶点的边列表 - How to find the list of edges incident to a particular vertex 这是DFS,BFS的Graph的正确实现吗 - Is this a proper implementation of a Graph for DFS, BFS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM