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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started