Skip to content

Commit a5fe917

Browse files
linehkegonSchiele
authored andcommitted
Add 06_breadth-first_search Golang example (egonSchiele#81)
1 parent 2ab2e9c commit a5fe917

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package main
2+
3+
func person_is_seller(name string) bool {
4+
return name[len(name)-1] == 'm'
5+
}
6+
7+
var graph = make(map[string][]string)
8+
9+
func main() {
10+
graph["you"] = []string{"alice", "bob", "claire"}
11+
graph["bob"] = []string{"anuj", "peggy"}
12+
graph["alice"] = []string{"peggy"}
13+
graph["claire"] = []string{"thom", "jonny"}
14+
graph["anuj"] = []string{}
15+
graph["peggy"] = []string{}
16+
graph["thom"] = []string{}
17+
graph["jonny"] = []string{}
18+
19+
search("you")
20+
}
21+
22+
func search(name string) bool {
23+
var search_queue []string
24+
search_queue = append(search_queue, graph[name]...)
25+
var searched []string
26+
var person string
27+
for len(search_queue) != 0 {
28+
person = search_queue[0]
29+
search_queue = search_queue[1:]
30+
if person_not_in_searched(person, searched) {
31+
if person_is_seller(person) {
32+
println(person + " is mango seller!")
33+
return true
34+
} else {
35+
search_queue = append(search_queue, graph[person]...)
36+
searched = append(searched, person)
37+
}
38+
}
39+
}
40+
return false
41+
}
42+
43+
func person_not_in_searched(person string, searched []string) bool {
44+
for _, n := range searched {
45+
if n == person {
46+
return false
47+
}
48+
}
49+
return true
50+
}

0 commit comments

Comments
 (0)