简体   繁体   English

图中两个节点之间的深度优先搜索和宽度优先搜索

[英]Depth first search and Breadth first search between two nodes in graph

I have an undirected connected graph. 我有一个无向的连通图。 I have implemented it using an adjacency matrix which is a 2 dimensional array. 我已经使用一个二维数组的邻接矩阵实现了它。

As I understand, a DFS visits children nodes before siblings. 据我了解,DFS在兄弟姐妹之前访问子节点。 BFS visits siblings before children. BFS在孩子之前拜访兄弟姐妹。

I implemented these two like this: 我这样实现了这两个:

    public void DFT(int firstVertex) {
    connectedVertices = 0;
    int v;
    Stack<Integer> stack = new Stack<Integer>();
    for (int i = 0; i < numVertices; i++) {
        if(vertex[i] != null){
            vertex[i].setPushed(false);
        }
    }
    stack.push(firstVertex);
    connectedVertices++;
    vertex[firstVertex].setPushed(true);

    while(!stack.isEmpty()){
        v = stack.pop();
        vertex[v].visit();
        for (int i = 0; i < numVertices; i++) {
            if(adj[v][i] != 0 && !vertex[i].getPushed()){
                stack.push(i);
                connectedVertices++;
                vertex[i].setPushed(true);
            }   
        }
    }

}

public void BFT(int firstVertex) {
    connectedVertices = 0;
    int v;
    Queue<Integer> queue = new LinkedList<Integer>();
    for (int i = 0; i < numVertices; i++) {
        if(vertex[i] != null){
            vertex[i].setPushed(false);
        }
    }
    queue.add(firstVertex);
    connectedVertices++;
    vertex[firstVertex].setPushed(true);

    while(!queue.isEmpty()){
        v = queue.remove();
        vertex[v].visit();
        for (int i = 0; i < numVertices; i++) {
            if(adj[v][i] != 0 && !vertex[i].getPushed()){
                queue.add(i);
                connectedVertices++;
                vertex[i].setPushed(true);
            }   
        }
    }

}

As it is these methods take only one parameter, the start vertex. 因为这些方法仅采用一个参数,即起始顶点。 What if I am asked to give the DFS and BFS from one node to a different node? 如果要求我将DFS和BFS从一个节点提供给另一节点该怎么办? Here is a simple example of a connected undirected graph. 这是一个连接的无向图的简单示例。 在此处输入图片说明

If I am asked to perform a DFS from D to E, would it be D,C,A,E or D,E. 如果要求我执行从D到E的DFS,是D,C,A,E还是D,E。 I thought DFS and BFS must visit every node, in this case B cannot be visited. 我以为DFS和BFS必须访问每个节点,在这种情况下B无法访问。 I dont know how I should change my current methods to satisfy these requirements. 我不知道如何更改当前方法来满足这些要求。

I'm not sure it really matters if you visit C or E first. 如果您先访问C或E,我不确定这是否真的重要。 They are both children of D, right? 他们都是D的孩子,对吗? It's implementation specific behavior, DFS doesn't define which child you visit first. 这是特定于实现的行为,DFS并未定义您首先访问哪个孩子。

In DFS, if you pick child C first then you should visit A before you visit E. 在DFS中,如果您首先选择子级C,则应在访问E之前先访问A。

In BFS, you should have visited both E and C before you visit A or B. 在BFS中,您应该先访问E和C,然后再访问A或B。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM