Skip to content

Commit 41f3569

Browse files
committed
Refactor prio queue
1 parent e69819a commit 41f3569

File tree

2 files changed

+2
-92
lines changed

2 files changed

+2
-92
lines changed

mst.go

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func MST(g Iterator) (parent []int) {
2222
}
2323

2424
// Prim's algorithm
25-
Q := newMstQueue(cost)
25+
Q := newQueue(cost)
2626
for Q.Len() > 0 {
2727
v := heap.Pop(Q).(int)
2828
g.Visit(v, func(w int, c int64) (skip bool) {
@@ -36,53 +36,3 @@ func MST(g Iterator) (parent []int) {
3636
}
3737
return
3838
}
39-
40-
type mstQueue struct {
41-
heap []int // vertices in heap order
42-
index []int // index of each vertex in the heap
43-
cost []int64
44-
}
45-
46-
func newMstQueue(cost []int64) *mstQueue {
47-
n := len(cost)
48-
h := &mstQueue{
49-
heap: make([]int, n),
50-
index: make([]int, n),
51-
cost: cost,
52-
}
53-
for i := range h.heap {
54-
h.heap[i] = i
55-
h.index[i] = i
56-
}
57-
return h
58-
}
59-
60-
func (m *mstQueue) Len() int { return len(m.heap) }
61-
62-
func (m *mstQueue) Less(i, j int) bool {
63-
return m.cost[m.heap[i]] < m.cost[m.heap[j]]
64-
}
65-
66-
func (m *mstQueue) Swap(i, j int) {
67-
m.heap[i], m.heap[j] = m.heap[j], m.heap[i]
68-
m.index[m.heap[i]] = i
69-
m.index[m.heap[j]] = j
70-
}
71-
72-
func (m *mstQueue) Push(x interface{}) {} // Not used
73-
74-
func (m *mstQueue) Pop() interface{} {
75-
n := len(m.heap) - 1
76-
v := m.heap[n]
77-
m.index[v] = -1
78-
m.heap = m.heap[:n]
79-
return v
80-
}
81-
82-
func (m *mstQueue) Update(v int) {
83-
heap.Fix(m, m.index[v])
84-
}
85-
86-
func (m *mstQueue) Contains(v int) bool {
87-
return m.index[v] >= 0
88-
}

path.go

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func ShortestPaths(g Iterator, v int) (parent []int, dist []int64) {
4444
dist[v] = 0
4545

4646
// Dijkstra's algorithm
47-
Q := emptySpQueue(dist)
47+
Q := emptyQueue(dist)
4848
heap.Push(Q, v)
4949
for Q.Len() > 0 {
5050
v = heap.Pop(Q).(int)
@@ -66,43 +66,3 @@ func ShortestPaths(g Iterator, v int) (parent []int, dist []int64) {
6666
}
6767
return
6868
}
69-
70-
type spQueue struct {
71-
heap []int // vertices in heap order
72-
index []int // index of each vertex in the heap
73-
dist []int64
74-
}
75-
76-
func emptySpQueue(dist []int64) *spQueue {
77-
return &spQueue{dist: dist, index: make([]int, len(dist))}
78-
}
79-
80-
func (pq *spQueue) Len() int { return len(pq.heap) }
81-
82-
func (pq *spQueue) Less(i, j int) bool {
83-
return pq.dist[pq.heap[i]] < pq.dist[pq.heap[j]]
84-
}
85-
86-
func (pq *spQueue) Swap(i, j int) {
87-
pq.heap[i], pq.heap[j] = pq.heap[j], pq.heap[i]
88-
pq.index[pq.heap[i]] = i
89-
pq.index[pq.heap[j]] = j
90-
}
91-
92-
func (pq *spQueue) Push(x interface{}) {
93-
n := len(pq.heap)
94-
v := x.(int)
95-
pq.heap = append(pq.heap, v)
96-
pq.index[v] = n
97-
}
98-
99-
func (pq *spQueue) Pop() interface{} {
100-
n := len(pq.heap) - 1
101-
v := pq.heap[n]
102-
pq.heap = pq.heap[:n]
103-
return v
104-
}
105-
106-
func (pq *spQueue) Update(v int) {
107-
heap.Fix(pq, pq.index[v])
108-
}

0 commit comments

Comments
 (0)