File tree Expand file tree Collapse file tree 2 files changed +30
-2
lines changed Expand file tree Collapse file tree 2 files changed +30
-2
lines changed Original file line number Diff line number Diff line change @@ -45,8 +45,9 @@ func components(g Iterator) (sets disjointSets, count int) {
4545 return
4646}
4747
48- // Union-find with path compression performs any sequence of
49- // m ≥ n find and n – 1 union operations in O(m log n) time.
48+ // Union-find with path compression performs any sequence of m ≥ n find
49+ // and n – 1 union operations in O(m log n) time. Union by rank doesn't
50+ // seem to improve performance here.
5051type disjointSets []int
5152
5253func makeSingletons (n int ) disjointSets {
Original file line number Diff line number Diff line change 11package graph
22
33import (
4+ "math/rand"
45 "testing"
56)
67
@@ -50,3 +51,29 @@ func TestComponents(t *testing.T) {
5051 t .Errorf ("Connected %s" , mess )
5152 }
5253}
54+
55+ func BenchmarkConnected (b * testing.B ) {
56+ n := 1000
57+ b .StopTimer ()
58+ g := New (n )
59+ for i := 0 ; i < n ; i ++ {
60+ g .AddBoth (rand .Intn (n ), rand .Intn (n ))
61+ }
62+ b .StartTimer ()
63+ for i := 0 ; i < b .N ; i ++ {
64+ _ = Connected (g )
65+ }
66+ }
67+
68+ func BenchmarkComponents (b * testing.B ) {
69+ n := 1000
70+ b .StopTimer ()
71+ g := New (n )
72+ for i := 0 ; i < n ; i ++ {
73+ g .AddBoth (rand .Intn (n ), rand .Intn (n ))
74+ }
75+ b .StartTimer ()
76+ for i := 0 ; i < b .N ; i ++ {
77+ _ = Components (g )
78+ }
79+ }
You can’t perform that action at this time.
0 commit comments