ASSIGNMENT 2 FRONT SHEET
Qualification BTEC Level 5 HND Diploma in Computing
Unit number and title Unit 19: Data Structures and Algorithms
Submission date Date Received 1st submission
Re-submission Date Date Received 2nd submission
Student Name Nguyen Phuoc Trong Student ID GCD191107
Class GCD0904 Assessor name Pham Thanh Son
Student declaration
I certify that the assignment submission is entirely my own work and I fully understand the consequences of plagiarism. I understand that
making a false declaration is a form of malpractice.
Student’s signature Trong
Grading grid
P4 P5 P6 P7 M4 M5 D3 D4
1|Page
Summative Feedback: Resubmission Feedback:
Grade: Assessor Signature: Date:
Internal Verifier’s Comments:
IV Signature:
2|Page
Contents
Introduction ................................................................................................................................................... 5
P4 Implement a complex ADT and algorithm in an executable programming language to solve a well
defined problem. ........................................................................................................................................... 5
❖ Algorithms and ADT in programming languages ........................................................................... 5
1. The implementation steps of the algorithm...................................................................................... 5
1.1. Implement program .................................................................................................................... 5
1.2. FlowChart ..................................................................................................................................... 6
2. Code..................................................................................................................................................... 8
P5 Implement error handling and report test results. ............................................................................... 11
❖ Error and result ............................................................................................................................. 11
1. Test case ............................................................................................................................................ 11
2. Result of test case and explanation ................................................................................................. 12
P6 Discuss how asymptotic analysis can be used to assess the effectiveness of an algorithm ............... 15
❖ Algorithm efficiency ...................................................................................................................... 15
1. Big Oh Notation (Ο): ......................................................................................................................... 17
2. Omega Notation (Ω): ........................................................................................................................ 19
3. Theta Notation (Θ):........................................................................................................................... 20
P7 Determine two ways in which the efficiency of an algorithm can be measured, illustrating your
answer with an example. ............................................................................................................................ 20
❖ Time efficiency and Space efficiency ............................................................................................ 20
1. Time efficiency. ................................................................................................................................. 21
Practice:.................................................................................................................................................... 21
2. Space efficiency. ............................................................................................................................... 21
Practice:.................................................................................................................................................... 22
Reference ..................................................................................................................................................... 24
Figures
Figure 1 FlowChart of my program ................................................................................................................ 7
Figure 2 directory tree .................................................................................................................................... 8
3|Page
Figure 3 Code .................................................................................................................................................. 9
Figure 4 Code (1) ............................................................................................................................................ 9
Figure 5 Code (2) .......................................................................................................................................... 10
Figure 6 Code (3) .......................................................................................................................................... 11
Figure 7 ............................................................................................................ Error! Bookmark not defined.
Figure 8 ............................................................................................................ Error! Bookmark not defined.
Figure 9 Test Case 1 ...................................................................................................................................... 13
Figure 10 Test Case 2 .................................................................................................................................... 13
Figure 11 Test Case 3 .................................................................................................................................... 14
Figure 12 Test Case 4 .................................................................................................................................... 14
Figure 13 Test Case 5 .................................................................................................................................... 15
Figure 14 Common Asymptotic Notations ................................................................................................... 16
Figure 15 Big Oh ........................................................................................................................................... 17
Figure 16 Linear Time Algorithms – O(n) ..................................................................................................... 18
Figure 17 Constant Time Algorithms – O(1) ................................................................................................. 18
Figure Omega .............................................................................................................................................. 19
Figure Theta ................................................................................................................................................. 20
Figure Calculate the execution time of the program .................................................................................. 22
Figure Test time of transferring .................................................................................................................. 23
Figure Test time of transferring (1) ............................................................................................................. 23
Tables
Table 1 Test Case .......................................................................................................................................... 12
4|Page
Introduction
For middleware currently under development, part of the delivery interface is how notifications can be
transferred and processed across layers. In order to transmit, typically a buffer of queue messages is
deployed and for processing, systems require a stack of messages. Our team is currently developing these
types of collections for the system. We designed the ADT / algorithm for these 2 structures and
implemented the demo version with the message being a string of up to 250 characters. The demo will
demonstrate some important operations of these structures. The following is a report on the
implementation of two data structures and how to measure the effectiveness of related algorithms and
evaluate the use of ADT in design and development, including complexity, exchange and benefits.
P4 Implement a complex ADT and algorithm in an executable
programming language to solve a well defined problem.
❖ Algorithms and ADT in programming languages
1. The implementation steps of the algorithm.
1.1. Implement program
I started ADT/calculus planning for these two structures and created a dashboard titled sending a 250
character string to the destination. The operation of software based on Queue to store a buffer of less
than 250 characters from the source to the destination, during the execution of the program, it is necessary
to have measures to check by test cases to find errors and correct them accordingly. physical.
Lined up. It's a data structure that simulates queuing to perform processing. Whoever gets in first will be
dealt with first, whoever gets in last will be dealt with later. This is called the FIFO - First In, First Out
principle.
The advantage of applying Queue in this scheme is: everyone will be fair in the process and the system will
not be overloaded. The model of using the Queue data structure is widely applied in order processing
systems, from purchasing to booking tickets, from e-commerce to the fintech sector such as securities.
Here are the steps of the algorithm:
5|Page
Input : Source message A;
Output : Destination message Q
➢ Step 1: From the first letter of the sentence to the nearest dot, the software will split the entire
message, adding each segment as items to an array until the end of the sentence.
➢ Step 2: The software begins comparing the array to the required criteria. The software will pause
and provide an error notice about illegal input if a sentence is empty or more than 250 characters.
➢ Step 3: The computer will add each element of the array to the queue if the message is valid.
➢ Step 4: The software begins determining whether or not the queue is empty. When there are no
more items in the queue, the program advances to the next step. If the queue still contains items,
it takes the element and adds it to the stack before deleting its spot in the queue.
➢ Step 5: The software will determine if the stack is empty or not after adding every element from
the queue to it. The program will display the elements from the stack as a message if the stack
contains any elements because of the First In Last Out rule. The first element will be printed last,
as indicated by this. The program ends if there are no more elements.
1.2. FlowChart
6|Page
Figure 1 FlowChart of my program
7|Page
2. Code
Code Snippet Of The Application:
program directory tree
Figure 2 directory tree
The queue and stack library I created in asignment 1 will be imported. All of the ADT files are saved in the
Message package, and I also utilize the Java library java.util.Scanner to get data from users. Then, I must
define a new stack called MessageStack and a new queue called MessageQueue.
inputMessage function is used to get input from user.
8|Page
Figure 3 Code
The sendMessage function is in responsibility of moving the message to the queue after determining
whether the condition in the if loop is true.
Figure 4 Code (1)
9|Page
The stackProcess function first checks to see if the queue is empty before clearing it until there are no
more elements in it. If it is not, it takes an element from the queue and pushes it directly onto the stack.
The messageStack function will determine whether there are any elements on the stack before printing
messages from it. The element will be printed at the end if it is. The software will stop if that happens.
Figure 5 Code (2)
This is the main class where I wrote the program and invoked the aforementioned functions to deal with
the message. Create a fresh message at first, then save it as a modifiable text. I then use the
inputMessage() function to obtain the user's message. The time left until the message is processed is then
stored in a start variable, which I then declare. Then I use the sendMessage(), stackProcess(), and
messageStack() functions to process the input message. I declare end and time variables in order to
calculate the program's execution time. In order to handle IllegalArgumentException whenever the input
is erroneous, I wrapped everything in a try-catch sentence.
10 | P a g e
Figure 6 Code (3)
P5 Implement error handling and report test results.
❖ Error and result
1. Test case
A test case is a set of activities performed on a system to determine if it meets the requirements and
functionality of the software correctly. Test cases are used to determine if various elements of the system
are working as intended and to ensure that the system complies with all standards, rules and customer
requirements current goods. Here is a table of my application test cases. The test cases table will provide
test case name, input, expected output, actual output and case status.
Test case Input data Expected output Actual output Result
Enter empty “Message is empty or has over Figure 7 PASS
string No input than 250 characters”
Enter over 250 Water (chemical formula H2O) is Figure 10 PASS
characters in a an inorganic, transparent,
sentence tasteless, odorless, and nearly “Message is empty or has over
colorless chemical substance, than 250 characters”
11 | P a g e
which is the main constituent of
Earth's hydrosphere and the fluids
of all known living organisms (in
which it acts as a solvent[1])
Enter 3 It is vital for all known forms of The hydrogen atoms are attached Figure 11
sentences life, despite providing neither to the oxygen atom at an angle of
with less than food, energy, nor organic 104.45°. Its chemical formula, H2O,
250 characters micronutrients. Its chemical indicates that each of its molecules
formula, H2O, indicates that each contains one oxygen and two PASS
of its molecules contains one hydrogen atoms, connected by
oxygen and two hydrogen atoms, covalent bonds. It is vital for all
connected by covalent bonds. The known forms of life, despite
hydrogen atoms are attached to providing neither food, energy, nor
the oxygen atom at an angle of organic micronutrients.
104.45°.
Enter 2 Water covers about 71% of the [3] Small portions of water occur as Figure 12
sentences Earth's surface, mostly in seas and groundwater (1.7%), in the glaciers
with less than oceans (about 96.5%). and the ice caps of Antarctica and
250 characters Greenland (1.7%), and in the air as
and 1 line [3] Small portions of water occur vapor, clouds (consisting of ice and
break as groundwater (1.7%), in the liquid water suspended in air), and FAIL
glaciers and the ice caps of precipitation (0.001%).
Antarctica and Greenland (1.7%),
and in the air as vapor, clouds Water covers about 71% of the
(consisting of ice and liquid water Earth's surface, mostly in seas and
suspended in air), and oceans (about 96.5%).
precipitation (0.001%).
Enter 250 Water (chemical formula H2O) is Water (chemical formula H2O) is an Figure 13
characters in 1 an inorganic, transparent, inorganic, transparent, tasteless,
sentence tasteless, odorless, and nearly odorless, and nearly colorless
colorless chemical substance, chemical substance, which is the PASS
which is the main constituent of main constituent of Earth's
Earth's hydrosphere and the fluids hydrosphere and the fluids of all
of all known living organisms (in known living organisms (in which it
which it acts as a solvent) acts as a solvent)
Table 1 Test Case
2. Result of test case and explanation
In test case 1, I will leave input empty. The actual and expected results are the same. The program displays
a warning to the user that their message is invalid.
Test case 1: PASS
12 | P a g e
Figure 8 Test Case 1
In test case 2, I would enter more than 250 characters in a sentence for the error handler to catch. Actual
results are as expected. The program prints to the screen the error "Message is empty or has over than
250 characters".
Test case 2: PASS
Figure 9 Test Case 2
In test case 3, I will enter 3 sentences with less than 250 characters each separated by ".". Actual results
are as expected.
Test case 3: PASS
13 | P a g e
Figure 10 Test Case 3
In test case 4, I will enter 2 sentences with less than 250 characters and there will be 1 line break between
them to check if the program is working properly or not. Actual results are different from expected results.
Test case 4: FAIL
Figure 11 Test Case 4
14 | P a g e
In test case 5, I will enter 250 characters in 1 sentence. Actual results are as expected.
Test case 5: PASS
Figure 12 Test Case 5
P6 Discuss how asymptotic analysis can be used to assess the
effectiveness of an algorithm
❖ Algorithm efficiency
Asymptotic analysis of an algorithm refers to determining the mathematical/framework limit of its runtime
performance.
It tries to estimate the time as a function of the input size. For example, when analyzing the worst-case
runtime of a list of numbers, we consider the operation time as the length of the input list.
15 | P a g e
Figure 13 Common Asymptotic Notations
This analysis requires a transform input to the algorithm, otherwise the work is assumed to require
a constant amount of time. All factors other than the input operation are considered constant.
Asymptotic analysis refers to calculating the running time of any operation in a computational unit.
For example, it's running time for one operation is f(n) and for other operations it is g(n2). It is
realized that the first run time will increase linearly with increasing n and the running time of the
second operation will increase exponentially as n increases. And the running time of the two
operations will be the same if n is the smallest.
Typically, the time required by the algorithm falls into three categories:
o Best case - The minimum time required to execute the program.
o Average case - The average time required to execute the program.
o Worst case - The maximum time required to execute the program
Asymptotic analysis is bound to the input if there is no input to the algorithm, it is concluded to
operate in constant time.
Also, in Asymptotic Analysis we always talk about input size larger than constant value. It's possible
that these large inputs are never made available to your software, and the slower this is, which
always works better for your particular situation. So you can end up choosing a slower but faster
algorithm for your software.
16 | P a g e
Although runtime performance can be calculated using a variety of functions, the algorithm's
limited behavior is graphically represented using simple notation:
o Ο(n): Is the upper limit of the running time of the algorithm and measures the worst case
of how long the algorithm may take to complete a certain operation.
o Ω(n): Is the lower limit of the algorithm's runtime and measures the best case scenario of
how long the algorithm may take to complete a given operation.
o Θ(n): Diagram of both the upper and lower running time boundaries, with the average case
scenario expressed as the average between each border
1. Big Oh Notation (Ο):
Ο(n): Big O is used to measure the performance or complexity of an algorithm. In mathematical terms, it
is the upper limit of the growth rate of a function, or if the function g(x) grows no faster than the function
f(x), then g is said to be a member of O(f). . Generally, it is used to represent the upper bound of an
algorithm and give a measure of the worst or longest time complexity an algorithm may take to complete.
Big-oh is the formal method that refers to the upper bound of the algorithm's runtime. It is a measure of
the longest amount of time. The function f(n) = O(g(n)) [read as “f of n is the big-oh of g in n”] if and only
if there exists a positive constant c and the like (DataCamp, 2019).
Figure 14 Big Oh
Example code 1 Linear Time Algorithms – O(n):
Here is the simple algorithm presented above that will grow linearly with its input size.
17 | P a g e
We'd prefer a run time of 0.1n than (1000n + 1000), but both are still linear algorithms; they both grow
directly in proportion to the size of their inputs.
Again, if the algorithm was changed to the following:
Figure 15 Linear Time Algorithms – O(n)
The runtime would still be linear in the size of its input, n. We denote linear algorithms as follows: O(n).
As with the constant time algorithms, we don't care about the specifics of the runtime. O(2n+1) is the
same as O(n), as Big O Notation concerns itself with growth for input sizes.
Example code 2 Constant Time Algorithms – O(1):
Clearly, it doesn't matter what n is, above. This piece of code takes a constant amount of time to run. It's
not dependent on the size of n.
Figure 16 Constant Time Algorithms – O(1)
The above example is also constant time. Even if it takes 3 times as long to run, it doesn't depend on the
size of the input, n. We denote constant time algorithms as follows: O(1). Note that O(2), O(3) or even
O(1000) would mean the same thing.
We don't care about exactly how long it takes to run, only that it takes constant time.
Example code 3 Logarithmic Time Algorithms – O(log n)
Constant time algorithms are (asymptotically) the quickest. Logarithmic time is the next quickest.
Unfortunately, they're a bit trickier to imagine.
One common example of a logarithmic time algorithm is the binary search algorithm.
18 | P a g e
What is important here is that the running time grows in proportion to the logarithm of the input (in this
case, log to the base 2):
If n is 8, the output will be the following:
Our simple algorithm ran log(8) = 3 times.
2. Omega Notation (Ω):
Ω(n): The notation Ω(n) is the formal way to express the lower bound of an algorithm’s running time. It
measures the best-case time complexity or the best amount time an algorithm can possibly take to
complete. The function f (n) = Ω (g (n)) [“f of n is omega of g of n”] if and only if there exists positive
constant c and no such that (DataCamp, 2019).
Figure 17 Omega
F (n) ≥ k * g (n) for all n, n ≥ no
19 | P a g e
3. Theta Notation (Θ):
Θ(n): The notation θ(n) us the formal way to express the lower bound of an algorithm running time. It
measures the best-case time complexity or the best amount of time an algorithm can possibly take to
complete. The function f (n) = θ (g (n)) [“f is the theta of g of n”] if and only if there exists positive constant
k1, k2 and k0 such that (DataCamp, 2019).
Figure 18 Theta
For Example:
3n + 2 = θ (n) as 3n + 2 ≤ 3n and 3n + 2 ≤ 4n, for n
k1 = 3, k2 = 4 and n0 = 2
Hence, the complexity of f (n) can be represented as θ (g (n)).
The Theta Notation is precise than both the big – oh and Omega notation. The function f (n) = function f
(n) = θ (g (n)) if g(n) is both an upper and lower bound.
P7 Determine two ways in which the efficiency of an algorithm can be
measured, illustrating your answer with an example.
❖ Time efficiency and Space efficiency
Time efficiency and spatial effectiveness depend on a number of things, including:
• Quality of the code.
• The nature of the processor.
20 | P a g e
• Input data.
1. Time efficiency.
Time efficiency is a function that describes the amount of algorithm time used by the number of inputs to
the algorithm. "Time" can mean the number of memory accesses performed, the number of comparisons
between integers, the number of times an internal loop has been executed, or some other relevant natural
unit. ... the amount of real time that the algorithm will take.
Theory:
• Algorithm analysis, often using time complexity analysis to estimate run time as a function of the
size of the input data.
• Expected run time:
o Number of activities of Method 1: select the activity (s) most frequently performed and
determines the number of activities performed each time.
o 2-step counting method: determining the total number of steps, possibly coding,
performed by the program.
• Actual runtime depends on many factors:
o Computer speed: CPU (not just clock speed), I / O, etc.
o Compiler, compiler option.
o Amount of data.
o The last actual data
Practice:
• Use benchmarks to calculate algorithm time. Many programming languages have functions
available that provide CPU time.
• By executing the code, we can:
o Benchmarking: running programs on different data sets and measuring performance.
o Profile: A report of the amount of time spent on each program routine, used to find and
correct hotspots in it.
2. Space efficiency.
Space efficiency is a function that describes the amount of memory (space) that an algorithm uses
according to the number of inputs to the algorithm. We usually talk about the "backend" memory needed,
not counting the memory needed to store the input data. Again, we use natural (but fixed-length) units to
measure this. We can use bytes, number of fixed size structs, etc.
In the end, the function we come up with will be independent of the actual number of bytes needed to
represent the unit.
21 | P a g e
Theory:
This section will deal with memory resource usage while the algorithm is being executed, typically using
space complexity analysis to estimate the runtime memory required as a function such as size of data.
Even though. Input data. The result is usually specified by Big O.
Practice:
There are up to four aspects of memory usage to consider:
• The amount of memory required to store the code for the algorithm.
• Amount of memory required for input data.
• Amount of memory required for all output data
• Amount of memory required as workspace during calculation.
Example code in Java:
Code to calculate the execution time of the program.
Figure 19 Calculate the execution time of the program
22 | P a g e
Figure 20 Test time of transferring
Figure 21 Test time of transferring (1)
Based on the previous cases, we see that increasing the input can change the processing time. However,
the elapsed time may be more or less depending on how strong or weak the computer's processor is. If
the computer reaches the most favorable conditions, the processing time will be less, and if the computer
is in the most unfavorable conditions, the processing speed will be slow.
23 | P a g e
Reference
[1] Orry Messer (2018). Practical Java Examples of the Big O Notation | Baeldung. [online]
Baeldung. Available at: https://www.baeldung.com/java-algorithm-complexity.
[2] Team, D. (2021). Asymptotic Analysis of Algorithms in Data Structures. [online] DataFlair.
Available at: https://data-flair.training/blogs/asymptotic-analysis-of-algorithms-in-data-
structures/.
[3] GeeksforGeeks. (2014). Data Structures - GeeksforGeeks. [online] Available at:
https://www.geeksforgeeks.org/data-structures/.
24 | P a g e