Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The following is an implementation of an undirected graph without any weights on

ID: 3834854 • Letter: T

Question

The following is an implementation of an undirected graph without any weights on the edges. Assume that the graph has already been loaded into the adjacency linked lists. Fill in the required method to find the shortest path from vertex numbered x to vertex numbered y. You may use, without implementation, any of the standard methods of the Queue and Stack classes. For anything else, you will need to implement your own code (including helper methods, if needed). You may add fields to the Graph class as necessary. public class Neighbor {public int vnum; public Neighbor next; ...} public class Graph {Neighbor [] adjusts;//adjacency linked lists for all vertices//add other fields as necessary//returns the length of the shortest path from x to y (assume y different from x)//or -1 if y is not reachable from x public int shortest Path (int x, int y) {//FILL IN THIS METHOD

Explanation / Answer

public int shortestPath(int x, int y) { // Assumption there are total n nodes in a graph numbered 0 to n uniquely. boolean vis[] = new boolean[n]; int distance[] = new int[n]; // mark all distances as infinity for(int i=0;i