0% found this document useful (0 votes)
16 views4 pages

Dijkstra's

Dijkstra's algorithm is a method for finding the shortest path from a single source vertex to all other vertices in a graph, using a greedy approach to select the nearest unvisited vertex. It is applicable to both directed and undirected graphs but does not function with graphs that contain negative edge weights, for which the Bellman-Ford algorithm is recommended. The algorithm requires marking visited vertices and maintaining the current shortest distances as it iterates through the graph.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views4 pages

Dijkstra's

Dijkstra's algorithm is a method for finding the shortest path from a single source vertex to all other vertices in a graph, using a greedy approach to select the nearest unvisited vertex. It is applicable to both directed and undirected graphs but does not function with graphs that contain negative edge weights, for which the Bellman-Ford algorithm is recommended. The algorithm requires marking visited vertices and maintaining the current shortest distances as it iterates through the graph.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Dijkstra's Algorithm

Dijkstra's algorithm finds the shortest path from one vertex to all other
vertices.

It does so by repeatedly selecting the nearest unvisited vertex and


calculating the distance to all the unvisited neighboring vertices. The
algorithm uses a greedy approach in the sense that we find the next best
solution hoping that the end result is the best solution for the whole
problem.

Dijkstra's algorithm is used for solving single-source shortest path problems


for directed or undirected paths. Single-source means that one vertex is
chosen to be the start, and the algorithm will find the shortest path from that
vertex to all other vertices.

Dijkstra's algorithm does not work for graphs with negative edges. For graphs
with negative edges, the Bellman-Ford algorithm that is described on the
next page, can be used instead.

To find the shortest path, Dijkstra's algorithm needs to know which vertex is
the source, it needs a way to mark vertices as visited, and it needs an
overview of the current shortest distance to each vertex as it works its way
through the graph, updating these distances when a shorter distance is
found.

Example of Dijkstra's algorithm


It is easier to start with an example and then think about the algorithm.

Start with a weighted graph


Choose a starting vertex and assign infinity path values to all other devices

Go to each vertex and update its path length

If the path length of the adjacent vertex is lesser than new path length, don't update it
Avoid updating path lengths of already visited vertices

After each iteration, we pick the unvisited vertex with the least path length. So we choose 5
before 7

Notice how the rightmost vertex has its path length updated twice
Repeat until all the vertices have been visited

You might also like