Tell us what’s happening:
Inside your new if statement you should assign 0 to the node in the distances dictionary.
I’ve tried various methods of inputting the assigned node into the “distances” dictionary and still can’t seem to figure it out.
Your code so far
my_graph = {
'A': [('B', 3), ('D', 1)],
'B': [('A', 3), ('C', 4)],
'C': [('B', 4), ('D', 7)],
'D': [('A', 1), ('C', 7)]
}
# User Editable Region
def shortest_path(graph, start):
unvisited = []
distances = {}
for node in graph:
unvisited.append(node)
if node == start:
distances = {[node]: 0}
# User Editable Region
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36
Challenge Information:
Learn Algorithm Design by Building a Shortest Path Algorithm - Step 28
Step 28
The distance from the starting node is zero, because the algorithm begins its assessment right from there.
After appending node
to unvisited
in your loop, create an if
statement that triggers if the node is equal to the starting node. Then assign 0
to that node inside the distances
dictionary.
I forgot to add the steps
Hi there and welcome to our community!
joelatrev22:
distances = {[node]: 0}
You can use bracket notation to access key/value pairs within a dictionary and to reassign values, or to add new key/value pairs.
EXAMPLE:
nato_alphabet = {
'A': 'Alpha',
'B': 'Beta',
'C': 'Charlie'
}
print(nato_alphabet['B']) # Beta
letter = 'B'
nato_alphabet[letter] = 'Banana'
print(nato_alphabet[letter]) # Banana
nato_alphabet['B'] = 'Bravo'
print(nato_alphabet['B']) #Bravo
nato_alphabet['D'] = 'Delta'
print(nato_alphabet) # { 'A': 'Alpha', 'B': 'Bravo', 'C':'Charlie', 'D': 'Delta' }
You can access/modify/add a key/value pair within a dictionary explicitly or via a variable. Hope that makes sense!
1 Like
Thank you very much, i figured since node was not a key in the dictionary that i had to define it.
2 Likes