Skip to content

Commit 314bc0c

Browse files
committed
Changes to Dijkstra and Binary heap as well as unit testing.
1 parent 934c0e9 commit 314bc0c

7 files changed

Lines changed: 481 additions & 376 deletions

File tree

Binary file not shown.

‎Numerics/.vs/Numerics/v17/.suo‎

5.5 KB
Binary file not shown.

‎Numerics/.vs/Numerics/v17/.wsuo‎

5 KB
Binary file not shown.

‎Numerics/Mathematics/Optimization/Dynamic/BinaryHeap.cs‎

Lines changed: 88 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,10 @@ public Node(float nodeWeight, int nodeIndex, T nodeValue)
6060
}
6161

6262
private readonly Node[] _heap;
63+
private readonly Dictionary<int, int> _positionMap = new();
64+
6365
private int _n = 0; // Number of nodes.
64-
private int _p = 0; // Parent Index
66+
//private int _p = 0; // Parent Index
6567

6668
public int Count => _n;
6769

@@ -71,30 +73,83 @@ public BinaryHeap(int heapSize)
7173
}
7274

7375
/// <summary>
74-
/// Add a node to the heap.
76+
/// Putting the new inem in the first vacant cell in the array.
77+
/// Then move it up in the heap based on its value compared to its parent.
7578
/// </summary>
76-
/// <param name="value">Node to add.</param>
77-
public void Add(Node value)
79+
/// <param name="i">Index of the node.</param>
80+
private void BubbleUp(int i)
81+
{
82+
while (i > 0)
83+
{
84+
int parent = (i - 1) / 2;
85+
if (_heap[i].Weight >= _heap[parent].Weight) break;
86+
87+
//Swap
88+
(_heap[i], _heap[parent]) = (_heap[parent], _heap[i]);
89+
90+
91+
_positionMap[_heap[i].Index] = i;
92+
_positionMap[_heap[parent].Index] = parent;
93+
i = parent;
94+
}
95+
}
96+
97+
/// <summary>
98+
/// Used in heap deletion. Compares the parent nodes with child nodes in subtree.
99+
/// </summary>
100+
/// <param name="i"></param>
101+
private void BubbleDown(int i)
78102
{
79-
_heap[_n] = value;
80-
_n += 1;
103+
while (true)
104+
{
105+
int left = 2 * i + 1;
106+
int right = 2 * i + 2;
107+
int smallest = i;
108+
109+
if (left <_n && _heap[left].Weight < _heap[smallest].Weight)
110+
smallest = left;
111+
if (right < _n && _heap[right].Weight < _heap[smallest].Weight)
112+
smallest = right;
113+
if (smallest == i) break;
114+
115+
(_heap[i], _heap[smallest]) = (_heap[smallest], _heap[i]);
116+
_positionMap[_heap[i].Index] = i;
117+
_positionMap[_heap[smallest].Index] = smallest;
81118

82-
//Push the node up the tree.
83-
if (_n <= 1) return;
84-
int i = _n - 1;
85-
_p = (i - 1) / 2; // parent index (use integer division).
86-
Node parentNode = _heap[_p];
87-
Node currentNode = value;
119+
i = smallest;
120+
}
121+
}
88122

89-
while (i > 0 && currentNode.Weight < parentNode.Weight)
123+
/// <summary>
124+
/// Updates the distance (priority) of a node if a shorter path is found.
125+
/// </summary>
126+
/// <param name="newNode"></param>
127+
public void DecreaseKey(Node newNode)
128+
{
129+
if(!_positionMap.TryGetValue(newNode.Index, out int position))
90130
{
91-
_heap[i] = parentNode;
92-
_heap[_p] = currentNode;
93-
i = _p;
94-
_p = (i - 1) / 2; // parent index (use integer division).
95-
parentNode = _heap[_p];
96-
currentNode = _heap[i];
131+
Add(newNode);
132+
return;
97133
}
134+
if (newNode.Weight >= _heap[position].Weight) return; // No need to decrease key if the new weight is not smaller.
135+
136+
_heap[position] = newNode;
137+
BubbleUp(position);
138+
}
139+
140+
/// <summary>
141+
/// Add a node to the heap.
142+
/// </summary>
143+
/// <param name="value">Node to add.</param>
144+
public void Add(Node node)
145+
{
146+
if (_n >= _heap.Length)
147+
throw new InvalidOperationException("Heap is full.");
148+
149+
_heap[_n] = node;
150+
_positionMap[node.Index] = _n; // Map the index to the position in the heap array (for Replace method)
151+
BubbleUp(_n);
152+
_n++;
98153
}
99154

