|
| 1 | +#include <iostream> |
| 2 | +#include <vector> |
| 3 | +#include <queue> |
| 4 | +#include <algorithm> |
| 5 | +using namespace std; |
| 6 | + |
| 7 | +class Solution { |
| 8 | + public: |
| 9 | + vector<int> shortestPath(vector<vector<int>>& edges, int N, int M, int src) { |
| 10 | + // Step 1: Create adjacency list to represent the graph |
| 11 | + vector<vector<int>> adj(N); |
| 12 | + // Build the adjacency list from the edges |
| 13 | + for (int i = 0; i < edges.size(); i++) { |
| 14 | + int u = edges[i][0]; // Start vertex of the edge |
| 15 | + int v = edges[i][1]; // End vertex of the edge |
| 16 | + adj[u].push_back(v); // Add v as a neighbor of u |
| 17 | + adj[v].push_back(u); // Add u as a neighbor of v (since the graph is undirected) |
| 18 | + } |
| 19 | + |
| 20 | + // Step 2: Initialize visited and parent vectors |
| 21 | + // visited[i] will be true if node i is visited |
| 22 | + vector<bool> visited(N, false); |
| 23 | + // parent[i] will store the previous node of i in the shortest path |
| 24 | + vector<int> parent(N, -1); |
| 25 | + |
| 26 | + // Step 3: Perform BFS traversal from the source node |
| 27 | + queue<int> q; |
| 28 | + q.push(src); // Add the source node to the queue |
| 29 | + visited[src] = true; // Mark the source node as visited |
| 30 | + |
| 31 | + // Step 4: BFS to explore all nodes and find the shortest path |
| 32 | + while (!q.empty()) { |
| 33 | + int node = q.front(); // Get the front node from the queue |
| 34 | + q.pop(); // Remove the front node from the queue |
| 35 | + |
| 36 | + // Step 5: Explore all neighbors of the current node |
| 37 | + for (auto i : adj[node]) { |
| 38 | + // If the neighbor hasn't been visited, mark it as visited |
| 39 | + if (!visited[i]) { |
| 40 | + visited[i] = true; // Mark the neighbor as visited |
| 41 | + parent[i] = node; // Set the parent of the neighbor |
| 42 | + q.push(i); // Add the neighbor to the queue for further exploration |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + // Step 6: If the destination (M) is not reachable, return {-1} |
| 48 | + if (!visited[M]) { |
| 49 | + return {-1}; // If destination node M is not visited, no path exists |
| 50 | + } |
| 51 | + |
| 52 | + // Step 7: Reconstruct the path from source to destination |
| 53 | + vector<int> ans; |
| 54 | + int currentNode = M; // Start from the destination node |
| 55 | + while (currentNode != src) { |
| 56 | + ans.push_back(currentNode); // Add the current node to the path |
| 57 | + currentNode = parent[currentNode]; // Move to the parent node |
| 58 | + } |
| 59 | + ans.push_back(src); // Add the source node to the path |
| 60 | + |
| 61 | + // Step 8: Reverse the path to make it from source to destination |
| 62 | + reverse(ans.begin(), ans.end()); // Reverse the path to get it from src to M |
| 63 | + return ans; // Return the path |
| 64 | + } |
| 65 | +}; |
| 66 | + |
| 67 | +int main() { |
| 68 | + int N = 9, M = 4; // Number of nodes (N) and the destination node (M) |
| 69 | + vector<vector<int>> edges = { |
| 70 | + {0, 1}, {0, 3}, {3, 4}, {4, 5}, {5, 6}, |
| 71 | + {1, 2}, {2, 6}, {6, 7}, {7, 8}, {6, 8} |
| 72 | + }; |
| 73 | + int src = 0; // Starting node |
| 74 | + |
| 75 | + Solution sol; |
| 76 | + // Call the function to find the shortest path from src to M |
| 77 | + vector<int> result = sol.shortestPath(edges, N, M, src); |
| 78 | + |
| 79 | + // Step 9: Output the result |
| 80 | + if (result.size() == 1 && result[0] == -1) { |
| 81 | + cout << "No path exists" << endl; // No path found from src to M |
| 82 | + } else { |
| 83 | + cout << "Shortest path from " << src << " to " << M << ": "; |
| 84 | + // Print the nodes in the shortest path |
| 85 | + for (int node : result) { |
| 86 | + cout << node << " "; |
| 87 | + } |
| 88 | + cout << endl; |
| 89 | + } |
| 90 | + |
| 91 | + return 0; |
| 92 | +} |
0 commit comments