Djikstra à tester
This commit is contained in:
parent
854d6439f7
commit
a0bcb97341
1 changed files with 80 additions and 2 deletions
|
@ -1,5 +1,12 @@
|
|||
package org.insa.graphs.algorithm.shortestpath;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import org.insa.graphs.model.Node;
|
||||
import org.insa.graphs.model.Arc;
|
||||
import org.insa.graphs.model.Path;
|
||||
import org.insa.graphs.algorithm.AbstractSolution;
|
||||
import org.insa.graphs.algorithm.utils.BinaryHeap;
|
||||
|
||||
public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||
|
||||
public DijkstraAlgorithm(ShortestPathData data) {
|
||||
|
@ -9,9 +16,80 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
|||
@Override
|
||||
protected ShortestPathSolution doRun() {
|
||||
final ShortestPathData data = getInputData();
|
||||
ShortestPathSolution solution = null;
|
||||
// TODO:
|
||||
return solution;
|
||||
int tailleGraphe = data.getGraph().size();
|
||||
int nbMarques = 0;
|
||||
int index = 0;
|
||||
|
||||
ArrayList<Label> labelSommets = new ArrayList<Label>();
|
||||
Label currentLabel;
|
||||
|
||||
BinaryHeap<Node> leTas = new BinaryHeap<Node>();
|
||||
|
||||
for(int i = 0; i< tailleGraphe; i++ )
|
||||
{
|
||||
currentLabel = new Label(data.getGraph().get(i));
|
||||
labelSommets.set(i, currentLabel);
|
||||
}
|
||||
|
||||
currentLabel = labelSommets.get(data.getOrigin().getId());
|
||||
currentLabel.setNouveauChemin(null, 0);
|
||||
labelSommets.set(data.getOrigin().getId(), currentLabel);
|
||||
leTas.insert(data.getOrigin());
|
||||
|
||||
Node currentNode;
|
||||
double currentCost;
|
||||
double newCost;
|
||||
|
||||
|
||||
while (nbMarques != tailleGraphe)
|
||||
{
|
||||
currentNode = leTas.deleteMin();
|
||||
index = currentNode.getId();
|
||||
|
||||
currentLabel = labelSommets.get(currentNode.getId());
|
||||
currentLabel.setMarqueTrue();
|
||||
labelSommets.set(index, currentLabel);
|
||||
|
||||
nbMarques++;
|
||||
|
||||
for(Arc arc : currentNode.getSuccessors())
|
||||
{
|
||||
currentLabel = labelSommets.get(arc.getDestination().getId());
|
||||
if(!currentLabel.getMarque())
|
||||
{
|
||||
currentCost = currentLabel.getCost();
|
||||
newCost = arc.getLength();
|
||||
if(newCost < currentCost || currentCost == -1.0)
|
||||
{
|
||||
currentLabel.setNouveauChemin(arc, newCost);
|
||||
leTas.insert(arc.getDestination());
|
||||
labelSommets.set(arc.getDestination().getId(), currentLabel);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Node finalNode = data.getDestination();
|
||||
index = 0;
|
||||
ArrayList<Arc> arcListe = new ArrayList<Arc>() ;
|
||||
|
||||
while(!finalNode.equals(data.getOrigin()) || index < tailleGraphe)
|
||||
{
|
||||
arcListe.add(labelSommets.get(finalNode.getId()).pere);
|
||||
finalNode = arcListe.get(arcListe.size()-1).getOrigin();
|
||||
index++;
|
||||
}
|
||||
if(index != tailleGraphe)
|
||||
{
|
||||
return new ShortestPathSolution(data, AbstractSolution.Status.FEASIBLE, new Path(data.getGraph(),arcListe ));
|
||||
}
|
||||
else
|
||||
{
|
||||
return new ShortestPathSolution(data, AbstractSolution.Status.INFEASIBLE);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue