Docsity

Data Structures And Algorithms | Assignment 1, Assignments of Data Structures and Algorithms

Toggle sidebar
bg1
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
Bui Duy Hung
Student ID
GCH200680
Class
GCH1002
Assessor name
Do Hong Quan
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
Hung
Grading grid
P4
P5
P6
P7
M4
M5
D3
D4
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19

Partial preview of the text

Download Data Structures And Algorithms | Assignment 1 and more Assignments Data Structures and Algorithms in PDF only on Docsity!

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 Bui Duy Hung Student ID GCH

Class GCH1002 Assessor name Do Hong Quan

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 Hung

Grading grid

P 4 P 5 P 6 P 7 M 4 M 5 D3 D

Summative Feedback:Resubmission Feedback:

Grade: Assessor Signature: Date:

Internal Verifier’s Comments:

IV Signature:

  • I. Design and implementation of Stack ADT and Queue ADT
    • a. Stack
        1. Introduction
        1. Operation
        1. Run program
    • b. Queue
        1. Definition
        1. Operation
        1. Run program
  • II. Application
    • a. Reverse a Stack without recusion
    • b. Reverse a Stack with recusion
  • III. Implement error handling and report test result
    • a. Test case..............................................................................................................................................................
    • b. Evaluation
  • IV. Discuss how asymptotic analysis can be used to assess the effectiveness of an algorithm
    • a. Discuss assymtotic analysis
    • b. Assymtotic notations
        1. Big-O Notation (O – notation)
        1. Omega notation (Ω-notation)
        1. Theta Notation (Θ-notation)
    • c. Type Big O notation
        1. Constant time complexity O(1)
        1. Linear Time Complexity O(n)
        1. Quadratic Time Complexity O(n^2)
        1. Logarithmic Time Complexity O(log n)
        1. Exponential Time Complexity O(2^n)
  • example....................................................................................................................................................................... V. Determine two ways in which the efficiency of an algorithm can be mesured, illustrating your answer with an
    • a. Space complexity
      1. Time complexity
  • References
  • Figure 1: Stack constructor
  • Figure 2: push
  • Figure 3: pop
  • Figure 4: isFull Stack......................................................................................................................................................
  • Figure 5: isEmpty Stack
  • Figure 6: size Stack
  • Figure 7: Run program
  • Figure 8: Result
  • Figure 9: enqueueTail
  • Figure 10: Dequeue.....................................................................................................................................................
  • Figure 11: isEmpty queue
  • Figure 12: Run program queue
  • Figure 13: result
  • Figure 14: Reverse without stack................................................................................................................................
  • Figure 15:Result
  • Figure 16: Result reverse with recursion
  • Figure 17: Big o notation.............................................................................................................................................
  • Figure 18: Ω-notation..................................................................................................................................................
  • Figure 19: Θ-notation..................................................................................................................................................
  • Figure 20: O(1)
  • Figure 21: O(n)
  • Figure 22: O(n^2)
  • Figure 23: O(log n)
  • Figure 24: O(2^n)
  • Figure 25: Insertion sort..............................................................................................................................................
  • Figure 26: Bubble sort.................................................................................................................................................

I. Design and implementation of Stack ADT and Queue ADT

a. Stack

1. Introduction

A Stack is a linear data structure that follows the LIFO (Last-In-First-Out) principle. Stack has one

end, whereas the Queue has two ends (front and rear). It contains only one pointer top pointer pointing

to the topmost element of the stack. Whenever an element is added in the stack, it is added on the top

of the stack, and the element can be deleted only from the stack. In other words, a stack can be defined

as a container in which insertion and deletion can be done from the one end known as the top of the

stack.

2. Operation

  • Stack constructor: Determine size of stack memory via size
  • Pop: Before ejecting an element, the stack must be checked to see if it is full. If full it will print "The

stack is empty" and stop ejecting the element from the stack. Otherwise, the next step is to push out the

head element at the top of the stack, then the head of the removed element will be the next head of its

adjacent element. From there we will get the deleted element out and the size will decrease by 1.

Figure 3 : pop

  • isFull(): If the memory stack is equal to the maximum capacity then the result will return true otherwise

it will return false.

Figure 4 : isFull Stack

  • isEmpty(): If the stack is empty and size = 0, it will return true, otherwise, if size is non-zero, it will

return false.

Figure 5 : isEmpty Stack

  • size: Determine the number of elements in the stack set Figure 6 : size Stack

3. Run program

Figure 7 : Run program

  • Dequeue: If the first node is not selected, it will print "Empty Queue" and return null. Then it will move

to the next node, if there is a node, then it will shift the head to that node and remove any association

with that head node. At the end we will have the value of that dropped node.

Figure 10 : Dequeue

  • isEmpty(): If the stack is empty then the value will return true and else it will return false if the size is

not 0.

Figure 11 : isEmpty queue

2. Run program

Figure 12 : Run program queue

  • In this program we will call Queue class with queue. Then we will perform the action of pushing the

