Dépôt du be graphe
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DijkstraAlgorithm.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package org.insa.graphs.algorithm.shortestpath;
  2. import java.util.ArrayList;
  3. import java.util.Collections;
  4. import org.insa.graphs.algorithm.AbstractInputData.Mode;
  5. import org.insa.graphs.algorithm.AbstractSolution.Status;
  6. import org.insa.graphs.algorithm.utils.BinaryHeap;
  7. import org.insa.graphs.model.Arc;
  8. import org.insa.graphs.model.Label;
  9. import org.insa.graphs.model.Node;
  10. import org.insa.graphs.model.Path;
  11. public class DijkstraAlgorithm extends ShortestPathAlgorithm {
  12. protected Label[] labels;
  13. public DijkstraAlgorithm(ShortestPathData data) {
  14. super(data);
  15. }
  16. protected void initAlgo() {
  17. // Dijkstra Init
  18. this.labels = new Label[data.getGraph().size()];
  19. for(Node node : data.getGraph().getNodes()) {
  20. labels[node.getId()] = new Label(node);
  21. }
  22. }
  23. @Override
  24. protected ShortestPathSolution doRun() {
  25. final ShortestPathData data = getInputData();
  26. initAlgo();
  27. double shortestCostToDestination = Double.POSITIVE_INFINITY;
  28. // it's the cost of the last marked node. In order to stop algorithm when it's not
  29. // useful
  30. double lastMarkedNodeCost = 0;
  31. // Let's set the origin cost at 0
  32. labels[data.getOrigin().getId()].setNewCost(null, 0d);
  33. // We need to add a binaryMinHeap to get min at each iteration
  34. BinaryHeap<Label> minHeap = new BinaryHeap<Label>();
  35. // We add into our binaryMinHeap the origin
  36. minHeap.insert(labels[data.getOrigin().getId()]);
  37. //We notify observers that we initialized the origin
  38. notifyOriginProcessed(data.getOrigin());
  39. // We can start searching for the shortestPath
  40. // We stop the algorithm when the cost of last marked point is equal to the shortest found path
  41. // or if we searched all the graph
  42. while(lastMarkedNodeCost < shortestCostToDestination && !minHeap.isEmpty()) {
  43. //We get the shortest not marked label
  44. Label minLabel = minHeap.deleteMin();
  45. //We mark it
  46. minLabel.setMarked();
  47. //We notify we marked it
  48. notifyNodeMarked(minLabel.getCurrent());
  49. lastMarkedNodeCost = minLabel.getCost();
  50. //We look at their successors
  51. for(Arc successor : minLabel.getCurrent().getSuccessors()) {
  52. //We see if we need to update
  53. Label label = labels[successor.getDestination().getId()];
  54. if(!label.isMarked() ) {
  55. //We calculate new costs
  56. double newCost;
  57. // If the arc can't be reached thanks to transport mode (car, bike)
  58. // Then it's like the cost to reach it is equal to infinity
  59. if(!data.isAllowed(successor)) {
  60. newCost = Double.POSITIVE_INFINITY;
  61. }
  62. else if(data.getMode() == Mode.TIME) {
  63. newCost = minLabel.getCost() + successor.getMinimumTravelTime();
  64. }
  65. else {
  66. newCost = minLabel.getCost() + successor.getLength();
  67. }
  68. if(newCost < label.getCost()) {
  69. //we need to update !
  70. //if the result is finite, then we need to remove first
  71. //on the heap
  72. if(Double.isFinite(label.getCost())) {
  73. minHeap.remove(label);
  74. }
  75. else {
  76. notifyNodeReached(label.getCurrent());
  77. }
  78. label.setNewCost(successor, newCost);
  79. minHeap.insert(label);
  80. //we see if we have updated the destination
  81. if(successor.getDestination().equals(data.getDestination())) {
  82. shortestCostToDestination = newCost;
  83. }
  84. }
  85. }
  86. }
  87. }
  88. //Here we have calculated minimum cost to destination, or no destination is unreachable
  89. //We see if we find something
  90. if(labels[data.getDestination().getId()].getFather() == null) {
  91. //here there is no route available
  92. return new ShortestPathSolution(data, Status.INFEASIBLE);
  93. }
  94. //There is a path, let's find it
  95. notifyDestinationReached(data.getDestination());
  96. ArrayList<Arc> path = new ArrayList<Arc>();
  97. //First arc
  98. Arc arc = labels[data.getDestination().getId()].getFather();
  99. while(arc != null) {
  100. //while there is a father
  101. //add the arc to the path
  102. path.add(arc);
  103. arc = labels[arc.getOrigin().getId()].getFather();
  104. }
  105. // We need to reverse the path to get in the right order
  106. Collections.reverse(path);
  107. // Send the solution
  108. return new ShortestPathSolution(data, Status.OPTIMAL, new Path(data.getGraph(), path));
  109. }
  110. }