Docsity

DS&A - 1649 - Assignment 2, Assignments of Data Structures and Algorithms

Toggle sidebar
bg1
Page | 1
ASSIGNMENT 2 FRONT SHEET
Qualification
BTEC Level 5 HND Diploma in Computing
Unit number and title
Unit 19: Data Structures and Algorithms
Submission date
02/07/2023
Date Received 1st
submission
Re-submission Date
Date Received 2nd
submission
Student Name
Student ID
Class
GCH1103
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
Grading grid
P4
P5
P6
P7
M4
M5
D3
D4
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16

Partial preview of the text

Download DS&A - 1649 - Assignment 2 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 02/07/2023 Date Received 1st submission Re-submission Date Date Received 2nd submission Student Name Student ID Class GCH1103 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 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 (P4)
      1. Stack
      • 1.1. Definition
      • 1.2. Operation
      1. Queue
      • 2.1. Definition
      • 2.2. Operation
  • II. Implement error handling and report test results (P5)
      1. Error handling
      1. Testing plan
      1. Evaluation
  • III. How asymptotic analysis can be used to assess the effectiveness of an algorithm (P6)
      1. Big-O Notation
      1. Big-Ω Notation
      1. Big-Theta Notation
  • IV. Determine two ways in which the efficiency of an algorithm can be measured, illustrating your answer with an example (P7)
  • References
  • Figure 1: Push operation
  • Figure 2: Pop operation
  • Figure 3: Peek operation
  • Figure 4: isEmpty operation
  • Figure 5: size operation..................................................................................................................................................................................................
  • Figure 6: search operation
  • Figure 7: Enqueue() operation
  • Figure 8: Dequeue() operation.....................................................................................................................................................................................
  • Figure 9: getFront() operation
  • Figure 10: getRear() operation.....................................................................................................................................................................................
  • Figure 11: isEmpty() operation
  • Figure 12: size() operation
  • Figure 13: search() operation.......................................................................................................................................................................................
  • Figure 14: Error handling example
  • Figure 15: Error handling example
  • Figure 16: Constant time complexity
  • Figure 17: Linear time complexity
  • Figure 18: Logarithmic time complexity
  • Figure 19: Example
  • Figure 20: Example

I. Design and implementation of Stack ADT and Queue ADT (P4)

1. Stack

1.1. Definition

A stack is an abstract data type (ADT) that represents a collection of elements with a specific set of operations. It follows the Last-In- First-Out (LIFO) principle, where the most recently added element is the first one to be removed. (GeeksforGeeks, n.d.) To implement the stack, it is required to maintain the pointer to the top of the stack, which is the last element to be inserted because we can access the elements only on the top of the stack.

1.2. Operation

Push(E data): void Figure 1 : Push operation

isEmpty(): Boolean Figure 4 : isEmpty operation size(): int Figure 5 : size operation search(E element): int

Figure 6 : search operation

2. Queue

2.1. Definition

A queue is an abstract data type (ADT) that represents a collection of elements with a specific set of operations. It follows the First- In-First-Out (FIFO) principle, where the first element added is the first one to be removed. In a queue, elements are added to the rear (also known as the "back") and removed from the front (also known as the "front" or "head") of the queue.

2.2. Operation

Enqueue(E data): void

Figure 8 : Dequeue() operation getFront(): E Figure 9 : getFront() operation getRear(): E

Figure 10 : getRear() operation isEmpty(): boolean Figure 11 : isEmpty() operation size(): int Figure 12 : size() operation search(E element): int

Figure 14 : Error handling example 1 The code uses if/else statements to handle error conditions related to empty stacks or specific conditions within the code logic. In the pop() operation, an if condition is used to check if the stack is empty ( top == null ). If it is empty, null is returned to indicate the error condition. The code also includes a try/catch block to handle a NullPointerException that may occur during the pop() operation. If a NullPointerException is caught, the catch block executes, printing an error message to the console and returning null to indicate the error condition.

