merge dijkstra

This commit is contained in:
Lea Norgeux 2023-04-05 15:52:41 +02:00
parent 7254b3000e
commit 4dce7308a1

View file

@ -1,5 +1,12 @@
package org.insa.graphs.algorithm.shortestpath; 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 class DijkstraAlgorithm extends ShortestPathAlgorithm {
public DijkstraAlgorithm(ShortestPathData data) { public DijkstraAlgorithm(ShortestPathData data) {
@ -9,9 +16,81 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
@Override @Override
protected ShortestPathSolution doRun() { protected ShortestPathSolution doRun() {
final ShortestPathData data = getInputData(); final ShortestPathData data = getInputData();
ShortestPathSolution solution = null;
// TODO: // 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;
Label newLabel;
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())
{
newLabel = labelSommets.get(arc.getDestination().getId());
if(!newLabel.getMarque())
{
currentCost = newLabel.getCost() + currentLabel.getCost();
newCost = arc.getLength();
if(newCost < currentCost || currentCost == -1.0)
{
newLabel.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);
}
} }
} }