Created
May 15, 2024 13:05
-
-
Save rodgtr1/7e9bfdc9e97cc4cd5e3a9ff8ad968ab1 to your computer and use it in GitHub Desktop.
Example code from the DSA For Non-Traditionals: 6 - Graphs and the Breadth-first search algorithm in the Travis Media Community - https://community.travis.media/recordings/dsa-for-non-traditionals-graphs-and-the-breadth-first-search-algorithm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import deque | |
graph = {} | |
graph["you"] = ["alice", "bob", "claire"] | |
graph["bob"] = ["anuj", "peggy"] | |
graph["alice"] = ["peggy"] | |
graph["claire"] = ["thom", "jonny"] | |
graph["anuj"] = [] | |
graph["peggy"] = [] | |
graph["thom"] = [] | |
graph["jonny"] = [] | |
# We don't really have a way to tell if the person is a mango seller or not here. If facebook friends, perhaps we could check their occupation. For now we'll just say that if their name has an m on the end, they are a mango seller. | |
def person_is_seller(name): | |
return name[-1] == 'm' | |
def search(name): | |
search_queue = deque() | |
search_queue += graph[name] | |
searched = [] | |
while search_queue: | |
person = search_queue.popleft() | |
if not person in searched: | |
if person_is_seller(person): | |
print(person + " is a mango seller!") | |
return True | |
else: | |
search_queue += graph[person] | |
searched.append(person) | |
return False | |
search("you") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment