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

Tell us what’s happening:

i don’t know how to do next, can someone help me ?

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
        else:
            distances[node] = float('inf')
    print(f'Unvisited: {unvisited}\nDistances: {distances}')
    

# 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/128.0.0.0 Safari/537.36 Edg/128.0.0.0

Challenge Information:

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

can you explain what issue are you having? what do you find confusing of the instructions and implementing them?

Now, I have to call my function passing my_graph and ‘A’ as the arguments.

Ok, and what issue are you having with calling the function?

I tried this: shortest_path(my_graph, ‘A’) and the code doesn’t pass

and where did you try that? can you show your 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 = []
    distances = {}
    for node in graph:
        unvisited.append(node)
        if node == start:
            distances[node] = 0
        else:
            distances[node] = float('inf')
    print(f'Unvisited: {unvisited}\nDistances: {distances}')
    shortest_path(my_graph, 'A')

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

you can’t call the function inside the function for it to work, remember the importance of indentation

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