Graph Data Structure 4. Dijkstra’s Shortest Path Algorithm

Computer Science Lessons · 1.6M views · 2016 · 10:52

Open on YouTube

Algorithms & data structuresEducation & learningexplainer

Why Stack Overflow recommends it

Stack Overflow users link it as a short, clear walkthrough of Dijkstra's algorithm for people implementing or learning it, including askers who say they followed it.

Recommended by 4 people across 4 different months, 2016 to 2022.

What it is

A 10-minute explainer from the Computer Science Lessons channel, part four of a graph data structure series, walking through Dijkstra's shortest path algorithm. It shows how the algorithm builds shortest paths from a start vertex to every other vertex and why it counts as a greedy algorithm.

What people said

Recommends john · 2018-05 · Stack Overflow · on “Dijkstra algorithm: should it work if there are 2 graphs that are not connected?”

@juvian I don't get where I am mistaking... I followed the explanation of this guy [this video] and implemented it myself

Recommends Nolan R · 2020-04 · Stack Overflow · on “Algorithm for finding an entity in a maze without prior knowledge”

I think the most applicable algorithm for this sort of problem would be Dijkstra's algorithm. [LINK] In short, the algorithm starts at some root node, scans all neighbors and choose the node with the shortest path from the root to visit. A table is kept that contains every node's Shortest path from the root The last node in the path to get to this node The shortest path to a node is updated in the table whenever a shorter path is discovered. When your entity is visited, its shortest path will be the shortest path from root->entity, and backtracking through its parents will yield the actual node path. (Here's another video [this video] if you're confused).

Recommends Luis · 2020-06 · Stack Overflow · on “How to find the farthest two points in a randomly generated maze?”

There is an algorithm called Dijkstra's algorithm that was originally designed to find he shortest route between two points. This sounds like the opposite to what you are looking for, but if you learn the algorithm, you can use the conditions to get the opposite: find the longest route. Here is the wiki explaining the algorithm: [LINK] Here's a video explaining the algorithm: [this video]

Recommends KKJ · 2020-11 · Stack Overflow · on “What kind of graph I need for Dijkstra's algorithm? C++”

Dijkstra's algorithm is a variation of a BFS traversal. The difference is, it utilizes a min heap to traverse to the closest node every time. Your function that randomly generates a weighted undirected graph should not create a pair of nodes where node_a and node_b have a distance of 0. This means the node is in the same exact place - thus, no need to traverse. Dijkstra's algorithm works on any weighted graphs (with positive only weights) where you have a starting and an end condition. Here's a 10 minute video explaining the algorithm: [this video]

Cites Kumayl Fazal · 2016-11 · Stack Overflow · on “Dijkstra’s Algorithm in python”

I am trying to implement Dijkstra’s algorithm on my python code but I can't really get the algorithm right. The algorithm I am using is from this youtube link: [this video] So basically my class has these 3 variables: self.nodes = [] #a,b,c self.neighbours = {} # a:[b,c], b:[c], c:[a] self.weights = {} #[a,b] = 2, [a,c] = 5 Here is how I partially implemented my shortest path function using the algorithm provided in the video: def dijkstra(self, start, end): nodes = {} for n in self.nodes: if n == start: nodes[n] = 0 else: nodes[n] = float('inf') unvisited = self.neighbours visited = [] current_node = start current_distance = 0 while unvisited: for n in unvisited[current_node]: print(n) #calc_wei

Cites Joachim · 2022-03 · Stack Overflow · on “Djikstra's algorithm with change in cost function”

I am facing the following coding challenge question: "You are at start location [0, 0] in mountain area of NxN and you can only move in one of the four cardinal directions (i.e. North, East, South, West). Return minimal number of climb rounds to target location [N-1, N-1]. Number of climb rounds between adjacent locations is defined as difference of location altitudes (ascending or descending). Location altitude is defined as an integer number (0-9)." So for example, the result of "010", "010", "010" Will be 2 because we climb in [0,1] from 0 to 1 and in [2,2] we go down from 1 to 0. My idea is to implement the Dijkstra's algorithm, with a change in the cost function. The distance will be gi