# Learn Algorithm Design by Building a Shortest Path Algorithm - Step 40

### Tell us what’s happening:

I created a nested if statement, and updated it after it gave a hint. I can’t see beyond that. Any hints?

``````my_graph = {
'A': [('B', 3), ('D', 1)],
'B': [('A', 3), ('C', 4)],
'C': [('B', 4), ('D', 7)],
'D': [('A', 1), ('C', 7)]
}

def shortest_path(graph, start):
unvisited = list(graph)
distances = {node: 0 if node == start else float('inf') for node in graph}
paths = {node: [] for node in graph}
paths[start].append(start)

/* User Editable Region */

while unvisited:
current = min(unvisited, key=distances.get)
for node, distance in graph[current]:
if distance + distances[current] < distances[node]:
distances[node] = distance + distances[current]
if paths[node][-1] = node:
pass

/* User Editable Region */

print(f'Unvisited: {unvisited}\nDistances: {distances}\nPaths: {paths}')

#shortest_path(my_graph, 'A')
``````

User Agent is: `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36`

### Challenge Information:

Learn Algorithm Design by Building a Shortest Path Algorithm - Step 40

You appear to have created this post without editing the template. Please edit your post to Tell us what’s happening in your own words.

You are using assignment `=` instead of equality operator.

Blockquote
If the distance for the node in the processed tuple has been updated, the last item in its path is the node itself.

Then, inside the `if distance + distances[current] < distances[node]:` we update the distance: `distances[node] = distance + distances[current]`.

So it makes sense to use the variable (`distances[node]`) from the previous `if` statement to check if it is equal to the path.

So I updated the nested `if` statement to this: `if distances[node] == paths[node][-1]: pass`

Is this line of thinking correct?

You were close to the answer before. Here your only mistake is the syntax:

Since `=` is to assign while you need to compare.

2 Likes

Thanks! Worked it out!

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.