Compare commits
2 commits
3d095626ab
...
b2aa8243cc
| Author | SHA1 | Date | |
|---|---|---|---|
| b2aa8243cc | |||
| d1b49fbafc |
3 changed files with 197 additions and 1 deletions
64
UML/diagramme2/CompleteGraph.uml
Normal file
64
UML/diagramme2/CompleteGraph.uml
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Diagram>
|
||||
<ID>JAVA</ID>
|
||||
<OriginalElement>org.insa.graphs.algorithm.AbstractAlgorithm</OriginalElement>
|
||||
<nodes>
|
||||
<node x="0.0" y="524.0">org.insa.graphs.algorithm.AbstractInputData</node>
|
||||
<node x="127.49999999999999" y="89.0">org.insa.graphs.algorithm.shortestpath.ShortestPathAlgorithm</node>
|
||||
<node x="151.5" y="0.0">org.insa.graphs.algorithm.AbstractAlgorithm</node>
|
||||
<node x="0.0" y="178.0">org.insa.graphs.algorithm.shortestpath.DijkstraAlgorithm</node>
|
||||
<node x="394.0" y="440.0">org.insa.graphs.algorithm.shortestpath.ShortestPathSolution</node>
|
||||
<node x="6.0" y="613.0">org.insa.graphs.algorithm.shortestpath.ShortestPathData</node>
|
||||
<node x="0.0" y="697.0">org.insa.graphs.algorithm.AlgorithmFactory</node>
|
||||
<node x="285.0" y="178.0">org.insa.graphs.algorithm.shortestpath.BellmanFordAlgorithm</node>
|
||||
<node x="310.0" y="524.0">org.insa.graphs.algorithm.ArcInspectorFactory</node>
|
||||
<node x="0.0" y="440.0">org.insa.graphs.algorithm.shortestpath.ShortestPathTextObserver</node>
|
||||
<node x="298.0" y="697.0">org.insa.graphs.algorithm.ArcInspector</node>
|
||||
<node x="24.0" y="351.0">org.insa.graphs.algorithm.shortestpath.ShortestPathObserver</node>
|
||||
<node x="418.0" y="351.0">org.insa.graphs.algorithm.AbstractSolution</node>
|
||||
<node x="18.0" y="267.0">org.insa.graphs.algorithm.shortestpath.AStarAlgorithm</node>
|
||||
</nodes>
|
||||
<notes />
|
||||
<edges>
|
||||
<edge source="org.insa.graphs.algorithm.shortestpath.ShortestPathData" target="org.insa.graphs.algorithm.AbstractInputData">
|
||||
<point x="0.0" y="-19.5" />
|
||||
<point x="0.0" y="19.5" />
|
||||
</edge>
|
||||
<edge source="org.insa.graphs.algorithm.shortestpath.DijkstraAlgorithm" target="org.insa.graphs.algorithm.shortestpath.ShortestPathAlgorithm">
|
||||
<point x="0.0" y="-19.5" />
|
||||
<point x="132.5" y="153.0" />
|
||||
<point x="205.75" y="153.0" />
|
||||
<point x="-78.25" y="19.5" />
|
||||
</edge>
|
||||
<edge source="org.insa.graphs.algorithm.shortestpath.ShortestPathSolution" target="org.insa.graphs.algorithm.AbstractSolution">
|
||||
<point x="0.0" y="-19.5" />
|
||||
<point x="0.0" y="19.5" />
|
||||
</edge>
|
||||
<edge source="org.insa.graphs.algorithm.shortestpath.ShortestPathAlgorithm" target="org.insa.graphs.algorithm.AbstractAlgorithm">
|
||||
<point x="0.0" y="-19.5" />
|
||||
<point x="0.0" y="19.5" />
|
||||
</edge>
|
||||
<edge source="org.insa.graphs.algorithm.shortestpath.BellmanFordAlgorithm" target="org.insa.graphs.algorithm.shortestpath.ShortestPathAlgorithm">
|
||||
<point x="0.0" y="-19.5" />
|
||||
<point x="435.5" y="153.0" />
|
||||
<point x="362.25" y="153.0" />
|
||||
<point x="78.25" y="19.5" />
|
||||
</edge>
|
||||
<edge source="org.insa.graphs.algorithm.shortestpath.ShortestPathTextObserver" target="org.insa.graphs.algorithm.shortestpath.ShortestPathObserver">
|
||||
<point x="0.0" y="-19.5" />
|
||||
<point x="0.0" y="19.5" />
|
||||
</edge>
|
||||
<edge source="org.insa.graphs.algorithm.shortestpath.AStarAlgorithm" target="org.insa.graphs.algorithm.shortestpath.DijkstraAlgorithm">
|
||||
<point x="0.0" y="-19.5" />
|
||||
<point x="0.0" y="19.5" />
|
||||
</edge>
|
||||
</edges>
|
||||
<settings layout="Hierarchic Group" zoom="1.0" x="347.5" y="368.0" />
|
||||
<SelectedNodes>
|
||||
<node>org.insa.graphs.algorithm.AbstractAlgorithm</node>
|
||||
</SelectedNodes>
|
||||
<Categories />
|
||||
<SCOPE>All</SCOPE>
|
||||
<VISIBILITY>private</VISIBILITY>
|
||||
</Diagram>
|
||||
|
||||
|
|
@ -1,5 +1,16 @@
|
|||
package org.insa.graphs.algorithm.shortestpath;
|
||||
|
||||
import org.insa.graphs.algorithm.AbstractSolution;
|
||||
import org.insa.graphs.algorithm.utils.ElementNotFoundException;
|
||||
import org.insa.graphs.model.Arc;
|
||||
import org.insa.graphs.model.Graph;
|
||||
import org.insa.graphs.algorithm.utils.BinaryHeap;
|
||||
import org.insa.graphs.model.Path;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
|
||||
|
||||
public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||
|
||||
public DijkstraAlgorithm(ShortestPathData data) {
|
||||
|
|
@ -9,9 +20,78 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
|||
@Override
|
||||
protected ShortestPathSolution doRun() {
|
||||
final ShortestPathData data = getInputData();
|
||||
|
||||
Graph graph = data.getGraph();
|
||||
final int nbNodes = graph.size();
|
||||
|
||||
// Initialize array of Labels.
|
||||
Label[] labels = new Label[nbNodes];
|
||||
for (int i = 0; i < nbNodes; i++) {
|
||||
labels[i] = new Label(i);
|
||||
}
|
||||
final int origin = data.getOrigin().getId();
|
||||
final int destination = data.getDestination().getId();
|
||||
labels[origin].setCost(0);
|
||||
|
||||
// Initialize the heap
|
||||
BinaryHeap<Label> heap = new BinaryHeap<>();
|
||||
heap.insert(labels[origin]);
|
||||
|
||||
int iterationCounter= 0;
|
||||
|
||||
while (!heap.isEmpty() && labels[destination].isNotMarked()) {
|
||||
Label parent = heap.deleteMin();
|
||||
parent.setMarked(true);
|
||||
System.out.println(parent.getCost());
|
||||
iterationCounter++;
|
||||
for (Arc arc : graph.getNodes().get(parent.getAssociatedNode()).getSuccessors()) {
|
||||
Label current = labels[arc.getDestination().getId()];
|
||||
notifyNodeReached(arc.getDestination());
|
||||
|
||||
if (current.isNotMarked() && data.isAllowed(arc)) {
|
||||
this.updateCost(arc, parent, current, heap);
|
||||
}
|
||||
}
|
||||
}
|
||||
System.out.println("Nb itérations: " + iterationCounter);
|
||||
ShortestPathSolution solution = null;
|
||||
// TODO:
|
||||
|
||||
// Destination has no predecessor, the solution is infeasible...
|
||||
if (labels[destination].getFatherArc() == null) {
|
||||
solution = new ShortestPathSolution(data, AbstractSolution.Status.INFEASIBLE);
|
||||
} else {
|
||||
// The destination has been found, notify the observers.
|
||||
notifyDestinationReached(data.getDestination());
|
||||
|
||||
// Create the path from the array of predecessors...
|
||||
ArrayList<Arc> arcs = new ArrayList<>();
|
||||
Arc arc = labels[destination].getFatherArc();
|
||||
while (arc != null) {
|
||||
arcs.add(arc);
|
||||
arc = labels[arc.getOrigin().getId()].getFatherArc();
|
||||
}
|
||||
|
||||
// Reverse the path...
|
||||
Collections.reverse(arcs);
|
||||
|
||||
// Create the final solution.
|
||||
solution = new ShortestPathSolution(data, AbstractSolution.Status.OPTIMAL, new Path(graph, arcs));
|
||||
}
|
||||
|
||||
return solution;
|
||||
}
|
||||
|
||||
private void updateCost(Arc arc, Label parent, Label current, BinaryHeap<Label> heap) {
|
||||
double newCost = parent.getCost() + arc.getLength();
|
||||
if (newCost < current.getCost()) {
|
||||
try {
|
||||
heap.remove(current);
|
||||
} catch (ElementNotFoundException e) {
|
||||
}
|
||||
current.setCost(newCost);
|
||||
current.setFatherArc(arc);
|
||||
heap.insert(current);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,52 @@
|
|||
package org.insa.graphs.algorithm.shortestpath;
|
||||
|
||||
import org.insa.graphs.model.Arc;
|
||||
|
||||
public class Label implements Comparable<Label> {
|
||||
private int associatedNode;
|
||||
private double cost;
|
||||
|
||||
private boolean marked;
|
||||
|
||||
private Arc fatherArc;
|
||||
|
||||
public Label(int associatedNode) {
|
||||
this.associatedNode = associatedNode;
|
||||
this.marked = false;
|
||||
this.cost = Double.POSITIVE_INFINITY;
|
||||
this.fatherArc = null;
|
||||
}
|
||||
|
||||
public int getAssociatedNode() {
|
||||
return associatedNode;
|
||||
}
|
||||
|
||||
public double getCost() {
|
||||
return cost;
|
||||
}
|
||||
|
||||
public void setCost(double cost) {
|
||||
this.cost = cost;
|
||||
}
|
||||
|
||||
public boolean isNotMarked() {
|
||||
return !marked;
|
||||
}
|
||||
|
||||
public void setMarked(boolean marked) {
|
||||
this.marked = marked;
|
||||
}
|
||||
|
||||
public Arc getFatherArc() {
|
||||
return fatherArc;
|
||||
}
|
||||
|
||||
public void setFatherArc(Arc fatherArc) {
|
||||
this.fatherArc = fatherArc;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(Label label) {
|
||||
return this.cost - label.getCost() > 0 ? 1 : -1;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue