Assignment 2 Front Sheet: Qualification BTEC Level 5 HND Diploma in Computing Unit Number and Title Submission Date

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 24

ASSIGNMENT 2 FRONT SHEET

Qualification BTEC Level 5 HND Diploma in Computing

Unit number and title Unit 14: Business Intelligence

Submission date May 8, 2021 Date Received 1st submission

Re-submission Date Date Received 2nd submission

Student Name Lê Hoàng Hiệp Student ID GCS190464

Class GCS0805A_PPT Assessor name Vũ Thanh Hiền

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 Hiệp

Grading grid

P4 P5 P6 P7 M4 M5 D3 D4
 Summative Feedback:  Resubmission Feedback:

Grade: Assessor Signature: Date:


Internal Verifier’s Comments:

IV Signature:

Page 2
ASSIGNMENT 2 BRIEF
Qualification BTEC Level 5 HND Diploma in Business

Unit number Unit 19: Data Structures and Algorithms

Assignment title Implement and assess specific DSA

Academic Year 2021

Unit Tutor

Issue date Submission date

IV name and date

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

Pass Merit Distinction

LO1 Implement complex data structures and algorithms

P4 Implement a complex ADT M4 Demonstrate how the


D3 Critically evaluate the
and algorithm in an implementation of an
complexity of an implemented
executable programming ADT/algorithm solves a well-
ADT/algorithm
defined problem
language to solve a well
defined problem.

P5 Implement error handling


and report test results.

LO4 Assess the effectiveness of data structures and algorithms

P6 Discuss how asymptotic M5 Interpret what a trade-off is


analysis can be used to when specifying an ADT using D4 Evaluate three benefits of
assess the effectiveness of an an example to support your using implementation
algorithm answer independent data structures
P7 Determine two ways in
which the efficiency of an
algorithm can be measured,
illustrating your answer with
an example.

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);
    }

Convert 10 to binary (Use of Stack)

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();
    }
}

I use Stack to change the coefficients.


 Enter any int.
 Put the remainder of k chi for 2 (n% 2) on the stack.
 Assign n /= 2
 Reverse the stack I get the value to count.

Page 7
P5 Implement error handling and report test results.
Algorithm – Convert 10 to binary (Use of Stack)

Algorithm – Convert 10 to binary (Implementation)


At the beginning, I give the value divided by 0 (n = n / 0) and Java throws an exception after which the
program is terminated and an error message is displayed informing the user which exception was raised

Result:

After using the try-catch command to skip it and program execution can continue.

Page 8
Result:

P6 Discuss how asymptotic analysis can be used to assess the effectiveness of


an algorithm
1. Asymptotic Analysis – Analysis of Algorithms
What is Asymptotic Analysis?
Asymptotic analysis is the process of calculating the running time of an algorithm in mathematical units
to find the program’s limitations, or “run-time performance.” The goal is to determine the best case, worst
case and average case time required to execute a given task. While not a method of deep learning training,
Asymptotic analysis is a crucial diagnostic tool for programmers to evaluate an algorithm’s efficiency,
rather than just its accuracy.

How Does Asymptotic Analysis Work?


This analysis needs a variable input to the algorithm, otherwise the work is assumed to require a
constant amount time. All factors other than the input operation are considered constant.

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.

1.1. Big Oh Notation, Ο


The notation Ο(n) is the formal way to express the upper bound of an algorithm's running time. It measures
the worst case time complexity or the longest amount of time an algorithm can possibly take to complete.

For example, for a function f(n)


Ο(f(n)) = { g(n) : there exists c > 0 and n0 such that f(n) ≤ c.g(n) for all n > n0. }

1.2. Omega Notation, Ω


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 of time an algorithm can possibly take to complete.

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. }

1.3. Theta Notation, θ


The notation θ(n) is the formal way to express both the lower bound and the upper bound of an
algorithm's running time. It is represented as follows –

θ(f(n)) = { g(n) if and only if g(n) = Ο(f(n)) and g(n) = Ω(f(n)) for all n > n0. }

2. The execution Time of Algorithms


Each operation in an algorithm (or a program) has a cost.
 Each operation takes a certain of time.
count = count + 1;
 take a certain amount of time, but it is constant
A sequence of operations:
count = count + 1; Cost: c1
sum = sum + count; Cost: c2
 Total Cost = c1 + c2

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;
            }
        }
    }
}

