Question
How do you implement public String depthFirstSearch(int startVertex){} ? Method declarations can NOT be changed. No other public interfaces can be added. import java.util.*; public
How do you implement public String depthFirstSearch(int startVertex){} ? Method declarations can NOT be changed. No other public interfaces can be added. 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; } /** * 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