-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathucs.py
More file actions
30 lines (24 loc) · 841 Bytes
/
ucs.py
File metadata and controls
30 lines (24 loc) · 841 Bytes
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
def ucs(graph, start, goal):
visited = []
entry_counter = 0
queue = [(0, entry_counter, start, [])]
while queue:
print(queue)
cost, entry, node, path = queue.pop(0)
print((cost, entry, node, path))
if node == goal:
path.append(node)
visited.append(node)
return visited, path
if node in visited:
continue
visited.append(node)
for neighbor in graph.neighbors(node):
weight = graph.get_edge_data(node, neighbor).get("weight", 0)
if neighbor not in visited:
new_cost = cost + weight
new_path = path + [node]
entry_counter += 1
queue.append((new_cost, entry_counter, neighbor, new_path))
queue.sort()
return [], visited