diff --git a/AStar.cpp b/AStar.cpp index 120ca76..e7ff52e 100644 --- a/AStar.cpp +++ b/AStar.cpp @@ -28,8 +28,11 @@ #include #include // for pair #include // for reconstructing the path +#include +#include using namespace std; +#define INF 1e9 // Graph representation using adjacency list class Graph { @@ -60,15 +63,76 @@ class Graph { // Your implementation goes here // 1. Create a priority queue for vertices being processed + priority_queue, vector>, greater>> openSet; //pair + unordered_set closedSet; + // 2. Create arrays for g_score (actual distance from start) // and f_score (g_score + heuristic) // 3. Initialize all g_scores as INFINITE and src g_score as 0 + vector gScore(V, INF); + vector fScore(V, INF); + vector from(V, -1); + + gScore[src] = 0; + fScore[src] = heuristic[src]; + openSet.push({fScore[src], src}); // 4. Process vertices in order of their f_score (not just distance) + while(!openSet.empty()) + { + int current = openSet.top().second; + int currentfScore = openSet.top().first; + openSet.pop(); + + if(fScore[current] < currentfScore) continue; + if(current == dest) break; + closedSet.insert(current); + + for(auto& edge : adj[current]) + { + int next = edge.first; + int weight = edge.second; + + int gNext = gScore[current] + weight; + if(gNext < gScore[next]) + { + gScore[next] = gNext; + fScore[next] = gScore[next] + heuristic[next]; + from[next] = current; + // if(closedSet.find(next) == closedSet.end()) + // { + openSet.push({fScore[next], next}); + // } + } + } + } // 5. Reconstruct and print the shortest path from src to dest + if(gScore[dest] == INF) cout << "No path exists from " << src << " to " << dest << endl; + else + { + stack path; + cout << "Shortest travel time: " << gScore[dest] << " hours" << endl; + cout << "Shortest travel time: " << fScore[dest] << " hours" << endl; + cout << "Shortest path from " << src << " to " << dest << ": "; + int current = dest; + while(current != -1) + { + path.push(current); + current = from[current]; + } + while(!path.empty()) + { + cout << path.top() << '(' << gScore[path.top()] << ')'; + path.pop(); + if(!path.empty()) cout << " -> "; + } + cout << endl; + } } }; int main() { + std::ios::sync_with_stdio(false); + std::cin.tie(0); // Create graph with 6 cities (labeled 0 to 5) Graph g(6); diff --git a/AStar.exe b/AStar.exe new file mode 100644 index 0000000..572d753 Binary files /dev/null and b/AStar.exe differ diff --git a/Dijkstra.cpp b/Dijkstra.cpp index 7d39740..c6440e4 100644 --- a/Dijkstra.cpp +++ b/Dijkstra.cpp @@ -27,6 +27,8 @@ #include // for pair #include // for reconstructing the path +#define INF 1e9 + using namespace std; // Graph representation using adjacency list @@ -51,26 +53,85 @@ class Graph { // Your implementation goes here // 1. Create a priority queue for vertices being processed + priority_queue, vector>, greater>> q; //pair // 2. Create arrays for distances and for tracking the path // 3. Initialize all distances as INFINITE and src distance as 0 + vector dist(V, INF); + vector prev(V, -1); + dist[src] = 0; + q.push({0, src}); // 4. Process vertices in order of their distance from src + while(!q.empty()) + { + int current = q.top().second; + q.pop(); + + if(current == dest) break; + + for(auto& edge : adj[current]) + { + int next = edge.first; + int weight = edge.second; + if(dist[next] > dist[current] + weight) + { + dist[next] = dist[current] + weight; + prev[next] = current; + q.push({dist[next], next}); + } + } + } // 5. Reconstruct and print the shortest path from src to dest + if(dist[dest] == INF) cout << "No path exists from " << src << " to " << dest << endl; + else + { + stack path; + int current = dest; + while(current != -1) + { + path.push(current); + current = prev[current]; + } + cout << "Shortest travel time: " << dist[dest] << " hours" << endl; + cout << "Path: "; + while(!path.empty()) + { + cout << path.top(); + path.pop(); + if(!path.empty()) cout << " -> "; + } + cout << endl; + } } }; int main() { + + int n; + cout << "Enter the number of cities: " << endl; + cin >> n; // Create graph with 6 cities (labeled 0 to 5) - Graph g(6); + Graph g(n); // Add roads with travel times (directed edges with weights) - g.addEdge(0, 1, 2); // From city 0 to city 1, travel time: 2 hours - g.addEdge(0, 2, 4); // From city 0 to city 2, travel time: 4 hours - g.addEdge(1, 2, 1); // From city 1 to city 2, travel time: 1 hour - g.addEdge(1, 3, 7); // From city 1 to city 3, travel time: 7 hours - g.addEdge(2, 4, 3); // From city 2 to city 4, travel time: 3 hours - g.addEdge(3, 5, 1); // From city 3 to city 5, travel time: 1 hour - g.addEdge(4, 3, 2); // From city 4 to city 3, travel time: 2 hours - g.addEdge(4, 5, 5); // From city 4 to city 5, travel time: 5 hours + // g.addEdge(0, 1, 2); // From city 0 to city 1, travel time: 2 hours + // g.addEdge(0, 2, 4); // From city 0 to city 2, travel time: 4 hours + // g.addEdge(1, 2, 1); // From city 1 to city 2, travel time: 1 hour + // g.addEdge(1, 3, 7); // From city 1 to city 3, travel time: 7 hours + // g.addEdge(2, 4, 3); // From city 2 to city 4, travel time: 3 hours + // g.addEdge(3, 5, 1); // From city 3 to city 5, travel time: 1 hour + // g.addEdge(4, 3, 2); // From city 4 to city 3, travel time: 2 hours + // g.addEdge(4, 5, 5); // From city 4 to city 5, travel time: 5 hours + + int u, v, w; + cout << "Enter the edges: (u, v, w) , input u as -1 to exit" << endl; + while(true) + { + cin >> u; + if(u == -1) break; + cin >> v >> w; + + g.addEdge(u, v, w); + } // Find the shortest path from city 0 (start) to city 5 (destination) cout << "Shortest path from city 0 to city 5:" << endl; @@ -94,5 +155,5 @@ int main() { * * Optional challenge: * - Modify the implementation to handle different start and destination cities as user input - * - Visualize the graph and the shortest path + * - Visualize the graph and the shortest path */ diff --git a/Dijkstra.exe b/Dijkstra.exe new file mode 100644 index 0000000..5d26dc1 Binary files /dev/null and b/Dijkstra.exe differ