# Learn algorithm design by building a shortest path algorithm step - 50

**URL:** <https://forum.freecodecamp.org/t/learn-algorithm-design-by-building-a-shortest-path-algorithm-step-50/736278>\
**Category:** Curriculum Help\
**Tags:** python\
**Created:** [February 23, 2025, 4:22pm UTC](https://forum.freecodecamp.org/t/learn-algorithm-design-by-building-a-shortest-path-algorithm-step-50/736278 "2025-02-23T16:22:35Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![swaydoesitall](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/swaydoesitall/32/348510_2.png) [@swaydoesitall](https://forum.freecodecamp.org/u/swaydoesitall)\
**Post date:** [February 23, 2025, 4:22pm UTC](https://forum.freecodecamp.org/t/learn-algorithm-design-by-building-a-shortest-path-algorithm-step-50/736278/1 "2025-02-23T16:22:36Z")

</div>

Can you all help me with this?

 ![image](https://global.discourse-cdn.com/freecodecamp/original/4X/f/0/e/f0e9b14cb0e9322e9fdef9d467efdc8a6bb00c80.png)

---

<div class="post-metadata">

**Author:** ![stephenmutheu](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/stephenmutheu/32/421476_2.png) [@stephenmutheu](https://forum.freecodecamp.org/u/stephenmutheu)\
**Post date:** [February 23, 2025, 4:34pm UTC](https://forum.freecodecamp.org/t/learn-algorithm-design-by-building-a-shortest-path-algorithm-step-50/736278/2 "2025-02-23T16:34:46Z")

</div>

Hi.

Instead of a screenshot, can you include actual code, its easieier for campers to work with.

---

<div class="post-metadata">

**Author:** ![pkdvalis](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/pkdvalis/32/350571_2.png) [@pkdvalis](https://forum.freecodecamp.org/u/pkdvalis)\
**Post date:** [February 23, 2025, 4:52pm UTC](https://forum.freecodecamp.org/t/learn-algorithm-design-by-building-a-shortest-path-algorithm-step-50/736278/3 "2025-02-23T16:52:16Z")

</div>

If you have a question about a specific challenge as it relates to your written code for that challenge and need some help, click the **Help** button located on the challenge. This button only appears if you have tried to submit an answer at least three times.

 ![image](https://global.discourse-cdn.com/freecodecamp/original/4X/8/4/5/845a0766ef6ab46f557dbba35f20c53e6dc49387.png)

The **Help** button will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.

---

<div class="post-metadata">

**Author:** ![swaydoesitall](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/swaydoesitall/32/348510_2.png) [@swaydoesitall](https://forum.freecodecamp.org/u/swaydoesitall)\
**Post date:** [February 23, 2025, 5:05pm UTC](https://forum.freecodecamp.org/t/learn-algorithm-design-by-building-a-shortest-path-algorithm-step-50/736278/4 "2025-02-23T17:05:02Z")

</div>

```auto
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]:
                distances[node] = distance + distances[current]
               if paths[node] != [] and paths[node][-1] == node:
                                  paths[node] = paths[current]

```

---

<div class="post-metadata">

**Author:** ![pkdvalis](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/pkdvalis/32/350571_2.png) [@pkdvalis](https://forum.freecodecamp.org/u/pkdvalis)\
**Post date:** [February 23, 2025, 6:38pm UTC](https://forum.freecodecamp.org/t/learn-algorithm-design-by-building-a-shortest-path-algorithm-step-50/736278/5 "2025-02-23T18:38:05Z")

</div>

> [@swaydoesitall](#):
>
> `paths[node] != []`

Can you think of a way to simplify this logic?

Please provide a link to the step you are working on.

---

<div class="post-metadata">

**Author:** ![pkdvalis](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/pkdvalis/32/350571_2.png) [@pkdvalis](https://forum.freecodecamp.org/u/pkdvalis)\
**Post date:** [February 23, 2025, 6:38pm UTC](https://forum.freecodecamp.org/t/learn-algorithm-design-by-building-a-shortest-path-algorithm-step-50/736278/6 "2025-02-23T18:38:39Z")

</div>

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.

 ![Pre-formatted-text](https://global.discourse-cdn.com/freecodecamp/original/3X/1/b/1b18854f1059217f29370444396145cc3a0cd8ae.gif)

See [this post](https://forum.freecodecamp.org/t/forum-code-formatting/25574) to find the backtick on your keyboard.  
**Note:** Backticks (`) are not single quotes (').

---

<div class="post-metadata">

**Author:** ![pkdvalis](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/pkdvalis/32/350571_2.png) [@pkdvalis](https://forum.freecodecamp.org/u/pkdvalis)\
**Post date:** [February 23, 2025, 6:44pm UTC](https://forum.freecodecamp.org/t/learn-algorithm-design-by-building-a-shortest-path-algorithm-step-50/736278/7 "2025-02-23T18:44:13Z")

</div>

Closed on February 23, 2025, 6:44pm UTC


