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.

Need help with Java program! Implement Warshall's Algorithm to the code provided

--------------------------------------------------------------------------

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(); } }

0 1inf inf inf inf 0 infinf 1 1 inf inf 0inf inf inf inf inf inf 0 inf inf nf inf01inf 1 inf Inf inf inf 1 inf 0 Inf inflnflnfIfinf inf inf inf 0 1 inf 1

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!