Question
Need help with Java program! Implement Warshall's Algorithm to the code provided to find the transitive closure for a graph. Use the graph below to
Need help with Java program!
Implement Warshall's Algorithm to the code provided to find the transitive closure for a graph. Use the graph below to test the code.
--------------------------------------------------------------------------
class StackX { private final int SIZE = 20; private int[] st; private int top;
public StackX() { st = new int[SIZE]; top = -1; }
public void push(int j) { st[++top] = j; }
public int pop() { return st[top--]; }
public int peek() { return st[top]; }
public boolean isEmpty() { return (top == -1); }
}
class Vertex { public char label; public boolean wasVisited;
public Vertex(char lab) { label = lab; wasVisited = false; }
}
class Graph { private final int MAX_VERTS = 20; private Vertex vertexList[]; private int adjMat[][]; private int nVerts; private StackX theStack;
public Graph() { vertexList = new Vertex[MAX_VERTS];
adjMat = new int[MAX_VERTS][MAX_VERTS]; nVerts = 0; for(int y=0; y public void addVertex(char lab) { vertexList[nVerts++] = new Vertex(lab); } public void addEdge(int start, int end) { adjMat[start][end] = 1; adjMat[end][start] = 1; } public void displayVertex(int v) { System.out.print(vertexList[v].label); } public void dfs() { vertexList[0].wasVisited = true; displayVertex(0); theStack.push(0); while( !theStack.isEmpty() ) { // get an unvisited vertex adjacent to stack top int v = getAdjUnvisitedVertex( theStack.peek() ); if(v == -1) theStack.pop(); else { vertexList[v].wasVisited = true; displayVertex(v); theStack.push(v); } } for(int j=0; j public int getAdjUnvisitedVertex(int v) { for(int j=0; j } class DFSApp { public static void main(String[] args) { Graph theGraph = new Graph(); theGraph.addVertex('A'); theGraph.addVertex('B'); theGraph.addVertex('C'); theGraph.addVertex('D'); theGraph.addVertex('E'); theGraph.addEdge(0, 1); theGraph.addEdge(1, 2); theGraph.addEdge(0, 3); theGraph.addEdge(3, 4); System.out.print("Visits: "); theGraph.dfs(); System.out.println(); } }
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