Package dijkstra

Source Code of dijkstra.Previous

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
    }
   
  }
 

}
TOP

Related Classes of dijkstra.Previous

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.