DAA: Dijkstra’s Algorithm (Shortest Path)

Dijkstra’s Algorithm (Shortest Path)

Dijkstra’s algorithm finds the shortest distance from a source to all the vertices in a graph. This algorithm is used in network protocols like IS-IS and OSPF(Open source shortest path first).

For example, Suppose we draw a graph in which nodes represent the cities, and weighted edges represent the driving distances between pairs of cities connected by a direct road. When Dijkstra’s algorithm is applied,  it gives the shortest route between one city and all other cities.

Visualization

Take a graph with weights on edges.

Dijkstra’s Algorithm (Shortest Path)

Select a vertex and assign all others as infinity from that vertex. Here the vertex chosen will be marked as 0.

Dijkstra’s Algorithm (Shortest Path)

Go to its adjacent vertex and change the length as the weight on that edge to that vertex.

Dijkstra’s Algorithm (Shortest Path)

If the adjacent path length is less than the new path length, we do not change.

Dijkstra’s Algorithm (Shortest Path)

The length of already visited vertices will not change.

Dijkstra’s Algorithm (Shortest Path)

The vertex with minimum path length will be visited, So we choose 5 before 7.

Dijkstra’s Algorithm (Shortest Path)

The rightmost vertex will change two times.

Dijkstra’s Algorithm (Shortest Path)

Repeat till all the vertex are visited.

Dijkstra’s Algorithm (Shortest Path)

Steps to implement the algorithm

Keeping the above steps in mind, let us look at the algorithm.

Algorithm

  1.  To keep track of vertices included in the shortest-path, we create a set sptSet (shortest path set). Initially, the set is empty.
  2.  Like the above illustration, declare all distance values as INFINITE. The picked vertex will have a distance of 0.
  3. Until all the vertexes are in sptSet
  4. Take a vertex u, not in the spSet, and has a minimum value and add to sptSet.
  5. Change distance value of all adjacent vertices of u. To update the distance values, loop through all adjacent vertices. For every adjacent vertex v, if the sum of a distance value of u (from source) and weight of edge u-v is less than the distance value of v, then update the distance value of v.

NOTE

  1. A vertex v will be in SPT if spset[v] is true.
  2.  Array distance[]  stores the shortest distance of every vertex.

C++ code:

 #include <bits/stdc++.h>
 using namespace std;
 const int numberVertex = 10; // The number of vertex in graph
 // To get minimum distance from adjacent nodes
 int minDistance(int distance[], bool ShortestPathTree[])
 {
           // Initialise min
     int min = INT_MAX, min_index;
           // Iterate in adjacent vertices
     for (int vertex = 0; vertex < numberVertex; vertex++) {
         if (ShortestPathTree[vertex] == false && distance[vertex] <= min) {
             min = distance[vertex];
             min_index = vertex;
         }
     }
     return min_index;
 }
 void dijkstra_Algo(int graph[numberVertex][numberVertex], int source)
 {
     int distance[numberVertex]; // to keep minimum distance from source to the vertexes
     bool ShortestPathTree[numberVertex];
     for (int i = 0; i < numberVertex; i++) {
         distance[i] = INT_MAX;
         ShortestPathTree[i] = false;
     }
     distance[source] = 0; // Initially distance from source is 0
     for (int count = 0; count < numberVertex - 1; count++) {
         int u = minDistance(distance, ShortestPathTree);
         ShortestPathTree[u] = true;
         for (int vertex = 0; vertex < numberVertex; vertex++)
             if (ShortestPathTree[vertex] == 0 && graph[u][vertex] != 0 && distance[u] != INT_MAX && distance[u] + graph[u][vertex] < distance[vertex])
                 distance[vertex] = distance[u] + graph[u][vertex];
     }
     for (int i = 0; i < numberVertex; i++)
         cout << "Node " << i << "\t\t"
              << "Distance " << distance[i] << endl;
 }
 int main()
 { // Create the graph with numberVertex
     int graph[numberVertex][numberVertex] = { { 0, 14, 0, 7, 0, 0, 0, 8, 0, 10 },
         { 14, 0, 8, 0, 0, 0, 0, 11, 0, 0 },
         { 0, 8, 0, 7, 0, 4, 0, 0, 2, 0 },
         { 7, 0, 7, 0, 9, 12, 0, 0, 0, 5 },
         { 0, 0, 0, 9, 0, 0, 0, 0, 0, 0 },
         { 0, 0, 4, 0, 0, 0, 2, 0, 0, 11 },
         { 0, 0, 0, 12, 0, 2, 0, 1, 6, 15 },
         { 8, 11, 0, 0, 0, 0, 1, 0, 7, 0 },
         { 0, 0, 2, 0, 0, 0, 6, 7, 0, 0 },
         { 10, 0, 0, 5, 0, 11, 15, 0, 0, 0 } };
     dijkstra_Algo(graph, 0);
     return 0;
 } 

