Write Code in Java Create a class which constructs a graph and performs a few gr
ID: 3683304 • Letter: W
Question
Write Code in Java
Create a class which constructs a graph and performs a few graph operations. Test out your code with the driver.
Download the class called Graph from here. It already has some instance variables and constructors. It is up to you to finish it by adding the following methods.
addVertex: this method which returns nothing and takes in a string parameter corresponding to the name of the new vertex. It will only add a new vertex if that name is not already in the list of vertices.
vertexIsContained: this method returns a true or false if there exists a vertex in the graph that matches the name passed in via the parameter.
addEdge: this method returns nothing and takes in two string parameters and a weight. The two string parameters correspond to the names of vertices and the weight is the weight of the newly created edge. The newly created edge is then placed in the neighbor’s edge list inside the first vertex. If either the first or second vertex doesn’t exist the method should not continue.
getVertex: this method returns an instance of a vertex if the name (passed in via a parameter) is found in the vertex list.
printDFS: This prints out the vertices name’s in depth first order. You can find the algorithm the presentation provided.
printBFS: This prints out the vertices name’s in breadth first order. Like above you can find the algorithm in the presentation.
printLazyDFS: In the class there is an instance variable called maxDist which corresponds to the maximum distance that is allowed to travel. Write another DFS but make sure it doesn’t traverse edges that are greater than the max distance.
Graph Class
Tester Driver Class
Example Print Out:
Adding verticies
Adding edges
Printing DFS
v1
v2
v4
v7
v6
v8
v5
v3
Printing BFS
v1
v2
v4
v6
v5
v7
v8
v3
Printing Lazy DFS
v1
v2
v4
v7
v6
Explanation / Answer
DFS
Now, you can do anything you want with the results, so it is possible to write proper unit tests.
Note:-You can add the vertices as A,B,C... OR V1,V2,V3...
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.