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

I do not understand this question. To me

neighbor node

is

undefined

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)]
}

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)

    while unvisited:
        current = min(unvisited, key=distances.get)
        for node, distance in graph[current]:

/* User Editable Region */

            if distance + distances[current] < distances[node]:
                distances(node) = distance + distances(current)

/* User Editable Region */

    print(f'Unvisited: {unvisited}\nDistances: {distances}\nPaths: {paths}')
    
#shortest_path(my_graph, 'A')

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) 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 39

I just looked at my question and to my untrained eye this is like an infinite loop or a merry go round type of question.

instead of () common brackets use [] square brackets.

@aaronvincent6411 thanks for your suggestion. I remove the (common brackets)/parenthesis and put the square brackets instead and my code has not yet passed. What else can we do?

can u share the updated code

Okay. But how do I do that? I tried editing it here on the forum page but when I try to save it throws an error. What is the best way of updating my code?

In this post u will see how to share updated code

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)

    while unvisited:
        current = min(unvisited, key=distances.get)
        for node, distance in graph[current]:
            if distance + distances[current] < distances[node]:
                distance[node] = distance + distance[current]
    print(f'Unvisited: {unvisited}\nDistances: {distances}\nPaths: {paths}')
    
#shortest_path(my_graph, 'A')

Hello Zack

You are forgetting the s. You need to get it out of distances. Just like in your if statement. distance without the s is the literal distance integer from your graph.

2 Likes

Hi @HungryBee , thanks. I think one of the things baffling me is the neighbor node. Its like I don’t seem to understand the whole question anymore because it has stretched for so long. To get back to a level where I understand the whole question I think I might have to start all over again. A pity really. I thought I was beginning to understand all these algorithms. Well like I said somewhere else I am a slow learner so Back to GO and do not collect 200 pounds (that is Monopoly game)

2 Likes

It makes sense, it may also help to just take a small break and re-read the current code again and try to tell yourself what every specific part is doing so far. You’ve already gotten far though, once you start to understand it you’ll only go further!

Yaaay!!! @HungryBee . I got it Thanks. . I think I can decrement my Dunce level by 1 like so dunceLevel -=1

1 Like

Let’s do -= 10. This was a tough one to get! :blush:

Hey Zack, I feel your pain… I’m going over this module myself and I feel the same sentiments. As soon as I felt like I was getting it, it feel like the rug was swept from under me… All I can say is that we have to keep pushing through… It will all make sense one day at least thats what Im hoping… :pray:

1 Like