Output:

Dijkstra’s Algorithm (Shortest Path)

Uses:

1) It is used in Google Maps and finding Shortest Path.

2) Social networking.

3) Flight agenda.


Related Topics

DAA: Dijkstra’s Algorithm (Shortest Path)

Dijkstra’s Algorithm (Shortest Path) Dijkstra’s algorithm finds the shortest distance from a source to all the vertices in a graph. This algorithm is used in network protocols like IS-IS and OSPF(Open...

3 minutes read.

Introduction to Sorting in DAA

DAA: What is Sorting? The technique in which a data structure is rearranged in decreasing order, increasing order, or in a specified order is called sorting. We apply to sort in our...

4 minutes read.

DAA: Application of DFS and BFS

Application of DFS and BFS Depth-first search and breadth-first searches are the most famous algorithms used in daily life and the programming world. Let us now explore each application in which...

3 minutes read.

DAA: Algorithm of Right View of a Binary Tree

Algorithm of Right View of a Binary Tree The right view of a binary tree is the visible nodes from the right side of the tree. In the given tree, the visible...

5 minutes read.

DAA: Bottom view of a Binary Tree

Bottom view of a Binary Tree The bottom view of a binary tree is the number of nodes visible when viewed from the bottom. At every horizontal distance, there would be...

3 minutes read.

DAA: Bead Sort Algorithm

Bead Sort Algorithm The bead sort is also known as the gravity sort algorithm. The algorithm is based on the natural phenomena of gravity. The phenomenon is the falling of things...

3 minutes read.

DAA: Find the Height or Maximum Depth of a Binary Tree

Find the Height or Maximum Depth of a Binary Tree We have a binary tree structure and we need to find its height. It is defined by the distance from the...

3 minutes read.

DAA: Breadth First Search (BFS) for a Graph

Breadth First Search (Bfs) For A Graph The algorithm in which all the graph nodes are traversed is known as the breadth-first search algorithm. In this algorithm, we select one node,...

5 minutes read.

DAA: Algorithm to Find the Maximum Width of a Tree

Algorithm to Find the Maximum Width of a Tree The width of a binary tree is defined as the maximum number of nodes at a given level. The level having the...

5 minutes read.

DAA: Bubble Sort Algorithm on Linked List

Bubble Sort Algorithm on Linked List In this article, we will sort a Link List using the bubble sort technique. Example: Input : 20->30->40->10 Output :10->20->30->40 Input : 20->4->3 Output : 3->4->20 Sorting Technique The bubble sort technique...

4 minutes read.

Recurrence relation in DAA

Recurrence relation in DAA The model that uses mathematical concepts to calculate the time complexity of an algorithm is known as the recurrence relational model. A recursive relation, T(n), is a recursive...

5 minutes read.

Symmetric Trees in DAA

Symmetric Trees The trees that are mirror images of themselves are known as symmetric trees. Look at the following tree image below: The tree is symmetric as the left subtree is the mirror...

4 minutes read.

DAA: Insert a node in Binary Search Tree

Insert a node in Binary Search Tree (BST) We have a Binary search tree and a key. Insert the key in the binary search tree if not present. In the above figure,...

4 minutes read.

Invert Binary Tree in DAA

Invert Binary Tree: A binary tree is a tree in which each node of the tree contains two children, i.e., left children and right children. Let us suppose we have...

2 minutes read.

DAA: Rabin Karp Algorithm

Rabin Karp Algorithm The Rabin Karp or Karp Rabin algorithm is used to matching a specific pattern in the string. It uses the technique of hashing to match a specific text. There also...

6 minutes read.

DAA: Density of a Binary Tree Algorithm

The Density of a Binary Tree Algorithm The density of a binary tree is defined as the ratio of the tree’s size to the tree’s height.  The height of the tree is...

2 minutes read.

DAA: Continuous Tree

Continuous Tree A continuous tree is the one in which the nodes from root to leaf path, the two adjacent node values, have a difference of 1. Input :          3                     /   \                   ...

5 minutes read.

DAA: Interpolation Search Algorithm

Interpolation Search Algorithm There is no doubt that binary search is a great algorithm with average time complexity of log n. The feature of discarding one half of the array reduces...

4 minutes read.

DAA: Insertion Sort Algorithm on Singly Link List

Insertion Sort Algorithm on Singly Link List We will sort a singly link list using the bubble sort technique. Example: Input : 20->30->40->10 Output :10->20->30->40 Input : 20->4->3 Output : 3->4->20 Sorting Technique The insertion sort technique works...

3 minutes read.

DAA: Depth-First Search Algorithm

Depth-first search: DFS is a traversing algorithm of a graph or tree in which one node is taken as arbitrary, and with the help of that arbitrary node, all its...

6 minutes read.