Two simple questions and my brain's not working. I'm using Floyd's Algorithm and trying to reconstruct the path taken from vertex U to vertex V. Here's my code to reconstruct the path.
public List<Integer> findCheapestPath(int u, int v)
{
if (u >= adjMatrix.length || v >= adjMatrix.length || u < 0 || v < 0)
{
throw new IllegalArgumentException("Error--Illegal Arugment Exception: One of the verticies are not valid.");
}
int intermediate;
intermediate = path[u][v];
if (intermediate == -1)
{
pathList.add(u);
return pathList;
} else
{
findCheapestPath(u, intermediate);
findCheapestPath(intermediate, v);
}
return pathList;
}
For one example, let u = 0 and v = 3, and let the shortest path from 0 to 3 be 0,1,2,3. However I have two problems. My pathList is an instance variable of the class and:
Any help?
Don't use a class variable, and make a wrapper function.
In more detail, change your function to this signature:
private List<Integer> findCheapestPathInner(int u, int v, pathList)
(obviously also changing the recursive call inside it to fit the new signature), and create another function:
public List<Integer> findCheapestPath(int u, int v) {
List<Integer> pathList = new ArrayList<Integer>();
pathList.add(u);
return findCheapestPathInner(u, v, pathList);
}