|
-
Mar 21st, 2005, 03:36 PM
#1
Thread Starter
Stuck in the 80s
Problem converting Java to PHP
I've tried converting the following Java code to PHP, but each time I do, the code doesn't work. It executes without error, but doesn't produce the same results as it's Java counterpart. It's pretty basic code, nothing language specific in it. Just syntax differences.
Any know what might not be translating well? Here's the java code:
Code:
class DistPar {
public int distance;
public int parentVert;
public DistPar(int pv, int d) {
distance = d;
parentVert = pv;
}
}
class Vertex {
public char label;
public boolean isInTree;
public Vertex(char lab) {
label = lab;
isInTree = false;
}
}
class Graph {
private final int MAX_VERTS = 20;
private final int INFINITY = 1000000;
private Vertex vertexList[]; // list of vertices
private int adjMat[][]; // adjacency matrix
private int nVerts; // current number of vertices
private int nTree; // number of verts in tree
private DistPar sPath[]; // array for shortest-path data
private int currentVert; // current vertex
private int startToCurrent; // distance to currentVert
public Graph() {
vertexList = new Vertex[MAX_VERTS];
adjMat = new int[MAX_VERTS][MAX_VERTS];
nVerts = 0;
nTree = 0;
for(int j=0; j<MAX_VERTS; j++)
for(int k=0; k<MAX_VERTS; k++)
adjMat[j][k] = INFINITY;
sPath = new DistPar[MAX_VERTS];
}
public void addVertex(char lab) {
vertexList[nVerts++] = new Vertex(lab);
}
public void addEdge(int start, int end, int weight) {
adjMat[start][end] = weight;
}
public void path() {
int startTree = 0;
vertexList[startTree].isInTree = true;
nTree = 1;
for(int j=0; j<nVerts; j++) {
int tempDist = adjMat[startTree][j];
sPath[j] = new DistPar(startTree, tempDist);
}
while(nTree < nVerts) {
int indexMin = getMin(); // get minimum from sPath
int minDist = sPath[indexMin].distance;
if(minDist == INFINITY) {
System.out.println("There are unreachable vertices");
break;
} else {
currentVert = indexMin;
startToCurrent = sPath[indexMin].distance;
}
vertexList[currentVert].isInTree = true;
nTree++;
adjust_sPath();
}
displayPaths();
nTree = 0;
for(int j=0; j<nVerts; j++)
vertexList[j].isInTree = false;
}
public int getMin() {
int minDist = INFINITY;
int indexMin = 0;
for(int j=1; j<nVerts; j++) {
if( !vertexList[j].isInTree && sPath[j].distance < minDist ) {
minDist = sPath[j].distance;
indexMin = j;
}
}
return indexMin;
}
public void adjust_sPath() {
int column = 1; // skip starting vertex
while(column < nVerts) {
if( vertexList[column].isInTree ) {
column++;
continue;
}
int currentToFringe = adjMat[currentVert][column];
int startToFringe = startToCurrent + currentToFringe;
int sPathDist = sPath[column].distance;
if(startToFringe < sPathDist) {
sPath[column].parentVert = currentVert;
sPath[column].distance = startToFringe;
}
column++;
} // end while(column < nVerts)
}
public void displayPaths() {
for(int j=0; j<nVerts; j++) {
System.out.print(vertexList[j].label + "="); // B=
if(sPath[j].distance == INFINITY)
System.out.print("inf"); // inf
else
System.out.print(sPath[j].distance); // 50
char parent = vertexList[ sPath[j].parentVert ].label;
System.out.println("(" + parent + ") "); // (A)
}
System.out.println("");
}
}
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.addVertex('F'); // 5
theGraph.addVertex('G'); // 6
theGraph.addVertex('H'); // 7
theGraph.addVertex('I'); // 8
theGraph.addEdge(0, 2, 1);
theGraph.addEdge(0, 5, 1);
theGraph.addEdge(0, 7, 1);
theGraph.addEdge(1, 4, 1);
theGraph.addEdge(1, 5, 1);
theGraph.addEdge(1, 6, 1);
theGraph.addEdge(2, 3, 1);
theGraph.addEdge(2, 7, 1);
theGraph.addEdge(3, 4, 1);
theGraph.addEdge(3, 8, 1);
theGraph.addEdge(5, 6, 1);
theGraph.addEdge(5, 7, 1);
theGraph.addEdge(7, 8, 1);
System.out.println("Shortest paths");
theGraph.path(); // shortest paths
System.out.println();
} // end main()
} // end class PathApp
////////////////////////////////////////////////////////////////
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|