Example: Insertion Sort


public static void insertionSort(int[] array) {
    for (int i = 1; i < array.length; i++) {
        int current = array[i];
        int j = i - 1;
        while(j >= 0 && current < array[j]) {
            array[j+1] = array[j];
            j--;
        }
        // at this point we've exited, so j is either -1
        // or it's at the first element where current >= a[j]
        array[j+1] = current;
    }
}

Example: Selection Sort


public static void selectionSort(int[] array) {
    for (int i = 0; i < array.length; i++) {
        int min = array[i];
        int minId = i;
        for (int j = i+1; j < array.length; j++) {
            if (array[j] < min) {
                min = array[j];
                minId = j;
            }
        }
        // swapping
        int temp = array[i];
        array[i] = min;
        array[minId] = temp;
    }
}

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

P7 Determine two ways in which the efficiency of an algorithm can be


measured, illustrating your answer with an example.
1. Measurement of algorithm efficiency

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.

Notation that used to show the complexity time of algorithms:

Notation Execution time / number of step


O(1) Constant function, independent of input size, n
Example: Finding the first element of a list.
O(log n) Problem complexity increases slowly as the problem size increases.
Squaring the problem size only doubles the time.
Characteristic: Solve a problem by splitting into constant fractions of the problem
(e.g., throw away ½ at each step)
O(n) Problem complexity increases linearly with the size of the input, n
Example: counting the elements in a list.
O(n log n) Log-linear increase - Problem complexity increases a little faster than n
Characteristic: Divide problem into subproblems that are solved the same way
Example: mergesort
O(n^2) Quadratic increase.
Problem complexity increases fairly fast, but still manageable
Characteristic: Two nested loops of size n
O(n^3) Cubic increase.
Practical for small input size, n.
O(2^n) Exponential increase - Increase too rapidly to be practical Problem complexity
increases very fast Generally unmanageable for any meaningful n
Example: Find all subsets of a set of n elements

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."

public static void printAllItems(int[] items) {

for (int item : items) {

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.

public static void printAllPossibleOrderedPairs(int[] items) {

for (int firstItem : items) {

for (int secondItem : items) {

System.out.println(firstItem + ", " + secondItem);

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.

2. Common Growth Rates:

Page 16
Running times for small inputs

Running times for moderate 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 .

The algorithm is order n^2 (In fact, k is 3 and n0 is 2)

3*n^2 > n^2–3*n+10 for all n ≥ 2 .

Thus, the algorithm requires no more than k*n^2 time units for n ≥ n0. So it is O(n^2)

3. A Comparison of Growth-Rate Functions

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: file:///C:/Users/ASUS/Downloads/Data%20Structures%20and%20Algorithms%20in


%20Java.pdf

[Accessed May 8, 2021]

2. Wu, C. Thomas. An Introduction to Object-Oriented Programming with Java, 5th Edition.

Available at: file:///C:/Users/ASUS/Downloads/An%20Introduction%20to%20Object-Oriented


%20Programming%20with%20Java,%205th%20Edition.pdf

[Accessed May 8, 2021]

3. Data Structures - Asymptotic Analysis

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.

[Accessed May 8, 2021]

4. Asymptotic Analysis

Available at: https://deepai.org/machine-learning-glossary-and-terms/asymptotic-analysis

[Accessed May 8, 2021]

5. Data Structure

Available at: https://sites.google.com/site/mukhlechur1/subjects/data-structure

[Accessed May 8, 2021]

6. CENG 707 Data Structures and Algorithms. Algorithm Analysis [online]

Available at: https://user.ceng.metu.edu.tr/~tcan/ceng707_f1112/Schedule/week2.pdf

[Accessed May 8, 2021]

7. Big O Notation [online]

Available at: https://www.interviewcake.com/article/java/big-o-notation-time-and-space-complexity

[Accessed May 8, 2021]

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

[Accessed May 8, 2021]

9. Quora.com. 2020. How Do I Measure An Algorithms Efficiency. [online]

Available at: https://www.quora.com/What-is-the-best-way-to-determine-the-efficiency-of-an-algorithm

[Accessed May 8, 2021]

10. Granville Barnett and Luca Del Tongo. Data Structures and Algorithms: Annotated Reference with
Examples, 1st edition.

Available at: file:///C:/Users/ASUS/Downloads/Dsa.pdf

[Accessed May 8, 2021]

Page 24

You might also like