@@ -0,0 +1,3259 @@
+package com.github.conradoqg.studydsa.hackerrank.challenges.ctci;%0A%0Aimport java.util.LinkedList;%0Aimport java.util.Scanner;%0A%0Apublic class BFSShortestReach %7B%0A    public static class Node %7B%0A        int id;%0A        int distance = -1;%0A        LinkedList%3CNode%3E connectedNodes = new LinkedList%3CNode%3E();%0A%0A        public Node(int id) %7B%0A            this.id = id;%0A        %7D%0A%0A        public void addEdge(Node otherNode) %7B%0A            connectedNodes.add(otherNode);%0A        %7D%0A    %7D%0A%0A    public static class Graph %7B%0A        Node%5B%5D nodes = null;%0A%0A        public Graph(int size) %7B%0A            nodes = new Node%5Bsize%5D;%0A%0A            for (int i = 0; i %3C nodes.length; i++) %7B%0A                nodes%5Bi%5D = new Node(i);%0A            %7D%0A        %7D%0A%0A        public void addEdge(int first, int second) %7B%0A            Node firstNode = nodes%5Bfirst%5D;%0A            Node secondNode = nodes%5Bsecond%5D;%0A%0A            firstNode.addEdge(secondNode);%0A            secondNode.addEdge(firstNode);%0A        %7D%0A%0A        public int%5B%5D shortestReach(int startId) %7B // 0 indexed%0A            Node baseNode = nodes%5BstartId%5D;%0A            int%5B%5D distances = new int%5Bnodes.length%5D;%0A%0A            for (int nextId = 0; nextId %3C nodes.length; nextId++) %7B%0A                if (nextId == startId)%0A                    continue;%0A%0A                int distance = bfsFindDistance(baseNode, nodes%5BnextId%5D);%0A                distances%5BnextId%5D = (distance == -1) ? -1 : (6 * distance);%0A            %7D%0A%0A            return distances;%0A        %7D%0A%0A        public int bfsFindDistance(Node source, Node target) %7B%0A            LinkedList%3CNode%3E nextToVisit = new LinkedList%3CNode%3E();            %0A            nextToVisit.add(source);%0A            source.distance = 0;%0A%0A            while (!nextToVisit.isEmpty()) %7B%0A                Node nextNode = nextToVisit.remove();               %0A%0A                for (Node childNode : nextNode.connectedNodes) %7B%0A                    if (childNode.distance == -1) %7B                    %0A                        childNode.distance = nextNode.distance + 1;%0A                        nextToVisit.add(childNode);%0A                    %7D                    %0A                %7D%0A            %7D%0A            %0A            return target.distance;%0A        %7D%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Scanner scanner = new Scanner(System.in);%0A%0A        int queries = scanner.nextInt();%0A%0A        for (int t = 0; t %3C queries; t++) %7B%0A%0A            // Create a graph of size n where each edge weight is 6:%0A            Graph graph = new Graph(scanner.nextInt());%0A            int m = scanner.nextInt();%0A%0A            // read and set edges%0A            for (int i = 0; i %3C m; i++) %7B%0A                int u = scanner.nextInt() - 1;%0A                int v = scanner.nextInt() - 1;%0A%0A                // add each edge to the graph%0A                graph.addEdge(u, v);%0A            %7D%0A%0A            // Find shortest reach from node s%0A            int startId = scanner.nextInt() - 1;%0A            int%5B%5D distances = graph.shortestReach(startId);%0A%0A            for (int i = 0; i %3C distances.length; i++) %7B%0A                if (i != startId) %7B%0A                    System.out.print(distances%5Bi%5D);%0A                    System.out.print(%22 %22);%0A                %7D%0A            %7D%0A            System.out.println();%0A        %7D%0A%0A        scanner.close();%0A    %7D%0A%7D%0A

Add bfs shortest reach CTCI challenge