0% found this document useful (0 votes)
46 views

Airline Reservation System DSA Jury 2

Uploaded by

harshudawkhar17
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Airline Reservation System DSA Jury 2

Uploaded by

harshudawkhar17
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Airline Reservation System

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>

using namespace std;

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)

: flightID(id), departureTime(dep), arrivalTime(arr), destination(dest), distance(dist),


seatsAvailable(seats) {}

};

class AirlineSystem {

unordered_map<int, vector<Flight>> flights;

unordered_map<int, string> airportNames;


public:

void addAirport(int id, string name) {

airportNames[id] = name;

void addFlight(int src, int dest, int distance, string depTime, string arrTime, int seats) {

static int flightID = 1;

flights[src].push_back(Flight(flightID++, depTime, arrTime, dest, distance, seats));

void viewFlightsFromAirport(int airportID) {

if (flights.find(airportID) == flights.end()) {

cout << "No flights available from airport " << airportNames[airportID] << endl;

return;

cout << "Available flights from " << airportNames[airportID] << ":\n";

for (Flight &f : flights[airportID]) {

cout << "Flight ID: " << f.flightID

<< ", Destination: " << airportNames[f.destination]

<< ", Departure: " << f.departureTime

<< ", Arrival: " << f.arrivalTime

<< ", Seats available: " << f.seatsAvailable << endl;

void bookFlight(int flightID) {

for (auto &airport : flights) {

for (Flight &f : airport.second) {

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";

void findShortestPath(int start, int target) {

unordered_map<int, int> distances;

unordered_map<int, int> prev;

for (auto &flight : flights) {

distances[flight.first] = numeric_limits<int>::max();

distances[start] = 0;

priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;

pq.push({0, start});

while (!pq.empty()) {

int current = pq.top().second;

int currentDist = pq.top().first;

pq.pop();

if (current == target) {

cout << "Shortest distance from " << airportNames[start] << " to " << airportNames[target]
<< " is " << currentDist << " km" << endl;

return;

for (Flight &flight : flights[current]) {

int newDist = currentDist + flight.distance;


if (newDist < distances[flight.destination]) {

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.addAirport(1, "JFK Airport");

system.addAirport(2, "LAX Airport");

system.addAirport(3, "Heathrow Airport");

system.addAirport(4, "Dubai International");

system.addFlight(1, 2, 5000, "10:00 AM", "1:00 PM", 100);

system.addFlight(1, 3, 5600, "12:00 PM", "8:00 PM", 50);

system.addFlight(2, 4, 8000, "2:00 PM", "9:00 PM", 200);

system.addFlight(3, 4, 6000, "10:00 AM", "6:00 PM", 150);

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.

 The adjacency list (unordered_map<int, vector<Flight>> flights) is used to store flights


(edges) from a particular airport (node).

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 code uses a min-heap priority queue (priority_queue<pair<int, int>, vector<pair<int,


int>>, greater<pair<int, int>>> pq) to implement Dijkstra’s algorithm.

 The queue orders the airports based on the shortest distance, ensuring that the next node
processed always has the smallest known distance.

Hashing / Hash Maps

 Unordered Maps (unordered_map<int, vector<Flight>> flights and unordered_map<int,


string> airportNames):

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:

o This is a blueprint for a flight. Each flight has:

 flightID: A unique number for each flight.

 departureTime and arrivalTime: When the flight starts and ends.

 seatsAvailable: Number of seats left on the flight.

 destination: The airport where the flight will land.

 distance: How far the destination is from the departure airport.

2. AirlineSystem Class:

o This class manages airports and flights.

o It stores a list of airports and their flights using two key pieces of data:

 flights: A list of flights from each airport.

 airportNames: The names of the airports.

Main Features (Class Methods):

1. addAirport(int id, string name):

o This method adds a new airport to the system.

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 Adds a flight from one airport (src) to another (dest).

o It specifies the distance between the airports, the departure/arrival times, and the
number of available seats.

o The flight is automatically given a unique ID.

3. viewFlightsFromAirport(int airportID):

o Shows all available flights departing from a particular airport, identified by airportID.

o If there are no flights from that airport, it informs the user.

4. bookFlight(int flightID):

o Allows a user to book a seat on a flight.

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.

5. findShortestPath(int start, int target):

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.

How It Works (in main function):

1. Airports and Flights Setup:

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.

4. Finding Shortest Path:

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.

Stepwise Explanation of Code-


1. Including Libraries

 These libraries provide different functionalities that the program needs:

o iostream: For input and output (like cout for displaying messages).

o vector: For storing a list of flights.

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).

o string: To handle airport names and flight times as text.


Defining the Flight Structure

The Flight structure holds the details of a flight:

o flightID: A unique number for each flight.


o departureTime and arrivalTime: The times the flight starts and ends.
o seatsAvailable: How many seats are left on the flight.
o destination: The ID of the airport where the flight is going.
o distance: How far the destination is from the current airport.

The constructor (Flight(...)) is used to set the values for a new flight when it's
created.

Defining the AirlineSystem Class

This class manages the entire airline system:

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

 This function adds a new airport to the system.


 It takes two inputs:
o id: A unique number for the airport.
o name: The name of the airport (like "JFK Airport").
 The function stores the airport in airportNames using the ID as the key.

Adding a Flight

 This function adds a flight between two airports:


o src: The starting airport.
o dest: The destination airport.
o distance: The distance between the two airports.
o depTime and arrTime: The departure and arrival times.
o seats: The number of available seats.
 A unique flightID is automatically generated for each flight.
 The flight is added to the list of flights leaving from the src airport.

Viewing Flights from an Airport

 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

 This function books a seat on a specific flight using the flightID.


 It checks all the flights to find the one with the matching ID:
o If seats are available, it reduces the number of seats by 1 and confirms
the booking.
o If no seats are available, it says the flight is full.
o If the flight ID doesn’t exist, it shows an error message.

Finding the Shortest Path Between Airports

 This function finds the shortest distance between two airports:


o start: The ID of the starting airport.
o target: The ID of the destination airport.
 It uses Dijkstra’s algorithm to calculate the shortest path:
o It starts by assuming the distance to all airports is infinite
(numeric_limits<int>::max()), except for the starting airport, which is
0.
o It processes the airports one by one, updating the shortest known
distance to each destination.
o If it finds a valid path to the destination, it shows the distance. If not,
it says "No path found."

Main Function

In the 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.

You might also like