This commit is contained in:
Lea Norgeux 2023-04-05 18:16:16 +02:00
commit 968e5af387
2 changed files with 60 additions and 24 deletions

View file

@ -1,6 +1,8 @@
package org.insa.graphs.algorithm.shortestpath;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import org.insa.graphs.model.Node;
import org.insa.graphs.model.Arc;
import org.insa.graphs.model.Path;
@ -18,64 +20,80 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
final ShortestPathData data = getInputData();
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>();
BinaryHeap<Label> leTas = new BinaryHeap<Label>();
//Initialisation ----------------------------------------------------
for(int i = 0; i< tailleGraphe; i++ )
{
currentLabel = new Label(data.getGraph().get(i));
labelSommets.set(i, currentLabel);
labelSommets.add(currentLabel);
}
currentLabel = labelSommets.get(data.getOrigin().getId());
currentLabel.setNouveauChemin(null, 0);
labelSommets.set(data.getOrigin().getId(), currentLabel);
leTas.insert(data.getOrigin());
currentLabel.setMarqueTrue();
leTas.insert(currentLabel);
Node currentNode;
Node currentNode = data.getOrigin() ;
double currentCost;
double newCost;
Label newLabel;
//Boucle principale ----------------------------------------------------
while (nbMarques != tailleGraphe)
while (!leTas.isEmpty() && !currentNode.equals(data.getDestination()))
{
currentNode = leTas.deleteMin();
currentLabel = leTas.deleteMin();
currentNode = currentLabel.getSommetCourant();
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)
currentCost = newLabel.getCost();
newCost = arc.getLength() + currentLabel.getCost();
if(currentCost == -1.0)
{
newLabel.setNouveauChemin(arc, newCost);
leTas.insert(arc.getDestination());
labelSommets.set(arc.getDestination().getId(), currentLabel);
leTas.insert(newLabel);
}
else if(newCost < currentCost)
{
leTas.remove(newLabel);
newLabel.setNouveauChemin(arc, newCost);
leTas.insert(newLabel);
}
}
}
}
//Retour chemin obtenu ----------------------------------------------------
Node finalNode = data.getDestination();
index = 0;
ArrayList<Arc> arcListe = new ArrayList<Arc>() ;
while(!finalNode.equals(data.getOrigin()) || index < tailleGraphe)
if(labelSommets.get(data.getDestination().getId()).getCost() == -1)
{
return new ShortestPathSolution(data, AbstractSolution.Status.INFEASIBLE);
}
while(!finalNode.equals(data.getOrigin()) && index < tailleGraphe)
{
arcListe.add(labelSommets.get(finalNode.getId()).pere);
finalNode = arcListe.get(arcListe.size()-1).getOrigin();
@ -83,12 +101,11 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
}
if(index != tailleGraphe)
{
Collections.reverse(arcListe);
return new ShortestPathSolution(data, AbstractSolution.Status.FEASIBLE, new Path(data.getGraph(),arcListe ));
}
else
{
return new ShortestPathSolution(data, AbstractSolution.Status.INFEASIBLE);
}
return new ShortestPathSolution(data, AbstractSolution.Status.INFEASIBLE);
}

View file

@ -3,7 +3,7 @@ package org.insa.graphs.algorithm.shortestpath;
import org.insa.graphs.model.Arc;
import org.insa.graphs.model.Node;
public class Label {
public class Label implements Comparable<Label>{
/* ATTRIBUTS */
@ -66,7 +66,26 @@ public class Label {
this.coutRealise = coutRealise;
}
/**
* Compare the ID of this node with the ID of the given node.
*
* @param other Node to compare this node with.
*
* @see java.lang.Comparable#compareTo(java.lang.Object)
*/
@Override
public int compareTo(Label other) {
if (this.getCost() < other.getCost())
{
return -1;
}
else if (this.getCost() > other.getCost())
{
return 1;
}
return 0;
}
}