diff --git a/Graphs Java/src/BreadthFirstSearch/MessageRoutes.java b/Graphs Java/src/BreadthFirstSearch/MessageRoutes.java new file mode 100644 index 0000000..5ae4d19 --- /dev/null +++ b/Graphs Java/src/BreadthFirstSearch/MessageRoutes.java @@ -0,0 +1,111 @@ +import java.util.Scanner; + + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.Queue; + + class Graph { + public HashMap> map; + public ArrayList path; + public int[] parent; + int n; + public Graph(int n){ + this.n=n; + parent=new int[n+1]; + Arrays.fill(parent,-1); + path=new ArrayList<>(); + map=new HashMap<>(); + for(int i=1;i<=n;i++) + { + map.put(i,new HashSet()); + } + } + + + public void printPath(){ + int i=n; + while(parent[i]!=-1){ + path.add(0,i); + i=parent[i]; + } + System.out.print(1+" "); + for(i=0;i q=new LinkedList<>(); + q.offer(src); + visited[src]=true; + + int moves=0; + + while(!q.isEmpty()) + { + int size=q.size(); + while(size-->0){ + + int val=q.poll(); + + if(val==des){ + return moves; + } + + for(int neighbour : map.get(val)){ + if(!visited[neighbour]){ + visited[neighbour]=true; + parent[neighbour]=val; + q.offer(neighbour); + } + } + + } + moves++; + } + return -1; + } +} + +public class MessageRoutes { + + String url="https://cses.fi/problemset/task/1667/"; + public static void main(String[] args) { + try (Scanner sc = new Scanner(System.in)) { + int numberOfComputers=sc.nextInt(); + int numberOfConnections=sc.nextInt(); + //1 to numberOfComputers + int a;int b; + + Graph g=new Graph(numberOfComputers); + + for(int i=0;i