-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbusqueda_anchura.py
47 lines (42 loc) · 1.41 KB
/
busqueda_anchura.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
# coding=utf-8
from header import *
def busqueda_por_anchura(gph):
count = gph.count
visitado_verti = [0] * count
i = 0
while i < count:
if visitado_verti[i] == 0:
busqueda_por_anchura_Queue(gph, i, visitado_verti)
i += 1
def ShortestPath(gph, raiz):
count = gph.count
distancia_cada_uno = [-1] * count
path = [-1] * count
rastro = deque([])
rastro.append(raiz)
distancia_cada_uno[raiz] = 0
while len(rastro) != 0:
curr = rastro.popleft()
head = gph.array[curr].head
while head != None:
if distancia_cada_uno[head.destino_ver_dat] == -1:
distancia_cada_uno[head.destino_ver_dat] = distancia_cada_uno[curr] + 1
path[head.destino_ver_dat] = curr
rastro.append(head.destino_ver_dat)
head = head.next
i = 0
while i < count:
print path[i] , "a" , i , "para" , distancia_cada_uno[i]
i += 1
def busqueda_por_anchura_Queue(gph, index, visitado_verti):
rastro = deque([])
visitado_verti[index] = 1
rastro.append(index)
while len(rastro) != 0:
curr = rastro.popleft()
head = gph.array[curr].head
while head != None:
if visitado_verti[head.destino_ver_dat] == 0:
visitado_verti[head.destino_ver_dat] = 1
rastro.append(head.destino_ver_dat)
head = head.next