Last active
September 21, 2016 01:32
-
-
Save VitorDiToro/cdad976a6672ddf73c787b395e9d36d0 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
def BFS(G,s): | |
Q = [] | |
state = [None for i in range(len(G))] | |
pi = [None for i in range(len(G))] | |
for u in range(len(G)): | |
state[u]='N' | |
pi[u] = None | |
state[s] = 'D' | |
pi[s] = None | |
Q.append(s) | |
while Q != []: | |
u = Q.pop(0) | |
print(u) | |
for v in G[u]: | |
print(u,v) | |
if(state[v] == 'N'): | |
state[v] = 'D' | |
pi[v] = u | |
Q.append(v) | |
state[u] = 'P' | |
for i in range(len(pi)): | |
print("Pai de", i ,"eh",pi[i]) | |
def main(): | |
#G eh a minha lista de adjacencias | |
G=((1,4),(0,2,3,4),(1,3),(1,2,4),(0,1,3)) | |
BFS(G,0) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment