Question: How do you implement public String depthFirstSearch(int startVertex){} and public String breadthFirstSearch(int startVertex){} ? import java.util.*; public class MyGraph { private int[][] graph; private int

How do you implement public String depthFirstSearch(int startVertex){} and public String breadthFirstSearch(int startVertex){} ? import java.util.*; public class MyGraph { private int[][] graph; private int numberOfVertices; /** * @param numberOfVertices number of vertices of the graph */ public MyGraph(int numberOfVertices){ this.numberOfVertices = numberOfVertices; graph = new int[numberOfVertices+1][numberOfVertices+1]; } /** * @param graph The matrix representation on the given graph. Assume column 0 and row 0 are not used */ public MyGraph(int [][] graph){ this.graph = graph; // change any 0 to infinity if the 0 is not on diagonal for(int i = 1; i < graph.length; i++){ for(int j = 1; j < graph.length; j++){ if(i == j) graph[i][j] = 0; else if(i != j && graph[i][j] == 0) graph[i][j] = Integer.MAX_VALUE; } } numberOfVertices = graph.length - 1; } /** * BREADTH-FIRST SEARCH * return a String that represent the vertices in order if the BFS algorithm is used to traversal the graph * starting from the given vertex. If the startVertex not exists, return an error message * @param startVertex The vertex where the traversal starts * @return A String that describes the vertices visited in order */ public String breadthFirstSearch(int startVertex){ //NEEDS TO BE IMPLEMENTED } /** * DEPTH-FIRST SEARCH * return a String that represents the vertices in order if the DFS algorithm is used to traversal the graph * starting from the given vertex. If the startVertex not exist, return an error message * @param startVertex The vertex where the traversal starts * @return An ArrayList of Integer that represents the vertices visited in order */ public String depthFirstSearch(int startVertex){ //NEEDS TO BE IMPLEMENTED } }

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!