Example output
### Problem Definition and Constraints:
**Problem:** Find the shortest path in a weighted graph from a given source vertex to a destination vertex.
**Constraints:**
- The graph is directed and may contain cycles.
- Each edge has a non-negative weight.
- The graph may be sparse or dense.
- The number of vertices (n) and edges (m) can vary.
- The graph may be represented as an adjacency matrix or adjacency list.
### Approach:
#### Dijkstra's Algorithm:
Dijkstra's algorithm is a common and efficient algorithm for finding the shortest path in a weighted graph. It works well when all edge weights are non-negative. The algorithm maintains a set of vertices whose shortest distance from the source is known and continuously updates the distances to neighboring vertices.
**Time Complexity:** O((V + E) log V) with a binary heap as the priority queue.
**Space Complexity:** O(V + E) for the distance array and priority queue.
#### Step-by-Step Explanation:
1. **Initialization:**
- Create an array `dist` to store the shortest distances from the source to each vertex.
- Initialize `dist` with infinity for all vertices except the source, set `dist[source]` to 0.
- Use a priority queue (min heap) to store vertices and their distances.
2. **Main Loop:**
- Repeat until the priority queue is not empty:
- Extract the vertex `u` with the minimum distance from the priority queue.
- For each neighbor `v` of `u`:
- If `dist[u] + weight(u, v) < dist[v]`, update `dist[v]`.
- Enqueue `v` with the updated distance.
3. **Implementation:**
```cpp
#include <iostream>
#include <vector>
#include <queue>
#include <limits>
using namespace std;
#define INF numeric_limits<int>::max()
// Function to add an edge to the graph
void addEdge(vector<pair<int, int>> adj[], int u, int v, int weight) {
adj[u].emplace_back(v, weight);
}
// Dijkstra's Algorithm
void dijkstra(vector<pair<int, int>> adj[], int V, int src) {
// Priority queue to store vertices and their distances
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
// Distance array to store the shortest distances
vector<int> dist(V, INF);
// Enqueue the source vertex with distance 0
pq.push({0, src});
dist[src] = 0;
while (!pq.empty()) {
// Extract the vertex with the minimum distance
int u = pq.top().second;
pq.pop();
// Update distances to neighboring vertices
for (const auto& neighbor : adj[u]) {
int v = neighbor.first;
int weight = neighbor.second;
if (dist[u] + weight < dist[v]) {
dist[v] = dist[u] + weight;
pq.push({dist[v], v});
}
}
}
// Print the shortest distances
cout << "Shortest distances from source " << src << ":\n";
for (int i = 0; i < V; ++i) {
cout << "To " << i << ": " << (dist[i] == INF ? -1 : dist[i]) << "\n";
}
}
int main() {
// Example graph representation using an adjacency list
int V = 6;
vector<pair<int, int>> adj[V];
// Adding edges to the graph
addEdge(adj, 0, 1, 5);
addEdge(adj, 0, 2, 3);
addEdge(adj, 1, 3, 6);
addEdge(adj, 1, 4, 2);
addEdge(adj, 2, 4, 7);
addEdge(adj, 3, 5, 1);
addEdge(adj, 4, 5, 4);
// Source vertex
int src = 0;
// Apply Dijkstra's algorithm
dijkstra(adj, V, src);
return 0;
}
```
### Test Cases:
The given test case corresponds to the graph in the code:
```
0 --5--> 1 --6--> 3
\ | /
3 2 1
\ | /
--7--> 4
\ |
4 |
v
5
```
Expected Output:
```
Shortest distances from source 0:
To 0: 0
To 1: 5
To 2: 3
To 3: 11
To 4: 7
To 5: 12
```
Feel free to modify the graph and test cases to suit your requirements.