values 1, 3, 5, 8 respectively, it will print out 8, 5, 3, 1 respectively. But when we call the dequeue

function once, we will have to push out the element at the top and the result will be 5, 3, 1.

Figure 13 : result

Figure 15 :Result

b. Reverse a Stack with recusion

Figure 16 : Result reverse with recursion

III. Implement error handling and report test result

a. Test case

No. Scope Operatio n Testing type Input Expected Output Actual Output Status 1 Stack ADT: MyStack Push() Normal MyStack: [2,4,6]; Push(10) MyStack: [10,2,4,6] Size of myStack = 4 The same as expected output Passed 2 Pop() Normal MyStack: [4,6,7]; Pop() MyStack: [6,7] Size of myStack = 2 The same as expected output Passed 3 Pop() Data validation MyStack: []; Pop() MyStack: []; Print an error message MyStack: []; The system is stopped! Failed

EnqueueTail( 8 ) expected output 14 enqueueTail() Data validation MyQueue: [2,4,6] EnqueueTail( 6 ) MyQueue [2,4,6] Size = 3 The same as expected output Passed 15 Dequeue() Normal MyQueue: [2,4,6,8] Dequeue() MyQueue [4,6,8] Size() return 3 Peek() return 2 The same as expected output Passed 16 Dequeue() Data validation MyQueue: [] Dequeue(5) MyQueue [] Size() return 0 Print an error message The same as expected output Passed 17 Peek() Normal MyQueue: [2,4,6,8] Item = Peek() MyQueue [2,4,6,8] Size() return 4 Item = 1 The same as expected output Passed 18 Peek() Data validation MyQueue [] Item = Peek() MyQueue [] Size() return 0 Item = null Print an error message The same as expected output Passed 19 printAll() Normal MyQueue [2,4,6,8] Print 2 => 4 => 6 => 8 The same as expected output Passed 20 Size() Normal MyQueue [2,4,6] Size() MyQueue [2,4,6] Size = 3 Print(“The size of queue: ”) The same as expected output Passed

b. Evaluation

Through the above test cases, the correct results are obtained with no false cases. When the user

wants to push data into a full list, it will give an error or when the user tries to pop a data from an empty

list, it will also give an error. Both in Stack and Queue works fine things like above.

IV. Discuss how asymptotic analysis can be used to assess the effectiveness of an algorithm

a. Discuss assymtotic analysis

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

  • Asymptotic analysis is input bound i.e., if there's no input to the algorithm, it is concluded to work in a

constant time. Other than the "input" all other factors are considered constant.

  • Asymptotic analysis refers to computing the running time of any operation in mathematical units of

computation. For example, the running time of one operation is computed as f(n) and may be for

another operation it is computed as g(n2). This means the first operation running time will increase

linearly with the increase in n and the running time of the second operation will increase exponentially

when n increases. Similarly, the running time of both operations will be nearly the same if n is

significantly small. (tutorialspoint, 2022)

b. Assymtotic notations

1. Big-O Notation (O – notation)

  • One of the most essential mathematical notations in computer science for determining an algorithm's

effectiveness is the Big O notation. The amount of time, storage, other resources, and a change in input

size required to run an algorithm can all be used to evaluate how effective it is. Data structure's Big O

Notation provides information about an algorithm's performance under various conditions. In other

words, it provides the worst-case complexity or upper bound runtime of an algorithm. (simplilearn,

Figure 18 : Ω-notation

  • Ω(g(n)) = { f(n): there exist positive constants c and n0 such that 0 ≤ cg(n) ≤ f(n) for all n ≥ n0 }
  • If there is a positive constant c that, for sufficiently big n, lies above cg(n), the aforementioned

statement can be seen as a function f(n) belonging to the set (g(n)).

  • Omega (g(n)) provides the minimal amount of time the algorithm needs for any value of n. (simplilearn,

3. Theta Notation (Θ-notation)

  • The function from above and below is enclosed in theta notation. It is used to examine the average-

case complexity of an algorithm because it represents the upper and lower bounds of the running time

of an algorithm. (simplilearn, 2022)

Figure 19 : Θ-notation

Theta bounds the function within constants factors

  • For a function g(n), Θ(g(n)) is given by the relation:
    • Θ(g(n)) = { f(n): there exist positive constants c1, c2 and n0 such that 0 ≤ c1g(n) ≤ f(n) ≤ c2g(n) for

all n ≥ n0 }

  • If there are positive constants c1 and c2 that allow the aforementioned expression to be sandwiched

between c1g(n) and c2g(n), for sufficiently big n, then it can be said that the function f(n) belongs to the

set (g(n)).

  • If a function f(n) lies anywhere in between c1g(n) and c2g(n) for all n ≥ n0, then f(n) is said to be

asymptotically tight bound. (simplilearn, 2022)

c. Type Big O notation

1. Constant time complexity O(1)

  • O(1) is that it takes a constant time to run an algorithm, regardless of the size of the input. Figure 20 : O(1)