Files
TpMazeJava/src/dijkstra/Previous.java
T
antograb d46b3c3b99 Fix List to ArrayList
Add toString method for Box
Add code to print the shortest path for this particular case.
2017-12-10 17:08:54 +01:00

41 lines
1.1 KiB
Java

package dijkstra;
import java.util.ArrayList;
import java.util.Hashtable;
public class Previous implements PreviousInterface {
private Hashtable<VertexInterface, VertexInterface> hashPrevious = new Hashtable<VertexInterface, VertexInterface>() ;
private ArrayList<VertexInterface> shortestPath = new ArrayList<VertexInterface>() ;
public Previous(Hashtable<VertexInterface, VertexInterface> hashPrevious, ArrayList<VertexInterface> shortestPath) {
this.hashPrevious = new Hashtable<VertexInterface, VertexInterface>(hashPrevious);
this.shortestPath = new ArrayList<VertexInterface>(shortestPath);
}
public VertexInterface getPrevious(VertexInterface vertex) {
return hashPrevious.get(vertex) ;
}
public void setPrevious(VertexInterface father, VertexInterface son) {
hashPrevious.put(son, father) ;
}
public ArrayList<VertexInterface> getShortestPathTo(VertexInterface vertex) {
VertexInterface father = getPrevious(vertex) ;
shortestPath.add(father) ;
if (getPrevious(father) == null) return shortestPath ;
else {
return getShortestPathTo(father) ;
}
}
}