792 1601898451857 UNIT 19 Data Structures and Algorithms
792 1601898451857 UNIT 19 Data Structures and Algorithms
Computing
Unit 19: Data Structures & Algorithms
Assignment 01
General Guidelines
1. A Cover page or title page – You should always attach a title page to your assignment. Use
previous page as your cover sheet and make sure all the details are accurately filled.
2. Attach this brief as the first section of your assignment.
3. All the assignments should be prepared using a word processing software.
4. All the assignments should be printed on A4 sized papers. Use single side printing.
5. Allow 1” for top, bottom , right margins and 1.25” for the left margin of each page.
1. The font size should be 12 point, and should be in the style of Time New Roman.
2. Use 1.5 line spacing. Left justify all paragraphs.
3. Ensure that all the headings are consistent in terms of the font size and font style.
4. Use footer function in the word processor to insert Your Name, Subject, Assignment No, and
Page Number on each page. This is useful if individual sheets become detached for any reason.
5. Use word processing application spell check and grammar check function to help editing your
assignment.
Important Points:
1. It is strictly prohibited to use textboxes to add texts in the assignments, except for the
compulsory information. eg: Figures, tables of comparison etc. Adding text boxes in the body
except for the before mentioned compulsory information will result in rejection of your work.
2. Carefully check the hand in date and the instructions given in the assignment. Late submissions
will not be accepted.
3. Ensure that you give yourself enough time to complete the assignment by the due date.
4. Excuses of any nature will not be accepted for failure to hand in the work on time.
5. You must take responsibility for managing your own time effectively.
6. If you are unable to hand in your assignment on time and have valid reasons such as illness, you
may apply (in writing) for an extension.
7. Failure to achieve at least PASS criteria will result in a REFERRAL grade .
8. Non-submission of work without valid reasons will lead to an automatic RE FERRAL. You will
then be asked to complete an alternative assignment.
9. If you use other people’s work or ideas in your assignment, reference them properly using
HARVARD referencing system to avoid plagiarism. You have to provide both in-text citation and a
reference list.
10. If you are proven to be guilty of plagiarism or any academic misconduct, your grade could be
reduced to A REFERRAL or at worst you could be expelled from the course
Student Declaration
I hereby, declare that I know what plagiarism entails, namely to use another’s work and to present it as
my own without attributing the sources in the correct form. I further understand what it means to copy
another’s work.
Submission format
The submission should be in the form of a report, which contains code snippets (which must be
described well), text based descriptions, and diagrams where appropriate. References to external
sources of knowledge must be cited (reference list supported by in-text citations) using the
Harvard Referencing style.
LO1. Examine abstract data types, concrete data structures and algorithms.
LO2. Specify abstract data types and algorithms in a formal notation.
LO3. Implement complex data structures and algorithms.
LO4. Assess the effectiveness of data structures and algorithms.
Assignment Brief and Guidance:
Scenario
‘XYZ’ hotel chain has 100 banquet halls island wide. Any hotel has minimum one banquet hall to
maximum 5 halls. It provides online hotel banquet hall reservation facilities to their customers.
The reservation system uses a waiting list of the selected banquet halls.
Each banquet hall has unique id, name, location, maximum number guests, reservation date
and the three menu prices. Customer can search a banquet hall with the location, reservation
date and the number of guests. According to the hotel chain rules and regulations, when a
possible match between reservation and the banquet hall is found the customer is contacted via
email or SMS and informed. If customer is not responding within a day, he or she is placed back
in the waiting list and must wait again for another hall.
Task 2: Implement the above scenario using the selected data structure and its valid operations
for the design specification given in task 1 by using java programming. Use suitable error
handling and Test the application using suitable test cases and illustrate the system. Provide
evidence of the test cases and the test results.
Task 3:
Task 4: Sort the banquets halls with two different sorting algorithms and critically review the
performances of those two algorithms by comparing them.
Task 5: There are 8 hotels available in the Southern district in Sri Lanka. A customer plans to visit
all of these eight hotels through the shortest path within a day.
Analyse the above operation by using illustrations, of two shortest path algorithms, specify how
it operates using a sample graph diagram.
Task 6: Banquet hall details are stored from oldest to newest hall. The customer should be able
to find from the newest to the oldest banquet hall added to the hotel chain.
Using an imperative definition, specify the abstract data type for the above scenario.
Examine the advantages of encapsulation and information hiding when using an ADT selected
for the above scenario.
Object orientation is a paradigm where a computer program functions by objects calling the
methods of other objects, which ultimately produces program behaviour. It makes programming
simple, readable, and makes programs maintainable. Imperative ADTs are basis for object
orientation. Do you agree? Disccuss your answer.
Task 7: Implement the above scenario using the selected complex ADT by using java.
Demonstrate how the implementation of an ADT/algorithm solves the above problem.
Critically evaluate the complexity of an implemented ADT/algorithm.
Explain the different ways in which the efficiency of an algorithm can be measured, illustrating
your answer the algorithm which you have developed.
Task 8: Asymptotic analysis can be used to assess the effectiveness of an algorithm. Discuss
Asymptotic analysis for the scenario given in task 7.
Critically review the sort of trade-offs exists when you use an ADT for implementing programs
Describe the benefits of using independent data structures for implementing programs.
Grading Rubric.
Grading Criteria Achieved Feedback
List of Tables.
Table 1 Operations of Queue Data Structure used...............................................................................25
Table 2 Queue Data Structure Functions.............................................................................................34
Table 3 Test Cases and the Test Results (Author Developed)...............................................................37
Table 4 Comparison of Performance (Author Developed)...................................................................47
Task 01.
1.1. Data Structures.
A data structure is a particular method of organizing data in a computer for efficient use. The
name of the data structure itself indicates the organization of the data memory. There are
many ways to organize data in memory, and one of the data structures already seen is the
array in C language. An array is a collection of memory elements that store data one by one.
In other words, this means that the array continues to store the element. This data is organized
with the help of a number of data structures. There are other ways to organize data in
memory. There are some different types of data structures. Database is not a programming
language like C, C ++, Java. It is a set of algorithms that can be used in any programming
language to process memory data.[ CITATION jav21 \l 1033 ]
For example, it can store a list of items with the same data type using the array data structure.
1.1.2.1. Array.
What is an Array?
An array is a data structure that contains the same type of element. A data structure is a way
of organizing data; An array is a data structure because it organizes data sequentially. An
array is a large partition of memory divided into smaller parts, each of which has the ability
to store some value. Considering that an array consisting of 10 values is created, each of its
blocks stores the value of an integer type. If it tries to store different types of value, it is not
the right array and it throws up a compiler time error.[ CITATION jav211 \l 1033 ]
Code enawa
What is a stack?
A stack is a linear data structure. In an array, it can be accessed randomly, that is, any element
in an array can be accessed at any time, and only sequential access can be made in stack. It is
a container that follows the rule of entry and deletion. It follows the LIFO (Last in First Out)
principle of insertion and deletion on one side, as described above. In stack, the element of
the same data type can be inserted, that is, the element of different data types cannot be
included in the same stack. The two operations are performed in LIFO, namely push and pop
activation.[ CITATION jav212 \l 1033 ]
Code enawa
1.1.2.4. Queue.
What is a queue?
Queues are a linear data structure. This is a list of orders that follow the FIFO (First In -First
Out) principle. A queue is a structure that follows certain limits on entry and deletion. In the
case of queues, the entry is made from one end, and that end is called the rear end. Deletion is
done at the other end and that end is called the front end. In queues, the technical terms for
insertion and deletion are enqueue () and dequeue (), respectively, while in stack, the
technical terms for entry and deletion are push () and pop (). Its structure consists of a front
indicator and a rear indicator, the front indicator being the first element added to the queue
and the rear indicator being the last added element in the queue. [ CITATION jav212 \l 1033 ]
Figure 3 Queue
Code enawa
1.1.3. Non-Linear Data Structure.
1.1.3.1. Tree.
What is a tree?
The hierarchy is represented by a tree, which is a non-linear data structure. A tree is a
hierarchy formed by a collection of nodes that are connected together.
And have a look at some of the terms used in tree data structures.
1. Root node - The top node of a tree data structure is called the root node. A node with
no parents is called the root node.
2. Parent of a node - The closest precursor to a node is called the parent of the node. The
precursor here refers to the anterior node of that particular node.
3. Child of a node - The closest successor to a node is called the child of the node.
4. Leaf node - The leaf node is a node without a child node. An external node is another
name for it.
5. Non-leaf node - A node with at least one child node is referred to as a non-leaf node.
An internal node is another name for it.
6. Path - It is a sequence of continuous edges from the origin node to the destination
node. The edge here is a link between two nodes.
7. Ancestor - The antecedent node that occurs on the path from the beginning to that
node is called the ancestor.
8. Descendant - Successive nodes in the path from that node to the leaf node.
9. Sibling - All children with the same parent node are called siblings.
10. Degree - The number of children in a particular node is called a degree.
11. Depth of node - The length of the path from the root to that node is called the depth of
a node.
12. Height of a node - The number of edges that occur on the longest path from that node
to the leaf node is called the height of a node.
13. Level of node - The number of edges from the root node to the given node is called the
level of a node. [ CITATION jav21 \l 1033 ]
1.1.3.2. Graph.
What is a Graph?
A graph is a non-linear data structure that has a finite number of vertices and edges and is
used to connect these edge vertices. Vertical is used to store the data element and represents
the relationship between the vertical of the edges. Uses a graph for various real-time issues
such as telephone networks, circuit networks, social networks such as LinkedIn, and
Facebook. In the case of Facebook, a single user can be considered a node, while a user is
referred to as the edge of the connection with others. [ CITATION jav21 \l 1033 ]
Figure 5 Graph
Operations Description
1. Enqueue operation.
The queue maintains two front and rear data indicators. Therefore, it is difficult to execute
relative to the performance of its operating inventory.
To enqueue (insert) data into queues, do the following steps:
Step 3 - Increase the rear pointer to indicate to the next empty spot if the queue is not
yet full.
Rea Fron
Waiting list
After confirmation the specific reservation should be removed from the waiting list. Add to
confirmed reservation.
- After the Responding.
Need to add 4 reservations for queues. Then first go to the queue to check if the reservations
are full.
After confirmation, increase the rear pointer to the next empty space and add the new
reservation to the 4th row. The point where it faces backwards.
Rea Fron
Confirm
No change. Reservations must be on the waiting list if the customer does not respond.
Rea Fro
Waiting list
After confirmation, the customer does not change the requirements such as address, guests
count.
Rea Fron
Confirm
It will be removed during the confirmed reservation and added to the waiting list.
Rea Fron
Confirm
Remove
Reservation Reservation Reservation
3 2 1
Reservation
4
Rea Fron
Waiting list reservation
Then go to the customer reservation confirmed reservation and remove it from the waiting
list. In addition, the meal will be removed from the list of available meals.
Rea Fron
Waiting list
Remove
Reservation Reservation Reservation
3 2 1
Reservation
4
Rea Fron
Confirm
Rea Fron
Banquet hall
Remove
Banquet hall Banquet hall 2 Banquet hall
3 1
Banquet hall
4
2. Dequeue Operation.
Dequeue Operation in Queue - In Queue, a Dequeue Operation is when you access material
while removing it from the front end of the queue. Accessing data from the queue is a two-
pronged process, accessing the data shown on the front and deleting the data after access. The
dequeue operation is carried out in the following ways.
Step 1 - Check if the queues are empty.
Step 3 - If the queue is not empty, go to the data shown on the front.
Step 4 - Increase the front pointer to point to the next existing data element.
Before the function should check reservation, queue is empty it means the reservation is there
or not. If there is no blank message and a blank message is provided, remove the reservation
and add it to the waiting list.
Rea Fron
Waiting list reservation
Rea Fron
Dequeue
Reservation Reservation Reservation
4 3 2
Reservation
1
If customer need change or reject the reservation. The same procedure should be process.
3. Init ().
1. isEmpty.
In order to prevent operations on empty stack, the programmer needs to maintain the size of
the thrust internally and thus update during thrust and pop operations. isEmpty ()
Traditionally gives a boolean value: True if size is 0, else False.
2. Isfull ().
Since these use single-dimensional arrays to execute queues, check to see if the queue
indicator reaches MAXSIZE to determine if the queue is full. If this maintains a queue in a
circular linked list, the algorithm changes.
4. Search.
What is a search?
Search is the process of locating a given value in a list of values. It determines whether or not
there is a search key in the data. It is the algorithm process of finding a specific item in a
collection of items. It can be done on the internal data structure or on the external data
structure.
Search methods.
To find an element in a given array, it can be done in the following ways:
1. Sequential search.
Sequential search is also known as linear search. Sequential search starts at the beginning of
the list and examines every element of the list. It's a straightforward and easy-to-understand
search algorithm. Sequential search compares the element with all the other elements listed.
If the element matches, it returns the value index, or it returns -1.
2. Binary search.
Binary search is used to find an element in a sorted array. It is a fast search algorithm with
running time complexity in O (log n). Binary search works on the principle of division and
conquest. This search technique searches for a specific element by comparing it with the
middle part of the collection. It is useful when there are a large number of elements in the
array.[ CITATION tut213 \l 1033 ]
Task 02.
2.1. Implementing the Queue Data Structure.
OPERATIONS VARIABLES
Queues can be activated using an array, a vertical or a linked list. The easiest way to execute
a queue is to use an array.
Initially the head (FRONT) and tail (REAR) of the queues in the first index of the array
(starting the array index from 0). When the element is added to the queue, the tail moves
forward, always pointing to the point where the next element enters and the head is in the first
indicator.
2.1.1. Queue Implementation.
Push Operation.
Push operation involves inserting data items into a stack. Check out the restaurant plate
distributor in Figure 10. The push process adds plates (data items) to the plate distributor
(stack). The first plate is pushed to the bottom of the stack followed by all subsequent plates.
The first data item entered is the most inaccessible and is located at the bottom of the
inventory. [ CITATION stu211 \l 1033 ]
Code enawa
Code enawa
Task 04.
4.1.3.2. Implementation.
4.1.4.2. Implementation.
Figure 18 Selection Sort (Author Developed)
4.1.5. Comparison of Performance.
Algorithm.
01. Create a sptSet (a set of shortcut tree trees) that keeps the vertical notes inserted in the
shortest path tree, that is, the minimum distance from the source is calculated and completed.
Initially, this set was empty.
02. Give a distance value to all the verticals in the input graph. Start all distance values as
INFINITE. Assign the distance value to 0 for the origin header, then it will be selected first.
03. Not all verticals in sptSet are included
a) Choose a vertical plane with a minimum distance that sptSet does not have.
b) Add yourself to sptSet.
c) Update the distance value of all adjacent verticals in u. To update distance values,
re-run across all adjacent verticals. For each adjacent header, update the distance in
u (from the origin) and the distance in v if the edge weight is less than the distance
in u-v, v.
Advantages and Disadvantages of Dijkstra’s Algorithm
Advantages of Dijkstra.
1. One of its main advantages is its small complexity.
2. By stopping the algorithm after reaching the shortest distance to the destination
node, it can be used to calculate the shortest path between one node to all other
nodes and to a single destination node from a single source node.
3. It only works for oriented, weighted graphs and should have non-negative values
at all edges.
Disadvantages of Dijkstra.
1. It makes a vague quest that takes a lot of time to set up,
2. Negative edges cannot be manipulated.
3. Since it goes to the acyclic graph, the correct shortcut cannot be achieved and
there is a need to maintain vertical tracking.
Dijkstra Implementation.
Figure 20 Dijkstra Implementation 1 (Author Developed)
The Bellman-Ford algorithm is a chart search algorithm that finds the shortest path between a
given prototype and all other verticals in a graph. This algorithm can be used on both
weighted and unweighted charts. Like Dijkstra's shortcut algorithm, the Belman-Ford
algorithm guarantees finding the shortest path in a graph. Although it's slower than Dijkstra's
algorithm, Belman-Ford has the ability to handle graphs with negative edge loads, so it's
more versatile. It is worth noting that there is no shortcut if the chart has a negative cycle.
Traveling around the wheel on an indefinite basis will reduce the cost of the road (even if the
road length is longer). Because of this, Bellman-Ford is also able to detect negative-negative
cycles, which is a useful feature.[ CITATION bri21 \l 1033 ]
2. Queue -
1. IsFull (), is used to check whether queues are full.
2. IsEmpry (), is used to check whether queues are empty.
3. Insert (x), this is used to add x to the queue at the back end.
4. Delete (), this is used to delete one element from the front end of the queue.
5. Size (), this function is used to get the number of elements present in the queue.
Figure 29 ADT Queue
3. List -
1. Size (), uses this function to get the number of elements in the list.
2. Insert (x), this method is used to add a single item to a list.
3. Remove (x), this function is used to remove the given element from the list.
4. Get (i), this function is used to get the element at location i.
5. Replace (x, y), this function is used to replace the x value with the y value.
6.1.1. Encapsulation.
OAP is one of the four concepts in Encapsulation. The other three are inheritance, multimeter
and abstraction. Java encryption is the process of encoding data (variables) and data
(methods) into a single unit. In development, class variables are hidden from other classes
and can only be accessed by their current class methods. Hence it is also known as data
hiding. Technically, class variables or data are hidden from any other class and can only be
accessed through any member function of their own class that has published it. Circulation
can be achieved by declaring all the variables in the class as private and by writing common
class methods for setting and retrieving the values of the variables.
To Achieve Circulation in Java -
1. Private expression of class variables.
2. Provide common settings and retrieval modes for modifying and viewing variable
values.[ CITATION tut215 \l 1033 ]
6.1.1.1. Encapsulation example with Java.