100155
/// <summary>
@@ -103,76 +158,21 @@ public void Add(Node value)
103158
/// <returns></returns>
104159
public Node RemoveMin()
105160
{
106-
Node result = _heap[0];
107-
108-
//Push the empty node down the tree then replace with last node in array
109-
int r;
110-
int l;
111-
int i = 0;
112-
int pIndex = 0;
113-
Node lNode;
114-
Node rNode;
115-
do
116-
{
117-
r = 2 * i + 2; //right child index
118-
l = r - 1; //left child index
119-
if (r < _n)
120-
{
121-
lNode = _heap[l];
122-
rNode = _heap[r];
123-
if (lNode.Weight < rNode.Weight)
124-
{
125-
_heap[i] = lNode;
126-
pIndex = i;
127-
i = l;
128-
}
129-
else
130-
{
131-
_heap[i] = rNode;
132-
pIndex = i;
133-
i = r;
134-
}
135-
}
136-
else
137-
{
138-
if (l < _n)
139-
{
140-
_heap[i] = _heap[l];
141-
pIndex = i;
142-
i = l;
143-
}
144-
else
145-
{
146-
break;
147-
}
148-
}
149-
} while (i >= 0);
150-
//
151-
_n -= 1;
152-
//Replace with last node in array and push up.
153-
if (i != _n)
161+
if (_n == 0)
162+
throw new InvalidOperationException("Heap is empty.");
163+
164+
Node min = _heap[0];
165+
_positionMap.Remove(min.Index);
166+
167+
_n--;
168+
169+
if (_n > 0)
154170
{
155-
_heap[i] = _heap[_n];
156-
if (_heap[pIndex].Weight > _heap[i].Weight)
157-
{
158-
//Push the node up.
159-
if (i <= 0) return result;
160-
_p = pIndex;
161-
Node parentNode = _heap[_p];
162-
Node currentNode = _heap[i];
163-
while (i > 0 && currentNode.Weight < parentNode.Weight)
164-
{
165-
_heap[i] = parentNode;
166-
_heap[_p] = currentNode;
167-
i = _p;
168-
_p = (i - 1) / 2; // parent index (use integer division).
169-
parentNode = _heap[_p];
170-
currentNode = _heap[i];
171-
}
172-
}
171+
_heap[0] = _heap[_n];
172+
_positionMap[_heap[0].Index] = 0; // Update position map
173+
BubbleDown(0);
173174
}
174-
//
175-
return result;
175+
return min;
176176
}
177177

178178
/// <summary>
@@ -186,20 +186,7 @@ public void Replace(Node newNode)
186186
if (_heap[i].Index == newNode.Index)
187187
{
188188
_heap[i] = newNode;
189-
//Push the node up the tree
190-
if (i <= 0) return;
191-
_p = (i - 1) / 2; // parent index (use integer division).
192-
Node parentNode = _heap[_p];
193-
Node currentNode = newNode;
194-
while (i > 0 && currentNode.Weight < parentNode.Weight)
195-
{
196-
_heap[i] = parentNode;
197-
_heap[_p] = currentNode;
198-
i = _p;
199-
_p = (i - 1) / 2;
200-
parentNode = _heap[_p];
201-
currentNode = _heap[i];
202-
}
189+
BubbleUp(i);
203190
break;
204191
}
205192
}

0 commit comments

Comments
 (0)