forked from JustinShih0918/Navigation_Tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDijkstra.cpp
More file actions
159 lines (141 loc) · 5.11 KB
/
Dijkstra.cpp
File metadata and controls
159 lines (141 loc) · 5.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
* Dijkstra's Algorithm Practice Question
*
* Problem Statement:
* You are planning a road trip across a country with several cities. Each direct road between
* cities has a specific travel time (in hours). You want to find the shortest (minimum time)
* path from your starting city to your destination city.
*
* Given:
* - A graph representing cities and roads between them
* - Each road has a positive travel time (weight)
* - There are no negative weights (no time travel!)
* - The graph is directed (you can only travel in one direction on some roads)
* - The graph has no cycles that would create infinite loops
*
* Task:
* Implement Dijkstra's algorithm to find the shortest path (minimum total travel time)
* from the starting city (0) to the destination city (5).
*
* Also print the actual path taken (sequence of cities) from start to destination.
*/
#include <iostream>
#include <vector>
#include <queue>
#include <limits>
#include <utility> // for pair
#include <stack> // for reconstructing the path
#define INF 1e9
using namespace std;
// Graph representation using adjacency list
class Graph {
private:
int V; // Number of vertices (cities)
vector<vector<pair<int, int>>> adj; // adjacency list: pair<destination, weight>
public:
// Constructor
Graph(int vertices) : V(vertices) {
adj.resize(V);
}
// Add an edge (road) from city u to city v with travel time w
void addEdge(int u, int v, int w) {
adj[u].push_back(make_pair(v, w));
}
// TODO: Implement Dijkstra's algorithm
void shortestPath(int src, int dest) {
// 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(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
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;
g.shortestPath(0, 5);
return 0;
}
/*
* Student Task:
*
* 1. Implement the shortestPath method using Dijkstra's algorithm
* 2. Your implementation should:
* - Calculate the minimum travel time from src to dest
* - Print the sequence of cities in the shortest path
* - Handle edge cases (e.g., no path exists)
*
* Expected output format:
* - Shortest travel time: X hours
* - Path: 0 -> 1 -> ... -> 5
*
* Optional challenge:
* - Modify the implementation to handle different start and destination cities as user input
* - Visualize the graph and the shortest path
*/