Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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.

image text in transcribed

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

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design And Implementation

Authors: Edward Sciore

2nd Edition

3030338355, 978-3030338350

More Books

Students also viewed these Databases questions

Question

In a first-party case the standard is ___________ position.

Answered: 1 week ago