diff --git a/06_breadth-first_search/Golang/01_breadth-first_search.go b/06_breadth-first_search/Golang/01_breadth-first_search.go new file mode 100644 index 0000000..26510ba --- /dev/null +++ b/06_breadth-first_search/Golang/01_breadth-first_search.go @@ -0,0 +1,50 @@ +package main + +func person_is_seller(name string) bool { + return name[len(name)-1] == 'm' +} + +var graph = make(map[string][]string) + +func main() { + graph["you"] = []string{"alice", "bob", "claire"} + graph["bob"] = []string{"anuj", "peggy"} + graph["alice"] = []string{"peggy"} + graph["claire"] = []string{"thom", "jonny"} + graph["anuj"] = []string{} + graph["peggy"] = []string{} + graph["thom"] = []string{} + graph["jonny"] = []string{} + + search("you") +} + +func search(name string) bool { + var search_queue []string + search_queue = append(search_queue, graph[name]...) + var searched []string + var person string + for len(search_queue) != 0 { + person = search_queue[0] + search_queue = search_queue[1:] + if person_not_in_searched(person, searched) { + if person_is_seller(person) { + println(person + " is mango seller!") + return true + } else { + search_queue = append(search_queue, graph[person]...) + searched = append(searched, person) + } + } + } + return false +} + +func person_not_in_searched(person string, searched []string) bool { + for _, n := range searched { + if n == person { + return false + } + } + return true +}