diff --git a/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/DijkstraAlgorithm.java b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/DijkstraAlgorithm.java index bacb8e3..8aa1cf7 100644 --- a/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/DijkstraAlgorithm.java +++ b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/DijkstraAlgorithm.java @@ -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 Tas = new BinaryHeap() ; + + 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 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; } diff --git a/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/Label.java b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/Label.java new file mode 100644 index 0000000..a80e290 --- /dev/null +++ b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/Label.java @@ -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; + } + + +}