From 48b3f257186b9f5f05f72e23c579997d6fedb3c2 Mon Sep 17 00:00:00 2001 From: Nabzzz Date: Fri, 27 Mar 2020 19:42:31 +0100 Subject: [PATCH] =?UTF-8?q?Impl=C3=A9mentation=20du=20Dijkstra=20en=20cour?= =?UTF-8?q?s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../shortestpath/DijkstraAlgorithm.java | 67 +++++++++++++++++- .../graphs/algorithm/shortestpath/Label.java | 53 ++++++++++++++ .../shortestpath/DijkstraAlgorithm.class | Bin 976 -> 4126 bytes .../graphs/algorithm/shortestpath/Label.class | Bin 0 -> 1292 bytes 4 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/Label.java create mode 100644 be-graphes-algos/target/classes/org/insa/graphs/algorithm/shortestpath/Label.class 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 e57e5e6d40f2e1cf62341c53b3dc1d0774ef0e41..992c3a6d2ce20dd1d383a28dab4a361c67ba88d5 100644 GIT binary patch literal 4126 zcmcIn`*R!B75=U*`O5Mpp$?9-ab6@bu^lH0!41T692`3#3X+rJ;M5^4tfWQWM3$W0 zRfuT{DWwzwfkG*z;Y}Z=PYP{if`?9jW2S#Yr$6?8=rFDO-CZe?EsM!`rsEk&yZ4;$ zyuNep@!$XZ*fts1XMLX@`C^g`gktzuNC z!V5qkn9e^~$O#Rt)TSUy{Dt$Z#ThQL}4x1&u!%b0E~H(hX*4T0#&pgjk}XooJb zUf`Y;7D>YfbO?m#)BTEN0$Y~xXNpD4MmzzAvI$!xHVbqtwOJ*mVJq%n2^{MQmbkI2 zJ0`JRpl7K{h0{z_)`=Xpa)v!~%rvIM*oIC;nmaxHafvR0-un8BdLnKl&8+gYN5d}k z`T>qI3UaEI0iH818WCkN61N;T+JZgUD-jV`z4%1KUD!u$%QbDo&D#R&7HP*y?}^Zd z{Ssd%d^t@$IRXriRCm zq0^N2!)HE66q2Rk9M1dsjGMWP%dXpCvnw#p@NU2I(C?yliz#hE%VlRSZ3(L~qPoXbJzm!EBb8Wr;^e7VVkj>0YTdD+ZuBr-!D}jiKM~kl zo4=}o9bGyTRXY{%Q>FZdzzem?|HtIl)XR5NMdWQ2A*Xb)3LLDFy#>F@Y~_*=^p-1u zO*J^x$zjHG$_%HI%>S#?9Uo*!`C>l#_|O_ zWgfNEV!EzosobNwmm6Fxmox3*tl>B&ua+BMxz{YU$6L{#k+_3PVIvoQ@RLMD6F()N z2GPJjps8oq96}%SR|CTQ40~@|DY+Icd|u~ELBlFOQ68(Yh8O`Ai;vZK86loSyw+3X zqAe1;idZ+gyNHeZ1M}E6F^3)f!M4EX*xBF2x7|0en@e|S7F+eu4csm8Hyr4fdXp}T zh|Xi@gf8bWIHnGV1gcJwup%6ku2q~dJ5f42h zg?gCqZG~rXQ4g1Fo$YVdn~V5%e+v~_ltK~4bv#s7FCwkZC$FU{=jk`zLSOl@R?zcG zVI^+L5Fx8fdJ#O^CffqCxZI^VF^M-^2Nfx^w6hxu7vf{JYJdDt%v6D<2k&Z==~#W7Vs+jIy(8g zot0fr2I#EV2DGAse;c_TZN>n%=>)$|^U1_^t`IwLfjTbtf+x|1tLVlx^x#AE;v?+F z=h%yXVjsR_2^-KS)}Tk|=oDKqAok**7{H)7#KrL#b8s4m#Tg8XBsUzJzaQrkbQuqb zSGmF|mc2w)kv}Dt1;xuHmPv69Kf`r$P!rGLO}s@Gw~D9nHr`=W9byLW;yp&Rji~SA z1F~~~to%8C!3Zyi?f50-B)xwVzv8m|#kvQA_Z)~}3gq`?FsjEBrO$!LJ6aS delta 217 zcmYL@I|{;35Jm4q^B&2I=Aqyxen=w(6-5vfY%MGdZb)zqGQDfi#=^n{Sh)pT8y6s` zV=QcDxMwbh*~M3FFR#-Xz!)tJhcM-vig-xv!{Sbycqt8dq{`da=clr2=7SLN+VuEg zCa!`D&AlMmU?qo~h`jC*r6r~Py#xoo%wl#h(hZ;`8Da6SETkaGMP7I)3V8w*RN>e; dENU46Ip&=ay%?|_D8)yIk 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 0000000000000000000000000000000000000000..3940f6981978246e7f2b81240c8bd39ee8f201e8 GIT binary patch literal 1292 zcmaKr%Wl&^6o&svQ>P8?H7%6hftEV%#gG;@ph~C}t5A@NvgmFSti)9k2isxG3n2j# zEfNbJfT!UJ5dRs6QqqPjJTqtJ{O3ED{P^|lJAger&LN|)6-IS0Xvf;CN4nF9J>9H_ zQIIrRUfc+y#E+AXP8wcWSN&!VS%t|{{Z4yL-L88ttEYY~Q5cKER?ANg!fvG7JTI2} zRa@a3zv&(E`$Szt&Qt2XZ7ZtC~S;4cs25`Mv&CheIb zUSB0HXMmMsnGZ#6*TyLF3i)G>M$La7NUnJ^OFJ?~tQ;S+0a;|EA>3sozFFyMR`PKj z;ma|M^E<&)jb0h93+`7;x|?SxxHFhN$1Qj0oJMtymCwfc9>4QOc$`5di8ICTG-gqt zeFkg1znMy59rt;ns6pXGVgpVupvP75ZrJ^T;wNr05NXR8=ZuV z))Lad&IYEOrNE NAtNPO?3sv