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 }
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 }
func newAdjacencyMatrix(size uint32) (m *adjacencyMatrix) { m = &adjacencyMatrix{} m.size = size m.matrix = bitarray.NewBitArray(size*size, 1) return }