Question: Please modify this program so it displays the output i have posted at the bottom. thank you Please make sure that the output is the
Please modify this program so it displays the output i have posted at the bottom. thank you
Please make sure that the output is the exact same. Please post the entire program and the whole output that MUST be the same as the one i posted. Thanks
//import java.util.ArrayList;
//import java.util.List;
// path.java
// demonstrates shortest path with weighted, directed graphs
// to run this program: C>java PathApp
////////////////////////////////////////////////////////////////
class DistPar // distance and parent
{ // items stored in sPath array
public int distance; // distance from start to this vertex
public int parentVert; // current parent of this vertex
// -------------------------------------------------------------
public DistPar(int pv, int d) // constructor
{
distance = d;
parentVert = pv;
}
// -------------------------------------------------------------
} // end class DistPar
// /////////////////////////////////////////////////////////////
class Vertex {
public char label; // label (e.g. 'A')
public boolean isInTree;
public List edges; // list of vertices
// -------------------------------------------------------------
public Vertex(char lab) // constructor
{
label = lab;
isInTree = false;
edges = new ArrayList();
}
public void addEdge(Edge edge) {
this.edges.add(edge);
}
// -------------------------------------------------------------
} // end class Vertex
// //////////////////////////////////////////////////////////////
class Edge {
public int weight;
public int end;
public Edge(int weight, int end) {
this.weight = weight;
this.end = end;
}
}
// //////////////////////////////////////////////////////////////
class Graph {
private final int MAX_VERTS = 20;
private final int INFINITY = 1000000;
private Vertex[] vertexs;
private int nVerts;
private int nTree;
private DistPar sPath[]; // array for shortest-path data
private int currentVert; // current vertex
private int startToCurrent; // distance to currentVert
public Graph() {
this.vertexs = new Vertex[MAX_VERTS];
this.sPath = new DistPar[MAX_VERTS]; // shortest paths
}
public void addEdge(int start, int end, int weight) {
Edge edge = new Edge(weight, end);
vertexs[start].addEdge(edge);
}
// -------------------------------------------------------------
public void addVertex(char lab) {
vertexs[nVerts++] = new Vertex(lab);
}
// -------------------------------------------------------------
public void path() // find all shortest paths
{
for (int k = 0; k < nVerts; k++) {
int startTree = k; // start at vertex 0
vertexs[startTree].isInTree = true;
nTree = 1; // put it in tree
// transfer row of distances from adjMat to sPath
List edgesList = vertexs[startTree].edges;
for (int i = 0; i < MAX_VERTS; i++) {
sPath[i] = new DistPar(startTree, INFINITY);
}
for (int i = 0; i < edgesList.size(); i++) {
Edge tempEdge = edgesList.get(i);
sPath[tempEdge.end] = new DistPar(startTree, tempEdge.weight);
}
// until all vertices are in the tree
while (nTree < nVerts) {
int indexMin = getMin(); // get minimum from sPath
int minDist = sPath[indexMin].distance;
if (minDist == INFINITY) // if all infinite
{ // or in tree,
System.out.println("There are unreachable vertices");
break; // sPath is complete
} else { // reset currentVert
currentVert = indexMin; // to closest vert
startToCurrent = sPath[indexMin].distance;
// minimum distance from startTree is
// to currentVert, and is startToCurrent
}
// put current vertex in tree
vertexs[currentVert].isInTree = true;
nTree++;
adjust_sPath(); // update sPath[] array
} // end while(nTree
if (nTree == nVerts) {
System.out.println("There are all reachable vertices");
}
displayPaths(); // display sPath[] contents
nTree = 0; // clear tree
for (int j = 0; j < nVerts; j++)
vertexs[j].isInTree = false;
}
} // end path()
// -------------------------------------------------------------
public int getMin() // get entry from sPath
{ // with minimum distance
int minDist = INFINITY; // assume minimum
int indexMin = 0;
for (int j = 1; j < nVerts; j++) // for each vertex,
{ // if it's in tree and
if (!vertexs[j].isInTree && // smaller than old one
sPath[j].distance < minDist) {
minDist = sPath[j].distance;
indexMin = j; // update minimum
}
} // end for
return indexMin; // return index of minimum
} // end getMin()
// -------------------------------------------------------------
public void adjust_sPath() {
// adjust values in shortest-path array sPath
List list = vertexs[currentVert].edges;
for (int i = 0; i < list.size(); i++) // go across columns
{
// if this column's vertex already in tree, skip it
int v = list.get(i).end;
if (vertexs[v].isInTree) {
continue;
}
// calculate distance for one sPath entry
// get edge from currentVert to fringe
Edge e = list.get(i);
int fringe = e.end;
int currentToFringe = e.weight;
// add distance from start
int startToFringe = startToCurrent + currentToFringe;
// get distance of current sPath entry
int sPathDist = sPath[fringe].distance;
// compare distance from start with sPath entry
if (startToFringe < sPathDist) // if shorter,
{ // update sPath
sPath[fringe].parentVert = currentVert;
sPath[fringe].distance = startToFringe;
}
} // end while(column < nVerts)
} // end adjust_sPath()
// -------------------------------------------------------------
public void displayPaths() {
for (int j = 0; j < nVerts; j++) // display contents of sPath[]
{
System.out.print(vertexs[j].label + "="); // B=
if (sPath[j].distance == INFINITY)
System.out.print("inf"); // inf
else
System.out.print(sPath[j].distance); // 50
char parent = vertexs[sPath[j].parentVert].label;
System.out.print("(" + parent + ") "); // (A)
}
System.out.println("");
}
// -------------------------------------------------------------
} // end class Graph
// //////////////////////////////////////////////////////////////
class PathApp {
public static void main(String[] args) {
Graph theGraph = new Graph();
theGraph.addVertex('A'); // 0 (start)
theGraph.addVertex('B'); // 1
theGraph.addVertex('C'); // 2
theGraph.addVertex('D'); // 3
theGraph.addVertex('E'); // 4
theGraph.addEdge(0, 1, 50); // AB 50
theGraph.addEdge(0, 3, 80); // AD 80
theGraph.addEdge(1, 2, 60); // BC 60
theGraph.addEdge(1, 3, 90); // BD 90
theGraph.addEdge(2, 4, 40); // CE 40
theGraph.addEdge(3, 2, 20); // DC 20
theGraph.addEdge(3, 4, 70); // DE 70
theGraph.addEdge(4, 1, 50); // EB 50
System.out.println("Shortest paths");
theGraph.path(); // shortest paths
System.out.println();
} // end main()
} // end class PathApp
Needed Output Please:
Output MUST look like:
All-Points Shortest-Path Table
A B C D E
A: --- 50 100 80 140
B: --- --- 60 90 100
C: --- 90 --- 180 40
D: --- 110 20 --- 60
E: --- 50 110 140 ---
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
