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 0382dd0..80e6566 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 @@ -2,9 +2,9 @@ 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.algorithm.utils.EmptyPriorityQueueException; import org.insa.graphs.model.Graph; import org.insa.graphs.model.Node; import org.insa.graphs.model.Path; @@ -35,23 +35,35 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm { TabLabel[data.getOrigin().getId()].setCost(0); // on met le cost du sommet à 0 Tas.insert(TabLabel[data.getOrigin().getId()]); // on insert le label du sommet dans le tas - //int nbMarque = 0; // correspond au nombre de sommet marqué + notifyOriginProcessed(data.getOrigin()); + + + int nbMarque = 0; // correspond au nombre de sommet marqué while(!TabLabel[data.getDestination().getId()].isMarque() ) { // tant que la destination est non marqués - Node X = Tas.findMin().getSommetCourant(); // on prend le sommet du tas et on le stocke dans X + Node X ; + try { + X = Tas.findMin().getSommetCourant(); // on prend le sommet du tas et on le stocke dans X + } + catch(EmptyPriorityQueueException e) { + break; + } TabLabel[X.getId()].setMarque(true); // on le marque + this.notifyNodeMarked(X); + //System.out.println(TabLabel[X.getId()].getCost()); //Vérification que le coût des labels marqués est croissant au cours des itérations. Tas.remove(TabLabel[X.getId()]); - //nbMarque++; - for(Arc a : X.getSuccessors() ){ + nbMarque++; + for(Arc a : X.getSuccessors() ){ // Le nombre de successeurs explorés à chaque itération = au nombre de successeurs d'un node if (data.isAllowed(a)) { Node Y = a.getDestination(); if(!(TabLabel[Y.getId()].isMarque() ) ) { if(TabLabel[Y.getId()].getCost()==Double.POSITIVE_INFINITY ) { //Y n'est pas dans le tas - TabLabel[Y.getId()].setCost( TabLabel[X.getId()].getCost()+ a.getLength() ); + TabLabel[Y.getId()].setCost( TabLabel[X.getId()].getCost()+ data.getCost(a) ); Tas.insert(TabLabel[Y.getId()]); TabLabel[Y.getId()].setFather(a); + this.notifyNodeReached(Y); } - else if(TabLabel[Y.getId()].getCost() > TabLabel[X.getId()].getCost()+ a.getLength()) { - TabLabel[Y.getId()].setCost( TabLabel[X.getId()].getCost()+ a.getLength() ); + else if(TabLabel[Y.getId()].getCost() > TabLabel[X.getId()].getCost()+ data.getCost(a)) { + TabLabel[Y.getId()].setCost( TabLabel[X.getId()].getCost()+ data.getCost(a) ); Tas.remove(TabLabel[Y.getId()]); Tas.insert(TabLabel[Y.getId()]); TabLabel[Y.getId()].setFather(a); @@ -60,8 +72,10 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm { } } } + // 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(); @@ -70,17 +84,44 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm { solution = new ShortestPathSolution(data, Status.INFEASIBLE); } else { + float length = 0; + notifyDestinationReached(dest); while (a != null ) { arcs.add(a); + length += a.getLength(); a = TabLabel[a.getOrigin().getId()].getFather(); - System.out.println(a); + //System.out.println(a); } + Collections.reverse(arcs); - solution = new ShortestPathSolution(data, Status.OPTIMAL, new Path(graph, arcs)); - } + Path path = new Path(graph, arcs); + + + + // Vérifier que la longueur du chemin (avec methode de la class Path) est identique au PCC de Dijsktra + if(path.getLength() == length) { + System.out.println(" la longueur du chemin (avec methode de la class Path) est identique au PCC de Dijsktra"); + } + else { + System.out.println( "Erreur : la longueur du chemin (avec methode de la class Path) est identique au PCC de Dijsktra ") ; + } + //Vérifier que le chemin solution obtenu par Dijsktra est valide + if (path.isValid()) { + System.out.println("Le chemin obtenu est bien valide."); + } + else { + System.out.println("Le chemin obtenu n'est pas valide."); + } + + + + solution = new ShortestPathSolution(data, Status.OPTIMAL, new Path(graph, arcs));} + + + System.out.println("Nb iterations: " + nbMarque + ", Nb arcs du PCC: " + arcs.size()); return solution; - } } +} diff --git a/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/LabelStar.java b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/LabelStar.java new file mode 100644 index 0000000..82048e2 --- /dev/null +++ b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/LabelStar.java @@ -0,0 +1,36 @@ +package org.insa.graphs.algorithm.shortestpath; + +import org.insa.graphs.model.Node; + +public class LabelStar extends Label implements java.lang.Comparable