I currently have a Bellman Ford algorithm set up and I am trying to print the path to that node. My current algorithm is like this:
path = new int[totaledges];
path[source] = source;
distance[source] = 0;
String st = "";
for (int i = 0; i < totaledges; i++)
for (int j = 0; j < edges.size(); j++) {
int newDistance = distance[edges.get(j).getSource()] + edges.get(j).getWeight();
if (newDistance < distance[edges.get(j).getDestination()]){
distance[edges.get(j).getDestination()] = newDistance;
path[edges.get(j).getDestination()] = edges.get(j).getSource();
}
}
And this is how I am printing it out. It is recursive but how would I set it up so that it is iterative? I am currently getting a stack overflow error.
static void printedges(int source, int i, int[] paths)
{
// print the array that is get from step 2
if(source!=i){
printedges(source, paths[i], paths);
}
if(i == currentEdge){
System.out.print(i);
} else{
System.out.print(i+",");
}
}