-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheap.go
172 lines (128 loc) · 2.55 KB
/
heap.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
package goheap
import (
"errors"
"sync"
)
type heapItem struct {
id int
w float64
}
type minHeap struct {
items []heapItem
itemIndex map[int]int
vis []bool
cap, heapSize int
mu sync.Mutex
}
// p = parent
func (h *minHeap) p(i int) int {
return (i - 1) / 2
}
// lc = left child
func (h *minHeap) lc(i int) int {
return (2 * i) + 1
}
// rc = right child
func (h *minHeap) rc(i int) int {
return (2 * i) + 2
}
func (h *minHeap) updateExisting(id int, weight float64) bool {
if !h.vis[id] {
return false
}
i := h.itemIndex[id]
if h.items[i].w <= weight {
return true
}
h.updateW(i, weight)
return true
}
func (h *minHeap) updateW(i int, newWeight float64) {
h.items[i].w = newWeight
for i != 0 && h.items[h.p(i)].w > h.items[i].w {
h.swap(i, h.p(i))
i = h.p(i)
}
}
func (h *minHeap) Insert(id int, weight float64) error {
h.mu.Lock()
defer h.mu.Unlock()
if h.updateExisting(id, weight) {
return nil
}
h.vis[id] = true
if h.cap == h.heapSize {
return errors.New("heap full cap")
}
i := h.heapSize
h.heapSize++
h.items[i] = heapItem{id: id, w: weight}
h.itemIndex[id] = i
for i != 0 && h.items[h.p(i)].w > h.items[i].w {
h.swap(i, h.p(i))
i = h.p(i)
}
return nil
}
func (h *minHeap) deleteItemIndex(id int) {
_, ok := h.itemIndex[id]
if ok {
delete(h.itemIndex, id)
}
}
func (h *minHeap) ExtractMin() (id int, weight float64, err error) {
h.mu.Lock()
defer h.mu.Unlock()
if h.heapSize <= 0 {
return 0, 0, errors.New("no item in heap")
}
id = h.items[0].id
weight = h.items[0].w
h.vis[id] = false
if h.heapSize == 1 {
h.heapSize--
return id, weight, nil
}
root := h.items[0]
h.swap(0, h.heapSize-1)
h.heapSize--
h.deleteItemIndex(id)
h.minHeapify(0)
return root.id, root.w, nil
}
// swap over heap array indexes and updates itemIndex id ref to heap array index
func (h *minHeap) swap(i, j int) {
t := h.items[i]
u := t.id
v := h.items[j].id
h.items[i] = h.items[j]
h.items[j] = t
tt := h.itemIndex[u]
h.itemIndex[u] = h.itemIndex[v]
h.itemIndex[v] = tt
}
func (h *minHeap) minHeapify(i int) {
l := h.lc(i)
r := h.rc(i)
ret := i
if l < h.heapSize && h.items[l].w < h.items[ret].w {
ret = l
}
if r < h.heapSize && h.items[r].w < h.items[ret].w {
ret = r
}
if ret != i {
h.swap(i, ret)
h.minHeapify(ret)
}
}
// NewMinHeap returns a new instance of MinHeap
func NewMinHeap(cap int) MinHeap {
return &minHeap{
cap: cap,
items: make([]heapItem, cap),
vis: make([]bool, cap),
itemIndex: map[int]int{},
mu: sync.Mutex{},
}
}