label.getCost + tas.isValid

This commit is contained in:
Cavailles Kevin 2020-04-24 10:51:18 +02:00
parent 10320f4a49
commit 5ef3259735
3 changed files with 53 additions and 20 deletions

View file

@ -27,14 +27,14 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
final int nbNodes = graph.size(); final int nbNodes = graph.size();
// Create the heap and a static array to store the labels // 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]; Label labelsList[] = new Label[nbNodes];
Arrays.fill(labelsList, null); Arrays.fill(labelsList, null);
//Put the origin in the heap //Put the origin in the heap and in the list
Node origin = data.getOrigin(); Node origin = data.getOrigin();
labelsList[origin.getId()] = new Label(data.getOrigin(), 0, null); labelsList[origin.getId()] = new Label(origin, 0, null);
labelsHeap.insert(new Label(origin, 0, null)); labelsHeap.insert(labelsList[origin.getId()]);
// Notify observers about the first event (origin processed). // Notify observers about the first event (origin processed).
notifyOriginProcessed(data.getOrigin()); notifyOriginProcessed(data.getOrigin());
@ -49,6 +49,10 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
while (!labelsHeap.isEmpty() while (!labelsHeap.isEmpty()
&& (labelsList[data.getDestination().getId()] == null || !labelsList[data.getDestination().getId()].isMarked() )) { && (labelsList[data.getDestination().getId()] == null || !labelsList[data.getDestination().getId()].isMarked() )) {
// if(!labelsHeap.isValid()) {
// System.out.println("arbre non valide");
// }
// Remove the min // Remove the min
current = labelsHeap.findMin(); current = labelsHeap.findMin();
//System.out.println("cout :"+current.getCost()); //System.out.println("cout :"+current.getCost());
@ -59,8 +63,9 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
} }
current.setMark(); 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()) { for (Arc arc : graph.get(current.getCurrent().getId()).getSuccessors()) {
if (!data.isAllowed(arc)) { if (!data.isAllowed(arc)) {
continue; continue;
@ -69,14 +74,14 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
//System.out.println("origine : "+arc.getOrigin().getId()+" destination : "+ arc.getDestination().getId()); //System.out.println("origine : "+arc.getOrigin().getId()+" destination : "+ arc.getDestination().getId());
next = labelsList[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()) { if ( next != null && next.isMarked()) {
continue; continue;
} }
// Either create it or check if the associated cost can be reduced // Either create it or check if the associated cost can be reduced
if (next == null) { 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; labelsList[arc.getDestination().getId()] = next;
labelsHeap.insert(next); labelsHeap.insert(next);
@ -88,6 +93,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
next.setFather(arc); next.setFather(arc);
} }
} }
notifyNodeReached(arc.getDestination()); notifyNodeReached(arc.getDestination());
} }
@ -112,7 +118,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
arc = labelsList[arc.getOrigin().getId()].getFather(); arc = labelsList[arc.getOrigin().getId()].getFather();
} }
// Reverse the path... // Reverse the arcs' path...
Collections.reverse(arcs); Collections.reverse(arcs);
// Create the final solution. // Create the final solution.

View file

@ -2,7 +2,6 @@ package org.insa.graphs.algorithm.shortestpath;
import org.insa.graphs.model.Arc; import org.insa.graphs.model.Arc;
import org.insa.graphs.model.Node; import org.insa.graphs.model.Node;
import org.insa.graphs.model.Path;
public class Label implements Comparable<Label>{ public class Label implements Comparable<Label>{
private Node sommetCourant; private Node sommetCourant;
@ -45,10 +44,16 @@ public class Label implements Comparable<Label>{
@Override @Override
public String toString() { public String toString() {
return ""+this.sommetCourant.getId(); return ""+this.getCost();
} }
@Override @Override
public int compareTo(Label o) { 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;
}
} }
} }

View file

@ -17,7 +17,6 @@ import java.util.TreeSet;
*/ */
public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> { public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
// Number of elements in heap. // Number of elements in heap.
@ -26,12 +25,14 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
// The heap array. // The heap array.
protected final ArrayList<E> array; protected final ArrayList<E> array;
/** /**
* Construct a new empty binary heap. * Construct a new empty binary heap.
*/ */
public BinaryHeap() { public BinaryHeap() {
this.currentSize = 0; this.currentSize = 0;
this.array = new ArrayList<E>(); 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. * @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) { private void percolateUp(int index) {
E x = this.array.get(index); E x = this.array.get(index);
for (; index > 0 for (; index > 0
&& x.compareTo(this.array.get(indexParent(index))) < 0; index = indexParent( && x.compareTo(this.array.get(indexParent(index))) < 0; index = indexParent(
index)) { index)) {
@ -110,16 +111,22 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
if (!hasRight || left.compareTo(right) < 0) { if (!hasRight || left.compareTo(right) < 0) {
// Left is smaller // Left is smaller
if (left.compareTo(current) < 0) { if (left.compareTo(current) < 0) {
this.arraySet(index, left); this.arraySet(index, left);
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);
this.arraySet(iright, current); this.arraySet(iright, current);
this.percolateDown(iright); this.percolateDown(iright);
} }
} }
@ -151,13 +158,12 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
public void remove(E x) throws ElementNotFoundException { public void remove(E x) throws ElementNotFoundException {
if(this.isEmpty()){ if(this.isEmpty() || x == null){
throw new ElementNotFoundException(x); throw new ElementNotFoundException(x);
} }
int index = -1; 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++) { for(int i=0; i<this.currentSize;i++) {
if(this.array.get(i).equals(x)) { if(this.array.get(i).equals(x)) {
index = i; 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); throw new ElementNotFoundException(x);
} }
//If it exists, it is replaced by the "last" element //If it exists, it is replaced by the "last" element
E lastItem = this.array.get(--this.currentSize); E lastItem = this.array.get(--this.currentSize);
this.arraySet(index, lastItem); this.arraySet(index, lastItem);
@ -194,6 +203,19 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
return minItem; 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. * Creates a multi-lines string representing a sorted view of this binary heap.
* *