Question: Been stuck for a while. Please try and follow the pseudocode as best as possible Implement the BFS and DFS traversal methods for directed graphs

Been stuck for a while. Please try and follow the pseudocode as best as possible
Implement the BFS and DFS traversal methods for directed graphs in Java.
For BFS, implement it to find the distance (shortest path length) from
a source vertex s to every vertex in the graph.
You will need to complete the method bodies of:
void bfs(Vertex s)
void dfs(Vertex s)
void dfsVisit(Vertex current)
The BFS class is coded as a subclass of the MyGraph class and is also
using the MyLinkedQueue class, which is a modified version of the
LinkedQueue class from Module 3. These two classes are provided in the
"MyGraph.java" and "MyLinkedQueue.java" files. Please place them under
the same package/folder.
Your output may look as follows:
--------------------------------
DFS traversal starting from vertex 3: 3406578912
BFS traversal starting from vertex 3: 3460597812
Shortest path lengths from 3 to every vertex:
Vertices: 0123456789
Distances: 2340121332
========== Note ============
1. DO NOT MODIFY OR DELETE ANY GIVEN CODE OR COMMENTS!!!
2. You ONLY need to write code under each comment "YOUR CODE GOES HERE".
3. Modify the file name to "GraphSearch.java" to compile and run.
4. Make sure that you place the "MyGraph.java" and "MyLinkedQueue.java"
files under the same package/folder as your current file to compile
and run the code.
============================
import java.util.ArrayList;
import java.util.LinkedList;
//----- Vertex class -------
class Vertex {
L label;
int color; //0= GRAY, 1= RED, 2= BLACK
int distance;
public Vertex(L label){
this.label = label;
this.color =0; // GRAY
this.distance = Integer.MAX_VALUE;
}
}//---- End of Vertex class -----
public class GraphSearch extends MyGraph>{
// Breadth-First Search to find shortest path lengths from source vertex s to every vertex
public void bfs(Vertex s){
if (!vertices.contains(s))
throw new IllegalArgumentException("Source vertex "+ s.label +" not found!");
MyLinkedQueue> queue = new MyLinkedQueue>();
// YOUR CODE GOES HERE --Part 1/3--
}
// Depth-First Search starting from vertex s
public void dfs(Vertex s){
if (!vertices.contains(s))
throw new IllegalArgumentException("Source vertex "+ s.label +" not found!");
// YOUR CODE GOES HERE --Part 2/3--
}
private void dfsVisit(Vertex current){
// YOUR CODE GOES HERE --Part 3/3--
}
public ArrayList> getVertices(){
return vertices;
}
//------ Driver -------
public static void main(String[] args){
// Create a graph
GraphSearch graph = new GraphSearch>();
// Add ten vertices with integer labels
Vertex[] vertices = new Vertex[10];
for (int i =0; i 10; i++){
vertices[i]= new Vertex>(i);
graph.addVertex(vertices[i]);
}
// Define 18 edges
int[][] edges ={
{1,2},{1,3},{1,5},{2,4},{2,5},{2,7},
{3,4},{3,6},{4,0},{5,7},{5,8},{6,5},
{6,9},{7,0},{8,9},{8,0},{9,0},{9,1}
};
// Add the edges
for (int[] edge : edges){
int u = edge[0];
int v = edge[1];
graph.addEdge(vertices[u], vertices[v]);
}
int s =3;
// Perform DFS from vertex 3
// Print out the order of vertices being first visited by DFS
System.out.print("DFS traversal starting from vertex "+ vertices[s].label +": ");
graph.dfs(vertices[s]);
System.out.println();
// Perform BFS from vertex 3
// Print out the order of vertices being first visited by BFS
System.out.print("BFS traversal starting from vertex "+ vertices[s].label +": ");
graph.bfs(vertices[s]);
System.out.println();
// Print the distance/shortest path lengths from vertex 3 to other vertices
System.out.println("Shortest path lengths from "+ s +" to every vertex:");
System.out.print("Vertices: ");
for (Vertex vertex : graph.getVertices())
System.out.print(vertex.label +"");
System.out.println();
System.out.print("Distances: ");
for (Vertex vertex : graph.getVertices())
System.out.print(vertex.distance +"");
}
}
Been stuck for a while. Please try and follow the

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