需要帮助使用广度优先搜索(Java)获得邻接矩阵(图)的第n级吗

需要帮助使用广度优先搜索(Java)获得邻接矩阵(图)的第n级吗,java,algorithm,breadth-first-search,directed-graph,adjacency-matrix,Java,Algorithm,Breadth First Search,Directed Graph,Adjacency Matrix,public int-bfs(int-maxDepth){ int src=2; int dest=2; int i; int深度=0; int countpath=0; int元素; queue.add(src); 而(!queue.isEmpty()&&depth 0) { 加入(i); 如果(i==dest) countpath++; } i++; } } queue.clear(); 返回路径; } 你好!!给定一个源和一个目标,我需要找到一条路径。我的BFS算法在遍历图

public int-bfs(int-maxDepth){
int src=2;
int dest=2;
int i;
int深度=0;
int countpath=0;
int元素;
queue.add(src);
而(!queue.isEmpty()&&depth 0)
{
加入(i);
如果(i==dest)
countpath++;
}       
i++;
}
}
queue.clear();
返回路径;
}
你好!!给定一个源和一个目标,我需要找到一条路径。我的BFS算法在遍历图形时运行良好。我的问题是让它在我想停的时候停下来。我找出了我增加深度的地方,所以我看起来不像个十足的白痴。我希望有人能帮忙。基本上,我想知道如何跟踪当前深度。谢谢大家!

例如:

找到从C到C的路径的#,最多有3个站点。答案有两条:

C->D->C(2站)

C->E->B->C(3站)

示例2:找到从A到C的路径的#,最多有3个站点。答案是三条路

A->B->C(2站)

A->D->C(2站)


A->E->B->C->(3次停止)

因为执行bfs时每个节点都应该与其深度相关联,所以可以创建一个类,在其中存储节点和深度(如果要打印路径,还包括父节点):

Q:基本上,我想知道如何跟踪当前深度

一种解决方案是创建一个带有额外字段depth的包装器类,正如@svs在回答中所解释的那样。但是,有一个巧妙的技巧可以避免创建包装器类,而且非常简单:

queue.add(src);
while(!queue.isEmpty())
{
     int size = queue.size();
     for(int i=0; i<size; i++)
     {
     .. //do processing for the current depth
     }
}
queue.add(src);
而(!queue.isEmpty())
{
int size=queue.size();

对于(int i=0;iHi,看起来你不能仅仅用一个变量
depth
来衡量你的深度。你考虑过用另一个队列来跟踪深度吗?我们称之为
depth\u queue
depth\u queue
中的每个元素都是
队列中一个元素的深度。在我看来,有两个元素检测何时终止的策略:(1)
depth\u队列
的头超过
maxDepth
(不确定是否是这种情况,您需要考虑一下)(2)这更可靠:检查整个
depth\u队列
如果每个元素都超过
maxDepth
,则可以终止
package stackoverflow;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;

public class BFSTest {
    public static class BFSNode {
        public int node, depth;
        public BFSNode parent;

        public BFSNode(int node) {
            this.node = node;
            this.depth = 0;
            this.parent = null;
        }

        public BFSNode(int node, BFSNode parent) {
            this.node = node;
            this.depth = parent.depth+1;
            this.parent = parent;
        }
    }

    ArrayDeque<BFSNode> queue;
    int[][] arr;

    public BFSTest() {
        queue = new ArrayDeque<BFSNode>();
        arr = new int[5][5];
        arr[0][1] = arr[0][3] = arr[0][4] = 1;
        arr[1][2] = 1;
        arr[2][3] = arr[2][4] = 1;
        arr[3][2] = arr[3][4] = 1;
        arr[4][1] = 1;
    }

    public int bfs(int src, int dest, int maxDepth){
        int i;
        int countPaths = 0;
        BFSNode element;

        queue.add(new BFSNode(src));

        while(!queue.isEmpty())
        {   
            element = queue.poll();
            if (element.depth > maxDepth)
                break;
            i = 0;

            while(i < 5) 
            {   
                if(arr[element.node][i] > 0)
                {
                    if(i == dest && element.depth +1 <= maxDepth) {
                        BFSNode tmp = element;
                        List<Integer> path = new ArrayList<Integer>();
                        path.add(dest);
                        while (tmp != null) {
                            path.add(tmp.node);
                            tmp = tmp.parent;
                        }
                        for (int j = path.size() - 1; j >= 0; j--) {
                            System.out.print(path.get(j) + " ");
                        }
                        System.out.println();
                        countPaths++;
                    }

                    queue.add(new BFSNode(i, element));

                }       
                i++;
            }
        }

        queue.clear();
        return countPaths;
    }

    public static void main(String[] args) {
        BFSTest test = new BFSTest();
        System.out.println(test.bfs(2, 2, 3));
        System.out.println(test.bfs(0, 2, 3));
    }

}
//src = 2, dest=2
2 3 2   //path 1
2 4 1 2 //path 2
2       //total
//src=0. dest=2
0 1 2   //path 1
0 3 2   //path 2
0 4 1 2 //path 3
3       //total
queue.add(src);
while(!queue.isEmpty())
{
     int size = queue.size();
     for(int i=0; i<size; i++)
     {
     .. //do processing for the current depth
     }
}