Imagine you are standing at Connaught Place in Delhi, and you open Google Maps. You want to find the shortest travel distance from your current location to different locations across Delhi.
Single Source Shortest Path (SSSP) algorithms are the solution to this problem. They let you find the shortest distance from a given source vertex to every other vertex in a graph.
Let's explore a few possible variations and understand how their approaches differ.
Naive Approach: Bellmand-Ford Algorithm
Bellman-Ford guarantees that one complete pass through all the edges of the graph, performing edge relaxation on each edge, discovers shortest paths containing at most one additional edge. This guarantee is based on the following two important observations:
-
Optimal Substructure Property: A shortest path from a source vertex to a destination is composed of shortest paths from the source to each of its intermediate vertices.
-
Maximum Number of Edges in a Shortest Path: In a graph with
(V)vertices, a shortest path can contain at most(V-1)edges, provided there is no reachable negative-weight cycle. This is because a shortest path never needs to repeat a vertex.
Implementation
Since each pass guarantees the discovery of shortest paths containing at most one additional edge, after (V-1)
passes, it's guaranteed to have discovered the shortest distances from the source to every reachable vertex.
Connaught Place is the starting point, so its distance is 0 km.
- From
- –
- To
- –
- Road length
- –
- Distance via this road
- –
Time Complexity
Space Complexity
Better Approach: Shortest Path Faster Algorithm (SPFA)
Bellman-Ford processes every edge in every pass, even when the edge's starting vertex has not received a better distance. The key observation behind SPFA is to only process the outgoing edges of vertices whose shortest distances have changed.
An improvement in one node's shortest path can lead to improvements in other nodes' shortest distances. Therefore, whenever we discover a shorter path to a node, we need to explore its neighbours again using the updated distance.
Implementation
SPFA uses a queue to keep track of vertices whose distances have improved and whose outgoing edges need to be explored.
Connaught Place is the starting point, so its distance is 0 km, and it is the only place waiting.
- From
- –
- To
- –
- Road length
- –
- Distance via this road
- –
Time Complexity
The time complexity depends on the the following operations:
-
Initializing the adjacency list: We iterate over all vertices to initialize their adjacency lists, which takes
O(V)time. We then iterate over all edges to populate the adjacency list. Since the graph is undirected, each edge is added to the adjacency lists of both endpoints. This takesO(E)time. Therefore, this takesO(V + E). -
Queue processing: A node can be enqueued and processed multiple times. Whenever we process a node, we explore all its neighbours. In the worst case, every single vertex gets updated and re-queued
(V - 1)times. f every vertex is poppedO(V)times and each pop checks all its outgoing edges, the sum of all edge checks across all vertices equalsO(V * E). ------ Need to explore this more ----- -
Converting unreachable nodes to
-1: We iterate over theanswerarray once to identify unreachable nodes and update their values to-1. Since the array contains(V)elements, this takesO(V).
Hence, the overall time complexity is O(V + E) + O(V * E) + O(V) = O(V * E).
Space Complexity
The space complexity is primarily determined by three data structures:
-
Adjacency list: Each of the
(V)vertices requires an adjacency list. Each of the(E)edges is stored twice because the graph is undirected. This takesO(V + E). -
answerarray: Since the array contains(V)elements, this takesO(V). -
Queue: In the standard SPFA implementation, we maintain an additional
inQueue[]array. Whenever we enqueue a node, we mark it as present in the queue. We only enqueue it again after it has been removed from the queue. This ensures that at any given time, each vertex can appear in the queue at most once. This takesO(V).
Hence, the overall space complexity is O(V + E). ------ Need to explore this more -----
Suitable Approach: Dijkstra's Algorithm
SPFA avoids unnecessary edge processing, but it still has an important limitation. It uses a regular FIFO queue. This means vertices are processed in the order they are added, not according to how close they are to the source. Key observations that make dijkstra work:
-
Out of all the nodes whose shortest path are not yet confirmed, we can pick the node with the smallest known distance from the source. Any alternative path to this node must pass through another unprocessed node. Since all edge weights are non-negative, that path cannot produce a shorter distance than the one we have already found.
-
Once we know the shortest path of a node from the source, we can explore its neighbours and attempt to improve their shortest known distances.
Implementation
The algorithm uses a greedy approach combined with a min-priority queue to always process the node with the smallest known distance from the source. By repeatedly exploring its neighbours and relaxing edges, we gradually determine the shortest paths to all reachable nodes.
Connaught Place is the starting point, so its distance is 0 km, and it is the closest place waiting.
- From
- –
- To
- –
- Road length
- –
- Distance via this road
- –
Time Complexity
The time complexity of Dijkstra's Algorithm can be broken down into four major operations:
-
Building the Adjacency List: We iterate over all vertices
(V)to initialize their adjacency lists and iterate over all edges(E)to populate them. This operation takesO(V + E). -
Adding nodes to the Priority Queue: Whenever we discover a shorter path to a neighbouring node, we insert an updated entry into the priority queue. Every successful relaxation corresponds to examining a particular edge. Hence, the number of successful relaxations is bounded by
(E). Considering each insertion into a binary min-heap takesO(V)time, the total node addition isO(E * logV). -
Removing nodes from the Priority Queue: Since there can be at most
(E)successful relaxations, there can be at most(E)extractions. Considering each extraction takesO(logV)time, the total node removal isO(E * logV). -
Exploring the neighbours of a node: Since each vertex is processed at most once, and all its adjacency entries are examined once, the total neighbour traversal is
O(V + E).
Combining all four operations, the overall time complexity is O(V + E) + O(E * logV) + O(E * logV) + O(V + E) = O(V + E + E * logV) ~ O(E * logV).
Space Complexity
Let's break it down into the different data structures we maintain.
-
Adjacency List: Every vertex has an adjacency list, requiring
O(V)space. Every edge is stored in the adjacency list. Therefore, this takesO(V + E). -
Distance array: We maintain a
dist[]array to store the shortest known distance from the source to every vertex. Since there are(V)vertices, the array requiresO(V). -
Priority queue: Since each successful relaxation can result in one insertion, and there are at most
O(E)successful relaxations, the priority queue can contain up toO(E)entries.
Therefore, the overall space complexity is O(V+E).