Question: Need a simple exercise with a graph based on an adjacency list. provide two method implementations for the following class: import java . util .

Need a simple exercise with a graph based on an adjacency list. provide two method implementations for the following class:

import java . util . Map ; import java . util . Set ; import java . util . List ; public class Graph { public Graph () { adjacency = new java . util . HashMap (); } public void addEdge (T start , T end ) { } public boolean testPath ( List vertexList ) { } private Map > adjacency ; }

the following methods are needed: 1. void addEdge(T start, T end) This code adds the directed edge (start, end) to the graph. In the map adjacency, get or create the Set associated with start, and add end to this Set. 2. boolean testPath(List vertexList) This method should return true if each consecutive pair of vertices in the list exists in the graph, false otherwise. Throw an exception if vertexList doesnt have at least one edges worth of vertices in it!

Here is a SAMPLE TEST CLASS

Need a simple exercise with a graph based on an adjacency list.

Here is the Sample Output

provide two method implementations for the following class: import java . util

public static void main (String[] args) Graph g - new Graph); g.addEdge ( "A", "?"); 8. addEdge ("B" "C g.addEdge("A" , "B" System.out println("Test results"+ g.testPath (Arrays.asList ("A", "?", "C"))); System.out.println("Test results: "+ g.testPath(Arrays.asList ("B", .C", "A"))); g.addEdge ( "?", "?"); System.out.println ("Test results: "+ g.testPath (Arrays.asList("B", "C", "A"))); try g.testPath (Arrays.asList ("Z")); catch (Exception e) System.out.println("Graph threw an exception (I expected that!)") e.printStackTrace public static void main (String[] args) Graph g - new Graph); g.addEdge ( "A", "?"); 8. addEdge ("B" "C g.addEdge("A" , "B" System.out println("Test results"+ g.testPath (Arrays.asList ("A", "?", "C"))); System.out.println("Test results: "+ g.testPath(Arrays.asList ("B", .C", "A"))); g.addEdge ( "?", "?"); System.out.println ("Test results: "+ g.testPath (Arrays.asList("B", "C", "A"))); try g.testPath (Arrays.asList ("Z")); catch (Exception e) System.out.println("Graph threw an exception (I expected that!)") e.printStackTrace

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 Databases Questions!