Skip to content

Commit 57dd4e1

Browse files
Vish511egonSchiele
authored andcommitted
Implementation of breadth first search in Golang (egonSchiele#83)
* Implementation of breadth first search in Golang * fixed package name in go lang implementation of breadth first search * fixed package name in go lang implementation of breadth first search
1 parent ba1e366 commit 57dd4e1

File tree

1 file changed

+55
-0
lines changed
  • 06_breadth-first_search/golang

1 file changed

+55
-0
lines changed

06_breadth-first_search/golang/bfs.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
func PersonIsSeller(name string) bool {
8+
/* Assuming the person whose name ends with m as the mango seller as per the book */
9+
return string(name[len(name)-1]) == "m"
10+
}
11+
12+
func search(name string) bool {
13+
graph := make(map[string][]string)
14+
graph["you"] = []string{"alice", "bob", "claire"}
15+
graph["bob"] = []string{"anuj", "peggy"}
16+
graph["alice"] = []string{"peggy"}
17+
graph["claire"] = []string{"thom", "jonny"}
18+
graph["anuj"] = []string{}
19+
graph["peggy"] = []string{}
20+
graph["thom"] = []string{}
21+
graph["jonny"] = []string{}
22+
23+
//search queue
24+
var search_queue []string
25+
search_queue = append(search_queue, graph[name]...)
26+
var searched []string
27+
for len(search_queue) > 0 {
28+
var person = search_queue[0]
29+
search_queue = search_queue[1:]
30+
var person_already_searched = false
31+
/* Checking to see if this person has already been searched */
32+
for i := 0; i < len(searched); i++ {
33+
if searched[i] == person {
34+
person_already_searched = true
35+
36+
}
37+
}
38+
if person_already_searched == false {
39+
if PersonIsSeller(person) {
40+
fmt.Println(person + " is the mango seller!")
41+
return true
42+
} else {
43+
search_queue = append(search_queue, graph[person]...)
44+
searched = append(searched, person)
45+
}
46+
}
47+
48+
}
49+
return false
50+
51+
}
52+
53+
func main() {
54+
search("you")
55+
}

0 commit comments

Comments
 (0)