Difference between Prim and Dijkstra's algorithms

I know they are different in what they do, but in terms of implementation they look identical to me save for one detail: the weight. The weight for the MST is just the weight of the edge, whereas for the shortest path it's the sum of the edge plus the distance it took to get to that node.

Is this correct? Is there any other difference I am missing? I'm just confused because they are name as different algorithms but they are so similar in my head they are the same thing, just applied with minimal differences to achieve a different result. I am also a bit confused because for the shortest path I keep hearing about something like "updating the values in the PORQUE", but:

a) can't you just add the new value and whatever is shortest wins?

b) Doesn't this same concept apply to Prim's algorithm if you want to keep a tidier PQ? I don't understand why it is named in connection more with Dijstkra than Prim when the updating purpose seems to be the same: avoiding duplicates in the PQ.

This next part is me thinking off the top of my head so it might not make any sense.

b.1) Is it worth it to implement updating the PQ? It seems like a more complicated implementation that would cause as much overhead as just as much as keeping the duplicates would cost. Specially if you have to go finding the node. That would make it probably even worse. I guess you could have a separate DS that cross references each other, but then you have duplicates anyways...although I guess just a max of 1.

πŸ‘︎ 24
πŸ’¬︎
πŸ‘€︎ u/choicesintime
πŸ“…︎ Jan 11 2022
🚨︎ report
Tips to speed up my Dijkstra algorithm up for day 15 part 2

Apologies for the damage to your retinas the below code will cause. As you won't need telling once you've read my code, I am very much a beginner! I read up on the Dijkstra algorithm and hacked together something that was capable of solving the first part, but it is massively too slow for the 2nd. Any pointers on where I should focus my efforts would be gratefully received. The code worked for the test case of part 2, but would probably still be running at the time of the heat death of the universe and still not returned a result.

import numpy as np
from copy import copy

def get_neighbours(coords, shape):
    x_max, y_max = shape
    x, y = coords
    neighbours = []
    if x + 1 <= x_max - 1:
        neighbours.append((x + 1, y))
    if x - 1 >= 0:
        neighbours.append((x - 1, y))
    if y + 1 <= y_max - 1:
        neighbours.append((x, y + 1))
    if y - 1 >= 0:
        neighbours.append((x, y - 1))
    return neighbours

def find_lowest_unvisited_node(visisted, distances):
    all_nodes = {(x, y) for x in range(distances.shape[0]) for y in range(distances.shape[1])}
    unvisisted = all_nodes - visisted
    min_node = np.inf
    for node in unvisisted:
        x, y = node
        if distances[x, y] < min_node:
            coords = (x, y)
            min_node = distances[x, y]
    return coords

def iterative_shortest_path(graph):
    node = (0, 0)
    distances = np.ones((data.shape[0], data.shape[1])) * np.inf
    distances[0, 0] = 0
    visisted = set()
    all_nodes = {(x, y) for x in range(distances.shape[0]) for y in range(distances.shape[1])}
    unvisisted = all_nodes - visisted
    while len(unvisisted) > 0 and (graph.shape[0], graph.shape[1] not in visisted):
        x, y = node
        neighbours = get_neighbours(node, graph.shape)
        for n in neighbours:
            a, b = n
            if distances[a, b] > distances[x, y] + graph[a, b]:
                distances[a, b] = distances[x, y] + graph[a, b]
        visisted.add(node)
        unvisisted = all_nodes - visisted
        if len(unvisisted) > 0:
            node = find_lowest_unvisited_node(visisted, distances)
    return distances

with open('D:/Users/jcaddick/aoc/aoc/2021/input_day_15.txt') as f:
    data = f.read().split('\n')
