Airline Reservation System DSA Jury 2
Airline Reservation System DSA Jury 2
Problem Statement-
Develop an airline reservation system that accurately represents flights and their connections,
manages and organizes flight schedules, and efficiently processes booking requests.
C++ code-
#include <iostream>
#include <vector>
#include <queue>
#include <unordered_map>
#include <limits>
#include <string>
struct Flight {
int flightID;
string departureTime;
string arrivalTime;
int seatsAvailable;
int destination;
int distance;
Flight(int id, string dep, string arr, int dest, int dist, int seats)
};
class AirlineSystem {
airportNames[id] = name;
void addFlight(int src, int dest, int distance, string depTime, string arrTime, int seats) {
if (flights.find(airportID) == flights.end()) {
cout << "No flights available from airport " << airportNames[airportID] << endl;
return;
cout << "Available flights from " << airportNames[airportID] << ":\n";
if (f.flightID == flightID) {
if (f.seatsAvailable > 0) {
f.seatsAvailable--;
cout << "Successfully booked flight ID " << flightID << ". Seats left: " << f.seatsAvailable
<< endl;
} else {
cout << "No seats available on flight ID " << flightID << endl;
return;
cout << "Flight ID " << flightID << " not found.\n";
distances[flight.first] = numeric_limits<int>::max();
distances[start] = 0;
pq.push({0, start});
while (!pq.empty()) {
pq.pop();
if (current == target) {
cout << "Shortest distance from " << airportNames[start] << " to " << airportNames[target]
<< " is " << currentDist << " km" << endl;
return;
distances[flight.destination] = newDist;
prev[flight.destination] = current;
pq.push({newDist, flight.destination});
cout << "No path found from " << airportNames[start] << " to " << airportNames[target] << endl;
};
int main() {
AirlineSystem system;
system.viewFlightsFromAirport(1);
system.bookFlight(1);
system.findShortestPath(1, 4);
return 0;
}
Use of DSA-
Graph Representation:
The code models airports and flights as a graph where airports are nodes, and flights
between them are edges.
Dijkstra's Algorithm:
The code implements Dijkstra's shortest path algorithm to find the shortest distance
between two airports.
It uses a priority queue to select the next airport with the smallest tentative distance.
The algorithm relaxes the edges (flights) and updates the shortest path distance to
destination airports.
Priority Queue:
The queue orders the airports based on the shortest distance, ensuring that the next node
processed always has the smallest known distance.
o Hashmaps are used to store and quickly access flight details from a specific airport
and retrieve airport names from airport IDs.
o This offers average O(1) time complexity for lookup and insertion operations.
Explanation of key components of code-
1. Flight Structure:
2. AirlineSystem Class:
o It stores a list of airports and their flights using two key pieces of data:
o The airport is identified by a unique id and is given a name (like "JFK Airport").
2. addFlight(int src, int dest, int distance, string depTime, string arrTime, int seats):
o It specifies the distance between the airports, the departure/arrival times, and the
number of available seats.
3. viewFlightsFromAirport(int airportID):
o Shows all available flights departing from a particular airport, identified by airportID.
4. bookFlight(int flightID):
o If the flight is found and seats are available, it reduces the number of available seats
by 1.
o If no seats are left, it tells the user that the flight is full.
o If the flight ID doesn't exist, it gives an error message.
o Uses Dijkstra's algorithm to find the shortest path (in terms of distance) between
two airports.
o The algorithm finds the shortest distance from the start airport to the target airport.
o If a path exists, it shows the shortest distance; if not, it informs the user that no path
was found.
o The system first adds four airports: "JFK Airport", "LAX Airport", "Heathrow Airport",
and "Dubai International."
o It then adds several flights between these airports, including distances and times.
2. Viewing Flights:
o The system shows available flights departing from "JFK Airport" (with ID 1). The user
can see details like flight ID, destination, departure time, and available seats.
3. Booking a Flight:
o The user tries to book a seat on the flight with flightID = 1. If successful, the number
of available seats is reduced by 1.
o The system finds the shortest distance between "JFK Airport" and "Dubai
International". It uses Dijkstra’s algorithm to calculate the shortest distance based on
the flights between the airports.
o iostream: For input and output (like cout for displaying messages).
o queue: For implementing a priority queue, which helps with Dijkstra’s algorithm.
o unordered_map: A way to quickly store and access airports and their flights using a
unique ID.
o limits: Provides a way to get the largest possible number (used in finding shortest
path distances).
The constructor (Flight(...)) is used to set the values for a new flight when it's
created.
o flights: A list of all flights, organized by the airport they depart from.
o airportNames: A map that links each airport's ID to its name (like ID 1 →
"JFK Airport").
Adding an Airport
Adding a Flight
This function shows all flights that depart from a specific airport:
o airportID: The ID of the airport you want to check.
If there are no flights from this airport, it displays a message saying "No
flights available."
If flights exist, it prints the details of each flight: the flight ID, destination,
departure and arrival times, and seats available.
Booking a Flight
Main Function
Airports and flights are added: Airports like "JFK" and "LAX" are added,
and flights are created between them.
View flights: The system shows available flights from JFK Airport.
Book a flight: A seat is booked on a flight from JFK.
Find shortest path: The shortest path between JFK and Dubai is calculated.