-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathBFS.py
69 lines (56 loc) · 1.27 KB
/
BFS.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from queue import *
def bfs(graph, vertex, fr, ar):
fr = 0
ar = 3
dist = []
vstd = []
for x in range(0,vertex):
dist.append(-1)
vstd.append(False)
pass
dist[ int(fr) ] = 0
vstd[ int(fr) ] = True
q = Queue()
q.put( fr )
while not q.empty():
u = q.get()
print( "Vértice " + str(u) + " com distância " + str(dist[int(u)]) )
for w in graph[ int(u) ]:
if dist[ int(w) ] == -1:
dist[ int(w) ] = int(dist[int(u)]) + 1
vstd[ int(w) ] = True
q.put(w)
pass
pass
pass
return vstd[ int(ar) ]
def initGraphDirect():
graph = [[]]
v = 4
for x in range(0,v-1):
graph.append( [] )
graph[0].append(1)
graph[0].append(2)
graph[1].append(2)
graph[2].append(3)
print(graph)
bfs(graph,v,0,3)
pass
def initGraphUndirected():
graph = [[]]
v = 4
for x in range(0,v-1):
graph.append( [] )
graph[0].append(1)
graph[1].append(0)
graph[0].append(2)
graph[2].append(0)
graph[1].append(2)
graph[2].append(1)
graph[2].append(3)
graph[3].append(2)
print(graph)
bfs(graph,v,0,3)
pass
initGraphDirect()
initGraphUndirected()