Question: IN JAVA (Find paths) Define a new class named UnweightedGraphWithGetPath that extends UnweightedGraph with a new method for finding a path between two vertices with
IN JAVA
(Find paths) Define a new class named UnweightedGraphWithGetPath that extends UnweightedGraph with a new method for finding a path between two vertices with the following header: public List getPath(int u, int v); The method returns a List that contains all the vertices in a path from u to v in this order. Using the BFS approach, you can obtain the shortest path from u to v. If there isnt a path from u to v, the method returns null. Write a test program that creates a graph for Figure 28.1. The program prompts the user to enter two cities and displays their paths.
Sample Run Enter a starting city: Seattle Enter an ending city: Miami The path is Seattle Denver Kansas City Atlanta Miami
Use https://liangpy.pearsoncmg.com/test/Exercise28_05.txt to test your code.
Class Name: Exercise28_05
If you get a logical or runtime error, please refer https://liveexample.pearsoncmg.com/faq.html.
I am getting those errors when i submit my answer to myprogramminglab :
Exercise28_05.java:386: error: class, interface, or enum expected import java.io.*; ^
Exercise28_05.java:387: error: class, interface, or enum expected import java.util.*;
My answer :
import java.io.*; import java.util.*; class gph { private int x; private LinkedList adj[];
gph(int x) { V = v; adj = new LinkedList[x]; for(int i=0; i adj[i] = new LinkedList(); }
void addEge(int a,int b) { adj[v].add(x); adj[w].add(a); }
Boolean isCyclicUtil(int v, Boolean visited[], int child) { visited[a] = true; Integer s;
Iterator it = adj[a].iterator(); while (it.hasNext()) { i = it.next();
if (!visited[i]) { if (isCyclicUtil(i, visited, v)) return true; } else if (i != parent) return true; } return false; } Boolean isCyclic() { Boolean visited[] = new Boolean[V]; for (int i = 0; i < a; i++) visited[i] = false;
for (int u = 0; u < a; u++) if (!visited[u]) if (isCyclicUtil(u, visited, -1)) return true; return false; } public static void main(String args[]) { Graph g1 = new gph(5); g1.addEge(1, 0); g1.addEge(0, 2); g1.addEge(2, 1); g1.addEge(0, 3); g1.addEge(3, 4); if (g1.isCyclic()) System.out.println("Gph"); else System.out.println("Gph"); Graph g1 = new Gph(3); g1.addEge(0, 1); g1.addEge(1, 2); if (g1.isCyclic()) System.out.println("Gph"); else System.out.println("Gph"); } }
in java
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
