Question: Attached below is Lab 1 2 import java.io . * ; import java.util. * ; public class Distance { private static List bfsShortestPath ( Map

Attached below is Lab 12
"import java.io.*; import java.util.*; public class Distance { private static List bfsShortestPath(Map> graph, int source, int destination){ Queue queue = new LinkedList<>(); Map predecessors = new HashMap<>(); queue.add(source); predecessors.put(source, null); while (!queue.isEmpty()){ int current = queue.poll(); if (current == destination){ List path = new LinkedList<>(); for (Integer at = destination; at != null; at = predecessors.get(at)){ path.add(0, at); } return path; } for (int neighbor : graph.getOrDefault(current, Collections.emptyList())){ if (!predecessors.containsKey(neighbor)){ queue.add(neighbor); predecessors.put(neighbor, current); }}} return null; } public static void main(String[] args){// Step 1: Parse the graph Map> graph = new HashMap<>(); try { Scanner scanner = new Scanner(new File("web.txt")); while (scanner.hasNextInt()){ int src = scanner.nextInt(); int dst = scanner.nextInt(); graph.computeIfAbsent(src, k -> new ArrayList<>()).add(dst); } scanner.close(); } catch (FileNotFoundException e){ System.err.println("File not found."); return; }// Step 2 & 3: Handle user input and compute shortest paths Scanner input = new Scanner(System.in); while (true){ System.out.print("Enter source and destination: "); int source = input.nextInt(); int destination = input.nextInt(); // Terminate if negative integers are entered if (source <0|| destination <0){ break; }// Find the shortest path using BFS List path = bfsShortestPath(graph, source, destination); if (path == null){ System.out.println(destination +" is unreachable from "+ source); } else { System.out.println("distance "+(path.size()-1)); System.out.print("path "); for (int node : path){ System.out.print(node +""); } System.out.println(); }} input.close(); }}"
Answer this question please
"How many vertices are distance 12 from vertex 98765 in the directed graph described in the file web.txt used in Lab 12?"

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!