Esempio n. 1
0
func (g *Graph) BFS(start int) {
	queue := []int{}
	queue = append(queue, start)
	visited := bitset.NewBitSet()
	for i := 0; i < len(queue); i++ {
		v := g.nodes[queue[i]]
		fmt.Printf("Visiting %d\n", v.Rank)
		// mark v as visited
		visited.Set(v.Rank)
		neighbors, _ := v.GetNeighbors()
		for _, neighbor := range neighbors {
			if !visited.IsSet(neighbor.Rank) {
				queue = append(queue, neighbor.Rank)
			}
		}
	}
}
Esempio n. 2
0
func (g *Graph) DoDFS(start int) {
	g.DFS(start, bitset.NewBitSet(), bitset.NewBitSet())
}