data = np.array([list(m
... keep reading on reddit ➑

πŸ‘︎ 9
πŸ’¬︎
πŸ‘€︎ u/Taxpayers_Money
πŸ“…︎ Jan 08 2022
🚨︎ report
How to "decrease priority" in a min-priority queue in Dijkstra's algorithm?

Wikipedia of Dijkstra's algorithm has the following pseudocode, which uses a min-priority queue:

1  function Dijkstra(Graph, source):
2      dist[source] ← 0                           // Initialization
3
4      create vertex priority queue Q
5
6      for each vertex v in Graph:          
7          if v β‰  source
8              dist[v] ← INFINITY                 // Unknown distance from source to v
9              prev[v] ← UNDEFINED                // Predecessor of v
10
11         Q.add_with_priority(v, dist[v])
12
13
14     while Q is not empty:                      // The main loop
15         u ← Q.extract_min()                    // Remove and return best vertex
16         for each neighbor v of u:              // only v that are still in Q
17             alt ← dist[u] + length(u, v)
18             if alt < dist[v]
19                 dist[v] ← alt
20                 prev[v] ← u
21                 Q.decrease_priority(v, alt)
22
23     return dist, prev

However, it's unclear how decrease_priority can be implemented in logarithmic time. Would anyone care to help?

πŸ‘︎ 19
πŸ’¬︎
πŸ‘€︎ u/Childish_Bob
πŸ“…︎ Jan 10 2022
🚨︎ report
[C++] Need to implement either Dijkstra’s or A* algorithm on a prebuilt 2d maze. I understand the theory of both but don’t know how to implement them? Does anyone know where I can start?

My lecturer has created a 20x20 maze where the player has to direct the starting player to the target. We need to build a pathfinding algorithm to automate the game. The lecture they covered this on has not been recorded. I have researched and understand the theory of both options, but I do not know how to implement these. Any help would be appreciated. Thanks

πŸ‘︎ 2
πŸ’¬︎
πŸ‘€︎ u/WhiskeyJoes
πŸ“…︎ Dec 28 2021
🚨︎ report
Dijkstra's Algorithm in 5 Steps with Python pythonalgos.com/2021/12/0…
πŸ‘︎ 21
πŸ’¬︎
πŸ‘€︎ u/help-me-grow
πŸ“…︎ Dec 08 2021
🚨︎ report
Graph Implementation with Dijkstra’s algorithm

Hello, this is a graph implementation that i made around two weeks ago and have been trying to improve it since then (take a look at first commit if you’re curious, using git to see my improvement progress). I’m planning to implement other pathfinding algorithms too and maybe visualizing the whole thing. Any advice would be appreciated! github link

πŸ‘︎ 3
πŸ’¬︎
πŸ‘€︎ u/MhamadRifaii
πŸ“…︎ Jan 14 2022
🚨︎ report
Dijkstra's shortest path algorithm in Desmos! v.redd.it/hyd0l3bi1xx71
πŸ‘︎ 69
πŸ’¬︎
πŸ‘€︎ u/vaultthestars
πŸ“…︎ Nov 06 2021
🚨︎ report
[2021 Day 15 (Part 1 & 2)][F#] Why is my implementation of Dijkstra’s Algorithm so slow?

Hello!

So this year I'm learning F# and functional programming. I could not figure out a immutable solution. So I went with an imperative, mutable solution and did pretty much a straight port from Red Blob Games including using a PriorityQueue.

Unfortunately, my implementation of Dijkstra’s Algorithm is much slower than what others are reporting for their implementation. Part 1 takes 13 seconds and Part 2 took 187 minutes!

Did I do something wrong when porting the code over to F#? Or is this particular implementation not optimized?

Would appreciate any insights! Thank you!

πŸ‘︎ 3
πŸ’¬︎
πŸ‘€︎ u/oddrationale
πŸ“…︎ Dec 16 2021
🚨︎ report
Dijkstra's Algorithm in 5 steps /r/HowToPython/comments/r…
πŸ‘︎ 10
πŸ’¬︎
πŸ‘€︎ u/help-me-grow
πŸ“…︎ Dec 08 2021
🚨︎ report
Attempting to implement a Dijkstras shortest path algorithm for a list of all imdb actors. - Info in comments
πŸ‘︎ 37
πŸ’¬︎
πŸ‘€︎ u/zeTheDoctor
πŸ“…︎ Oct 21 2021
🚨︎ report
Was taught Dijkstra's Algorithm in Math class and this sprung to mind.
πŸ‘︎ 6k
πŸ’¬︎
πŸ‘€︎ u/Andy-Banner
πŸ“…︎ Jun 14 2021
🚨︎ report
UNDERSTANDING DIJKSTRA PATHFINDING ALGORITHM dev.to/slickdev_raphael/u…
πŸ‘︎ 2
πŸ’¬︎
πŸ‘€︎ u/u-copycat
πŸ“…︎ Dec 16 2021
🚨︎ report
Dijkstra's Algorithm in 5 steps

Here's a simple breakdown of Dijkstra's Algorithm in Python

πŸ‘︎ 4
πŸ’¬︎
πŸ‘€︎ u/help-me-grow
πŸ“…︎ Dec 08 2021
🚨︎ report
Dijkstra’s algorithm number of relaxations done with n nodes and m edges

I thought the answer would be n-1 relaxations because we have to relax every other node other than the starting node.

I just went through a dijkstra example with 8 nodes and 12 nodes and I counted 9 relaxations which contradicts my previous statement unless I counted the relaxations wrong. Is there a way to pinpoint it that I'm not seeing?

πŸ‘︎ 3
πŸ’¬︎
πŸ‘€︎ u/2kfan
πŸ“…︎ Nov 29 2021
🚨︎ report
OpenOcean utilizes an optimized version of the Dijkstra algorithm

On-chain trading has never been so smooth before I came across the #OpenOcean protocol.

OpenOcean utilizes an optimized version of the Dijkstra algorithm (D-star) which then splits routing between different protocols for better transaction rates.

https://reddit.com/link/qzsqkw/video/349ggsll17181/player

πŸ‘︎ 6
πŸ’¬︎
πŸ‘€︎ u/Grabriel4
πŸ“…︎ Nov 22 2021
🚨︎ report
Dijkstra Algorithm Question.

I am trying to implement Dijkstra Algorithm for pathfinding in my 2D Grid (15x15 Array), So far the system works more or less but I have a few questions.

1.What is the best way to get/store neighbors? I currently do a nested for loop from -1 to 1 to get neighbors of the current node, but how well will neighbors storing in an array inside node class will work? (I guess this is more of a space vs time complexity question?)

2.weights, My current way of calculating weights is simple (A.x - B.x) and (A.y - B.y) this gives me distance after that i found a tutorial on youtube that had a very nice 10 value and 14 value formula for diagonal moves. tbh i don't understand it why give 14 value to diagonal nodes?

3.Okay now the most important one, after Path is found, I can't find another path until I reset the cost of each node back to maximum value or infinity. this is troublesome because it uses extra loops to do so any workaround on that?

πŸ‘︎ 2
πŸ’¬︎
πŸ‘€︎ u/Kismet_Valla
πŸ“…︎ Nov 11 2021
🚨︎ report
TIL Dijkstra invented Dijkstra's algorithm in 20 minutes in a coffee shop while shopping with his young fiance en.wikipedia.org/wiki/Dij…
πŸ‘︎ 892
πŸ’¬︎
πŸ‘€︎ u/CodeNamePika
πŸ“…︎ Apr 05 2021
🚨︎ report
Interactive Dijkstra's Algorithm

Hey everyone!

Recently I made an interactive pathfinding tool as a holiday project. I learnt how to use Dijkstra's algorithm in a networking subject, and thought it would be cool to implement it in code and make a handy tool for it.

This webpage allows you to build your own graph, then find the shortest path to all other nodes given any starting node. You can optionally ask it to walk you through each step of the algorithm too.

I found the algorithm a bit daunting at first, so I hope this helps anyone in the same position as me.

Link to the page is here - enjoy!

πŸ‘︎ 107
πŸ’¬︎
πŸ‘€︎ u/a-whale-in-a-tree
πŸ“…︎ Jul 02 2021
🚨︎ report
What is more important to know for FAANG interviews: Dijkstras or bellman Ford algorithm?
πŸ‘︎ 31
πŸ’¬︎
πŸ‘€︎ u/googleybruh
πŸ“…︎ Aug 18 2021
🚨︎ report
Dijkstra algorithm visualizer project v.redd.it/gjp4rcwu0sb71
πŸ‘︎ 18
πŸ’¬︎
πŸ‘€︎ u/Sincjefe
πŸ“…︎ Jul 17 2021
🚨︎ report
What is Dijkstra’s Algorithm? Examples and Applications of Dijkstra's Algorithm analyticssteps.com/blogs/…
πŸ‘︎ 2
πŸ’¬︎
πŸ‘€︎ u/analyticsstepss
πŸ“…︎ Oct 23 2021
🚨︎ report
Was taught Dijkstra's Algorithm in Math class and this sprung to mind.
πŸ‘︎ 391
πŸ’¬︎
πŸ‘€︎ u/Andy-Banner
πŸ“…︎ Jun 14 2021
🚨︎ report
Implementing Dijkstra's algorithm on the fly

I'm trying to use Dijkstra's algorithm to draw the shortest path between given source and destination vertices without needing to create the entire the adjacency list. I have most of the code done (according to the pseudocode below), but I can't figure out how to implement number 6. I think that this step needs to be done inside the while loop from number 5, but I can't figure out how to initiate the path with the destination and be able to update the next node as the while loop runs.

1. Initialize an empty priority queue `q` (use `PriorityQueue` class)
2. Get the source vertex (`source`) using the function `graph.get_vertex_from_coords(i,j)`.
3. Set the `source.d` field to 0 to indicate that distance of source from source is 0.
4. Add the source vertex to the priority queue (use `insert` method).
5. While the priority queue is not empty.
5.1 Get the vertex with minimum value of d and delete it (use `get_and_delte_min` function). Let's call this vertex `u`.
5.2 Set the processed field of `u` to True.
5.3 If `u` has the same coordinates as destination (use `u.x` and `u.y`) then 
    5.3.1 shortest path distance is `u.d` and break from the loop.
5.4 For each outgoing edge from `u` to `v` with weight `w` 
    5.4.1 If `v` is not already processed and `v.d > u.d + w` then 
          5.4.1.1 update `v.d` to `u.d + w`. Set `v.pi` to `u`.
          5.4.1.2 If `v` is already not in the priority queue, insert it into the queue
          5.4.1.3 Else, use the `update_vertex_weight` method of priority queue with `v` as the argument to make sure that `v` is moved to the appropriate place in the priority queue.
6. To get the path, start from the destination vertex and keep taking the parent pointer until we reach the source. Store the sequence of vertices in a path.
7. Return the (path, shortest path distance)
πŸ‘︎ 2
πŸ’¬︎
πŸ‘€︎ u/jynx_24
πŸ“…︎ Aug 18 2021
🚨︎ report
Dijkstra algorithm

https://www.youtube.com/watch?v=5GT5hYzjNoo&t=33s

Does this video explain Dijkstra algorithm wrongly? His method doesn't work for this graph when the objective is to get from node 0 to node 3. Or am I missing something very obvious as I usually do?

https://preview.redd.it/pozzlkzeq8l71.png?width=281&format=png&auto=webp&s=a8d41065856789035bd0e96801337dfd1161a9a9

πŸ‘︎ 3
πŸ’¬︎
πŸ‘€︎ u/SubhanKhanReddit
πŸ“…︎ Sep 03 2021
🚨︎ report

Please note that this site uses cookies to personalise content and adverts, to provide social media features, and to analyse web traffic. Click here for more information.