Free cookie consent management tool by TermsFeed Policy Generator

source: branches/RoutePlanning/HeuristicLab.Algorithms.GraphRouting/3.3/PriorityQueues/BinaryHeapV2.cs @ 8640

Last change on this file since 8640 was 8541, checked in by spimming, 12 years ago

#1894: renamed heap implementations

File size: 2.9 KB
Line 
1
2using System;
3using System.Collections.Generic;
4using HeuristicLab.Algorithms.GraphRouting.Interfaces;
5namespace HeuristicLab.Algorithms.GraphRouting.PriorityQueues {
6  // implementation based on C++ version from Peter Sanders
7  // http://www.mpi-inf.mpg.de/~sanders/programs/spq/
8  public sealed class BinaryHeapV2<K, V> : IHeap<K, V> where K : IComparable {
9    private class KNElement {
10      public K Key { get; set; }
11      public V Value { get; set; }
12    }
13
14    private int capacity;
15    private K supremum;
16    private int size; // index of last used element
17    private KNElement[] data;
18
19    public BinaryHeapV2(K sup, K infimum, int cap) {
20      size = 0;
21      capacity = cap;
22      data = new KNElement[cap + 2];
23      for (int i = 0; i < cap + 2; i++) {
24        data[i] = new KNElement();
25      }
26      data[0].Key = infimum;
27      data[capacity + 1].Key = sup;
28      supremum = sup;
29      Reset();
30
31    }
32
33    public int Size {
34      get { return size; }
35    }
36
37    public KeyValuePair<K, V> PeekMin() {
38      if (size == 0) {
39        throw new InvalidOperationException("Heap is empty");
40      }
41      return new KeyValuePair<K, V>(data[1].Key, data[1].Value);
42    }
43
44    public K PeekMinKey() {
45      return data[1].Key;
46    }
47
48    public V PeekMinValue() {
49      return data[1].Value;
50    }
51
52    public void RemoveMin() {
53      int i;
54      int l;
55      int r;
56      int smallest;
57
58      if (size <= 0) {
59        throw new InvalidOperationException("Cannot delete on an empty queue");
60      }
61
62      data[1].Key = data[size].Key;
63      data[1].Value = data[size].Value;
64
65      data[size].Key = supremum;
66      size--;
67      i = 1;
68      for (; ; ) {
69        l = (i << 1);
70        r = (i << 1) + 1;
71        if ((l <= size) && (data[l].Key.CompareTo(data[i].Key) == -1)) {
72          smallest = l;
73        } else {
74          smallest = i;
75        }
76        if ((r <= size) && (data[r].Key.CompareTo(data[smallest].Key) == -1)) {
77          smallest = r;
78        }
79        if (smallest == i) break;
80        KNElement temp = data[i];
81        data[i] = data[smallest];
82        data[smallest] = temp;
83        i = smallest;
84      }
85    }
86
87    public void Insert(K key, V value) {
88      size++;
89      int hole = size;
90      while ((hole > 1) && (data[hole >> 1].Key.CompareTo(key) == 1)) {
91        data[hole].Key = data[hole >> 1].Key;
92        data[hole].Value = data[hole >> 1].Value;
93        hole = hole >> 1;
94      }
95      data[hole].Key = key;
96      data[hole].Value = value;
97    }
98
99    public void DecreaseKey(K key, V value) {
100      throw new NotImplementedException();
101    }
102
103    // reset size to 0 and fill data array with sentinels
104    private void Reset() {
105      size = 0;
106      K sup = supremum;
107      int cap = capacity;
108      for (int i = 1; i <= cap; i++) {
109        data[i].Key = sup;
110      }
111    }
112  }
113}
Note: See TracBrowser for help on using the repository browser.