Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

1649 Datastructure Assignment 2 Pass, Assignments of Computer Science

agawuhenvaionvnenaiaeivnaaeiaj

Typology: Assignments

2020/2021

Uploaded on 06/29/2023

nicolasdaocter
nicolasdaocter 🇻🇳

23 documents

Partial preview of the text

Download 1649 Datastructure Assignment 2 Pass and more Assignments Computer Science in PDF only on Docsity! 1 Unuversity of Greenwich Data Structure & Algorithms Name:Nguyễn Minh Đạo Student ID : GCS210351 Subject ID: 1649 Class: GCS1003B Submission date: 14/03/2023 Subject Advisor:Lê Ngọc Thành Assignment No: 2/2 Assignment Brief 2 (RQF) Higher National Certificate/Diploma in Business Student Name/ID Number: Nguyen Minh Dao/ GCS210351 Unit Number and Title: Unit 19: Data Structures and Algorithms Academic Year: 2021 Unit Assessor: Le Ngoc Thanh Assignment Title: Implement and assess specific DSA Issue Date: Submission Date: Internal Verifier Name: Date: Submission Format: Format: ● The submission is in the form of an individual written report and a presentation. 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 Tutor. ● The form of submission will be a soft copy posted on http://cms.greenwich.edu.vn/ . ● Remember to convert the word file into PDF file before the submission on CMS. Note: ● The individual Assignment must be your own work, and not copied by or from another student. ● 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. 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 Date Received 1st submission Re-submission Date Date Received 2nd submission Student Name Student ID Class Assessor name 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 4  Summative Feedback:  Resubmission Feedback: Grade: Assessor Signature: Date: Internal Verifier’s Comments: IV Signature: 5 Table of Content Assignment Brief 2 (RQF)..................................................................................................................1 Higher National Certificate/Diploma in Business...........................................................................1 I. Implement ADT & algorithms.......................................................................................................8 1. Description of your application..............................................................................................8 2. ADT (P4, P5).........................................................................................................................8 2.1. Implement (P4)............................................................................................................8 2.1.1. Node.java...........................................................................................................9 2.1.2. Queue.java.......................................................................................................11 2.1.3. Stack.java.........................................................................................................13 2.1.4. Application.java................................................................................................15 2.1.5. Screenshot of result.........................................................................................17 2.2. Test case (P5)...........................................................................................................21 2.3. Implement error-handling for the app........................................................................22 II. Discuss how asymptotic analysis can be used to assess the effectiveness of an algorithm (P6)................................................................................................................................................24 1. Asymptotic analysis.............................................................................................................24 1.1. Definition....................................................................................................................24 1.2. Asymptotic notation...................................................................................................25 1.2.1. Big O notation (O)............................................................................................25 1.2.2. Omega notation (Ω)..........................................................................................25 1.2.3. Theta notation (ɵ).............................................................................................26 1.3. Common asymptotic notations..................................................................................27 1.4. How to find time complexity?.....................................................................................27 III. Determine two ways in which the efficiency of an algorithm can be measured, illustrating your answer with an example (P7)........................................................................................................28 1. Time complexity..................................................................................................................28 2. Space complexity................................................................................................................29 References....................................................................................................................................29 6 2.1.1. Node.java. Figure 2: Node class The class has two instance variables: ‘data’ and ‘next’. ‘data’ is a string variable that stores the data value of the node. ‘next’ is a reference to the next node in the linked list. 9 Figure 3: getter & setter method For the setter and getter methods in the Node class. I wrote ‘getData’ to get the data from a particular message. Then ‘setData’ to set the data for a specific declared variables. The 'setNext' method takes a 'Node' argument and sets the value of 'next' to the provided 'Node'. The ‘getNext’ method returns the reference to the next ‘Node’. 2.1.2. Queue.java. 10 Figure 4: Queue class I create a Queue class to manage the queue functions of the app The Queue class has the following instance variables: +‘front’: a reference to the first node in the queue. +‘rear’: a reference to the last node in the queue. +‘nodesCount’: an integer that stores the number of nodes in the queue. I also create some of the method in Queue class: •Queue(): a constructor that initializes the ‘front’, ‘rear’, and ‘nodesCount’ instance variables to null, null, and 0, respectively. Figure 5: enqueue method 11 Figure 11: print method In this method, system will print all the stack’s data from top to bottom in order. If the stack is empty, system prints a message and exit the app. Otherwise, it iterates through the stack from top to bottom, prints each node’s data field followed by an arrow symbol and prints ‘null’ at the end to show that stack is end. 2.1.4. Application.java. 14 Figure 12: Application class The main method begins by creating a new Scanner object to read user input, and then initializes a Queue object and a Stack object to store messages. 15 Figure13: do-while loop menu with switch case The program then enters a do-while loop that repeatedly displays a menu of options and prompts the user for input. There are several options for us to choose from ‘1’ to ‘5’. If you enter any of them, it’ll be read using the Scanner object and then processed using a switch case statement to determine which case to take based on the selected option. 16 Figure 18: option 3 case 1 This is the result of option 3 when the queue is empty. There’s no message in the queue so system prints ‘Empty’ Figure 19: option 3 case 2 This is the result of option 3 when the queue have message. The system prints the message in order LILO (last in last out) 19 There are 2 case when you choose option 4: Figure 20: option 4 case 1 This is the result of option 4 when the stack is empty. There’s no messages have been taken by the receiver in the queue so system prints ‘Empty’ Figure 21: option 4 case 2 This is the result of option 4 when the stack have received message. The system prints the message in order FILO (first in last out). 20 Figure 22: option 5 This is the result of option 5 when you chose it. The system execution will stop if you enter option 5. 2.2. Test case (P5). Test case Input data Expected output Actual output Result Type the message from 1 to 250 characters My name is Dao After sending the message, we can read the message appear in the console screen immediately Message sending successful and can be easily reading. Pass Type the message more than 250 characters Earn a certificate from a world class university and build job ready skills to grow your career in months instead of years You can complete these flexible and affordable online learning programs on your own schedule and you will benefit from interactive System will aware that the message is over 250 characters. Message can’t be sent because the characters is over 250. Pass Type the option 6 System will print System went error without no warning message Failed 21 Figure 25: Result after add an error handling code You can see above, error handling code is worked. It send a message that user type an invalid input and inform user to retype the selection as number. II. Discuss how asymptotic analysis can be used to assess the effectiveness of an algorithm (P6). 1. Asymptotic analysis. 1.1. Definition. Asymptotic analysis is a technique for calculating an algorithm's running time in mathematical units in order to determine the program's "run-time performance" (or "limitations"). The goal is to determine the optimum, optimal, and average times required to execute an activity. While not a technique for deep learning training, asymptotic analysis is an important diagnostic tool for programmers to examine an algorithm's effectiveness rather than just its correctness. 24 1.2. Asymptotic notation. As an algorithm's input tends toward a certain or limiting value, asymptotic notations are used to mathematically quantify the program's running duration. When the input array is already sorted, for example, the bubble sort method takes linear time, which is the best-case situation. 1.2.1. Big O notation (O). Big-O notation is used to express an algorithm's maximum allowable running time. As a result, it provides an algorithm's worst-case complexity. 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.2. Omega notation (Ω). With omega notation, the lowest bound of an algorithm's execution time is shown. As a result, it provides the best-case complexity of an algorithm. 25 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.2.3. Theta notation (ɵ). Theta notation includes the functions from above and below. It is used to investigate an algorithm's average-case complexity since it reflects the upper and lower boundaries of an algorithm's execution time. Example: θ(f(n)) = { g(n) if and only if g(n) = Ο(f(n)) and g(n) = Ω(f(n)) for all n > n0. } 26 Time Complexity: O(log(log n)) 2. Space complexity. Space complexity is simply the amount of memory space used by a problem or method while that problem or technique is being executed. The space complexity is determined not only by the variables' use of space in the issue or procedure, but also by the space for input values. The distinction between auxiliary and spatial complexity is frequently misunderstood by people. Example: Explain: In the above example, the input value is 'n,' which is a constant that will take up O space (1). Now consider auxiliary space, which is likewise O(1) because I and'sum' are constants. As a result, the overall space complexity is O. (1). References https://www.facebook.com/kdnuggets (2022). How To Calculate Algorithm Efficiency - KDnuggets. [online] KDnuggets. Available at: https://www.kdnuggets.com/2022/09/calculate-algorithm- efficiency.html [Accessed 14 Mar. 2023]. Programiz.com. (2023). Learn Data Structures and Algorithms. [online] Available at: https://www.programiz.com/dsa#:~:text=A%20data%20structure%20is%20a,efficient%20and %20optimized%20computer%20programs. [Accessed 14 Mar. 2023]. GeeksforGeeks. (2015). Stack Data Structure. [online] Available at: https://www.geeksforgeeks.org/stack- data-structure/ [Accessed 14 Mar. 2023]. 29 GeeksforGeeks. (2016). Queue Interface In Java. [online] Available at: https://www.geeksforgeeks.org/queue-interface-java/ [Accessed 14 Mar. 2023]. Thakkar, N. (2022). Stack and Queue in Java - Scaler Topics - Scaler Topics. [online] Scaler Topics. Available at: https://www.scaler.com/topics/java/stack-and-queue-in-java/ [Accessed 14 Mar. 2023]. Educative: Interactive Courses for Software Developers. (2020). Data structures 101: How to use stacks and queues in Java. [online] Available at: https://www.educative.io/blog/data-structures-stack-queue-java- tutorial [Accessed 14 Mar. 2023]. Tutorialspoint.com. (2023). Data Structures - Asymptotic Analysis. [online] Available at: https://www.tutorialspoint.com/data_structures_algorithms/asymptotic_analysis.htm#:~:text=Asymptotic %20analysis%20refers%20to%20computing,as%20g(n2). [Accessed 14 Mar. 2023]. Upadhyay, S. (2021). Time and Space Complexity in Data Structure: Complete Guide. [online] Simplilearn.com. Available at: https://www.simplilearn.com/tutorials/data-structure-tutorial/time-and- space-complexity#:~:text=Time%20complexity%20is%20a%20function,of%20input%20to%20the %20method. [Accessed 14 Mar. 2023]. GeeksforGeeks. (2021). Time Complexity and Space Complexity. [online] Available at: https://www.geeksforgeeks.org/time-complexity-and-space-complexity/ [Accessed 14 Mar. 2023]. Faceprep.in. (2023). FACE Prep | The right place to prepare for placements. [online] Available at: https://www.faceprep.in/data-structures/space-complexity/ [Accessed 14 Mar. 2023]. 30
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved