Ajout Dijkstra et Label

This commit is contained in:
Auriane Lartigue 2020-03-27 19:16:27 +01:00
parent fe3a4d51e9
commit d477427f51
2 changed files with 110 additions and 1 deletions

View file

@ -1,5 +1,15 @@
package org.insa.graphs.algorithm.shortestpath;
import java.util.ArrayList;
import java.util.Collections;
import org.insa.graphs.algorithm.AbstractSolution.Status;
import org.insa.graphs.algorithm.utils.BinaryHeap;
import org.insa.graphs.model.Graph;
import org.insa.graphs.model.Node;
import org.insa.graphs.model.Path;
import org.insa.graphs.model.Arc;
public class DijkstraAlgorithm extends ShortestPathAlgorithm {
public DijkstraAlgorithm(ShortestPathData data) {
@ -10,7 +20,54 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
protected ShortestPathSolution doRun() {
final ShortestPathData data = getInputData();
ShortestPathSolution solution = null;
// TODO:
Graph graph = data.getGraph();
final int nbNodes = graph.size();
BinaryHeap<Node> Tas = new BinaryHeap<Node>() ;
Label[] TabLabel = new Label[nbNodes];
//Initialisation
for (Node node: graph.getNodes()) {
TabLabel[node.getId()]= new Label(node); // on crée un Label pour chaque node
}
TabLabel[data.getOrigin().getId()].setCost(0); // on met le cost du sommet à 0
Tas.insert(data.getOrigin()); // on insert le sommet dans le tas
int nbMarque = 0; // correspond au nombre de sommet marqué
while(nbMarque != nbNodes) { // il existe des sommets non marqués
Node X = Tas.findMin(); // on prend le sommet du tas et on le stocke dans X
Label LabelX = TabLabel[X.getId()];
LabelX.setMarque(true);
nbMarque++;
for(Arc a : X.getSuccessors() ){
Node Y = a.getDestination();
if(!(TabLabel[Y.getId()].isMarque())) {
Label LabelY = TabLabel[Y.getId()];
double aux = LabelY.getCost();
LabelY.setCost(Math.min(LabelY.getCost(), LabelX.getCost()+ a.getLength() ));
if (LabelY.getCost() != aux) { //cost(y) a été mis a jour
Tas.insert(Y);
LabelY.setFather(a);
}
}
}
}
// Create the path from the array of predecessors...
ArrayList<Arc> arcs = new ArrayList<>();
Node dest = data.getDestination();
Label Labeldest = TabLabel[dest.getId()];
Arc a = Labeldest.getFather();
while (a != null ) {
arcs.add(a);
a = TabLabel[a.getOrigin().getId()].getFather();
System.out.println(a);
}
Collections.reverse(arcs);
new ShortestPathSolution(data, Status.OPTIMAL, new Path(graph, arcs));
return solution;
}

View file

@ -0,0 +1,52 @@
package org.insa.graphs.algorithm.shortestpath;
import org.insa.graphs.model.Arc;
import org.insa.graphs.model.Node;
public class Label{
private Node sommetCourant ;
private boolean marque;
private double cost ;
private Arc father ;
public Label(Node node) {
this.setSommetCourant(node);
this.setMarque(false);
this.setCost(Double.POSITIVE_INFINITY);
this.setFather(null);
}
public Arc getFather() {
return father;
}
public void setFather(Arc father) {
this.father = father;
}
public double getCost() {
return cost;
}
public void setCost(double cost) {
this.cost = cost;
}
public boolean isMarque() {
return marque;
}
public void setMarque(boolean marque) {
this.marque = marque;
}
public Node getSommetCourant() {
return sommetCourant;
}
public void setSommetCourant(Node sommetCourant) {
this.sommetCourant = sommetCourant;
}
}