Back to Freecodecamp

Step 40

curriculum/challenges/english/blocks/learn-algorithm-design-by-building-a-shortest-path-algorithm/65578d0f6c78a0b868a43b9c.md

latest1.3 KB
Original Source

--description--

Inside the while loop, the first thing to do is define the current node to visit. For that you can use the min() function. It returns the smallest item from the iterable passed as the argument.

Remove pass, then create a variable called current and assign it min(unvisited).

--hints--

You should create a current variable in your while loop.

js
({ test: () => assert(runPython(`_Node(_code).find_function("shortest_path").find_whiles()[0].has_variable("current")`)) })

You should assign min(unvisited) to your current variable. Remember to delete pass.

js
({ test: () => assert(runPython(`_Node(_code).find_function("shortest_path").find_whiles()[0].find_bodies()[0].is_equivalent("current = min(unvisited)")`)) })

--seed--

--seed-contents--

py
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)
--fcc-editable-region--
    while unvisited:
        pass
--fcc-editable-region--    
    print(f'Unvisited: {unvisited}\nDistances: {distances}\nPaths: {paths}')
    
#shortest_path(my_graph, 'A')