首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >需要帮助使用宽度优先搜索(Java)到邻接矩阵(图)的第n级。

需要帮助使用宽度优先搜索(Java)到邻接矩阵(图)的第n级。
EN

Stack Overflow用户
提问于 2015-09-22 04:38:06
回答 2查看 684关注 0票数 6

代码语言:javascript
复制
public int bfs(int maxDepth){
        int src = 2;
        int dest = 2;
        int i;
        int depth = 0;
        int countPaths = 0;
        int element;

        queue.add(src);

        while(!queue.isEmpty() && depth <= maxDepth)
        {   
            element = queue.remove();
            i = 0;

            while(i < 5) 
            {   
                if(arr[element][i] > 0)
                {
                    queue.add(i);

                    if(i == dest)
                        countPaths++;
                }       
                i++;
            }
        }

        queue.clear();
        return countPaths;
    }

你好!!给定一个源和一个目的地,我需要找到一条路径。我的BFS算法在遍历图时工作得很好。我的问题是当我想让它停下来的时候。我拿出了我在增加深度的地方,这样我看起来就不像个十足的白痴了。我希望有人能帮忙。本质上,我想知道如何跟踪当前的深度。谢谢!

示例:

找到从C到C的路径#,最多3站。答案是两条途径:

C -> D -> C (2站)

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

示例2:找到从A到C的路径#,最大数目为3次。答案是三条路。

A -> B -> C (2站)

-> D -> C (2站)

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-09-22 07:08:54

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

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

代码语言:javascript
复制
queue.add(src);
while(!queue.isEmpty())
{
     int size = queue.size();
     for(int i=0; i<size; i++)
     {
     .. //do processing for the current depth
     }
}

for循环中,对该级别的节点做任何您想做的事情,包括在队列中放置新元素(即深度等于current_depth +1的节点)。在queue.size()循环中只迭代for次数将保证您只处理当前级别的节点,而while循环将保证所有级别都将被处理(如果扩展停止循环的条件,则只处理其中的一些级别)。

票数 0
EN

Stack Overflow用户

发布于 2015-09-22 05:31:20

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

代码语言:javascript
复制
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));
    }

}

你会得到

代码语言:javascript
复制
//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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32708490

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档