Assignment 2 Front Sheet: Qualification BTEC Level 5 HND Diploma in Computing Unit Number and Title Submission Date
Assignment 2 Front Sheet: Qualification BTEC Level 5 HND Diploma in Computing Unit Number and Title Submission Date
Assignment 2 Front Sheet: Qualification BTEC Level 5 HND Diploma in Computing Unit Number and Title Submission Date
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.
Grading grid
P4 P5 P6 P7 M4 M5 D3 D4
Summative Feedback: Resubmission Feedback:
IV Signature:
Page 2
ASSIGNMENT 2 BRIEF
Qualification BTEC Level 5 HND Diploma in Business
Unit Tutor
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
Assignment Brief and Guidance:
Scenario: Continued from Assignment 1.
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
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 3
Learning Outcomes and Assessment Criteria
Page 4
Table of Contents
P4 Implement a complex ADT and algorithm in an executable programming language to solve a well
defined problem................................................................................................................................6
P5 Implement error handling and report test results.........................................................................8
P6 Discuss how asymptotic analysis can be used to assess the effectiveness of an algorithm.............9
1. Asymptotic Analysis – Analysis of Algorithms................................................................................9
1.1. Big Oh Notation, Ο........................................................................................................................10
1.2. Omega Notation, Ω.......................................................................................................................10
1.3. Theta Notation, θ.........................................................................................................................11
2. The execution Time of Algorithms...............................................................................................11
3. General Rules for Estimation........................................................................................................14
P7 Determine two ways in which the efficiency of an algorithm can be measured, illustrating your answer
with an example..............................................................................................................................14
1. Measurement of algorithm efficiency..........................................................................................14
2. Common Growth Rates................................................................................................................17
3. A Comparison of Growth-Rate Functions.....................................................................................19
REFERENCES....................................................................................................................................23
Page 5
ASSIGNMENT 2 ANSWERS
P4 Implement a complex ADT and algorithm in an executable programming
language to solve a well defined problem.
LinkedList implementation of Stack
package LinkedListStack;
import java.util.LinkedList;
public class LinkedListStack<E> {
LinkedList<E> list;
public LinkedListStack() {
list = new LinkedList<E>();
}
public boolean isEmpty() {
return list.isEmpty();
}
public void push(E x) {
list.addFirst(x);
}
public E pop() {
if (isEmpty())
return null;
return list.removeFirst();
}
public E top() {
if (isEmpty())
return null;
return list.get(0);
}
Page 6
Convert 10 to binary (Implementation)
package ArrayStack;
import java.util.Scanner;
public class Test2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Nhập vào một số nguyên: ");
int n = sc.nextInt();
ArrayStack<Integer> stack = new ArrayStack<Integer>();
while (n > 0) {
stack.push(n % 2);
n /= 2;
}
System.out.println("Số nhị phân tuong ứng: ");
while (!stack.isEmpty())
System.out.print(stack.pop());
sc.close();
}
}
Page 7
P5 Implement error handling and report test results.
Algorithm – Convert 10 to binary (Use of Stack)
Result:
After using the try-catch command to skip it and program execution can continue.
Page 8
Result:
Page 9
For a simple example, the running time of a given data mining query is considered f(n), and its corollary
search operation is calculated as g(n2). So the first operation’s running time increases linearly with the rise
in n, while the running time of the second operation increases exponentially as n is enlarged.
While run-time performance can be calculated with many different functions, the limiting behavior of
the algorithm is expressed graphically using simple notation:
Ο(n): Is the upper bound of an algorithm's running time and measures the worst case scenario of
how long an algorithm can possibly take to complete a given operation.
Ω(n): Is the lower bound of an algorithm's running time and measures the best case scenario of how
long an algorithm can possibly take to complete a given operation.
Θ(n): Is charting both the upper and lower running time boundaries, with the average case scenario
express as the average between each border.
Page 10
For example, for a function f(n)
Ω(f(n)) ≥ { g(n) : there exists c > 0 and n0 such that g(n) ≤ c.f(n) for all n > n0. }
θ(f(n)) = { g(n) if and only if g(n) = Ο(f(n)) and g(n) = Ω(f(n)) for all n > n0. }
Page 11
Example: Bubble Sort
public static void bubbleSort(int[] a) {
Page 12
boolean sorted = false;
int temp;
while(!sorted) {
sorted = true;
for (int i = 0; i < array.length - 1; i++) {
if (a[i] > a[i+1]) {
temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
sorted = false;
}
}
}
}
Page 13
3. General Rules for Estimation
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 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 Statement: 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
Some algorithms are more efficient than others. We would prefer to chose an efficient algorithm, so it
would be nice to have metrics for comparing algorithm efficiency.
The complexity of an algorithm is a function describing the efficiency of the algorithm in terms of the
amount of data the algorithm must process. Usually there are natural units for the domain and range of
this function. There are two main complexity measures of the efficiency of an algorithm:
Time complexity is a function describing the amount of time an algorithm takes in terms of the
amount of input to the algorithm. "Time" can mean the number of memory accesses performed,
the number of comparisons between integers, the number of times some inner loop is executed, or
some other natural unit related to the amount of real time the algorithm will take. We try to keep
this idea of time separate from "wall clock" time, since many factors unrelated to the algorithm
itself can affect the real time (like the language used, type of computing hardware, proficiency of
the programmer, optimization in the compiler, etc.). It turns out that, if we chose the units wisely,
all of the other stuff doesn't matter and we can get an independent measure of the efficiency of the
algorithm.
Space complexity is a function describing the amount of memory (space) an algorithm takes in
terms of the amount of input to the algorithm. We often speak of "extra" memory needed, not
counting the memory needed to store the input itself. Again, we use natural (but fixed-length) units
Page 14
to measure this. We can use bytes, but it's easier to use, say, number of integers used, number of
fixed-sized structures, etc. In the end, the function we come up with will be independent of the
actual number of bytes needed to represent the unit. Space complexity is sometimes ignored
because the space used is minimal and/or obvious, but sometimes it becomes as important an issue
as time.
Typically, when people talk about algorithm efficiency they mention a thing called computational
complexity, basically, how hard is it to execute your algorithm depending on the input. It is very important
to note the part about input, as algorithms in the most common case perform differently depending on
how input looks like. To reflect that, there is best, average, and worst case computational complexity,
meaning the best possible input, worst possible input, and average case for an input of fixed size.
Some examples:
Page 15
public static void printFirstItem(int[] items) {
System.out.println(items[0]);
This method runs in O(1)O(1) time (or "constant time") relative to its input. The input array could be 1 item
or 1,000 items, but this method would still just require one "step."
System.out.println(item);
This method runs in O(n)O(n) time (or "linear time"), where nn is the number of items in the array. If the
array has 10 items, we have to print 10 times. If it has 1,000 items, we have to print 1,000 times.
Here we're nesting two loops. If our array has nn items, our outer loop runs nn times and our inner loop
runs nn times for each iteration of the outer loop, giving us n^2 total prints. Thus this method runs in
O(n^2) time (or "quadratic time"). If the array has 10 items, we have to print 100 times. If it has 1,000
items, we have to print 1,000,000 times.
Page 16
Running times for small inputs
Page 17
Order-of-Magnitude Analysis and Big O Notation
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)).
The function f(n) is called the algorithm’s growth-rate function.
Since the capital O is used in the notation, this notation is called the Big O notation.
If Algorithm A requires time proportional to n^2, it is O(n^2).
If Algorithm A requires time proportional to n, it is O(n).
Order of an Algorithm
Definition:
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.
If an algorithm requires n^2–3*n+10 seconds to solve a problem size n. If constants k and n0 exist such
that
Page 18
k*n^2 > n^2–3*n+10 for all n ≥ n0 .
Thus, the algorithm requires no more than k*n^2 time units for n ≥ n0. So it is O(n^2)
Page 19
Page 20
Properties of Growth-Rate Functions
Example
Page 21
Page 22
REFERENCES
1. Adam Drozdek. Data Structures and Algorithms in Java: SECOND EDITION.
Available at:
https://www.tutorialspoint.com/data_structures_algorithms/asymptotic_analysis.htm#:~:text=Asymptotic
%20analysis%20of%20an%20algorithm,of%20its%20run%2Dtime%20performance.&text=Asymptotic
%20analysis%20is%20input%20bound,other%20factors%20are%20considered%20constant.
4. Asymptotic Analysis
5. Data Structure
8. Nor Bahiah Hj Ahmad & Dayang Norhayati A. Jawawi. SCJ2013 Data Structure & Algorithms. Algorithm
Efficiency Analysis [online]
Page 23
Available at:
http://ocw.utm.my/pluginfile.php/971/mod_resource/content/0/Module/ocwChp3_2Algorithm.pdf
10. Granville Barnett and Luca Del Tongo. Data Structures and Algorithms: Annotated Reference with
Examples, 1st edition.
Page 24