Exemple #1
0
func BenchmarkSort1M(b *testing.B) {
	rand.Seed(time.Now().UnixNano())
	l := dllist.New()
	b.Log("Filling ...")
	for i := 0; i < 1000000; i++ {
		l.PushBack(key(rand.Int()))
	}
	b.Log("Sorting ...")
	b.ResetTimer()
	l.Sort()
	b.StopTimer()
	b.Log("Checking ...")
	for iter, node := dllist.NewIterator(l.First(), l.Last()); node != iter.Last(); node = iter.Next() {
		if node.Next().Data().Less(node.Data()) {
			b.FailNow()
		}
	}
}
Exemple #2
0
func (g *Graph) BFS() (path []*Node) {
	visited := bitarray.NewBitArray(uint32(len(g.nodes)), 1)
	queue := dllist.New()
	queue.PushBack(g.Root)
	for queue.Length() > 0 {
		m, err := queue.PopFront()
		if err != nil {
			break
		}
		n := m.(*Node)
		if visited.GetB(n.id) == 0 {
			visited.SetB(n.id, 1)
			path = append(path, n)
			for _, c := range g.Adjacent(n) {
				queue.PushBack(c)
			}
		}
	}
	return
}
Exemple #3
0
func (g *Graph) DFS() (path []*Node) {
	visited := bitarray.NewBitArray(uint32(len(g.nodes)), 1)
	stack := dllist.New()
	stack.PushFront(g.Root)
	for stack.Length() > 0 {
		m, err := stack.PopFront()
		if err != nil {
			panic(err)
		}
		n := m.(*Node)
		if visited.GetB(n.id) == 0 {
			path = append(path, n)
			visited.SetB(n.id, 1)
			for _, c := range g.Adjacent(n) {
				stack.PushFront(c)
			}
		}
	}
	return
}