【问题标题】:Need help getting to nth level of an adjacency matrix (graph) using a breadth-first search (Java)需要帮助使用广度优先搜索 (Java) 到达邻接矩阵(图)的第 n 级
【发布时间】:2015-09-22 04:38:06
【问题描述】:

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 站)

A -> D -> C(2 站)

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

【问题讨论】:

  • 您好,看来您不能只使用一个变量depth 来衡量您的深度。您是否考虑过使用另一个队列来跟踪深度?我们称之为depth_queuedepth_queue 中的每个元素都是queue 中元素的深度。在我看来,检测何时终止有两种策略:(1)depth_queue 的头部大于maxDepth(不确定是不是这样,你需要考虑一下)(2)这更确定:遍历整个depth_queue,如果每个元素都超过maxDepth,那么你可以终止

标签: java algorithm breadth-first-search directed-graph adjacency-matrix


【解决方案1】:

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

一种解决方案是创建一个带有附加字段的包装类,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
     }
}

for 循环中,对该级别的节点执行任何您想要执行的操作,包括将新元素放入队列(即深度等于 current_depth + 1 的节点)。在for 循环中仅迭代queue.size() 次将保证您只处理当前级别的节点,并且while 循环将保证将处理所有级别(或者只是其中一些,如果您将条件扩展为停止循环)。

【讨论】:

    【解决方案2】:

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

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多