Skip to content

Commit f3c7771

Browse files
committed
Add dijkstra algorithm with naive priority queue implementation and
corresponding tests Add parallel reduce and scan with corresponding tests and benchmarks
1 parent b0d210e commit f3c7771

File tree

7 files changed

+553
-0
lines changed

7 files changed

+553
-0
lines changed

bench/benchParaReduce_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package bench
2+
3+
import (
4+
"algo/parallel"
5+
"testhelper"
6+
"testing"
7+
)
8+
9+
func BenchmarkSeqReduce(b *testing.B) {
10+
input := testhelper.BigInput(1000000)
11+
b.ResetTimer()
12+
for i := 0; i < b.N; i++ {
13+
reduce(input, testhelper.Plus)
14+
}
15+
}
16+
17+
func BenchmarkParaReduce(b *testing.B) {
18+
input := testhelper.BigInput(1000000)
19+
b.ResetTimer()
20+
for i := 0; i < b.N; i++ {
21+
parallel.Reduce(input, testhelper.Plus, 0)
22+
}
23+
}
24+
25+
func reduce(in []int, f func(int, int) int) int {
26+
ans := in[0]
27+
for i := 1; i < len(in); i++ {
28+
ans = f(ans, in[i])
29+
}
30+
return ans
31+
}

bench/benchParaScan_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package bench
2+
3+
import (
4+
"algo/parallel"
5+
"testhelper"
6+
"testing"
7+
)
8+
9+
func BenchmarkSeqScan(b *testing.B) {
10+
input := testhelper.BigInput(1000000)
11+
b.ResetTimer()
12+
for i := 0; i < b.N; i++ {
13+
scan(input, testhelper.Plus)
14+
}
15+
}
16+
17+
func BenchmarkParaScan(b *testing.B) {
18+
input := testhelper.BigInput(1000000)
19+
b.ResetTimer()
20+
for i := 0; i < b.N; i++ {
21+
parallel.Scan(input, testhelper.Plus, 0)
22+
}
23+
}
24+
25+
func BenchmarkParaScan2(b *testing.B) {
26+
input := testhelper.BigInput(1000000)
27+
b.ResetTimer()
28+
for i := 0; i < b.N; i++ {
29+
parallel.Scan2(input, testhelper.Plus, 0)
30+
}
31+
}
32+
33+
func BenchmarkParaScan3(b *testing.B) {
34+
input := testhelper.BigInput(1000000)
35+
b.ResetTimer()
36+
for i := 0; i < b.N; i++ {
37+
parallel.Scan3(input, testhelper.Plus, 0, 4)
38+
}
39+
}
40+
41+
func BenchmarkParaScan4(b *testing.B) {
42+
input := testhelper.BigInput(1000000)
43+
b.ResetTimer()
44+
for i := 0; i < b.N; i++ {
45+
parallel.Scan4(input, testhelper.Plus, 0, 4)
46+
}
47+
}
48+
49+
func scan(in []int, f func(int, int) int) []int {
50+
ret := make([]int, len(in))
51+
copy(ret, in)
52+
for i := 1; i < len(ret); i++ {
53+
ret[i] = f(ret[i-1], ret[i])
54+
}
55+
return ret
56+
}

src/algo/dijkstra/dijkstra.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package dijkstra
2+
3+
import (
4+
"algo/union_find"
5+
"data"
6+
)
7+
8+
func Dijkstra(g [][]data.Edge, s int) []int {
9+
ret := make([]int, len(g))
10+
for i := 0; i < len(g); i++ {
11+
if i != s {
12+
ret[i] = 1 << 31
13+
}
14+
}
15+
uf := union_find.UnionFind{}
16+
uf.Init(len(g))
17+
next := s
18+
for next != -1 {
19+
u := next
20+
uf.Union(u, s)
21+
for _, e := range g[u] {
22+
// relaxing
23+
if ret[e.To] > ret[e.From]+e.W {
24+
ret[e.To] = ret[e.From] + e.W
25+
}
26+
}
27+
// should use fibonacci heap here, but not intend to implement that
28+
// use linear search instead
29+
next = -1
30+
min := 1 << 31
31+
for i := 0; i < len(g); i++ {
32+
if uf.IsUnion(i, s) {
33+
continue
34+
}
35+
if ret[i] < min {
36+
min = ret[i]
37+
next = i
38+
}
39+
}
40+
}
41+
return ret
42+
}

0 commit comments

Comments
 (0)