Browse Source

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

Lacroix Raphael 1 year ago
parent
commit
f023a2ae70

+ 1
- 0
.idea/misc.xml View File

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

+ 34
- 3
be-graphes-algos/src/main/java/org/insa/graphs/algorithm/utils/BinaryHeap.java View File

@@ -1,4 +1,5 @@
1 1
 package org.insa.graphs.algorithm.utils;
2
+import java.util.HashMap;
2 3
 
3 4
 import java.util.ArrayList;
4 5
 
@@ -16,6 +17,9 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
16 17
     // Number of elements in heap.
17 18
     private int currentSize;
18 19
 
20
+    // a hash map for the element - index link
21
+    private HashMap<E, Integer> map;
22
+
19 23
     // The heap array.
20 24
     protected final ArrayList<E> array;
21 25
 
@@ -25,6 +29,7 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
25 29
     public BinaryHeap() {
26 30
         this.currentSize = 0;
27 31
         this.array = new ArrayList<E>();
32
+        this.map = new HashMap<E, Integer>();
28 33
     }
29 34
 
30 35
     /**
@@ -35,6 +40,7 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
35 40
     public BinaryHeap(BinaryHeap<E> heap) {
36 41
         this.currentSize = heap.currentSize;
37 42
         this.array = new ArrayList<E>(heap.array);
43
+        this.map = new HashMap<E, Integer>(heap.map);
38 44
     }
39 45
 
40 46
     /**
@@ -50,6 +56,9 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
50 56
         else {
51 57
             this.array.set(index, value);
52 58
         }
59
+
60
+        // we need to add the correspondence between index - value
61
+        map.put(value,index);
53 62
     }
54 63
 
55 64
     /**
@@ -137,15 +146,36 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
137 146
 
138 147
     @Override
139 148
     public void remove(E x) throws ElementNotFoundException {
140
-    	// 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
141
-    	int i = this.array.indexOf(x);
142
-    	if (i == -1 || i > this.currentSize - 1) {
149
+    	/* LEGACY CODE :
150
+    	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
151
+    	int i = this.array.indexOf(x) */
152
+
153
+
154
+        int i = -1;
155
+
156
+        // check if it is in the hashMap
157
+        if (this.map.get(x) != null){
158
+            i = this.map.get(x);
159
+        } else {
160
+            throw new ElementNotFoundException(x);
161
+        }
162
+
163
+        // check if the value makes sense
164
+        if (i > this.currentSize - 1 || i == -1) {
143 165
     		throw new ElementNotFoundException(x);
144 166
     	}
145 167
         E lastItem = this.array.get(--this.currentSize);
146 168
         this.arraySet(i, lastItem);
147 169
         this.percolateDown(i);
148 170
         this.percolateUp(i);
171
+
172
+
173
+
174
+        // since we removed the element we need to make sure it's not referenced in the hashMap (hence putting it to -1)
175
+        // since usually (not sure for the case of java, depends on how collisions are handled) the deletion of a pair
176
+        // costs more than changing its value so could just set it to -1 but keeping it increases the chances of collisions
177
+        // We therefore chose to remove it
178
+        this.map.remove(x);
149 179
     }
150 180
 
151 181
     @Override
@@ -161,6 +191,7 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
161 191
         E lastItem = this.array.get(--this.currentSize);
162 192
         this.arraySet(0, lastItem);
163 193
         this.percolateDown(0);
194
+        this.map.remove(minItem);
164 195
         return minItem;
165 196
     }
166 197
 

Loading…
Cancel
Save