Skip to content

Instantly share code, notes, and snippets.

@aj07mm
Created September 5, 2020 00:17
Show Gist options
  • Save aj07mm/3613f54e0457fd6e6a120ca437915792 to your computer and use it in GitHub Desktop.
Save aj07mm/3613f54e0457fd6e6a120ca437915792 to your computer and use it in GitHub Desktop.
bfs.py
from collections import deque
grafo = {}
grafo["voce"] = ["alice", "bob", "claire"]
grafo["bob"] = ["anuj", "peggy"]
grafo["alice"] = ["peggy"]
grafo["claire"] = ["thom", "jonny"]
grafo["anuj"] = []
grafo["peggy"] = []
grafo["thom"] = []
grafo["jhonny"] = []
def pessoa_e_vendedor(nome):
return nome[-1] == 'm'
def pesquisa(nome):
fila_de_pesquisa = deque()
fila_de_pesquisa += grafo[nome]
verificadas = []
while fila_de_pesquisa:
pessoa = fila_de_pesquisa.popleft()
if not pessoa in verificadas:
if pessoa_e_vendedor(pessoa):
print(pessoa + " eh um vendedor de manga")
return True
else:
fila_de_pesquisa += grafo[pessoa]
verificadas.append(pessoa)
return False
pesquisa("voce")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment