Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions AStar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@
#include <limits>
#include <utility> // for pair
#include <stack> // for reconstructing the path
#include <unordered_set>
#include <unordered_map>

using namespace std;
#define INF 1e9

// Graph representation using adjacency list
class Graph {
Expand Down Expand Up @@ -60,15 +63,76 @@ class Graph {
// Your implementation goes here

// 1. Create a priority queue for vertices being processed
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> openSet; //pair<distance, city>
unordered_set<int> 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<int> gScore(V, INF);
vector<int> fScore(V, INF);
vector<int> 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<int> 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);

Expand Down
Binary file added AStar.exe
Binary file not shown.
81 changes: 71 additions & 10 deletions Dijkstra.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#include <utility> // for pair
#include <stack> // for reconstructing the path

#define INF 1e9

using namespace std;

// Graph representation using adjacency list
Expand All @@ -51,26 +53,85 @@ class Graph {
// Your implementation goes here

// 1. Create a priority queue for vertices being processed
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q; //pair<distance, city>
// 2. Create arrays for distances and for tracking the path
// 3. Initialize all distances as INFINITE and src distance as 0
vector<int> dist(V, INF);
vector<int> 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<int> 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;
Expand All @@ -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
*/
Binary file added Dijkstra.exe
Binary file not shown.