added hashMap to the binaryHeap to bring complexity from n to log(n)

This commit is contained in:
Lacroix Raphael 2022-05-24 18:01:11 +02:00
parent d46ea7fcb4
commit f023a2ae70
2 changed files with 35 additions and 3 deletions

View file

@ -7,4 +7,5 @@
</list> </list>
</option> </option>
</component> </component>
<component name="ProjectRootManager" version="2" project-jdk-name="11" project-jdk-type="JavaSDK" />
</project> </project>

View file

@ -1,4 +1,5 @@
package org.insa.graphs.algorithm.utils; package org.insa.graphs.algorithm.utils;
import java.util.HashMap;
import java.util.ArrayList; import java.util.ArrayList;
@ -16,6 +17,9 @@ 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;
// a hash map for the element - index link
private HashMap<E, Integer> map;
// The heap array. // The heap array.
protected final ArrayList<E> array; protected final ArrayList<E> array;
@ -25,6 +29,7 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
public BinaryHeap() { public BinaryHeap() {
this.currentSize = 0; this.currentSize = 0;
this.array = new ArrayList<E>(); this.array = new ArrayList<E>();
this.map = new HashMap<E, Integer>();
} }
/** /**
@ -35,6 +40,7 @@ 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.map = new HashMap<E, Integer>(heap.map);
} }
/** /**
@ -50,6 +56,9 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
else { else {
this.array.set(index, value); this.array.set(index, value);
} }
// we need to add the correspondence between index - value
map.put(value,index);
} }
/** /**
@ -137,15 +146,36 @@ 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 {
// index of is not optimized but it's better than nothing. We will come back to improve it if we have time at the end /* LEGACY CODE :
int i = this.array.indexOf(x); Index of is not optimized but it's better than nothing. We will come back to improve it if we have time at the end
if (i == -1 || i > this.currentSize - 1) { int i = this.array.indexOf(x) */
int i = -1;
// check if it is in the hashMap
if (this.map.get(x) != null){
i = this.map.get(x);
} else {
throw new ElementNotFoundException(x);
}
// check if the value makes sense
if (i > this.currentSize - 1 || i == -1) {
throw new ElementNotFoundException(x); throw new ElementNotFoundException(x);
} }
E lastItem = this.array.get(--this.currentSize); E lastItem = this.array.get(--this.currentSize);
this.arraySet(i, lastItem); this.arraySet(i, lastItem);
this.percolateDown(i); this.percolateDown(i);
this.percolateUp(i); this.percolateUp(i);
// since we removed the element we need to make sure it's not referenced in the hashMap (hence putting it to -1)
// since usually (not sure for the case of java, depends on how collisions are handled) the deletion of a pair
// costs more than changing its value so could just set it to -1 but keeping it increases the chances of collisions
// We therefore chose to remove it
this.map.remove(x);
} }
@Override @Override
@ -161,6 +191,7 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
E lastItem = this.array.get(--this.currentSize); E lastItem = this.array.get(--this.currentSize);
this.arraySet(0, lastItem); this.arraySet(0, lastItem);
this.percolateDown(0); this.percolateDown(0);
this.map.remove(minItem);
return minItem; return minItem;
} }