CENG 213 PA3 2023-v1
CENG 213 PA3 2023-v1
CENG 213
Data Structures
Fall ’2023-2024
Programming Assignment 3
Objectives
In this programming assignment, you are expected to implement the functional equivalent of a flight
finder application (i.e., flightsfinder.com, skyscanner.net etc.). To implement such an application,
you will implement MultiGraph and Hash Table data structures. Details of these data structures
will be explained in the following sections.
Keywords: C++, Data Structures, Graphs, Dijkstra Shortest Path Algorithm, Hash Table,
Quadratic Probing
Instead of a linked list, the graph holds its data (in this case, edges and vertices) in dynamic arrays.
For dynamic array implementation, the class utilizes std::vector<T> class from the Standard
Template Library (STL). Instead of pointers, indices of these vectors will be utilized for all of the
internal references. This enables the graph to be movable without invalidating all of the intra-
references and reduces data duplication. Graph class and its helper structures are declared in
MultiGraph.h header file, and their implementations (although most of it is empty) are defined in
MultiGraph.cpp file.
1
struct GraphEdge
{
std::string name; // Name of the vertex
float weight[2]; // Weights of the edge
// (used on shortest path)
int endVertexIndex;
};
struct GraphVertex
{
std::vector<GraphEdge> edges; // Adjacency List
std::string name; // Name of the vertex
};
class MultiGraph
{
private:
std::vector<GraphVertex> vertexList;
...
};
Listing 1: MultiGraph and Helper Structures data layout.
The graph utilizes a single dynamic array for its data, capable of some functionalities that will be
explained shortly.
1.3.1 MultiGraph();
The default constructor constructs an empty graph. It relies on the default constructors of the
std::vector.
2
This function is implemented for you however; it relies on InsertVertex(...) and Ins
ertEdge(...) functions.
β = w0 α + w1 (1 − α) (1)
If the shortest path exits, the function returns true, otherwise, it returns false. The actual path
should be written on the variable “orderedVertexEdgeIndexList”. This variable holds indices of the
shortest path starting from the index of “vertexNameFrom” and ends with the index of “vertex-
NameTo”. Since the class is a multigraph, only vertex indices would not suffice for a valid path.
To this end, there is an edge index between vertices. In an example, a valid path between v3 and
v5 can be v3 → (e3 ) → v7 → (e2 ) → v2 → (e0 ) → v5 . In this case, edge indices are the indices of
the originated vertex’s edge vector.
Please check the implemented void MultiGraph::PrintPath(...) function for further ex-
planation. This function reads and prints the path that is represented by the resulting vector
to the console.
Finally, this function should throw VertexNotFoundException when appropriate. In any case,
this function fails, or it returns a false output array that should not be modified.
3
1.3.8 bool FilteredShortestPath(std::vector<int>& orderedVertexEdgeIndexLis
t, const std::string& vertexNameFrom, const std::string& vertexNameTo
, float heuristicWeight, const std::vector<std::string>& edgeNames) c
onst;
This function is highly similar to the function above. However, when calculating the shortest path,
this function disregards the edges that their name is on the list “edgeNames”. Otherwise, the
behavior is the same.
• You are allowed to use std::priority_queue STL library class. A template instantiation
example is given in IntPair.h header file. By default, std::priority_queue is a max heap
and you should convert it to a min heap.
• To remain consistent between implementations, do not update the path weight if it is equal
to the previously found path weight. This means that the “first” shortest path will be chosen
as the shortest path if any other equally weighted shortest path exists. Do iterate arrays in
order when required.
• Still some inconsistencies may occur depending on the heap implementation. Because of that,
comparisons should be using the “less than” operation inside of the heap instead of “less than
and equal”.
You may use either DFS or BFS for this functionality. Iterative implementation will require
a queue or stack. You can repurpose the std::priority_queue for this purpose.
4
1.3.13 static float Lerp(float w0, float w1, float alpha);
This function should behave as described in Equation 1. The function void PrintPath(...) will
use this function to combine the weights of the edges.
You may want to use this function on your shortest-path implementations as well.
1.4 Exceptions
All of the exceptions for the graph are provided for you in the Exceptions.h header file. All of
these exceptions are implemented for you. You can check the file to how to utilize these exception
classes.
KeyedHashTable class and its helper structures are declared in HashTable.h header file and their
implementations are defined in another header file namely HashTable.hpp due to this class being a
template class.
3
X
h= P (i)S(i) (2)
i=1
2.1.3 HashTable();
The constructor initializes the hash table. For each entry, it sets the lruCounter to zero and marks
every spot on the table as empty.
5
// Sentinel for probing
#define SENTINEL_MARK 0xFFFFFFFF
#define EMPTY_MARK 0xFFFFFFFE
#define OCCUPIED_MARK 0x00000000
// Capacity threshold is the multiplicative inverse of the 1/2 (%50)
#define CAPACITY_THRESHOLD 2
struct HashData
{
// Data
std::vector<int> intArray;
unsigned int sentinel;
// Key
bool isCostWeighted;
int startInt;
int endInt;
// LRU Counter (to determine least recently used entry)
int lruCounter;
};
// Properties
HashData table[MAX_SIZE];
int elementCount;
...
};
Listing 2: Hash Table Structures
To generate keys, “intArray” should at least have a single element. If this is not true, the function
should throw InvalidTableArgException.
As discussed above, collisions are resolved via quadratic probing. The quadratic probing func-
tion should be similar to the one discussed in class. However; it is given in Equation 3 due to
completeness.
6
The function returns the pre-modified lruCounter of the returned item. If the inserted item is
new, it should return zero.
Please use the provided PrintLine function for printing due to consistency between tests
and your code.
7
struct HaltedFlight
{
std::string airportFrom;
std::string airportTo;
std::string airline;
float w0;
float w1;
};
class CENGFlight
{
private:
HashTable<FLIGHT_TABLE_SIZE> lruTable;
MultiGraph navigationMap;
...
};
Listing 3: METUMaps Class
Throughout the class, vertex names are given as airport names. Edge names are given as airline
names. Edge’s first weight is the cost (flight ticket price of that specific flight) and the second
weight is the time (although it is not important, it is in minutes) that this specific flight took.
Users can query optimal flights by selecting start/end airports and some weighting factor between
cost and price. CENG Flight System will return paths corresponding to the users’ needs.
8
3.1.3 void HaltFlight(const std::string& airportFrom, const std::string& ai
rportTo, const std::string& airlineName);
Sometimes a flight is not available due to plane maintenance. In this case, the edge with the
corresponding should temporarily be removed from the flight map. This function should try to
remove the edge defined by the airport names and the airline name. If such an edge exists CENG
Flight System should temporarily store the names and weights of this particular edge in a data
structure.
We did not specify the actual temporary data structure, you can use any data structure you
want. We do provide HaltedFlight structure for you to use, however.
If such an edge or airport does not exist, the function should print PrintCanNotHalt(...).
Caching only occurs when the given flight is either fully cost or time-weighted. Please check Section
3.2 for details.
9
– Select the first non-utilized airline on that visited airport (this is the lowest-indexed
non-utilized airline). If all airlines from that airport are on the sister airline list, print
the error.
– Visit every non-visited airport only using this airline. Add this airline to the sister
airline list.
Until all airports are visited or your visited airports did not change from the last
iteration.
• If the visited airport list did not change compared to the previous iteration, print the error.
• Finally, copy the found sister airlines to “airlineNames” and return.
The above algorithm indices are the indices of the edgeList or vertexList respectively. Although
this function is defined in the CENGFlight class it should delegate the arguments directly to the
graph and the graph should handle the operation. “The error” above is PrintSisterAirlinesDon
tCover(...) function. The output “airlineList” should only be modified if there is no error.
If the airport named “airportName” is not on the navigation map, print PrintSisterAirlinesDo
ntCover(...) as well.
3.1.10 voidPrintCache();
Similar to the function above, this function prints the cache. This function is implemented for
you.
3.2 Caching
When the shortest path is found with an alpha either zero or one, the CENGFlight class should
cache these onto the hash table. LRU entry on the table should be removed if and only if the table
is full. Otherwise, the empty space should be utilized. The key value of the hash table is the start
and end indices of the path (airport indices) and false if the alpha is zero or true when it is one.
Blended paths (where 0 < α < 1) should not be cached. Specific flights should never be
cached.
Regulations
1. Programming Language: You will use C++.
2. Standard Template Library is not allowed except “std::priority_queue” “std::strin
g” and “std::vector”. “std::priority_queue” should only be used on the appropriate
functions as a temporary data structure.
3. You are strictly forbidden to use “std::queue” and “std::stack” classes of the
STL. There is a neat trick to convert a heap to a stack or queue. Please utilize “std::prio
rity_queue” for this purpose.
4. Using external libraries other than those already included is not allowed.
5. Changing or modifying already implemented functions is not allowed
6. You can add any private member functions unless it is explicitly stated that you should not.
10
7. Late Submission Policy: Each student receives 5 late days for the entire semester. You
may use late days on programming assignments, and each allows you to submit up to 24
hours late without penalty. For example, if an assignment is due on Thursday at 11:30pm,
you could use 2 late days to submit it on Saturday by 11:30pm with no penalty. Once a
student has used up all their late days, each successive day that an assignment is late will
result in a loss of 5% on that assignment.
No assignment may be submitted more than 3 days (72 hours) late without permission from
the course instructor. In other words, this means there is a practical upper limit of 3 late days
usable per assignment. If unusual circumstances truly beyond your control prevent you from
submitting an assignment, you should discuss this with the course staff as soon as possible.
If you contact us well in advance of the deadline, we may be able to show more flexibility in
some cases.
8. Cheating: We have a zero tolerance policy for cheating. In case of cheating, all parts
involved (source(s) and receiver(s)) get zero. People involved in cheating will be punished
according to the university regulations. Remember that students of this course are bound to
the code of honor and its violation is subject to severe punishment.
9. Newsgroup: You must follow the Forum (odtuclass.metu.edu.tr) for discussions and
possible updates on a daily basis.
Submission
• Submission will be done via CengClass, (cengclass.ceng.metu.edu.tr).
• Don’t write a “main” function in any of your source files. It will clash with the test case(s)
main function and your code will not compile.
• A test environment will be ready in CengClass :
– You can submit your source files to CengClass and test your work with a subset of
evaluation inputs and outputs.
– Additional test cases will be used for evaluation of your final grade. So, your actual
grades may be different than the ones you get in CengClass.
– Only the last submission before the deadline will be graded.
11