-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgreedy.py
33 lines (30 loc) · 805 Bytes
/
greedy.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
def greedy(graph, costs, h, start, goal):
path = []
expanded = {}
for node in graph:
expanded[node] = 0
node = start
while True:
path.append(node)
if node == goal:
break
if len(graph[node]) == 0:
print('Nodo objetivo no encontrado')
return
expanded[node] += 1
neighbours = graph[node]
next = neighbours[0]
for i in range(1, len(neighbours)):
if h[neighbours[i]] < h[next]:
next = neighbours[i]
node = next
totalCost = 0
for i in range(len(path) - 1):
totalCost += costs[path[i], path[i + 1]]
print(path[i] + ' -> ', end='')
print(path[-1])
print('Costo:', totalCost)
print(start + ':', expanded[start])
for node in graph:
if node != start and node != goal:
print(node + ':', expanded[node])