Efficiency Analysis of Data Structures: A Comparison of Stacks and Queues, Essays (high school) of Computer science

An overview of two Abstract Data Types (ADTs), stacks and queues. It discusses their definitions, basic operations, and introduces two methods for measuring algorithm efficiency. The document also includes examples of stack and queue implementation and testing plans. Asymptotic analysis is used to assess the effectiveness of algorithms, specifically focusing on time complexity and the use of Θ, Big O, and Ω notations.

Typology: Essays (high school)

2021/2022

Uploaded on 10/18/2022

handsome-hoang
handsome-hoang 🇻🇳

3.7

(7)

8 documents

1 / 20

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

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 subm issio n
Re-sub mission
Date
26/07/2022
Date Received 2nd submission
26/07/2022
Student Name
Văn Đạt
Student ID
GCH18772
Class
Gch0906
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
Đạt
Grading grid
P5
P7
M4
M5
D3
D4
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14

Partial preview of the text

Download Efficiency Analysis of Data Structures: A Comparison of Stacks and Queues and more Essays (high school) Computer science 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 Receiv ed 1st subm issio n Re-sub m issio n Date 26/07/2022^ Date Receiv ed 2nd subm issio n 26/07/ Student Name Lê Văn Đạt Student ID GCH Class Gch0906 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 Đạt Grading grid P4 P5 P6 P7 M4 M5 D3 D

 Summat iv e Feedback:  Resubm issio n Feedba ck:

Grade: Assesso r Signa t ure: Date: Internal Verifier’s Comments: IV Signature:

I. Introduction

My report presents two types of ADTs, stack and queue. Define, basic operations, apply it by example are presented in ASM1. Most of the defined Stack and Queue operations as outlined in Exercise 1 are implemented and application. The report also presents a test plan for almost functionsof these two types of ADT. In addition, my report also shows how asymptotic analysis can be used to evaluate the efficiency of an algorithm. I will introduce two ways of measuring the efficiency of an algorithm with illustrative examples. The trade-off between these two algorithms when applied in practice for each problem.

II. Stack

Introduction LIFO, and Stack

LIFO data structure (Last Input First Output). The elements are pushed first, which will bepoped last. Stack is a Last In First Out (LIFO) data structure, it is based on the Last-In- First-Out principle (LIFO). The elements are pushed into the stack in order of entry first into the bottom of the stack. When getting the data, the elements are pop in reverse order, the top element is getted first (the last one pushed in). Stack in program have behaves like a real-world stack, for example a deck of cardsor a pile of plates. Java collections architecture offers a number of interfaces and classes to manage and implement application Easily. One of them is Stack

Operations of Stack There are mainly two operations: push and pop

  • push() – Insert an element at one end of the stack called top.
  • pop() – Remove and return the element at the top of the stack, if it is not empty. In the process of getting the data of the stack, sometimes we just need to get the information without deleting it. In addition, during the execution, we also need to check the status of the stack: empty, full... so the following functions were born to serve all operations on the stack:
  • peek() − get the top data element of the stack, without removing it. Return the element at the top of the stack without removing it, if the stack is not empty.
  • isEmpty() − check if stack is empty. Return true if the stack is empty, otherwise return false.
  • size() – Return the number of elements in the stack.

Implementation

Every Node in my Stack have attribute are “ data” and ‘next”. In my stack I stored 2 node

3 Peek Method 4 isEmpty Method

5 Size method

III. Queue

1. Introduction

FIFO data structure(First-In-First-Out ). The elements are pushed first, which will be poped first(data item stored first will be accessed first ) Queue is a First-In-First-Out (FIFO) data structure. Queue is an abstract data structure, somewhat similar to Stacks. different from stacks, a queue is open at both its ends. One end is always used toinsert data (enqueue) and the other is used to remove data (dequeue). Figure 5: Queue

2. Operations of a Queue

Somewhat similar to Stacks, There are mainly two operations: add and remove, calling enqueue() and dequeue()

  • enqueue() − add an item to the queue.
  • dequeue() − remove an item from the queue. In the process of getting the data of the queue, similar to Stacks, sometimes we just need to get the information without deleting it. In addition, during the execution, we also need to check thestatus of the Queue: empty, size so the following functions were born to serve all

3.2 Dequeue Method

3.3 Size 3.4 isEmpty

V. Implement error handling and report test results

1. Testing plans

Scope Operation Testing

type Input Expected output Actual output Status 1 Enqueue Normal Myqueue:[1,2,3] Enqueue(4) Myqueue:[1,2,3,4] Size of Myqueue: 4 Same as expected Pass 2 Dequeue Normal Myqueue[1,2,3,4] Dequeue(); Myqueue:[1,2,3] Size of Myqueue: Same as expected Pass 3 QueueADT isEmpty Data validation Myqueue[] isEmpty() true Same as expected Pass isEmpty Normal Myqueue:[1,2,3] false Same as expected Pass 4 Size Normal Myqueue:[1,2,3] 3 Same as expected Pass Size Data validation Myqueue[] 0 Same as expected Pass 5 Push Normal Mystack[1,2,3] Push(a) Mystack[1,2,3,a] Same as expected Pass 6 Pop Normal Mystack[1,2,3,a] Pop() Mystack[1,2,3] Same as expected Pass 7 Mystack Peek Normal Mystack[1,2,3] Peek() Mystack[1,2,3] 3 Same as expected Pass 8 isEmpty Normal Mystack[1,2,3] false Same as expected Pass isEmpty Data validation Mystack[] True Same as expected Pass 9 Size Normal Mystack[1,2,3] 3 Same as expected Pass Size Data validation Mystack[] 0 Same as expected Pass 10 removeDumplicate removeDumplicate Normal Myqueue[1,2,2,3] Myqueue[1,2,3] Size: 3 Same as expected Pass removeDumplicate Data validation Myqueuep[1,2,3] Myqueue[1,2,3] Size: 3 Same as expected Pass

that 0 <= c1g(n) <= f(n) <= c2g(n) for all n >= n0 } The above definition means, if f(n) is theta of g(n), then the value f(n) is always between c1g(n) and c2g(n) for large values of n (n >= n0). The definition of theta also requires that f(n) must be non- negative for values of n greater than n0. Big O Notation: The Big O notation defines an upper bound of an algorithm, it bounds a function only from above. For example, consider the case of Insertion Sort. It takes linear time in the best case and quadratic time in the worst case. We can safely say that the time complexity of Insertion sort is O(n^2). Note that O(n^2) also covers linear time. If we use Θ notation to represent time complexity of Insertion sort, we have to use two statements for best and worst cases: The worst-case time complexity of Insertion Sort is Θ(n^2).

  1. The best case time complexity of Insertion Sort is Θ(n). The Big O notation is useful when we only have an upper bound on the time complexity of an algorithm. Many times we easily find an upper bound by simply looking at the algorithm. O(g(n)) = { f(n): there exist positive constants c and n0 such that 0 <= f(n) <= cg(n) for all n >= n0} 3) Ω Notation: Just as Big O notation provides an asymptotic upper bound on a function, Ω notation provides an asymptotic lower bound. Ω Notation can be useful when we have a lower bound on the time complexity of an algorithm. As discussed in the previous post, the best case performance of an algorithm is generally not useful, the Omega notation is the least used notation among all three. For a given function g(n), we denote by Ω(g(n)) the set of functions. Ω (g(n)) = {f(n): there exist positive constants c and n0 such that 0 <= cg(n) <= f(n) for all n >= n0}. Let us consider the same Insertion sort example here. The time complexity of Insertion Sort can be written as Ω(n), but it is not very useful information about insertion sort, as we are generally interested in worst-case and sometimes in the average case. Examples : { (n^2+n) , (2n^2) , (n^2+log(n))} belongs to Ω( n^2) U { (n/4) , (2n+3) , (n/100 + log(n)) } belongs to Ω(n) U { 100 , log (2000) , 10^4 } belongs to Ω(1) 6. Determine two ways in which the efficiency of an algorithm can be measured, illustrating your answer with an example:

a. Efficiency of an algorithm:

Algorithm efficiency A measure of the average execution time necessary for an algorithm to complete work on a set of data. Algorithm efficiency is characterized by its order. 2 kinds of algorithm efficiency are Space efficiency and Time efficiency. In computer science, algorithmic efficiency is a property of an algorithm that relates to the number of computational resources used by the algorithm. Time Complexity: time (computation time or response time) consumed in performing a given task. Space Complexity: the data storage (RAM, HDD etc.) consumed in performing a given task

Space Complexity of an algorithm is the total space taken by the algorithm with respect to the input size. Space complexity includes both Auxiliary space and space used by input. Auxiliary Space is the extra space or temporary space used by an algorithm. Example of Space Complexity Figure 11: example of space complexity The space complexity for Bubble Sort is O(1), because only a single additional memory space is required for temp variable. It is an in-place sorting algorithm i.e. it modifies elements of the original array to sort the given array. Also, the best case time complexity will be O(n), it is when the list is already sorted.

7. Trade-off when specifying an ADT

Time Space Trade Off: It is a way of solving a problem or calculation in less time by using more storage space (or memory), or by solving a problem in very little space by spending a long time. It is a case where an algorithm or program trades increased space usage with decreased time. Here, space refers to the data storage consumed in performing a given task (RAM, HDD, etc), and time refers to the time consumed in performing a given task (computation time or response time) Need of Time Space Tradeoff: ❖ Less time by using more memory ❖ By solving a problem in very little space by spending a long time. Example : ArrayList with LinkedList, with two functions: adding and removing elements. ArrayList requires less memory than LinkedList. But ArrayList adding and removing elements is SLOWER than LinkedList. This is quite understandable because LinkedList only needs to change the pointer flow of the nodes in the list, so the complexity is O(1) and ArrayList has to increase/reverse all positions after the position you want to add/remove, so the complexity is is O(n).

8. Reference https://www.geeksforgeeks.org/abstract-data-types/ https://www.geeksforgeeks.org/understanding-time-complexity-simple-examples/ https://www.geeksforgeeks.org/analysis-of-algorithms-set-3asymptotic-notations/ https://adrianmejia.com/most-popular-algorithms-time-complexity-every-programmershould- know-free-online-tutorial-course/ https://www.tutorialspoint.com/data_structures_algorithms/stack_algorithm.htm https://citizenchoice.in/course/Data-Structures-and-Algorithms/Chapter%201/Time-Space-Trade- off-Abstract-Data-Types-ADT-