Compare commits

..

No commits in common. "539ea33752df774ebc8f891772171eb89441964f" and "db095f9ea970f42d544e84059d9f529473428b1d" have entirely different histories.

5 changed files with 41 additions and 45 deletions

View file

@ -56,10 +56,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
Node destination = data.getDestination(); Node destination = data.getDestination();
Node currentNode = null; Node currentNode = null;
// Debug / Tests
double previousCost = 0; double previousCost = 0;
int iterationCounter = 0;
while (!heap.isEmpty() && (currentNode == null || !currentNode.equals(destination))) { while (!heap.isEmpty() && (currentNode == null || !currentNode.equals(destination))) {
// Get the node with the minimum cost // Get the node with the minimum cost
@ -68,12 +65,11 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
currentNode = currentLabel.getNode(); currentNode = currentLabel.getNode();
/* /*
// Debug / Tests // Debug
assert(((BinaryHeap<Label>) heap).isValid()); assert(((BinaryHeap<Label>) heap).isValid());
assert(currentLabel.getTotalCost() >= previousCost); assert(currentLabel.getTotalCost() >= previousCost);
previousCost = currentLabel.getCost(); previousCost = currentLabel.getCost();
*/ */
iterationCounter++;
for (Arc arc : currentNode.getSuccessors()) { for (Arc arc : currentNode.getSuccessors()) {
Node dest = arc.getDestination(); Node dest = arc.getDestination();
@ -115,8 +111,6 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
currentLabel = labels.get(arc.getOrigin().getId()); currentLabel = labels.get(arc.getOrigin().getId());
} }
System.out.printf("Nombre de sommets explorés : %d\n", iterationCounter);
Collections.reverse(path); Collections.reverse(path);
return new ShortestPathSolution(data, AbstractSolution.Status.OPTIMAL, new Path(data.getGraph(), path)); return new ShortestPathSolution(data, AbstractSolution.Status.OPTIMAL, new Path(data.getGraph(), path));

View file

@ -1,12 +1,10 @@
package org.insa.graphs.algorithm.utils; package org.insa.graphs.algorithm.utils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
/** /**
* Implements a binary heap containing elements of type E. * Implements a binary heap containing elements of type E.
* <p> *
* Note that all comparisons are based on the compareTo method, hence E must * Note that all comparisons are based on the compareTo method, hence E must
* implement Comparable * implement Comparable
* *
@ -17,7 +15,6 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
// Number of elements in heap. // Number of elements in heap.
private int currentSize; private int currentSize;
private Map<E, Integer> elementIndexes = new HashMap<>();
// The heap array. // The heap array.
protected final ArrayList<E> array; protected final ArrayList<E> array;
@ -38,12 +35,10 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
public BinaryHeap(BinaryHeap<E> heap) { public BinaryHeap(BinaryHeap<E> heap) {
this.currentSize = heap.currentSize; this.currentSize = heap.currentSize;
this.array = new ArrayList<E>(heap.array); this.array = new ArrayList<E>(heap.array);
this.elementIndexes = new HashMap<>(heap.elementIndexes);
} }
/** /**
* Check if each element is lower than its children * Check if each element is lower than its children
*
* @return true if valid, false otherwise * @return true if valid, false otherwise
*/ */
public boolean isValid() { public boolean isValid() {
@ -72,10 +67,10 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
* @param value Element to set. * @param value Element to set.
*/ */
private void arraySet(int index, E value) { private void arraySet(int index, E value) {
elementIndexes.put(value, index);
if (index == this.array.size()) { if (index == this.array.size()) {
this.array.add(value); this.array.add(value);
} else { }
else {
this.array.set(index, value); this.array.set(index, value);
} }
} }
@ -109,7 +104,9 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
private void percolateUp(int index) { private void percolateUp(int index) {
E x = this.array.get(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)); E moving_val = this.array.get(indexParent(index));
this.arraySet(index, moving_val); this.arraySet(index, moving_val);
} }
@ -139,7 +136,8 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
this.arraySet(ileft, current); this.arraySet(ileft, current);
this.percolateDown(ileft); this.percolateDown(ileft);
} }
} else { }
else {
// Right is smaller // Right is smaller
if (right.compareTo(current) < 0) { if (right.compareTo(current) < 0) {
this.arraySet(index, right); this.arraySet(index, right);
@ -169,14 +167,21 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
@Override @Override
public void remove(E x) throws ElementNotFoundException { public void remove(E x) throws ElementNotFoundException {
Integer index = elementIndexes.remove(x); int index = 0;
if (index == null) throw new ElementNotFoundException(x); // We avoid array.indexOf(x) because we don't need to search
currentSize--; // through the whole list, we can stop at indexSize.
if (currentSize != 0 && index != currentSize) { boolean found = false;
arraySet(index, array.get(currentSize)); while(!found && index < currentSize) {
percolateUp(index); if (array.get(index) == x)
percolateDown(index); found =true;
else
index++;
} }
if (!found) throw new ElementNotFoundException(x);
arraySet(index, array.get(--currentSize));
percolateUp(index);
percolateDown(index);
} }
@Override @Override
@ -189,13 +194,9 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
@Override @Override
public E deleteMin() throws EmptyPriorityQueueException { public E deleteMin() throws EmptyPriorityQueueException {
E minItem = findMin(); E minItem = findMin();
elementIndexes.remove(minItem); E lastItem = this.array.get(--this.currentSize);
currentSize--; this.arraySet(0, lastItem);
if (currentSize != 0) { this.percolateDown(0);
E lastItem = this.array.get(this.currentSize);
this.arraySet(0, lastItem);
this.percolateDown(0);
}
return minItem; return minItem;
} }
@ -213,6 +214,7 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
* *
* @param maxElement Maximum number of elements to display. or {@code -1} to * @param maxElement Maximum number of elements to display. or {@code -1} to
* display all the elements. * display all the elements.
*
* @return a string containing a sorted view this binary heap. * @return a string containing a sorted view this binary heap.
*/ */
public String toStringSorted(int maxElement) { public String toStringSorted(int maxElement) {
@ -232,6 +234,7 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
* Creates a multi-lines string representing a tree view of this binary heap. * Creates a multi-lines string representing a tree view of this binary heap.
* *
* @param maxDepth Maximum depth of the tree to display. * @param maxDepth Maximum depth of the tree to display.
*
* @return a string containing a tree view of this binary heap. * @return a string containing a tree view of this binary heap.
*/ */
public String toStringTree(int maxDepth) { public String toStringTree(int maxDepth) {

View file

@ -4,7 +4,7 @@ package org.insa.graphs.algorithm.shortestpath;
public class AStarAlgorithmTest extends ShortestPathTest { public class AStarAlgorithmTest extends ShortestPathTest {
@Override @Override
protected ShortestPathSolution runShortestPathAlgo(ShortestPathData data) { protected ShortestPathSolution runShortestPathAlgo(ShortestPathData data) {
return new AStarAlgorithm(data).run(); return new AStarAlgorithm(data).doRun();
} }
} }

View file

@ -3,6 +3,6 @@ package org.insa.graphs.algorithm.shortestpath;
public class DijkstraAlgorithmTest extends ShortestPathTest { public class DijkstraAlgorithmTest extends ShortestPathTest {
@Override @Override
protected ShortestPathSolution runShortestPathAlgo(ShortestPathData data) { protected ShortestPathSolution runShortestPathAlgo(ShortestPathData data) {
return new DijkstraAlgorithm(data).run(); return new DijkstraAlgorithm(data).doRun();
} }
} }

View file

@ -62,7 +62,7 @@ public abstract class ShortestPathTest {
for (ArcInspector arcInspector : arcInspectors) { for (ArcInspector arcInspector : arcInspectors) {
ShortestPathData data = createData(maps[i], arcInspector, origIndexes[i], destIndexes[i]); ShortestPathData data = createData(maps[i], arcInspector, origIndexes[i], destIndexes[i]);
ShortestPathSolution sol = runShortestPathAlgo(data); ShortestPathSolution sol = runShortestPathAlgo(data);
ShortestPathSolution oracle = new BellmanFordAlgorithm(data).run(); ShortestPathSolution oracle = new BellmanFordAlgorithm(data).doRun();
assertTrue(sol.isFeasible()); assertTrue(sol.isFeasible());
Path path = sol.getPath(); Path path = sol.getPath();
assertTrue(path.isValid()); assertTrue(path.isValid());
@ -71,8 +71,7 @@ public abstract class ShortestPathTest {
assertEquals(path, Path.createShortestPathFromNodes(data.getGraph(), path.getNodes())); assertEquals(path, Path.createShortestPathFromNodes(data.getGraph(), path.getNodes()));
if (i == 1) if (i == 1)
assertEquals(path, Path.createFastestPathFromNodes(data.getGraph(), path.getNodes())); assertEquals(path, Path.createFastestPathFromNodes(data.getGraph(), path.getNodes()));
// Check result against Bellman Ford algorithm (except for the square map // Check result against Bellman Ford algorithm (except for the square map or there may be several paths with the same cost)
// where there may be several paths with the same cost)
if (i != 0) if (i != 0)
assertEquals(sol.getPath(), oracle.getPath()); assertEquals(sol.getPath(), oracle.getPath());
System.out.println(); System.out.println();