Created
September 5, 2020 00:17
-
-
Save aj07mm/3613f54e0457fd6e6a120ca437915792 to your computer and use it in GitHub Desktop.
bfs.py
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 | |
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