Need help getting the nth level of the adjacency matrix (graph) using width search (Java)

enter image description here enter image description here

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;
    }

Hello!! Given the source and destination, I need to find a way. My BFS algorithm works fine as the graph moves. My problem is getting him to stop when I want to. I pulled out where I increased the depth, so I don't look like a complete idiot. I hope someone can help. Essentially, I want to know how I can track the current depth. Thanks!

Example:

Find # tracks from C to C with a maximum of 3 stops. The answer is two ways:

C → D → C (2 stops)

C → E → B → C (3 stops)

Example 2: Find # tracks from A to C with a maximum of 3 stops. The answer is three ways.

A → B → C (2 stops)

A → D → C (2 stops)

A → E → B → C → (3 stops)

+4
2

node , bfs, , node ( , ):

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
0

Q: , , .

- , , @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). queue.size() for , , while , ( , ).

0

Source: https://habr.com/ru/post/1608419/


All Articles