Compare commits
3 commits
db095f9ea9
...
539ea33752
Author | SHA1 | Date | |
---|---|---|---|
539ea33752 | |||
460456581e | |||
a2db6d26a5 |
5 changed files with 45 additions and 41 deletions
|
@ -56,7 +56,10 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
|||
|
||||
Node destination = data.getDestination();
|
||||
Node currentNode = null;
|
||||
|
||||
// Debug / Tests
|
||||
double previousCost = 0;
|
||||
int iterationCounter = 0;
|
||||
|
||||
while (!heap.isEmpty() && (currentNode == null || !currentNode.equals(destination))) {
|
||||
// Get the node with the minimum cost
|
||||
|
@ -65,11 +68,12 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
|||
currentNode = currentLabel.getNode();
|
||||
|
||||
/*
|
||||
// Debug
|
||||
// Debug / Tests
|
||||
assert(((BinaryHeap<Label>) heap).isValid());
|
||||
assert(currentLabel.getTotalCost() >= previousCost);
|
||||
previousCost = currentLabel.getCost();
|
||||
*/
|
||||
iterationCounter++;
|
||||
|
||||
for (Arc arc : currentNode.getSuccessors()) {
|
||||
Node dest = arc.getDestination();
|
||||
|
@ -111,6 +115,8 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
|||
currentLabel = labels.get(arc.getOrigin().getId());
|
||||
}
|
||||
|
||||
System.out.printf("Nombre de sommets explorés : %d\n", iterationCounter);
|
||||
|
||||
Collections.reverse(path);
|
||||
|
||||
return new ShortestPathSolution(data, AbstractSolution.Status.OPTIMAL, new Path(data.getGraph(), path));
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
package org.insa.graphs.algorithm.utils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Implements a binary heap containing elements of type E.
|
||||
*
|
||||
* <p>
|
||||
* Note that all comparisons are based on the compareTo method, hence E must
|
||||
* implement Comparable
|
||||
*
|
||||
|
@ -15,6 +17,7 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
|
|||
|
||||
// Number of elements in heap.
|
||||
private int currentSize;
|
||||
private Map<E, Integer> elementIndexes = new HashMap<>();
|
||||
|
||||
// The heap array.
|
||||
protected final ArrayList<E> array;
|
||||
|
@ -35,10 +38,12 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
|
|||
public BinaryHeap(BinaryHeap<E> heap) {
|
||||
this.currentSize = heap.currentSize;
|
||||
this.array = new ArrayList<E>(heap.array);
|
||||
this.elementIndexes = new HashMap<>(heap.elementIndexes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if each element is lower than its children
|
||||
*
|
||||
* @return true if valid, false otherwise
|
||||
*/
|
||||
public boolean isValid() {
|
||||
|
@ -67,10 +72,10 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
|
|||
* @param value Element to set.
|
||||
*/
|
||||
private void arraySet(int index, E value) {
|
||||
elementIndexes.put(value, index);
|
||||
if (index == this.array.size()) {
|
||||
this.array.add(value);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
this.array.set(index, value);
|
||||
}
|
||||
}
|
||||
|
@ -104,9 +109,7 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
|
|||
private void percolateUp(int index) {
|
||||
E x = this.array.get(index);
|
||||
|
||||
for (; index > 0
|
||||
&& x.compareTo(this.array.get(indexParent(index))) < 0; index = indexParent(
|
||||
index)) {
|
||||
for (; index > 0 && x.compareTo(this.array.get(indexParent(index))) < 0; index = indexParent(index)) {
|
||||
E moving_val = this.array.get(indexParent(index));
|
||||
this.arraySet(index, moving_val);
|
||||
}
|
||||
|
@ -136,8 +139,7 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
|
|||
this.arraySet(ileft, current);
|
||||
this.percolateDown(ileft);
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
// Right is smaller
|
||||
if (right.compareTo(current) < 0) {
|
||||
this.arraySet(index, right);
|
||||
|
@ -167,22 +169,15 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
|
|||
|
||||
@Override
|
||||
public void remove(E x) throws ElementNotFoundException {
|
||||
int index = 0;
|
||||
// We avoid array.indexOf(x) because we don't need to search
|
||||
// through the whole list, we can stop at indexSize.
|
||||
boolean found = false;
|
||||
while(!found && index < currentSize) {
|
||||
if (array.get(index) == x)
|
||||
found =true;
|
||||
else
|
||||
index++;
|
||||
}
|
||||
|
||||
if (!found) throw new ElementNotFoundException(x);
|
||||
arraySet(index, array.get(--currentSize));
|
||||
Integer index = elementIndexes.remove(x);
|
||||
if (index == null) throw new ElementNotFoundException(x);
|
||||
currentSize--;
|
||||
if (currentSize != 0 && index != currentSize) {
|
||||
arraySet(index, array.get(currentSize));
|
||||
percolateUp(index);
|
||||
percolateDown(index);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public E findMin() throws EmptyPriorityQueueException {
|
||||
|
@ -194,9 +189,13 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
|
|||
@Override
|
||||
public E deleteMin() throws EmptyPriorityQueueException {
|
||||
E minItem = findMin();
|
||||
E lastItem = this.array.get(--this.currentSize);
|
||||
elementIndexes.remove(minItem);
|
||||
currentSize--;
|
||||
if (currentSize != 0) {
|
||||
E lastItem = this.array.get(this.currentSize);
|
||||
this.arraySet(0, lastItem);
|
||||
this.percolateDown(0);
|
||||
}
|
||||
return minItem;
|
||||
}
|
||||
|
||||
|
@ -214,7 +213,6 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
|
|||
*
|
||||
* @param maxElement Maximum number of elements to display. or {@code -1} to
|
||||
* display all the elements.
|
||||
*
|
||||
* @return a string containing a sorted view this binary heap.
|
||||
*/
|
||||
public String toStringSorted(int maxElement) {
|
||||
|
@ -234,7 +232,6 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
|
|||
* Creates a multi-lines string representing a tree view of this binary heap.
|
||||
*
|
||||
* @param maxDepth Maximum depth of the tree to display.
|
||||
*
|
||||
* @return a string containing a tree view of this binary heap.
|
||||
*/
|
||||
public String toStringTree(int maxDepth) {
|
||||
|
|
|
@ -4,7 +4,7 @@ package org.insa.graphs.algorithm.shortestpath;
|
|||
public class AStarAlgorithmTest extends ShortestPathTest {
|
||||
@Override
|
||||
protected ShortestPathSolution runShortestPathAlgo(ShortestPathData data) {
|
||||
return new AStarAlgorithm(data).doRun();
|
||||
return new AStarAlgorithm(data).run();
|
||||
}
|
||||
|
||||
}
|
|
@ -3,6 +3,6 @@ package org.insa.graphs.algorithm.shortestpath;
|
|||
public class DijkstraAlgorithmTest extends ShortestPathTest {
|
||||
@Override
|
||||
protected ShortestPathSolution runShortestPathAlgo(ShortestPathData data) {
|
||||
return new DijkstraAlgorithm(data).doRun();
|
||||
return new DijkstraAlgorithm(data).run();
|
||||
}
|
||||
}
|
|
@ -62,7 +62,7 @@ public abstract class ShortestPathTest {
|
|||
for (ArcInspector arcInspector : arcInspectors) {
|
||||
ShortestPathData data = createData(maps[i], arcInspector, origIndexes[i], destIndexes[i]);
|
||||
ShortestPathSolution sol = runShortestPathAlgo(data);
|
||||
ShortestPathSolution oracle = new BellmanFordAlgorithm(data).doRun();
|
||||
ShortestPathSolution oracle = new BellmanFordAlgorithm(data).run();
|
||||
assertTrue(sol.isFeasible());
|
||||
Path path = sol.getPath();
|
||||
assertTrue(path.isValid());
|
||||
|
@ -71,7 +71,8 @@ public abstract class ShortestPathTest {
|
|||
assertEquals(path, Path.createShortestPathFromNodes(data.getGraph(), path.getNodes()));
|
||||
if (i == 1)
|
||||
assertEquals(path, Path.createFastestPathFromNodes(data.getGraph(), path.getNodes()));
|
||||
// Check result against Bellman Ford algorithm (except for the square map or there may be several paths with the same cost)
|
||||
// Check result against Bellman Ford algorithm (except for the square map
|
||||
// where there may be several paths with the same cost)
|
||||
if (i != 0)
|
||||
assertEquals(sol.getPath(), oracle.getPath());
|
||||
System.out.println();
|
||||
|
|
Loading…
Reference in a new issue