Professional Documents
Culture Documents
Academic Year
Submission Format:
Format: The submission is in the form of an individual written report. This should be written in a
concise, formal business style using single spacing and font size 12. You are required to make
use of headings, paragraphs and subsections as appropriate, and all work must be supported
with research and referenced using the Harvard referencing system. Please also provide a
bibliography using the Harvard referencing system.
Submission Students are compulsory to submit the assignment in due date and in a way requested by
the Tutors. The form of submission will be a soft copy in PDF posted on corresponding course
of http://cms.greenwich.edu.vn/ Project also needs to be submitted in zip format.
Note: The Assignment must be your own work, and not copied by or from another student or from
books etc. If you use ideas, quotes or data (such as diagrams) from books, journals or other sources, you
must reference your sources, using the Harvard style. Make sure that you know how to reference
properly, and that understand the guidelines on plagiarism. If you do not, you definitely get fail
Tasks
For the middleware that is currently developing, one part of the provision interface is how message can be
transferred and processed through layers. For transport, normally a buffer of queue messages is implemented and
for processing, the systems requires a stack of messages.
The team now has to develop these kind of collections for the system. They should design ADT / algorithms for
these 2 structures and implement a demo version with message is a string of maximum 250 characters. The demo
Page 1
should demonstrate some important operations of these structures. Even it’s a demo, errors should be handled
carefully by exceptions and some tests should be executed to prove the correctness of algorithms / operations.
The team needs to write a report of the implementation of the 2 data structures and how to measure the efficiency
of related algorithms. The report should also evaluate the use of ADT in design and development, including the
complexity, the trade-off and the benefits.
Page 2
LO1 Implement complex data structures and algorithms .................................................................................................. 4
P4 Implement a complex ADT and algorithm in an executable programming language to solve a well defined
problem ...................................................................................................................................................................... 4
1.2. Implement a complex ADT (e.g. Linked List) and algorithm (convert 10 to binary) in an executable
programming language to solve a well defined problem ........................................................................... 5
P6 Discuss how asymptotic analysis can be used to assess the effectiveness of analgorithm ................................. 13
P7 Determine two ways in which the efficiency of an algorithm can be measured, illustrating your answer with an
example.................................................................................................................................................................... 17
Reference ......................................................................................................................................................................... 24
Page 3
LO1 Implement complex data structures and algorithms
✓ Divide the number to be converted by 2 (the result is the integer), then divide by 2 (and only take the
integer part), the resulting binary number is the set of the rest of the parts.
➢ The binary number we obtained is the set of remainders of division and the number 10 in binary
is 0101.
➢ Example:
Page 4
// User input
System.out.println("Enter decimal number: ");
int num = in.nextInt();
while (num != 0)
{
int d = num % 2;
stack.push(d);
num /= 2;
}
➢ Out put:
1.2. Implement a complex ADT (e.g. Linked List) and algorithm (convert 10 to binary) in an
executable programming language to solve a well defined problem
➢ Code:
LinkedList h;
public LinkedListStack() {
h = new LinkedList();
return h.isEmpty();
Page 5
h.addLast(x);
return h.removeLast();
class LinkedListStack {
MyList h;
public LinkedListStack(){
h=new MyList();
}
public boolean isEmpty(){
return h.isEmpty();
}
public void push(int x){
h.addTail(x);
}
public Object pop(){
if(isEmpty()) {
return null;
}
return h.deleteTail();
}
Page 6
while(k>0){
s.push(new Integer(k%2));
k=k/2;
while(!s.isEmpty()){
System.out.print(s.pop());
System.out.println("");
package com;
class LinkedListStack {
MyList h;
public LinkedListStack(){
h=new MyList();
}
public boolean isEmpty(){
return h.isEmpty();
}
public void push(int x){
h.addTail(x);
}
public Object pop(){
if(isEmpty()) {
return null;
}
return h.deleteTail();
}
public static void main(String args[]){ LinkedListStack
lls=new LinkedListStack(); lls.push(120);
System.out.println("Element removed from LinkedList:" +lls.pop());
System.out.println("Element removed from LinkedList:" +lls.pop());
lls.push(130);
Page 7
System.out.println("Element removed from LinkedList: "+lls.pop());
}
}
➢ Out put:
✓ Assign k = k / 0
➢ Code in Javascript:
Page 8
final int max1=max+max/2;
final Object[] a1 = new Object[max1];
for (int i = 0; i <= top; i++) {
a1[i]=a[i];
}
a=a1;
max=max1;
return true;
}
public void push(final Object x){
if(isFull() &&!grow())return;
a[++top] = x;
}
➢ Out put:
➢ The initial value is divided by 0 (k = k / 0), the program is terminated and an error message is displayed.
➢ Code:
decToBin(12);
while(k % 2);
k = k / 0;
while(!s.isEmpty()) {
System.out.print(s.pop());
Page 10
}
System.out.println("");
➢ Result:
➢ So I used the try-catch command to skip it and the execution could continue.
➢ Code:
decToBin(12);
Page 11
System.out.println("End Of Main");
catch(Exception e) {
while(k % 2);
k = k / 0;
while(!s.isEmpty()) {
System.out.print(s.pop());
System.out.println("");
Page 12
➢ Result:
P6 Discuss how asymptotic analysis can be used to assess the effectiveness of analgorithm.
1. Analysis of Algorithms:
➢ Asymptotic analysis of an algorithm refers to defining the mathematical boundation/framing of its run-
time performance. Using asymptotic analysis, we can very well conclude the best case, average case,
and worst-case scenario of an algorithm.
➢ To analyze algorithms:
✓ First, we start to count the number of significant operations in a particular solution to assess its
efficiency.
Page 13
2. The execution time of algorithms:
➢ Each operation in an algorithm (or a program) has a cost. It leads to the situation that each operations
take a certain of time, but it is constant.
➢ A sequence of operation:
Total Cost = c1 + c2
➢ Example:
Simple If-Statement
if (n<0)
absval = -n
else
absval = n;
Simple Loop
i = 1;
sum = 0;
while (i <= n) {
i = i + 1;
sum = sum + 1;
}
Nested Loop
i = 1;
sum = 0;
while (i <= n){
j = 1;
while (j <= n) {
sum = sum + 1;
Page 14
j = j + 1;
}
i = i +1;
}
➢ Loops: The running time of a loop is at most the running time of the statements inside of that loop times
the number of iterations.
➢ Nested Loops: Running time of a nested loop containing a statement in the inner most loop is the
running time of statement multiplied by the product of the sized of all loops.
➢ Consecutive Statements: Just add the running times of those consecutive statements.
➢ If/Else: Never more than the running time of the test plus the larger of running times of S1 and S2.
Page 15
Figure 1: Algorithm Growth Rates
Example:
i = 1;
sum = 0;
while (i <= n) {
i = i+ 1;
sum = sum + 1;
}
= (c3+c4+c5)8n + (c1+c2+c3)
= a*n +b
Page 16
P7 Determine two ways in which the efficiency of an algorithm can be measured, illustrating your
answer with an example.
1. Algorithmic Performance:
2. Analysis of algorithms:
➢ How do we compare the time efficiency of two algorithms that solve the same problem: implement
these algorithms in a programming language (C++), and run them to compare their time requirement:
✓ We should not compare implementations, because they are sensitive to programming style that may
cloud the issue of which algorithm is inherently more efficient.
Page 17
➢ What data should the program use?
✓ When we analyse algorithms, we should employ mathematical techniques that analyse algorithms
independently of specific implementations computers or data.
➢ To analyse algorithm:
✓ First, we start to count the number of significant operations in a particular solution to assess its
efficiency.
✓ Then, we will express the efficiency of algorithms using the growth functions.
c Constant
log N Logarithmic
Log2N Log-squared
N Linear
N log N
N2 Quadratic
N3 Cubic
2N Exponential
Page 18
Figure 2: Running Time (milliseconds)
✓ If Algorithm A requires time proportional to f(n), Algorithm A is said to be order f(n), and it is
denoted as O(f(n)).
✓ Since the capital O is used in the notation, this notation is called the Big O notation.
Page 19
✓ If Algorithm A requires time proportional to n, it is O(n).
5. Order of an Algorithm:
✓ Algorithm A is order f(n) – denoted as O(f(n)) – if constants k and n0 exist such that A requires no
more than k*f(n) time units to solve a problem of size n ≥ n0.
✓ The requirement of n ≥ n0 in the definition of O(f(n)) formalizes the notion of sufficiently large
problems. In general, many values of k and n can satisfy this definition.
✓ If an algorithm requires n2–3*n+10 seconds to solve a problem size n. If constants k and n0 exist
such that k*n2 > n2–3*n+10 for all n ≥ n0.
✓ Thus, the algorithm requires no more than k*n2 time units for n ≥ n0 , so it is O(n2)
6. Growth-Rate functions:
Page 20
Figure 5: Comparison of Growth-Rate functions
✓ O(log2n):Time requirement for a logarithmic algorithm increases increases slowly as the problem
size increases.
✓ O(n): Time requirement for a linear algorithm increases directly with the size of the problem.
✓ O(n*log2n): Time requirement for a n*log2n algorithm increases more rapidly than a linear
algorithm.
✓ O(n2): Time requirement for a quadratic algorithm increases rapidly with the size of the problem.
✓ O(n3): Time requirement for a cubic algorithm increases more rapidly with the size of the problem
than the time requirement for a quadratic algorithm.
✓ O(2n): As the size of the problem increases, the time requirement for an exponential algorithm
increases too rapidly to be practical.
➢ If an algorithm takes 1 second to run with the problem size 8, what is the time requirement
(approximately) for that algorithm with the problem size 16?
Page 21
✓ O(n2): T(n) = (1*162) / 82 = 4 seconds
➢ We can ignore a multiplicative constant in the higher-order term of an algorithm’s growth-rate function.
➢ Example:
i = 1;
sum = 0;
while (i <=n){
j = 1;
while (j <= n){
sum = sum + i;
j = j + 1;
}
i = i + 1;
}
= a*n2 + b*n + c
Page 22
Page 23
Reference:
[1] beginnersbook.com. 2020. Java Program To Convert Decimal To Binary. [online] Available at:
<https://beginnersbook.com/2014/07/java-program-to-convert-decimal-to-binary/> [Accessed 27 October 2020].
[2] Niithanoi.edu.vn. 2020. Cách Sử Dụng TRY CATCH Trong JAVA Xử Lý Tất Cả Ngoại Lệ. [online] Available at:
<https://niithanoi.edu.vn/try-catch-trong-java.html> [Accessed 27 October 2020].
[3] beginnersbook.com. 2020. Try Catch In Java - Exception Handling. [online] Available at:
<https://beginnersbook.com/2013/04/try-catch-in-java/> [Accessed 27 October 2020].
[4] Tutorialspoint.com. 2020. Data Structures - Asymptotic Analysis - Tutorialspoint. [online] Available at:
<https://www.tutorialspoint.com/data_structures_algorithms/asymptotic_analysis.htm#:~:text=Asymptotic%20analysis%20of
%20an%20algorithm,of%20its%20run-
time%20performance.&text=Asymptotic%20analysis%20is%20input%20bound,other%20factors%20are%20considered%20
constant.> [Accessed 29 October 2020].
[8] Runestone.academy. 2020. 3.3. Big-O Notation — Problem Solving With Algorithms And Data Structures. [online]
Available at:
<https://runestone.academy/runestone/books/published/pythonds/AlgorithmAnalysis/BigONotation.html#:~:text=The%20orde
r%20of%20magnitude%20function,of%20steps%20in%20the%20computation.> [Accessed 29 October 2020].
[10] Sciencedirect.com. 2020. Algorithm Performance - An Overview | Sciencedirect Topics. [online] Available at:
<https://www.sciencedirect.com/topics/computer-science/algorithm-performance> [Accessed 29 October 2020].
Page 24