label.getCost + tas.isValid
This commit is contained in:
parent
10320f4a49
commit
5ef3259735
3 changed files with 53 additions and 20 deletions
|
@ -27,14 +27,14 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
|||
final int nbNodes = graph.size();
|
||||
|
||||
// Create the heap and a static array to store the labels
|
||||
org.insa.graphs.algorithm.utils.PriorityQueue<Label> labelsHeap = new BinaryHeap<>();
|
||||
org.insa.graphs.algorithm.utils.BinaryHeap<Label> labelsHeap = new BinaryHeap<>();
|
||||
Label labelsList[] = new Label[nbNodes];
|
||||
Arrays.fill(labelsList, null);
|
||||
|
||||
//Put the origin in the heap
|
||||
//Put the origin in the heap and in the list
|
||||
Node origin = data.getOrigin();
|
||||
labelsList[origin.getId()] = new Label(data.getOrigin(), 0, null);
|
||||
labelsHeap.insert(new Label(origin, 0, null));
|
||||
labelsList[origin.getId()] = new Label(origin, 0, null);
|
||||
labelsHeap.insert(labelsList[origin.getId()]);
|
||||
|
||||
// Notify observers about the first event (origin processed).
|
||||
notifyOriginProcessed(data.getOrigin());
|
||||
|
@ -49,6 +49,10 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
|||
while (!labelsHeap.isEmpty()
|
||||
&& (labelsList[data.getDestination().getId()] == null || !labelsList[data.getDestination().getId()].isMarked() )) {
|
||||
|
||||
// if(!labelsHeap.isValid()) {
|
||||
// System.out.println("arbre non valide");
|
||||
// }
|
||||
|
||||
// Remove the min
|
||||
current = labelsHeap.findMin();
|
||||
//System.out.println("cout :"+current.getCost());
|
||||
|
@ -59,8 +63,9 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
|||
}
|
||||
|
||||
current.setMark();
|
||||
notifyNodeMarked(current.getCurrent());
|
||||
|
||||
// Iterate over the arc of the removed element
|
||||
// Iterate over the arcs of the removed element
|
||||
for (Arc arc : graph.get(current.getCurrent().getId()).getSuccessors()) {
|
||||
if (!data.isAllowed(arc)) {
|
||||
continue;
|
||||
|
@ -69,14 +74,14 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
|||
//System.out.println("origine : "+arc.getOrigin().getId()+" destination : "+ arc.getDestination().getId());
|
||||
next = labelsList[arc.getDestination().getId()];
|
||||
|
||||
//If the destination of an arc does not exist or is not marked
|
||||
//If the destination of an arc does not exist in the list or is not marked
|
||||
if ( next != null && next.isMarked()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Either create it or check if the associated cost can be reduced
|
||||
if (next == null) {
|
||||
next = new Label(arc.getDestination(),current.getCost() + data.getCost(arc), arc);
|
||||
next = new Label(arc.getDestination(), current.getCost() + data.getCost(arc), arc);
|
||||
|
||||
labelsList[arc.getDestination().getId()] = next;
|
||||
labelsHeap.insert(next);
|
||||
|
@ -88,6 +93,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
|||
next.setFather(arc);
|
||||
}
|
||||
}
|
||||
|
||||
notifyNodeReached(arc.getDestination());
|
||||
|
||||
}
|
||||
|
@ -112,7 +118,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
|||
arc = labelsList[arc.getOrigin().getId()].getFather();
|
||||
}
|
||||
|
||||
// Reverse the path...
|
||||
// Reverse the arcs' path...
|
||||
Collections.reverse(arcs);
|
||||
|
||||
// Create the final solution.
|
||||
|
|
|
@ -2,7 +2,6 @@ package org.insa.graphs.algorithm.shortestpath;
|
|||
|
||||
import org.insa.graphs.model.Arc;
|
||||
import org.insa.graphs.model.Node;
|
||||
import org.insa.graphs.model.Path;
|
||||
|
||||
public class Label implements Comparable<Label>{
|
||||
private Node sommetCourant;
|
||||
|
@ -45,10 +44,16 @@ public class Label implements Comparable<Label>{
|
|||
|
||||
@Override
|
||||
public String toString() {
|
||||
return ""+this.sommetCourant.getId();
|
||||
return ""+this.getCost();
|
||||
}
|
||||
@Override
|
||||
public int compareTo(Label o) {
|
||||
return (int) (this.getCost()-o.getCost() ) ;
|
||||
if( (this.getCost()-o.getCost() ) < 0) {
|
||||
return -1;
|
||||
}else if( (this.getCost()-o.getCost() ) > 0) {
|
||||
return 1;
|
||||
}else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,7 +17,6 @@ import java.util.TreeSet;
|
|||
*/
|
||||
|
||||
|
||||
|
||||
public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
|
||||
|
||||
// Number of elements in heap.
|
||||
|
@ -26,12 +25,14 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
|
|||
// The heap array.
|
||||
protected final ArrayList<E> array;
|
||||
|
||||
|
||||
/**
|
||||
* Construct a new empty binary heap.
|
||||
*/
|
||||
public BinaryHeap() {
|
||||
this.currentSize = 0;
|
||||
this.array = new ArrayList<E>();
|
||||
// this.indexArray = new Couple[70];
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -60,6 +61,7 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
|
|||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return Index of the parent of the given index.
|
||||
*/
|
||||
|
@ -81,7 +83,6 @@ 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)) {
|
||||
|
@ -110,16 +111,22 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
|
|||
if (!hasRight || left.compareTo(right) < 0) {
|
||||
// Left is smaller
|
||||
if (left.compareTo(current) < 0) {
|
||||
|
||||
this.arraySet(index, left);
|
||||
|
||||
this.arraySet(ileft, current);
|
||||
|
||||
this.percolateDown(ileft);
|
||||
}
|
||||
}
|
||||
else {
|
||||
// Right is smaller
|
||||
if (right.compareTo(current) < 0) {
|
||||
|
||||
this.arraySet(index, right);
|
||||
|
||||
this.arraySet(iright, current);
|
||||
|
||||
this.percolateDown(iright);
|
||||
}
|
||||
}
|
||||
|
@ -151,13 +158,12 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
|
|||
public void remove(E x) throws ElementNotFoundException {
|
||||
|
||||
|
||||
if(this.isEmpty()){
|
||||
if(this.isEmpty() || x == null){
|
||||
throw new ElementNotFoundException(x);
|
||||
}
|
||||
|
||||
int index = -1;
|
||||
|
||||
//Iterate over the array and check if the x exists
|
||||
// Iterate over the array and check if the x exists
|
||||
for(int i=0; i<this.currentSize;i++) {
|
||||
if(this.array.get(i).equals(x)) {
|
||||
index = i;
|
||||
|
@ -165,10 +171,13 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
|
|||
}
|
||||
}
|
||||
|
||||
if(index ==-1) {
|
||||
|
||||
if(index == -1) {
|
||||
throw new ElementNotFoundException(x);
|
||||
}
|
||||
|
||||
|
||||
|
||||
//If it exists, it is replaced by the "last" element
|
||||
E lastItem = this.array.get(--this.currentSize);
|
||||
this.arraySet(index, lastItem);
|
||||
|
@ -194,6 +203,19 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
|
|||
return minItem;
|
||||
}
|
||||
|
||||
|
||||
// public boolean isValid() {
|
||||
//
|
||||
// for(int i=1; i<this.size(); i++) {
|
||||
// E current = this.array.get(i);
|
||||
// E parent = this.array.get(this.indexParent(i));
|
||||
// if( current.compareTo(parent) < 0) {
|
||||
// return false;
|
||||
// }
|
||||
// }
|
||||
// return true;
|
||||
// }
|
||||
|
||||
/**
|
||||
* Creates a multi-lines string representing a sorted view of this binary heap.
|
||||
*
|
||||
|
|
Loading…
Reference in a new issue