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..f929eea 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,17 @@ package org.insa.graphs.algorithm.shortestpath; + +import java.util.Arrays; + +import java.util.Collections; +import java.util.ArrayList; + +import org.insa.graphs.algorithm.AbstractSolution.Status; +import org.insa.graphs.algorithm.utils.BinaryHeap; +import org.insa.graphs.model.Arc; +import org.insa.graphs.model.Graph; +import org.insa.graphs.model.Node; +import org.insa.graphs.model.Path; public class DijkstraAlgorithm extends ShortestPathAlgorithm { public DijkstraAlgorithm(ShortestPathData data) { @@ -10,7 +22,60 @@ 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]; + //Initilisation + for(Node n:graph.getNodes()) + { + tabLabel[n.getId()]=new Label(n); + } + tabLabel[data.getOrigin().getId()].setCout(0); + tas.insert(data.getOrigin()); + int nbMarque=0; + while(nbMarque!=nbNodes) + { + Node X=tas.findMin(); + tabLabel[X.getId()].setMarque(true); + nbMarque++; + for(Arc a: X.getSuccessors()) + { + Node Y=a.getDestination(); + if(!tabLabel[Y.getId()].isMarque()) + { + //double cout_avant=tabLabel[Y.getId()].getCout(); + if((tabLabel[X.getId()].getCout()+a.getLength()) arcs = new ArrayList<>(); + Arc arc = tabLabel[data.getDestination().getId()].getPere(); + while (arc != null) { + arcs.add(arc); + arc = tabLabel[arc.getOrigin().getId()].getPere(); + } + + // Reverse the path... + Collections.reverse(arcs); + + // Create the final solution. + solution = 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..99dbec0 --- /dev/null +++ b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/Label.java @@ -0,0 +1,53 @@ +package org.insa.graphs.algorithm.shortestpath; + +import org.insa.graphs.model.Node; +import org.insa.graphs.model.Arc; + +public class Label { + private Node sommetCourant; + private boolean isMarque; + private double cout; + private Arc pere; + + + + public Label(Node sommetCourant) + { + this.sommetCourant=sommetCourant; + this.setCout(Double.POSITIVE_INFINITY); + this.setMarque(false); + this.setPere(null); + } + + + public Arc getPere() { + return pere; + } + + public void setPere(Arc pere) { + this.pere = pere; + } + + public boolean isMarque() { + return isMarque; + } + + public void setMarque(boolean isMarque) { + this.isMarque = isMarque; + } + + public Node getSommetCourant() { + return sommetCourant; + } + + + public double getCout() { + return cout; + } + + + public void setCout(double cout) { + this.cout = cout; + } + +} diff --git a/be-graphes-algos/target/classes/org/insa/graphs/algorithm/shortestpath/DijkstraAlgorithm.class b/be-graphes-algos/target/classes/org/insa/graphs/algorithm/shortestpath/DijkstraAlgorithm.class index e57e5e6..992c3a6 100644 Binary files a/be-graphes-algos/target/classes/org/insa/graphs/algorithm/shortestpath/DijkstraAlgorithm.class and b/be-graphes-algos/target/classes/org/insa/graphs/algorithm/shortestpath/DijkstraAlgorithm.class differ diff --git a/be-graphes-algos/target/classes/org/insa/graphs/algorithm/shortestpath/Label.class b/be-graphes-algos/target/classes/org/insa/graphs/algorithm/shortestpath/Label.class new file mode 100644 index 0000000..3940f69 Binary files /dev/null and b/be-graphes-algos/target/classes/org/insa/graphs/algorithm/shortestpath/Label.class differ