#315 Renamed all files to snake_case (#993)

This commit is contained in:
Alfonso Rodríguez Pereira
2019-07-11 11:16:42 +02:00
committed by cclauss
parent b79a197e8c
commit 5f991f7740
23 changed files with 0 additions and 0 deletions

41
graphs/bfs.py Normal file
View File

@ -0,0 +1,41 @@
"""
BFS.
pseudo-code:
BFS(graph G, start vertex s):
// all nodes initially unexplored
mark s as explored
let Q = queue data structure, initialized with s
while Q is non-empty:
remove the first node of Q, call it v
for each edge(v, w): // for w in graph[v]
if w unexplored:
mark w as explored
add w to Q (at the end)
"""
import collections
def bfs(graph, start):
explored, queue = set(), [start] # collections.deque([start])
explored.add(start)
while queue:
v = queue.pop(0) # queue.popleft()
for w in graph[v]:
if w not in explored:
explored.add(w)
queue.append(w)
return explored
G = {'A': ['B', 'C'],
'B': ['A', 'D', 'E'],
'C': ['A', 'F'],
'D': ['B'],
'E': ['B', 'F'],
'F': ['C', 'E']}
print(bfs(G, 'A'))