// Shortest path in an unweighted graphclassGraph{// Constructorconstructor(v,s,d){this.V=v;this.source=s;this.destination=d;this.adj=newArray(v);for (leti=0;i<v;i++)this.adj[i]=[];// Mark all the vertices as not visited(By default falsethis.visited=newArray(v);for (leti=0;i<this.V;i++)this.visited[i]=false;this.parent=newArray(v);for (leti=0;i<this.V;i++)this.parent[i]=false;}// Function to add an edge into the graphaddEdge(v,w){this.adj[v].push(w);this.adj[w].push(v);}// prints BFS traversal from a given source sBFS(){letqueue=[];// Mark the current node as visited and enqueue itthis.visited[this.source]=true;this.parent[this.source]=-1;queue.push(this.source);while (queue.length>0){// Dequeue a vertex from queue and print itlets=queue[0];queue.shift();// Get all adjacent vertices of the dequeued// vertex s. If a adjacent has not been visited,// then mark it visited and enqueue itthis.adj[s].forEach((adjacent,i)=>{if (!this.visited[adjacent]){this.visited[adjacent]=true;this.parent[adjacent]=s;queue.push(adjacent);}});}}getDistance(){letanswere=[this.destination];letcurrent=this.destination;while (current!=this.source){current=this.parent[current];answere.push(current);}returnanswere;}}// Create a graph given in the above diagramgraphInstance=newGraph(8,0,7);graphInstance.addEdge(0,1);graphInstance.addEdge(0,3);graphInstance.addEdge(1,2);graphInstance.addEdge(3,4);graphInstance.addEdge(3,7);graphInstance.addEdge(4,5);graphInstance.addEdge(4,6);graphInstance.addEdge(4,7);graphInstance.addEdge(5,6);graphInstance.addEdge(6,7);console.log("Following is Breadth First Traversal "+"(starting from vertex 2)");graphInstance.BFS();console.log(graphInstance.getDistance()?.reverse());graphInstance=newGraph(8,2,6);graphInstance.addEdge(0,1);graphInstance.addEdge(0,3);graphInstance.addEdge(1,2);graphInstance.addEdge(3,4);graphInstance.addEdge(3,7);graphInstance.addEdge(4,5);graphInstance.addEdge(4,6);graphInstance.addEdge(4,7);graphInstance.addEdge(5,6);graphInstance.addEdge(6,7);console.log("Following is Breadth First Traversal "+"(starting from vertex 2)");graphInstance.BFS();console.log(graphInstance.getDistance()?.reverse());