Beispiel #1
0
func TestQuickFind(t *testing.T) {

	// test that constructor arguments must be positive integers
	for i := 0; i > -2; i-- {
		if uf.NewQuickFind(i) != nil {
			t.Errorf("NewQuickFind(%d) was non-nil", i)
		}
	}

	// test that constructed size is correct
	qf := uf.NewQuickFind(10)
	if qf.Size() != 10 {
		t.Error("QuickFind had incorrect size ", qf.Size())
	}
	if qf.Count() != 10 {
		t.Errorf("QuickFind had incorrect count. Got %d, expected %d", qf.Count(), 10)
	}

	// test find returns identity before union
	for i := 0; i < 10; i++ {
		if qf.Find(i) != i {
			t.Errorf("node %d not linked to self", i)
		}
	}

	// test union
	if qf.Count() != 10 {
		t.Errorf("pre-union component count: want %d, got %d", 10, qf.Count())
	}
	qf.Union(0, 1)
	if !qf.Connected(0, 1) {
		t.Errorf("nodes 0 and 1 not connected")
	}
	if qf.Count() != 9 {
		t.Errorf("post-union component count: want %d, got %d", 9, qf.Count())
	}
}
Beispiel #2
0
func main() {
	in := bufio.NewReader(os.Stdin)
	nc := readNodeCount(in)
	qf := uf.NewQuickFind(nc)

	for {
		p, q, err := readPair(in)
		if err != nil {
			break
		}
		if qf.Connected(p, q) {
			continue
		}
		qf.Union(p, q)
		fmt.Printf("%d %d\n", p, q)
	}

	fmt.Printf("%d components\n", qf.Count())

	return
}