Question
Task below: --------------------------------------------------------------------------------------------------------- test.java: package dsf_matrix_forStudents; /* * class: Test */ public class Test { public static void main(String[] args) { Graph theGraph = new
Task below:
---------------------------------------------------------------------------------------------------------
test.java:
package dsf_matrix_forStudents; /* * class: Test */
public class Test { public static void main(String[] args) { Graph theGraph = new Graph(); theGraph.addVertex('A'); // 0 (start for dfs) theGraph.addVertex('B'); // 1 theGraph.addVertex('C'); // 2 theGraph.addVertex('D'); // 3 theGraph.addVertex('E'); // 4 theGraph.addVertex('F'); // 4
theGraph.addEdge(0, 1); // AB theGraph.addEdge(0, 2); // AC theGraph.addEdge(1, 3); // BD theGraph.addEdge(2, 4); // CE theGraph.addEdge(2, 3); // CD theGraph.addEdge(0, 3); // AD theGraph.addEdge(3, 4); // DE theGraph.addEdge(3, 5); // DE
System.out.print("Stack based dfs visits:----------------------- "); theGraph.dfs(); System.out.println(); System.out.print("recursive dfs visits:----------------------- "); theGraph.recusive_dfs(0); } // end main() } // end class DFSApp
---------------------------------------------------------------------------------------------------------
stack.java:
package dsf_matrix_forStudents; /* * class: Stack */
class Stack { private final int SIZE = 50; private int[] st; private int top; //constructor public Stack() { st = new int[SIZE]; // make array top = -1; } // put item on stack public void push(int j) { st[++top] = j; } //take item off stack public int pop() { return st[top--]; } //peek at top of stack public int peek() { return st[top]; } //true if nothing on stack public boolean isEmpty() { return (top == -1); }
} // end class Stack
---------------------------------------------------------------------------------------------------------
graph.java:
package dsf_matrix_forStudents; /* * class: Graph */
class Graph { private final int MAX_VERTS = 20; private Vertex vertexList[]; // list of vertices private int adjMatrix[][]; // adjacency matrix private int nVerts; // current number of vertices private Stack theStack; //------------------------------------------------------------ public Graph() // constructor { vertexList = new Vertex[MAX_VERTS]; // adjacency matrix adjMatrix = new int[MAX_VERTS][MAX_VERTS]; nVerts = 0; for(int y=0; y /* * depth-first search: Recursive */ public void recusive_dfs(int start) { //TO-DO } // end depthFirstSearch } // end class Graph //////////////////////////////////////////////////////////////// class Vertex { public char data; // data public boolean isVisited; public Vertex(char d) // constructor { data = d; isVisited = false; } } // end class Vertex
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