Real World Applications of Shortest Path Algorithms

  • GPS Navigation
  • Network Routing
  • Transportation Planning
  • Airline Flight Planning
  • Social Network Analysis
  • Circuit Design

Shortest Path Algorithm Tutorial with Problems

In this article, we are going to cover all the commonly used shortest path algorithm while studying Data Structures and Algorithm. These algorithms have various pros and cons over each other depending on the use case of the problem. Also we will analyze these algorithms based on the time and space complexities which are frequently asked in coding interviews.

Table of Content

  • Types of Shortest Path Algorithms
  • 1. Shortest Path Algorithm using Depth-First Search(DFS
  • 2. Breadth-First Search (BFS) for Shortest Path Algorithm
  • 3. Multi-Source BFS for Shortest Path Algorithm
  • 4. Dijkstra’s Algorithm for Shortest Path Algorithm
  • 5. Bellman-Ford algorithm for Shortest Path Algorithm
  • 6. TopoLogical Sort
  • 7. Floyd-Warshall algorithm for Shortest Path Algorithm
  • 8. A* Search Algorithm for Shortest Path Algorithm
  • 9. Johnson’s Algorithm for Shortest Path Algorithm
  • Complexity Analysis For All Shortest Path Algorithms
  • Real World Applications of Shortest Path Algorithms

Similar Reads

What are the Shortest Path Algorithms?

The shortest path algorithms are the ones that focuses on calculating the minimum travelling cost from source node to destination node of a graph in optimal time and space complexities....

Types of Shortest Path Algorithms:

As we know there are various types of graphs (weighted, unweighted, negative, cyclic, etc.) therefore having a single algorithm that handles all of them efficiently is not possible. In order to tackle different problems, we have different shortest-path algorithms, which can be categorised into two categories:...

1. Shortest Path Algorithm using Depth-First Search(DFS):

DFS algorithm recursively explores the adjacent nodes untill it reaches to the depth where no more valid recursive calls are possible. For DFS to be used as a shortest path algorithm, the graph needs to be acyclic i.e. a TREE, the reason it won’t work for cyclic graphs is because due to cycles, the destination node can have multiple paths from the source node and dfs will not be able to choose the best path. If there does not exist a path between source node and destination node then we can store -1 as the shortest path between those nodes....

2. Breadth-First Search (BFS) for Shortest Path Algorithm:

BFS is a great shortest path algorithm for all graphs, the path found by breadth first search to any node is the shortest path to that node, i.e the path that contains the smallest number of edges in unweighted graphs. Reason: This works due to the fact that unlike DFS, BFS visits all the neighbouring nodes before exploring a single node to its neighbour. As a result, all nodes with distance ‘d‘ from the source are visited after all the nodes with distance smaller than d. In simple terms all the nodes with distance 1 from the node will be visited first, then distance 2 nodes, then 3 and so on....

3. Multi-Source BFS for Shortest Path Algorithm:

Like BFS, it is also applicable to unweighted graph. To perform a Multi-source search, we basically start BFS from multiple nodes at the same time. When all the BFS meet, we’ve found the shortest path....

4. Dijkstra’s Algorithm for Shortest Path Algorithm:

Dijkstra’s algorithm finds the shortest path from a source node to all other nodes in a weighted graph by iteratively selecting the node with the smallest tentative distance and updating the distances to its neighbours. It ensures the shortest path is progressively discovered and is based on the principle of greedy optimization....

5. Bellman-Ford algorithm for Shortest Path Algorithm:

Bellman-Ford is a single source shortest path algorithm that determines the shortest path between a given source vertex and every other vertex in a graph. This algorithm can be used on both weighted and unweighted graphs. A Bellman-Ford algorithm is also guaranteed to find the shortest path in a graph, similar to Dijkstra’s algorithm. Although Bellman-Ford is slower than Dijkstra’s algorithm, it is capable of handling graphs with negative edge weights, which makes it more versatile. The shortest path cannot be found if there exists a negative cycle in the graph. If we continue to go around the negative cycle an infinite number of times, then the cost of the path will continue to decrease (even though the length of the path is increasing). As a result, Bellman-Ford is also capable of detecting negative cycles, which is an important feature....

6. TopoLogical Sort:

Topological sorting can be used as a single source shortest path algorithm for Directed Acyclic Graphs (DAGs). This works because DAGs guarantees the non-existence of negative cycle. Working: In DAGs the nodes with indegree=0 will act as source node because of their unreachability from all other nodes. These source nodes are then put into a queue and are allowed to run a DFS traversal to the adjacent nodes to calcualte the shortest path for nodes one by one....

7. Floyd-Warshall algorithm for Shortest Path Algorithm:

The Floyd Warshall Algorithm is for solving all pairs of shortest-path problems. The problem is to find the shortest distances between every pair of vertices in a given edge-weighted directed Graph. It is an algorithm for finding the shortest path between all the pairs of vertices in a weighted graph. This algorithm follows the dynamic programming approach to find the shortest path....

8. A* Search Algorithm for Shortest Path Algorithm:

A* Search algorithm is one of the best and popular technique used in path-finding and graph traversals. Informally speaking, A* Search algorithms, unlike other traversal techniques, it has “brains”. What it means is that it is really a smart algorithm which separates it from the other conventional algorithms. This fact is cleared in detail in below sections. It is also worth mentioning that many games and web-based maps use this algorithm to find the shortest path very efficiently (approximation)....

9. Johnson’s Algorithm for Shortest Path Algorithm:

Using Johnson’s algorithm, we can find all pair shortest paths in O(V2log V + VE) time. Johnson’s algorithm uses both Dijkstra and Bellman-Ford as subroutines. If we apply Dijkstra’s Single Source shortest path algorithm for every vertex, considering every vertex as the source, we can find all pair shortest paths in O(V*VLogV) time. So using Dijkstra’s single-source shortest path seems to be a better option than Floyd Warshall’s Algorithm, but the problem with Dijkstra’s algorithm is, that it doesn’t work for negative weight edge. The idea of Johnson’s algorithm is to re-weight all edges and make them all positive, then apply Dijkstra’s algorithm for every vertex....

Complexity Analysis For All Shortest Path Algorithms:

Here V and E, denote the number of vertices and the number of Edges in the graph respectively....

Real World Applications of Shortest Path Algorithms:

GPS Navigation Network Routing Transportation Planning Airline Flight Planning Social Network Analysis Circuit Design...

Practice Problem related to Shortest Path Algorithms:

Problem Link Practice Minimum Cost Path with Left, Right, Bottom and Up moves allowed Link Shortest path in a Binary Maze Link Snake and Ladder Problem Link Shortest Path in Weighted undirected graph Link Shortest Path Using Atmost One Curved Edge Link Number of Ways to Arrive at Destination Link Shortest path in Undirected Graph having unit distance Link Sum of nodes on the longest path from root to leaf node Link...