-
Notifications
You must be signed in to change notification settings - Fork 0
/
bfs.go
54 lines (37 loc) · 914 Bytes
/
bfs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main
import "fmt"
//Queue implementation
type Queue []string
func (q *Queue) popLeft() string{
firstPerson := (*q)[0]
*q = (*q)[1:]
return firstPerson
}
func (q *Queue) addToQueue(items []string) {
*q = append(*q, items...)
}
func isPersonSeller(name string) bool {
return name[len(name)-1] == 'm'
}
func search(q *Queue, graph map[string][]string) bool {
for len(*q) != 0 {
person := (*q).popLeft()
if isPersonSeller(person) {
fmt.Println(person, "is a mango seller")
return true
} else {
(*q).addToQueue(graph[person])
}
}
return false
}
func main() {
var graph = make(map[string][]string)
graph["you"] = []string{"alice", "bob", "claire"}
graph["alice"] = []string{"peggy"}
graph["bob"] = []string{"anuj", "peggy"}
graph["claire"] = []string{"thom", "jonny"}
var queue Queue
queue = graph["you"]
search(&queue, graph)
}