Shortest Path Algorithm in C++
Last Updated :
26 Sep, 2024
The shortest path algorithms are used to find the most efficient route from source node to destination node of a graph in optimal time and space complexities. In this article, we will learn how to implement some commonly used shortest path algorithm in C++.
Types of Shortest Path Algorithms
There is no single algorithm that can solve all types of shortest path problems efficiently due to the different characteristics of graphs. Therefore, we have multiple algorithms to solve specific types of shortest path problems that can be classified into two categories:
Single Source Shortest Path Algorithms
These algorithms find the shortest path from a single source node to all other nodes in the graph. Some of the commonly used algorithms are:
- Depth-First Search (DFS)
- Breadth-First Search (BFS)
- Dijkstra’s Algorithm
- Bellman-Ford Algorithm
- A* Search Algorithm
All Pair Shortest Path Algorithms
Unlike single source algorithms, these algorithms calculate the shortest path between all pairs of nodes.
- Floyd-Warshall Algorithm
- Johnson’s Algorithm
Let's discuss each of these separately.
Dijkstra’s Algorithm in C++
Dijkstra’s algorithm finds the shortest path from a source node to all other nodes in a weighted graph by selecting the node with the smallest distance and updating the distances to its neighbours. It finds the shortest path using a greedy approach while building the shortest path tree.
When to use?
Dijkstra's Algorithm in C++ is used when you need to find the shortest path from a single source node to all other nodes in a weighted graph with non-negative edge weights. This algorithm will not work if the graph has negative edge weight or negative cycle.
Complexity Analysis
Time Complexity: O(E * log(V)) where E is the number of edges and V is the number of vertices.
Auxiliary Space: O(V)
Bellman-Ford Algorithm in C++
The Bellman-Ford algorithm computes the shortest path from a source node to all other nodes in a graph, even in the presence of negative edge weights. It works by relaxing all edges repeatedly, and it can detect negative weight cycles.
When to use?
The Bellman-Ford Algorithm is best used when you need to find the shortest path in a weighted graph that may contain negative edge weights. It is particularly useful when detecting negative weight cycles, which Dijkstra's Algorithm cannot handle.
Complexity Analysis
Time Complexity: O(V * E), where V is the number of vertices and E is the number of edges.
Auxiliary Space: O(V)
Floyd-Warshall Algorithm in C++
The Floyd-Warshall algorithm finds the shortest paths between all pairs of vertices in a weighted graph by considering all possible paths through an intermediate vertex. It can also detect the negative weight cycle.
When to use?
The Floyd-Warshall Algorithm is used to find the shortest paths between all pairs of nodes in a weighted graph. It works for both positive and negative edge weights, but it cannot handle graphs with negative weight cycles. It is also preferred when the graph is dense (many edges), and you are dealing with a relatively small number of vertices.
Complexity Analysis
Time Complexity: O(V3)
Auxiliary Space: O(V2)
Johnson’s Algorithm in C++
Johnson’s algorithm is efficient for sparse graphs. It reweights the graph to remove negative weights using Bellman-Ford and then applies Dijkstra’s algorithm for each vertex.
When to use?
Johnson's Algorithm is used to find the shortest paths between all pairs of nodes in a weighted graph. It is particularly useful when the graph has negative edge weights but no negative weight cycles. It is more efficient for graphs with fewer edges compared to Floyd-Warshall.
Complexity Analysis
Time Complexity: O(V² log V + VE) , Where V is the number of vertices and E is the number of edges.
Auxiliary Space: O(V²) , Where V is the number of vertices.
A* Search Algorithm in C++
The A* search algorithm is an informed search algorithm that finds the shortest path using heuristics to speed up the process. It combines the actual cost from the start node with the estimated cost to the goal node to prioritize nodes.
When to use?
The A* Search Algorithm in C++ is best used when you need to find the shortest path in a weighted graph and you want the algorithm to work efficiently by using a heuristic to guide the search. It is ideal for situations where the graph is large and the search space needs to be narrowed
Complexity Analysis
Time Complexity: O(E), where E is the number of edges.
Auxiliary Space: O(V)
Comparison of Different Shortest Path Algorithms
The below table illustrates the comparison among commonly used shortest path algorithms in C++.
Algorithm | Time Complexity | Space Complexity | Handles Negative Weights |
---|
Dijkstra’s | O(V2) or O((V+E)log V) with min-heap | O(V) | No |
---|
Bellman-Ford | O(V * E) | O(V) | Yes |
---|
A* Search | O(E) | O(V) | No |
---|
Floyd-Warshall | O(V³) | O(V²) | Yes |
---|
Johnson’s | O(V² log V + VE) | O(V²) | Yes |
---|
Applications of Shortest Path Algorithms
Following are the real-world applications of the shortest path algorithms:
- Used to finding the shortest or fastest route between two locations.
- Used for determining the most efficient path for data packets in a computer network.
- Useful in identifying the shortest connections between two individuals.
- Used to path planning for autonomous vehicles or robots.
- To optimize delivery routes or public transportation systems.
Similar Reads
Kahnâs Algorithm in C++
In this post, we will see the implementation of Kahnâs Algorithm in C++. What is Kahnâs Algorithm?Kahnâs Algorithm is a classic algorithm used for topological sorting of a directed acyclic graph (DAG). Topological sorting is a linear ordering of vertices such that for every directed edge u -> v,
4 min read
Prim's Algorithm in C++
Prim's Algorithm is a greedy algorithm that is used to find the Minimum Spanning Tree (MST) for a weighted, undirected graph. MST is a subset of the graph's edges that connects all vertices together without any cycles and with the minimum possible total edge weight In this article, we will learn the
6 min read
Johnson Algorithm in C++
Johnsonâs Algorithm is an algorithm used to find the shortest paths between all pairs of vertices in a weighted graph. It is especially useful for sparse graphs and can handle negative weights, provided there are no negative weight cycles. This algorithm uses both Bellman-Ford and Dijkstra's algorit
8 min read
Floyd-Warshall Algorithm in C++
The Floyd-Warshall algorithm is a dynamic programming technique used to find the shortest paths between all pairs of vertices in a weighted graph. This algorithm is particularly useful for graphs with dense connections and can handle both positive and negative edge weights, though it cannot handle n
4 min read
Data Structures and Algorithms (DSA) in C++
Data Structures and Algorithms (DSA) are fundamental part of computer science that allow you to store, organize, and process data in ways that maximize performance. This tutorial will guide you through the important data structures and key algorithms using C++ programming language. Why Learn DSA Usi
7 min read
Convex Hull Algorithm in C++
The Convex Hull problem is fundamental problem in computational geometry. In this, we need to find the smallest convex polygon, known as the convex hull, that can include a given set of points in a two-dimensional plane. This problem has various applications in areas such as computer graphics, geogr
15+ min read
std::is_sorted in C++
In C++, is_sorted() is a built-in function used to check whether the element of the given range is sorted or not in ascending order. In this article, we will learn about is_sorted() function in C++. Letâs take a quick look at a simple example that illustrates is_sorted() method: [GFGTABS] C++ #inclu
4 min read
Bellman Ford Algorithm in C++
The Bellman-Ford algorithm is a single-source shortest path algorithm that finds the shortest path from a given source vertex to all other vertices in a graph. Unlike Dijkstraâs algorithm, Bellman-Ford can handle graphs with negative edge weights, making it useful in various scenarios. In this artic
5 min read
Bellman-Ford Algorithm in C
The Bellman-Ford algorithm helps find the shortest path from one starting point to all other points in a graph, even if some paths have negative weights. It's useful for network routing problems. In this article, we will learn about the Bellman-Ford algorithm and how to implement it in C. The Bellma
4 min read
Learn DSA in C++: Master Data Structure and Algorithm in C++
Data Structures and Algorithms (DSA) are fundamental parts of computer science that allow you to store, organize, and process data in ways that maximize performance. This tutorial will guide you through the important data structures and algorithms using C++ programming language. Why Learn DSA in C++
9 min read