Figure 15 : Error handling example 2 The code uses an if/else statement to check if the queue is empty by calling the isEmpty() method. If the queue is empty, null is returned to indicate the error condition. The code includes a try/catch block to catch a NullPointerException. If a NullPointerException occurs during the execution of the method, the catch block is executed. It prints an error message to the console and returns null to indicate the error condition.

2. Testing plan

No. Scope Operation Testing

type

Input Expected Output Actual

Output

Status

Enqueue(1) search(2) - > return index 0 7 Enqueue() Normal MyQueue:[] Enqueue(2) Enqueue(6) Enqueue(9) MyQueue:[2,6,9] getFront() - > 2 getRear() - > 9 size() - > 3 The same as expected output Pass 8 Search() Data validation MyQueue:[a, b, c] search(a) Return index 0 The system is stopped Failed 9 Dequeue() Normal MyQueue: [2] Dequeue() MyQueue: [] The same as expected output Pass 10 Dequeue() Normal MyQueue: [2,4,6,8,10] Dequeue() Dequeue() Dequeue() MyQueue: [2,4] getFront() - > 2 getRear() - > 4 size() - > 2 The same as expected output Pass 11 Dequeue() Normal MyQueue: [] Dequeue() Print error message The same as expected output Pass 12 Dequeue() Data validation MyQueue: [] getFront() getRear() Print error message The system is stopped Failed

3. Evaluation

After 12 test plans, 8 out of 12 test plans were successful, indicating that the majority of the stack operations are functioning correctly. These successful test plans covered various aspects, including push(), pop(), size(), and isEmpty() operations. Beside that, 3 out of 12 test plans were failed. These failures were mainly related to the search operation, which only supported searching for integer values. Additionally, insufficient error handling was identified as another reason for the failures. Overall, I need to improve searching operation for different data types and enhancing error handling to provide reliable functionality.

III. How asymptotic analysis can be used to assess the effectiveness of an

algorithm (P6)

1. Big-O Notation

Big-O notation is a mathematical notation used to describe the upper bound or worst-case scenario of an algorithm's time complexity or space complexity. It provides a way to analyze and compare the efficiency of different algorithms by focusing on their growth rates as the input size increases. The formal definition of Big O notation is as follows: f(n) = O(g(n)) if and only if there exist positive constants C and K such that 0 ≤ f(n) ≤ c · g(n) for all sufficiently large values of n. In other words, Big O notation says that the function f(n) is asymptotically bounded by the function g(n). This means that as the input size n gets very large, the two functions will become increasingly close. 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. (GeeksforGeeks, 2023)

2. Big-Ω Notation

The above expression can be described as if f(n) is theta of g(n), then the value f(n) is always between c1 * g(n) and c2 * g(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. (GeeksforGeeks, 2023)

4. Example Figure 16 : Constant time complexity In this case, the time it takes to retrieve an element from the array does not depend on the size of the array. Regardless of how many elements are in the array, accessing a specific element directly using its index can be done in constant time. Figure 17 : Linear time complexity This method runs from 0 to arr.length. The time complexity of this algorithm is O(n), as the running time grows linearly with value of arr.

Figure 18 : Logarithmic time complexity The code in this example is logarithmic time because the number of times the code executes is proportional to the logarithm of the size of the input. In this case, the code executes once for each power of 2 less than or equal to n, so the code will take O(log n) time to run, where n is the input

IV. Determine two ways in which the efficiency of an algorithm can be measured,

illustrating your answer with an example (P7)

Time complexity (Big-O notation): Time complexity represents the amount of time an algorithm takes to run, based on the input size. It characterizes the upper bound or worst-case scenario of an algorithm's time requirements. For example, an algorithm with a time complexity of O(n) will take a time proportional to the size of the input, while an algorithm with a time complexity of O(n^2) will take a time proportional to the square of the size of the input. Space Complexity (Big-O notation): Space complexity refers to the amount of memory or space required by an algorithm to solve a problem based on the input size. It characterizes the upper bound or worst-case scenario of an algorithm's space requirements. For example, an algorithm with a space complexity of O(1) will use a constant amount of memory, while an algorithm with a space complexity of O(n) will use a amount of memory proportional to the size of the input.