This repository has been archived by the owner on Nov 22, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bheap.go
214 lines (158 loc) · 3.63 KB
/
bheap.go
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
// Copyright © 2019 Developer Network, LLC
//
// This file is subject to the terms and conditions defined in
// file 'LICENSE', which is part of this source code package.
package graph
import (
"errors"
"fmt"
"sync"
)
type Heap struct {
internal []interface{}
// kv mapping of a node value to the node pointer
nodes map[interface{}]*Node
// kv mapping of a node value to it's index in the internal array
values map[interface{}]int
size int
once sync.Once
}
func (h *Heap) Print() {
for i := 0; i < h.size; i++ {
v := h.nodes[h.internal[i]]
fmt.Printf("index: %v | value: %v | cost: %v\n", i, v.Value, v.Cost)
}
}
func (h *Heap) init() {
h.once.Do(func() {
h.internal = make([]interface{}, 0)
h.nodes = make(map[interface{}]*Node)
h.values = make(map[interface{}]int)
})
}
func (h *Heap) Swap(i, j int) {
h.init()
iv := h.internal[i]
jv := h.internal[j]
// Swap the internal values for the dictionary
h.internal[i] = jv
h.values[jv] = i
h.internal[j] = iv
h.values[iv] = j
}
func (h *Heap) Up(i int) {
h.init()
if h.size > 0 {
j := 0
if i > 1 {
j = i / 2 // Parent of i
}
if h.nodes[h.internal[i]].Cost < h.nodes[h.internal[j]].Cost {
h.Swap(i, j)
h.Up(j)
}
}
}
func (h *Heap) Down(i int) {
h.init()
if h.size > 0 {
var j int
if 2*i <= h.size {
if 2*i < h.size {
left := 2 * i
right := (2 * i) + 1
// set j as left, unless right is lower cost
j = left
if h.nodes[h.internal[right]] != nil && h.nodes[h.internal[right]].Cost < h.nodes[h.internal[left]].Cost {
j = right
}
} else {
j = 2 * i
}
if h.nodes[h.internal[j]] != nil {
if h.nodes[h.internal[i]] == nil || h.nodes[h.internal[j]].Cost < h.nodes[h.internal[i]].Cost {
h.Swap(i, j)
h.Down(j)
}
}
}
}
}
func (h *Heap) Insert(n *Node) (err error) {
h.init()
// Set the dictionary value
h.nodes[n.Value] = n
// Add the value to the internal array
h.internal = append(h.internal, n.Value)
// Increment the size
h.size++
// add the map entry for the new element's index
h.values[n.Value] = h.size - 1
// Heap Up
h.Up(h.size - 1)
return err
}
func (h *Heap) Min() (index int, err error) {
h.init()
if h.size > 0 {
// root node
index = 0
} else {
// empty heap
err = errors.New("empty heap")
}
return index, err
}
func (h *Heap) ExtractMin() (min *Node, err error) {
h.init()
var mini int
if mini, err = h.Min(); err == nil {
min = h.nodes[h.internal[mini]]
h.Delete(mini)
}
return min, err
}
func (h *Heap) Delete(i int) (err error) {
h.init()
// Clean up maps
v := h.internal[i]
delete(h.nodes, v)
delete(h.values, v)
// Delete an entry from the internal slice while
// maintaining it's ordering
//copy(h.internal[i:], h.internal[i+1:])
for index := i + 1; index < h.size; index++ {
// Shift cells up the array
v := h.internal[index]
h.internal[index-1] = v
h.values[v] = index - 1
h.internal[index] = nil
}
// decrease the size now that the heap has been re-adjusted
h.size--
// h.internal[h.size-1] = ""
// h.internal = h.internal[:len(h.internal)-1]
// Reorder the heap from the root
h.Down(0)
return err
}
func (h *Heap) Find(value interface{}) (index int, err error) {
return index, err
}
func (h *Heap) DeleteElem(value interface{}) (err error) {
h.init()
return err
}
func (h *Heap) ChangeCost(value interface{}, parent *Node, cost float64) {
h.init()
if h.nodes[value] != nil && cost < h.nodes[value].Cost {
h.nodes[value].Parent = parent
// Update the cost of the node
h.nodes[value].Cost = cost
// Move the node up the heap
h.Up(h.values[value])
}
}
func (h *Heap) Size() int {
return h.size
}