package dijkstra;
import java.util.ArrayList;
import java.util.Hashtable;
import labyrinthe.VertexInterface;
public class Previous implements PreviousInterface {
Hashtable<VertexInterface , VertexInterface> peres = new Hashtable<VertexInterface , VertexInterface>();
// defini y comme le pere de x
public void setPrevious(VertexInterface x, VertexInterface y) {
peres.put(x, y);
}
public VertexInterface getPrevious(VertexInterface x) {
try {
return peres.get(x);
} catch(Exception e) {
return null; //il n'y a pas de chemin solution
}
}
public ArrayList<VertexInterface> getShortestPathsTo(VertexInterface x)
{
ArrayList<VertexInterface> path = new ArrayList<VertexInterface>();
VertexInterface y = x;
try {
while(this.getPrevious(y) != null) {
VertexInterface sommetPrecedent = this.getPrevious(y);
path.add(sommetPrecedent);
y = sommetPrecedent;
}
return path;
} catch (Exception e) {
return null; //il n'y a pas de chemin solution
}
}
}