Docsity

1649_Data Structure & Algorithms_Assignment 2, 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 8/29/2022
Date Received 1st
submission
Re-submission Date Date Received 2nd
submission
Student Name Mai Tran Tuan Anh Student ID GCD201452
Class GCD0904 Assessor name Pham Thanh Son
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
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b

Partial preview of the text

Download 1649_Data Structure & Algorithms_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 8/29/2022 Date Received 1st submission Re-submission Date Date Received 2nd submission Student Name Mai Tran Tuan Anh Student ID GCD Class GCD0904 Assessor name Pham Thanh Son 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 D

 Summative Feedback:  Resubmission Feedback:

Grade: Assessor Signature: Date: Internal Verifier’s Comments: IV Signature:

  • I. OVERVIEW ABOUT THE APPLICATION......................................................................................................................
  • II. APPLICATION...........................................................................................................................................................
      1. Flowchart.............................................................................................................................................................
      1. Implementation...................................................................................................................................................
      1. Message class......................................................................................................................................................
      1. Queue class........................................................................................................................................................
      1. Stack class..........................................................................................................................................................
      1. Main class..........................................................................................................................................................
  • III. TEST CASE.............................................................................................................................................................
  • IV. BIG O....................................................................................................................................................................
      1. Definition...........................................................................................................................................................
      1. Big O usage:.......................................................................................................................................................
      1. BigO complextities.............................................................................................................................................
    • 3.1 O(1) — Constant Time.....................................................................................................................................
    • 3.2 O(n) - Linear time complexity..........................................................................................................................
    • 3.3 O(log n) - Logarithmic time complexity............................................................................................................
    • 3.4 O(n^2) - Quadratic time complexity.................................................................................................................
      1. Big O Cheat Sheet..............................................................................................................................................
  • V. ANALYZE PERFORMANCE......................................................................................................................................
  • VI. BIBLIOGRAPHY......................................................................................................................................................
  • Figure 1: Interface AbstractQueue..............................................................................................................................
  • Figure 2: Interface AbstractStack.................................................................................................................................
  • Figure 3: Class Message.............................................................................................................................................
  • Figure 4: Class Queue................................................................................................................................................
  • Figure 5: Function isEmpty and ensureNonEmpty.....................................................................................................
  • Figure 6: Function enQueue......................................................................................................................................
  • Figure 7: Function deQueue......................................................................................................................................
  • Figure 8: Function toString........................................................................................................................................
  • Figure 9: Class Stack...................................................................................................................................................
  • Figure 10: Stack properties........................................................................................................................................
  • Figure 11: Function toString......................................................................................................................................
  • Figure 12: Function push...........................................................................................................................................
  • Figure 13: Function pop.............................................................................................................................................
  • Figure 14: Function peek...........................................................................................................................................
  • Figure 15: Creating objects........................................................................................................................................
  • Figure 16: Function EnterMessage............................................................................................................................
  • Figure 17: Function TransferMessage........................................................................................................................
  • Figure 18: Function StackProcess..............................................................................................................................
  • Figure 19: Function Print...........................................................................................................................................
  • Figure 20: Executing code..........................................................................................................................................
  • Figure 21: O(1)...........................................................................................................................................................
  • Figure 22: O(n)...........................................................................................................................................................
  • Figure 23: O(log n).....................................................................................................................................................
  • Figure 24: O(n^2).......................................................................................................................................................
  • Figure 25: Big O cheat sheet......................................................................................................................................
  • Figure 26: Short message..........................................................................................................................................
  • Figure 27: Long message without dot........................................................................................................................
  • Figure 28: Long message multiple dots.....................................................................................................................
  • Figure 29: Changing timer place................................................................................................................................
  • Figure 30: Short message..........................................................................................................................................
  • Figure 31: Long message without dot........................................................................................................................
  • Figure 32: Long message multiple dots.....................................................................................................................

I. OVERVIEW ABOUT THE APPLICATION

  • How messages can be transmitted and processed through layers is one aspect of the provider interface for the middleware that is currently being developed. Typically, a buffer of queued messages is used for transit, and the system needs a stack of messages for processing. The application also required the deployment of queue
  • ADT and algorithms should be developed for these two structures, and a demo version should be implemented using a message string that is no longer than 250 characters. Although this is just a demo, the application should handle all the error as well as exceptions carefully. The report should come with a test case to test all functions of the application and an evaluation of the speed of the project, the use of ADT in design and development, including the complexity, the trade-off and the benefits.

II. APPLICATION

