Класс IndexMaxPQ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; namespace PriorityQueues { class IndexMaxPQ<Key> : IEnumerable<Key> //Maximum-oriented indexed PQ implementation using a binary heap. { private int n; // number of elements on PQ private int[] pq; // binary heap using 1-based indexing private int[] qp; // inverse of pq - qp[pq[i]] = pq[qp[i]] = i private Key[] keys; // keys[i] = priority of i private Comparer<Key> comparer; // Initializes an empty indexed priority queue with indices between 0 and maxN-1 public IndexMaxPQ(int maxN, Comparer<Key> comparer) { if (maxN < 0) throw new ArgumentException(); n = 0; keys = new Key[maxN + 1]; // make this of length maxN?? pq = new int[maxN + 1]; qp = new int[maxN + 1]; // make this of length maxN?? for (int i = 0; i <= maxN; i++) qp[i] = -1; this.comparer = comparer; } // IsEmpty() public bool IsEmpty() { return n == 0; } // Contains public bool Contains(int i) { return qp[i] != -1; } // Size public int Size() { return n; } // public int MaxIndex() { if (n == 0) throw new ArgumentException("Priority queue underflow"); return pq[1]; } public Key MaxKey() { if (n == 0) throw new ArgumentException("Priority queue underflow"); return keys[pq[1]]; } // Removes a maximum key and returns its associated index. public void DelMax(out int index, out Key key) { if (n == 0) throw new ArgumentException("Priority queue underflow"); int min = pq[1]; key = keys[min]; index = min; Exch(1, n--); Sink(1); Debug.Assert(pq[n + 1] == min); qp[min] = -1; // delete keys[min] = default(Key); // to help with garbage collection pq[n + 1] = -1; // not needed //return min; } // Associate key with index i public void Insert(int i, Key key) { if (Contains(i)) throw new ArgumentException("index is already in the priority queue"); n++; qp[i] = n; pq[n] = i; keys[i] = key; Swim(n); } // public Key KeyOf(int i) { if (!Contains(i)) throw new ArgumentException("index is not in the priority queue"); else return keys[i]; } // public void ChangeKey(int i, Key key) { if (!Contains(i)) throw new ArgumentException("index is not in the priority queue"); keys[i] = key; Swim(qp[i]); Sink(qp[i]); } // Increase the key associated with index {@code i} to the specified value. public void IncreaseKey(int i, Key key) { if (!Contains(i)) throw new ArgumentException("index is not in the priority queue"); //if (keys[i].compareTo(key) >= 0) if (comparer.Compare(keys[i], key) < 0) throw new ArgumentException("Calling increaseKey() with given argument would not strictly increase the key"); keys[i] = key; Swim(qp[i]); } // Decrease the key associated with index {@code i} to the specified value. public void DecreaseKey(int i, Key key) { if (!Contains(i)) throw new ArgumentException("index is not in the priority queue"); if (comparer.Compare(keys[i], key) > 0) throw new ArgumentException("Calling decreaseKey() with given argument would not strictly decrease the key"); keys[i] = key; Sink(qp[i]); } // Remove the key on the priority queue associated with index public void Delete(int i) { if (!Contains(i)) throw new ArgumentException("index is not in the priority queue"); int index = qp[i]; Exch(index, n--); Swim(index); Sink(index); keys[i] = default(Key); qp[i] = -1; } /*************************************************************************** * General helper functions. ***************************************************************************/ private bool Less(int i, int j) { // return keys[pq[i]].CompareTo(keys[pq[j]]) < 0; return comparer.Compare(keys[pq[i]], keys[pq[j]]) < 0; } private void Exch(int i, int j) { int swap = pq[i]; pq[i] = pq[j]; pq[j] = swap; qp[pq[i]] = i; qp[pq[j]] = j; } /*************************************************************************** * Heap helper functions. ***************************************************************************/ private void Swim(int k) { while (k > 1 && Less(k / 2, k)) { Exch(k, k / 2); k = k / 2; } } private void Sink(int k) { while (2 * k <= n) { int j = 2 * k; if (j < n && Less(j, j + 1)) j++; if (!Less(k, j)) break; Exch(k, j); k = j; } } /* public int CompareTo(Key other) { throw new NotImplementedException(); } */ public IEnumerator<Key> GetEnumerator() { return new HeapIterator<Key>(comparer, Size(), n, keys); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } private class HeapIterator<Key> : IEnumerator<Key> { public Key[] Keys { get; set; } // // store items at indices 1 to n, 0 not use.. public int Size { get; set; } public int N { get; set; } public Comparer<Key> Comparer { get; set; } // private int position = -1; private MaxPQ<Key> copy; public HeapIterator() { if (Comparer == null) copy = new MaxPQ<Key>(Size); else copy = new MaxPQ<Key>(Size, Comparer); for (int i = 0; i < N; N++) copy.Insert(Keys[i]); } public HeapIterator(Comparer<Key> Comparer, int Size, int N, Key[] keys) { this.Comparer = Comparer; this.Size = Size; this.N = N; this.Keys = keys; if (Comparer == null) copy = new MaxPQ<Key>(Size); else copy = new MaxPQ<Key>(Size, Comparer); for (int i = 0; i <= N; i++) copy.Insert(this.Keys[i]); } public bool HasNext() { return !copy.IsEmpty(); } public void Remove() { throw new ArgumentException("error"); } public Key Next() { if (!HasNext()) throw new ArgumentException("error"); return copy.DelMax(); } public void Dispose() { //throw new NotImplementedException(); } public bool MoveNext() { position++; //return !copy.IsEmpty(); return position < copy.Size(); } public void Reset() { position = -1; } // object IEnumerator.Current { get { return Current; } } public Key Current { get { try { // return PQ[position]; return Keys[position]; } catch (IndexOutOfRangeException) { throw new InvalidOperationException(); } } } } } } |
Класс IndexMinPQ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; namespace PriorityQueues { class IndexMinPQ<Key> : IEnumerable<Key> //Minimum-oriented indexed PQ implementation using a binary heap. { private int maxN; // max number of elements in PQ private int n; // number of elements on PQ private int[] pq; // binary heap using 1-based indexing private int[] qp; // inverse of pq - qp[pq[i]] = pq[qp[i]] = i private Key[] keys; // keys[i] = priority of i private Comparer<Key> comparer; // Initializes an empty indexed priority queue with indices between 0 and maxN-1 public IndexMinPQ(int maxN, Comparer<Key> comparer) { this.maxN = maxN; if (maxN < 0) throw new ArgumentException(); n = 0; keys = new Key[maxN + 1]; // make this of length maxN?? pq = new int[maxN + 1]; qp = new int[maxN + 1]; // make this of length maxN?? for (int i = 0; i <= maxN; i++) qp[i] = -1; this.comparer = comparer; } // IsEmpty() public bool IsEmpty() { return n == 0; } // Contains public bool Contains(int i) { if (i < 0 || i >= maxN) throw new ArgumentException("wrong index"); return qp[i] != -1; } // Size public int Size() { return n; } // public int MinIndex() { if (n == 0) throw new ArgumentException("Priority queue underflow"); return pq[1]; } public Key MinKey() { if (n == 0) throw new ArgumentException("Priority queue underflow"); return keys[pq[1]]; } // Removes a maximum key and returns its associated index. public void DelMin(out int index, out Key key) { if (n == 0) throw new ArgumentException("Priority queue underflow"); int min = pq[1]; key = keys[min]; index = min; Exch(1, n--); Sink(1); Debug.Assert(pq[n + 1] == min); qp[min] = -1; // delete keys[min] = default(Key); // to help with garbage collection pq[n + 1] = -1; // not needed //return min; } // Associate key with index i public void Insert(int i, Key key) { if (i < 0 || i > maxN) throw new ArgumentException("Wrong index"); if (Contains(i)) throw new ArgumentException("index is already in the priority queue"); n++; qp[i] = n; pq[n] = i; keys[i] = key; Swim(n); } // public Key KeyOf(int i) { if (i < 0 || i > maxN) throw new ArgumentException("Wrong index"); if (!Contains(i)) throw new ArgumentException("index is not in the priority queue"); else return keys[i]; } // public void ChangeKey(int i, Key key) { if (i < 0 || i > maxN) throw new ArgumentException("Wrong index"); if (!Contains(i)) throw new ArgumentException("index is not in the priority queue"); keys[i] = key; Swim(qp[i]); Sink(qp[i]); } // Increase the key associated with index {@code i} to the specified value. public void IncreaseKey(int i, Key key) { if (i < 0 || i > maxN) throw new ArgumentException("Wrong index"); if (!Contains(i)) throw new ArgumentException("index is not in the priority queue"); //if (keys[i].compareTo(key) >= 0) if (comparer.Compare(keys[i], key) < 0) throw new ArgumentException("Calling increaseKey() with given argument would not strictly increase the key"); keys[i] = key; Sink(qp[i]); } // Decrease the key associated with index {@code i} to the specified value. public void DecreaseKey(int i, Key key) { if (i < 0 || i > maxN) throw new ArgumentException("Wrong index"); if (!Contains(i)) throw new ArgumentException("index is not in the priority queue"); if (comparer.Compare(keys[i], key) > 0) throw new ArgumentException("Calling decreaseKey() with given argument would not strictly decrease the key"); keys[i] = key; Swim(qp[i]); } // Remove the key on the priority queue associated with index public void Delete(int i) { if (i < 0 || i > maxN) throw new ArgumentException("Wrong index"); if (!Contains(i)) throw new ArgumentException("index is not in the priority queue"); int index = qp[i]; Exch(index, n--); Swim(index); Sink(index); keys[i] = default(Key); qp[i] = -1; } /*************************************************************************** * General helper functions. ***************************************************************************/ private bool Greater(int i, int j) { return comparer.Compare(keys[pq[i]], keys[pq[j]]) > 0; } private void Exch(int i, int j) { int swap = pq[i]; pq[i] = pq[j]; pq[j] = swap; qp[pq[i]] = i; qp[pq[j]] = j; } /*************************************************************************** * Heap helper functions. ***************************************************************************/ private void Swim(int k) { while (k > 1 && Greater(k / 2, k)) { Exch(k, k / 2); k = k / 2; } } private void Sink(int k) { while (2 * k <= n) { int j = 2 * k; if (j < n && Greater(j, j + 1)) j++; if (!Greater(k, j)) break; Exch(k, j); k = j; } } public int CompareTo(Key other) { throw new NotImplementedException(); } public IEnumerator<Key> GetEnumerator() { return new HeapIterator<Key>(comparer, Size(), n, keys); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } private class HeapIterator<Key> : IEnumerator<Key> { public Key[] Keys { get; set; } // // store items at indices 1 to n, 0 not use.. public int Size { get; set; } public int N { get; set; } public Comparer<Key> Comparer { get; set; } // private int position = -1; private MaxPQ<Key> copy; public HeapIterator() { if (Comparer == null) copy = new MaxPQ<Key>(Size); else copy = new MaxPQ<Key>(Size, Comparer); for (int i = 0; i < N; N++) copy.Insert(Keys[i]); } public HeapIterator(Comparer<Key> Comparer, int Size, int N, Key[] keys) { this.Comparer = Comparer; this.Size = Size; this.N = N; this.Keys = keys; if (Comparer == null) copy = new MaxPQ<Key>(Size); else copy = new MaxPQ<Key>(Size, Comparer); for (int i = 0; i <= N; i++) copy.Insert(this.Keys[i]); } public bool HasNext() { return !copy.IsEmpty(); } public void Remove() { throw new ArgumentException("error"); } public Key Next() { if (!HasNext()) throw new ArgumentException("error"); return copy.DelMax(); } public void Dispose() { //throw new NotImplementedException(); } public bool MoveNext() { position++; return position < copy.Size(); } public void Reset() { position = -1; } // object IEnumerator.Current { get { return Current; } } public Key Current { get { try { return Keys[position]; } catch (IndexOutOfRangeException) { throw new InvalidOperationException(); } } } } } } |
Клиент тестирования
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
// --- INDEXED Priority Queue Max IndexMaxPQ<string> iMaxPQ = new IndexMaxPQ<string>(11,comparer); iMaxPQ.Insert(0,"S"); iMaxPQ.Insert(1,"T"); iMaxPQ.Insert(2,"R"); iMaxPQ.Insert(3,"P"); iMaxPQ.Insert(4,"N"); iMaxPQ.Insert(5,"O"); iMaxPQ.Insert(6,"A"); iMaxPQ.Insert(7,"E"); iMaxPQ.Insert(8,"I"); iMaxPQ.Insert(9,"H"); iMaxPQ.Insert(10,"G"); Console.WriteLine(" "); Console.WriteLine("--- INDEXED Priority Queue Max"); Console.WriteLine("maxIndex "+iMaxPQ.MaxIndex()); Console.WriteLine("maxKey " + iMaxPQ.MaxKey()); Console.WriteLine("keyOf(0) " + iMaxPQ.KeyOf(0)); while (!iMaxPQ.IsEmpty()) { iMaxPQ.DelMax(out int i,out string s); Console.Write(i + ":" + s + " "); } //--- INDEXED Priority Queue Min IndexMinPQ<string> iMinPQ = new IndexMinPQ<string>(11, comparer); iMinPQ.Insert(0, "S"); iMinPQ.Insert(1, "T"); iMinPQ.Insert(2, "R"); iMinPQ.Insert(3, "P"); iMinPQ.Insert(4, "N"); iMinPQ.Insert(5, "O"); iMinPQ.Insert(6, "A"); iMinPQ.Insert(7, "E"); iMinPQ.Insert(8, "I"); iMinPQ.Insert(9, "H"); iMinPQ.Insert(10, "G"); Console.WriteLine(" "); Console.WriteLine("--- INDEXED Priority Queue Min"); Console.WriteLine("minIndex " + iMinPQ.MinIndex()); Console.WriteLine("minKey " + iMinPQ.MinKey()); Console.WriteLine("keyOf(0) " + iMinPQ.KeyOf(0)); while (!iMinPQ.IsEmpty()) { iMinPQ.DelMin(out int i, out string s); Console.Write(i+":"+ s + " "); } Console.ReadLine(); |