Last active
July 5, 2019 18:43
-
-
Save mfilipelino/f100b7fc2f7227ce3f1bcfd621bdd680 to your computer and use it in GitHub Desktop.
Bfs algorithm
This file contains hidden or 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
import Queue | |
Grafo = { 0 : [1, 3], | |
1 : [0, 2], | |
2 : [1, 3], | |
3 : [0, 2] | |
} | |
def queue_insert(q, v): | |
q.insert(0, v) | |
def bfs(grafo, start): | |
mark = [ False for _ in range(4)] | |
l = [None for _ in range(4)] | |
p = [None for _ in range(4)] | |
queue = [] | |
queue_insert(queue, start) | |
l[start] = 0 | |
mark[start] = True | |
while queue != []: | |
u = queue.pop() | |
for v in grafo[u]: | |
if mark[v] == False: | |
mark[v] = True | |
queue_insert(queue, v) | |
l[v] = l[u] + 1 | |
p[v] = u | |
return p, l | |
#print bfs(Grafo, 0) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment