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
53 changes: 53 additions & 0 deletions AStar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@

using namespace std;

// Comparator for priority queue to make it a min-heap
struct CompareF {
bool operator()(pair<int,int> lhs, pair<int,int> rhs) {
return lhs.first > rhs.first; // Compare f-scores, smaller f-score has higher priority
}
};

// Graph representation using adjacency list
class Graph {
private:
Expand Down Expand Up @@ -65,6 +72,52 @@ class Graph {
// 3. Initialize all g_scores as INFINITE and src g_score as 0
// 4. Process vertices in order of their f_score (not just distance)
// 5. Reconstruct and print the shortest path from src to dest
priority_queue<pair<int,int>, vector<pair<int,int>>, CompareF> pq;
vector<int> f_score(V, INT32_MAX);
vector<int> g_score(V, INT32_MAX);
vector<bool> visited(V, false);
vector<int> path(V, -1);
g_score[src] = 0;
f_score[src] = heuristic[src];
pq.push(make_pair(f_score[src], src));
while ( !pq.empty() ) {
int cur = pq.top().second;
if ( cur == dest ) break;
pq.pop();
for ( auto[nd, weight] : adj[cur] ) {
if ( g_score[nd] > g_score[cur] + weight ) {
g_score[nd] = g_score[cur] + weight;
path[nd] = cur;
}
if ( f_score[nd] > g_score[nd] + heuristic[nd] ) {
f_score[nd] = g_score[nd] + heuristic[nd];
pq.push(make_pair(f_score[nd], nd));
}
}
}
if ( path[dest] == -1 ) {
cout << "cannot find path\n";
return;
}
stack<int> result;
int cur = dest;
while ( 1 ) {
result.push(cur);
if ( cur == src ) break;
cur = path[cur];
}
cout << "Shortest travel time: " << g_score[dest] << " hours\n";
cout << "Path: ";

cout << result.top();
result.pop();
while ( !result.empty() ) {
int cur = result.top();
result.pop();
cout << " -> " << cur;
}
cout << endl;

}
};

Expand Down
61 changes: 60 additions & 1 deletion Dijkstra.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@

using namespace std;

struct CompareDistance {
bool operator()(pair<int,int> lhs, pair<int,int> rhs) {
return lhs.second > rhs.second;
}
};

// Graph representation using adjacency list
class Graph {
private:
Expand All @@ -54,7 +60,60 @@ class Graph {
// 2. Create arrays for distances and for tracking the path
// 3. Initialize all distances as INFINITE and src distance as 0
// 4. Process vertices in order of their distance from src
// 5. Reconstruct and print the shortest path from src to dest
priority_queue<pair<int,int>, vector<pair<int,int>>, CompareDistance> pq;
vector<bool> visited(V, 0);
vector<int> cost(V, INT32_MAX);
vector<int> path(V, -1);
stack<int> result_path;
pq.push(make_pair(src, 0));
cost[src] = 0;

while ( visited[dest] == 0 && pq.size() > 0 ) {
auto cur = pq.top();
pq.pop();
visited[cur.first] = true;
// cout << "cur:" << cur.first << endl;

for ( auto ni : adj[cur.first] ) {
if ( visited[ni.first] ) continue;

if ( cost[ni.first] > cost[cur.first] + ni.second ) {
cost[ni.first] = cost[cur.first] + ni.second;
path[ni.first] = cur.first;
pq.push(make_pair(ni.first, cost[ni.first]));
// current method would cause duplicate nodes in pqueue,
// to improve is to use set<pair<int,int>>, since st.begin() is the smallest
// cout << "add neighbor: " << ni.first << "<>" << cost[ni.first] << endl;
}
}
}

if ( visited[dest] == 0 ) {
cout << "no path exists\n";
return;
}


for ( int it = dest; it != src; it = path[it] ) {
result_path.push(it);
}
result_path.push(src);


cout << "Shortest travel time: " << cost[dest] << " hours\n";

cout << "Path: ";

cout << result_path.top();
result_path.pop();
while ( !result_path.empty() ) {
int cur = result_path.top();
result_path.pop();
cout << " -> " << cur;
}
cout << endl;


}
};

Expand Down