BFS Distance

Explanation

This is an answer for a HackerRank question - Breadth First Search: Shortest Reach.

Consider an undirected graph where each edge is the same weight. Each of the nodes is labeled consecutively.

You will be given a number of queries. For each query, you will be given a list of edges describing an undirected graph. After you create a representation of the graph, you must determine and report the shortest distance to each of the other nodes from a given starting position using the breadth-first search algorithm (BFS). Distances are to be reported in node number order, ascending. If a node is unreachable, print -1 for that node. Each of the edges weighs 6 units of distance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import java.util.*;

public class BFSGraph {

    class Node {
        int value;
        int distance;
        Node(int v, int d) {
            value = v;
            distance = d;
        }
    }

    LinkedList<Integer>[] m_graph;

    int m_size;

    BFSGraph(int size) {
        m_size = size;
        m_graph = new LinkedList[size];

        for (int i = 0; i < m_size; i++) {
            m_graph[i] = new LinkedList<Integer>();
        }
    }

    void addEdge(int i, int j) {
        m_graph[i].add(j);
        m_graph[j].add(i);
    }

    int distance(int start, int end) {
         if (start == end) {
            return 0;
        }

        Queue<Node> myQueue = new LinkedList<Node>();
        myQueue.add(new Node(start, 0));
        Map<Integer, Boolean> visited = new HashMap<Integer, Boolean>();

        while (myQueue.size() > 0) {
            Node current = myQueue.poll();
            //current.distance++;

            for (int i = 0; i < m_graph[current.value].size(); i++) {
                int child = m_graph[current.value].get(i);

                if (child == end) {
                    current.distance++;
                    return current.distance;
                }

                if (! visited.containsKey(child)) {
                    visited.put(child, true);
                    myQueue.add(new Node(child, current.distance+1));
                }
            }
        }
        return -1;
    }

    public static void main (String args[]) {
        Scanner stdin = new Scanner(System.in);
        //int queries = stdin.next();
        String[] arr = stdin.nextLine().trim().split("\\s+");
        int queries = Integer.parseInt(arr[0]);


        for (int i = 0; i < queries; i++) {
            arr = stdin.nextLine().trim().split("\\s+");
            int size = Integer.parseInt(arr[0]);
            int edges = Integer.parseInt(arr[1]);

            BFSGraph bfsGraph = new BFSGraph(size + 1);

            for (int j = 0; j < edges; j++) {

                arr = stdin.nextLine().trim().split("\\s+");
                int x = Integer.parseInt(arr[0]);
                int y = Integer.parseInt(arr[1]);
                bfsGraph.addEdge(x, y);

            }

            arr = stdin.nextLine().trim().split("\\s+");
            int startNode = Integer.parseInt(arr[0]);

            for (int x = 1; x < size + 1; x++) {
                if (x != startNode) {
                    int value = bfsGraph.distance(startNode, x);
                    if (value != -1) {
                        System.out.printf("%d ", 6*value);
                    }
                    else {
                        System.out.printf("%d ", value);
                    }
                }

            }
            System.out.println();
        }

    }
}