1. Flowchart

  • The application flowchart is pretty simple. At first, the user will have to enter a string of message using the EnterMessage function. By the time the user has completed enter the message, a timer name start will be activated. This timer will be set and using System.nanoTime(), which will give us the most accurate possible elapsed time in nano second.
  • Then the real process started. Function TransferMessage(), using the for loop, will push the message that the user has entered previously into queue. This function is also the place where I handle the errors using exceptions. If the message is push successfully into queue, there will be an notice for the user, otherwise the system will display the exception.
  • Next is the StackStart() function. The while loop is requested by the function to determine whether the queue is empty or not. This function will end if it is empty. In contrast, the stack will begin to push the message that is dequeued from the queue while it is not empty.
  • StackStart() is the final function. The message is essentially printed out by this function. It will first print a brief warning informing users that a message is coming. The stack will then be checked via a loop to see if it is empty. There is no notification at all if the stack is empty. The stack will pop the message and print it out though if it is not empty.
  • After final function is executed, the system will printout the time, which is the executed time of the function and then exit.
  1. Implementation
  • Overall, the system will have 2 interfaces as well as 4 different classes. Interfaces will be for stack and queue, which hold all of theses’ needed properties
  • To prevent the system from having any difficulties with the remaining two, I implemented equivalent routines for the subsequent message working time. Figure 3 : Class Message
  1. Queue class
  • Next, we have Queue class. I will define the default capacity, the rear pointer and a queue. I also create a constructor, which create a new queue and a rear pointer. Figure 4 : Class Queue
  • Below are function isEmpty() and ensureNonEmpty(). With function isEmpty(), it will check the position of the rear pointer. If the index of rear pointer equal to -1, it will return true, which mean that the

queue is empty. The ensureNonEmpty() is also use to check whether the queue is empty or not, but this function will be used inside another function to ensure that the queue meets all the requirements before executing. Figure 5 : Function isEmpty and ensureNonEmpty

  • In order to insert new message into queue, I use function enQueue. First it will check whether the queue is full. If the queue is full, it will display a notice for the user, otherwise it will insert the message into queue. Figure 6 : Function enQueue
  • Function deQueue is used to get the message out of queue. After checking the queue is not empty, using a for loop, it will reassign all the index inside the queue and then take out the element that the user want to.

Figure 9 : Class Stack

  • The size function will return the length of the stack, while isEmpty() function will check whether the length is equal to 0 or not. If it is equal, the function will return true, which mean that the stack is currently empty Figure 10 : Stack properties
  • Function to string will print out all the elements inside the stack using a while loop

Figure 11 : Function toString

  • In order to push a new message into stack, I’m using a push function. This function will check if the stack length is maximum or not. If not, it will insert the new message into the stack Figure 12 : Function push
  • To retrieve a message out of a stack, I’m using the pop function. This function will check whether the stack is empty or not. If the not, it will retrieve the message out of the stack, reduce the length of it and return the message retrieved Figure 13 : Function pop
  • Function peek is used to retrieve the top element of the stack

Figure 17 : Function TransferMessage

  • The StackProcess() function is to push the message into stack. I will have to retrieve all the message out of queue and then push it again into stack Figure 18 : Function StackProcess
  • Final function is Print(), which is used to retrieve all message out of stack and display them on the screen.

Figure 19 : Function Print

  • This is the code to execute the program. The system will get the input from the user, push it into queue, get it all and push it again into stack. Finally, it will get the message out of stack and display it on the screen. I use the timer to check the time that the system needed to execute the program Figure 20 : Executing code

4 - Message with multiple sentences that separates with dots

  • The system should be able to split each sentence and display all the sentences in a reverse order Pass

IV. BIG O

1. Definition

  • Big O notation is a type of mathematical notation that expresses how a function limits itself when the argument tends to zero or infinity. It is a common mathematical notation that displays an algorithm's worst-case efficiency in relation to the size of its input. Two factors must be taken into account in order to determine an algorithm's efficiency:  Time Complexity: How long does it take for the process to finish?  Space Complexity: How much additional room will it need during the process?
  • As the input size increases, Big-O notation captures the upper bound to demonstrate how much time or space an algorithm might use under the worst-case scenario. Typically, this is written as: f ( n )= O ( inputSize )
  • The formal mathematical definition of Big O is given for now. Two positive functions, T(n) and f(n), shall exist. If there are positive constants M and n0 such that T(n) Mf(n) for all n n0, we write T(n) O(f(n)) and assert that T(n) has order of f(n). This graph displays an instance where the definition's prerequisites are all satisfied.
  • In essence, T(n) ∊ O(f(n)) means that T(n) doesn't grow faster than f(n).
    1. Big O usage:
  • In order to discuss the long-term growth rates of functions, big-O notation is utilized. The runtime of an algorithm or related ideas like space complexity are frequently discussed in algorithm analysis. Big-O notation is frequently used to describe how an algorithm's runtime scales with the amount of the input. For instance, because the runtime increases quadratically with the size of the array to sort, we would state that selection sort has an O(n2) runtime. That is, the runtime of selection sort should nearly double if the input size is doubled. It is customary to exclude lower-order words and drop coefficients when using big-O notation. For instance, it would be preferable to write that binary search runs in time O rather than O( log2 n + 17), despite the fact that doing so is mathematically correct (log n).
  • Big-O notation is formally used to quantify a function's long-term behavior. If there are fixed constants c and n0 such that f(n) c g(n) for every n n0, then we state that f(n) = O(g) (or f(n) O(g(n) in some sources). This formal definition explains why low-order words and constant factors are irrelevant to us because they can be incorporated by enlarging c and raising n0 (the c term absorbs them). Although it is rarely used informally, this formal term is frequently employed in the careful analysis of algorithms.