few fixes: dijkstra
This commit is contained in:
parent
4bd0c74fa4
commit
f51d3a26b5
2 changed files with 43 additions and 19 deletions
|
@ -21,7 +21,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
|||
// TODO:
|
||||
|
||||
// Heap of "sommets"
|
||||
BinaryHeap<Label> queue = new BinaryHeap<Label>(); // change to a comparable type
|
||||
BinaryHeap<Label> labels = new BinaryHeap<Label>(); // change to a comparable type
|
||||
|
||||
Graph graph = data.getGraph();
|
||||
final int nbNodes = graph.size();
|
||||
|
@ -35,41 +35,65 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
|||
notifyOriginProcessed(data.getOrigin());
|
||||
|
||||
// Init Dijkstra
|
||||
ArrayList<Label> labels = new ArrayList<Label>();
|
||||
//ArrayList<Label> labels = new ArrayList<Label>();
|
||||
for (Node node : graph.getNodes()) {
|
||||
labels.add(new Label(node));
|
||||
labels.insert(new Label(node));
|
||||
}
|
||||
// Nodes set
|
||||
for (Label label : labels) {
|
||||
for (Label label : labels.array) {
|
||||
label.marked = false;
|
||||
label.setPathCost(Float.MAX_VALUE);
|
||||
label.parentNode = null;
|
||||
}
|
||||
// Origin set
|
||||
Label s = labels.get(0);
|
||||
Label s = labels.array.get(0);
|
||||
s.setPathCost(0);
|
||||
queue.insert(s);
|
||||
labels.insert(s);
|
||||
|
||||
boolean trouve = false; // TODO : breaking condition
|
||||
Label x;
|
||||
while (!queue.isEmpty() && !trouve) {
|
||||
x = queue.ExtractMin();
|
||||
while (!labels.isEmpty() && !trouve) {
|
||||
x = labels.deleteMin();
|
||||
x.mark();
|
||||
|
||||
for (Arc arc : x.node.getSuccessors()) {
|
||||
// TODO : Retrieve label with node
|
||||
Label y;
|
||||
if (!arc.getDestination().marked) {
|
||||
float new_cost = x.getCost() + (float) data.getCost(arc);
|
||||
if (y.getCost() > new_cost) {
|
||||
y.setPathCost(new_cost);
|
||||
queue.insert(y);
|
||||
y.setParentNode(x.getNode());
|
||||
// y shall be all the successors of x
|
||||
float arc_cost = 0;
|
||||
for (Label y : labels.array) {
|
||||
if (y.getParentNode() == x.getNode()) {
|
||||
if (y.isMarked()) {
|
||||
// This loop serves to get the lentgh of the arc as
|
||||
// we know its origin and destination
|
||||
for (Arc arc : x.getNode().getSuccessors()) {
|
||||
if (y.getNode() == arc.getDestination()) {
|
||||
arc_cost = arc.getLength();
|
||||
}
|
||||
}
|
||||
float new_cost = x.getCost() + arc_cost;
|
||||
if (y.getCost() > new_cost) {
|
||||
y.setPathCost(new_cost);
|
||||
labels.insert(y);
|
||||
y.setParentNode(x.getNode());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Create the path ...
|
||||
ArrayList<Arc> arcs_path = new ArrayList<>();
|
||||
Label dest;
|
||||
for (Label label : labels.array) {
|
||||
if (label.getNode() == data.getDestination()){
|
||||
dest = label;
|
||||
}
|
||||
}
|
||||
|
||||
Node current = dest.getParentNode();
|
||||
while (current != null) {
|
||||
current
|
||||
}
|
||||
arc = predecessorArcs[arc.getOrigin().getId()];
|
||||
}
|
||||
|
||||
return solution;
|
||||
}
|
||||
|
|
|
@ -17,7 +17,7 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
|
|||
private int currentSize;
|
||||
|
||||
// The heap array, which can be larger than currentSize.
|
||||
protected final ArrayList<E> array;
|
||||
public final ArrayList<E> array;
|
||||
|
||||
/**
|
||||
* Construct a new empty binary heap.
|
||||
|
|
Loading…
Reference in a new issue