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

Tell us what’s happening:

OK, I fixed that but now it says “You should nest an if statement to check that node is equal to start inside your for loop” even though I already did. Output prints so I genuinely don’t know the issue here.

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, target = ''):
    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]:
                distances[node] = distance + distances[current]
                if paths[node] and paths[node][-1] == node:
                    paths[node] = paths[current][:]
                else:
                    paths[node].extend(paths[current])
                paths[node].append(node)
        unvisited.remove(current)
# User Editable Region
    targets_to_print = [target] if target else graph
    

    for node in targets_to_print:
     if node == start:
        continue
     print(f'\n{start}-{node} distance: {distances[node]}\nPath: {" -> ".join(paths[node])}')
    
shortest_path(my_graph, 'A')
# User Editable Region

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.3 Safari/605.1.15

Challenge Information:

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

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/learn-algorithm-design-by-building-a-shortest-path-algorithm/6559d86fe1b8947954b9178d.md at main · freeCodeCamp/freeCodeCamp · GitHub

Welcome to the forum @gphalen52,

Please reset this step and try again, being careful not to change the indentation of the print when you add the if statement above it.

Happy coding

I reset it, didn’t touch the “print” statement, still getting the same error

Please share your 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, target = ‘’):

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]:

            distances\[node\] = distance + distances\[current\]

if paths[node] and paths[node][-1] == node:

                paths\[node\] = paths\[current\]\[:\]

else:

                paths\[node\].extend(paths\[current\])

            paths\[node\].append(node)

    unvisited.remove(current)

targets_to_print = \[target\] if target else graph

for node in targets_to_print:

if node == start:

continue

print(f’\n{start}-{node} distance: {distances[node]}\nPath: {" → ".join(paths[node])}')

shortest_path(my_graph, ‘A’)

Please re-post your code formatted as follows:

There are two ways you can format your code to make it easier to read and test:

  1. After you copy/paste your code into the editor, select it by dragging your cursor over it then click the (</>) button in the toolbar to automatically wrap your code in backticks. (You can click on the animated demo image below to enlarge it.)

  1. Manually add three backticks on a new line above your code and on a new line after your code. Note that a backtick is NOT the same as a single quote('). To find the backtick key on your keyboard, see this post.

To see changes to your post as you make them, you can click the (M+) button on the toolbar to bring up the rich text editor:

Thank you.

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, target = ''):
    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]:
                distances[node] = distance + distances[current]
                if paths[node] and paths[node][-1] == node:
                    paths[node] = paths[current][:]
                else:
                    paths[node].extend(paths[current])
                paths[node].append(node)
        unvisited.remove(current)
    targets_to_print = [target] if target else graph
    for node in targets_to_print:
        if node == start:
          continue
        print(f'\n{start}-{node} distance: {distances[node]}\nPath: {" -> ".join(paths[node])}')
    
shortest_path(my_graph, 'A')

This is not indented correctly.

You’re correct, Python indentation still trips me up sometimes. Thanks.