Skip to content

Instantly share code, notes, and snippets.

@mfilipelino
Last active July 5, 2019 18:43
Show Gist options
  • Save mfilipelino/f100b7fc2f7227ce3f1bcfd621bdd680 to your computer and use it in GitHub Desktop.
Save mfilipelino/f100b7fc2f7227ce3f1bcfd621bdd680 to your computer and use it in GitHub Desktop.
Bfs algorithm
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