1. Trang chủ
  2. » Luận Văn - Báo Cáo

Assignment 2 Data Structure and Algorithms 1649 Greenwich

18 71 5

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 18
Dung lượng 1,32 MB

Nội dung

Assignment 2 Data Structure and Algorithms (1649) (điểm chuẩn Pass) năm 2022 đại học GW. Implement complex data structures and algorithms. Singly linked list and doubly linked list.The difference of singly linked list and doubly linked list. Implement singly linked list. Doubly linked list. Insert to middle of Linked list. Implement error handling and report test results. Testing plan. Discuss how asymptotic analysis can be used to assess the effectiveness of an algorithm. Theta Notation. Big O Notation. Omega Notation. Determine two ways in which the efficiency of an algorithm can be measured, illustrating your. P4 Implement a complex ADT and algorithm in an executable programming language to solve a well-defined problem. P5 Implement error handling and report test results. P6 Discuss how asymptotic analysis can be used to assess the effectiveness of an algorithm. P7 Determine two ways in which the efficiency of an algorithm can be measured, illustrating your answer with an example.

ASSIGNMENT FRONT SHEET Qualification BTEC Level HND Diploma in Computing Unit number and title Unit 19: Data Structures and Algorithms Submission date 31/08/2022 Date Received 1st submission Re-submission Date 31/08/2022 Date Received 2nd submission Student Name Nguyen Manh Tung Student ID GCH200064 Class GCH0907 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  Summative Feedback:  Resubmission Feedback: 2.1 Grade: Assessor Signature: Internal Verifier’s Comments: IV Signature: Date: Contents Implement complex data structures and algorithms I II 1.1 The difference of singly linked list and doubly linked list 1.2 Implement singly linked list 1.3 Doubly linked list Insert to middle of Linked list Implement error handling and report test results Testing plan Evaluation 10 III Discuss how asymptotic analysis can be used to assess the effectiveness of an algorithm 11 Theta Notation 11 Big O Notation 11 Omega Notation 12 Example 12 IV V Singly linked list and doubly linked list Determine two ways in which the efficiency of an algorithm can be measured, illustrating your 13 Reference 14 List of figures Figure 1: Structure of singly linked list and doubly linked list Figure 2: class Node Figure 3: addLast of Singly linked list Figure 4: removeLast of singly Figure 5: addFirst of singly Figure 6: removeFirst of singly Figure 7: toString of singly and doubly Figure 8: class Node of doubly Figure 9: addLast of doubly Figure 10: removeLast of doubly Figure 11: addFirst of doubly Figure 12: removeFirst of doubly Figure 13: insertMiddle of doubly Figure 14: Example of Theta notation (GeeksforGeeks, 2022) 11 Figure 15: Example of Big O notation (GeeksforGeeks, 2022) 12 Figure 16: Example of Omega Notation (GeeksforGeeks, 2022) 12 Figure 17: Example of O(1) 13 Figure 18: Example of O(n) 13 Figure 19: Selection sort 13 Figure 20: Change input from to 10 elements 14 List of tables Table 1: Compare Singly and doubly linked list Table 2: Test plan I Implement complex data structures and algorithms Singly linked list and doubly linked list 1.1 The difference of singly linked list and doubly linked list Singly linked list and doubly linked list are both list data structures used to store program information, they are all built on the basis of Linked list structure, the elements are linked together by nodes Figure 1: Structure of singly linked list and doubly linked list Difference between Singly linked list and doubly linked list Table 1: Compare Singly and doubly linked list Singly linked list The elements are only joined by the next node, Doubly linked list Two-way join elements ie the following element ie the previous element is connected to the will store the position of the element before and following element, but not vice versa after it, both next and previous nodes exist Only one-way access from head to tail Can access from head to tail or tail to head Requires memory cells for each element Requires memory cells for each element The next pointer of the last element is null The next pointer of the last element is null and the previous pointer of the first element is null Time complexity is large Time complexity is small Space complexity is small Space complexity is larger than singly but not significantly 1.2 Implement singly linked list Initialize MyLinkedList with generic a Class Node Initialize the Node class to store the element's value and a next Node to store the next Node object The constructor takes as an input parameter T data, assigns Node's data equal to the parameter and Node next equals null Figure 2: class Node b addLast Operation Singly Linked list has Nodes, head and tail are null, head is the first element, tail is the last element This operation will add the element to the end of the list If in case of list there is no element, assign head and tail equal to that Object Node (for first and last element are equal to new Node) If the list already has an element, then assign tail.next = new Node, then assign tail = new Node (the next position of tail is new Node, then assign tail = new Node so that tail remains the last element) Figure 3: addLast of Singly linked list c removeLast Operation If the function has no element, it cannot be deleted, so it will return null If the function has one element, assign both head and tail = null, then return the deleted value head.data In other cases, use a loop to move to the last element (next = null) and stop Before reaching the last element, there is an assignment to get the element near the end of the list After the loop is over, assign next to the last element = null, assign tail = the last element, and then return the deleted value Figure 4: removeLast of singly d addFirst operation The function adds to the head with the parameter T data, initializes the Node with the data parameter, assigns that Node's next = head and assigns head = new Node It will basically replace the head element with the new Node and set next to the old head Figure 5: addFirst of singly e removeFirst operation Function to remove the first element, if the list is empty, return null, if the list has an element, use the intermediate variable tmp to store the head, assign head = the 2nd element then assign the next of tmp = null, returns the deleted data value It cuts the link between elements and assigns the 2nd element to the first element Figure 6: removeFirst of singly f toString This function will use a loop to print out all the elements of the list as a string so that the user can check the array Figure 7: toString of singly and doubly 1.3.Doubly linked list Because doubly and singly are quite similar, I will explain the differences Like the Singly linked list, this function also has a Node class to store data, the next Node, in addition, it has an additional Node previous to store the previous Node Figure 8: class Node of doubly Like Singly's addLast, the only difference is that it assigns the previous of the newly added Node with the old Node tail Once done, increase the value of size a addLast Operation Like Singly's addLast, the only difference is that it assigns the previous of the newly added Node with the old Node tail Once done, increase the value of size Figure 9: addLast of doubly b removeLast operation It is quite similar to singly, the difference is: - In case there is element, after the deletion is done, the size will decrease by - In case of multiple elements, the element near the end will also be saved and assigned next = null, however, there is no need to use a loop to go from head but can go backwards from tail back with previous Figure 10: removeLast of doubly c addFirst operation The difference is that the previous head of the old head will be assigned to the new Node and increase the size value by Figure 11: addFirst of doubly d addLast operation The difference is that previous of the last element and next of the last element will be set to null to cut off the link between it and the last element Assign tail with the Node near the end, then reduce the size by Figure 12: removeFirst of doubly Also it has a method called toString but it's not different from Singly so I won't mention it again Insert to middle of Linked list To insert in the middle of the list, you must know the number of elements in the list, then go to that position and insert To perform the insertion, a Doubly linked list is used I will show some methods to insert in the middle of the list - Method 1: use a loop to go from head to the middle element and assign next of the element before it and previous of the element after it equal to it (new element) Then assign its previous to the element before it and next to the element after it - Method 2: Similar to method 1, but using a loop from tail My method is method Solution: In case the list is empty, call the addFirst operation to it In other cases, find the middle of the list with the formula: size/2 + Create a variable that stores the starting position of the list (from the first element) Use a loop with an iterator where the position variable is not larger than the size variable When these two variables are equal, that is, they are in the middle position, they will assign previous of the middle element (Node middle) and the next of the previous element (Node before) with the new Node, then assign the previous of the new Node with the Node before and next = Node middle Increase the value of size by Figure 13: insertMiddle of doubly II Implement error handling and report test results Testing plan Table 2: Test plan No Scope Operation Testing type Normal Input Excepted output Actual output Status SinglyLink edList ADT: MyLinked List S1 addLast(Element E) S1: [3,2,5] addLast(9) As expected addLast(Element E) Normal S1: [3] addLast(6) S1: [3,2,5,9] Print S1 = “3 9” S1: [3,6] Print S1 = “3 ” removeLast() Normal … and a= removeLast() S1: [3,2,5] a=9 removeLast() Validation S1: [] a = removeLast a = null addFirst(Element Normal E) S1: [3,2,5] addFirst(9) removeFirst() … and a removeFirst() Normal Passed S1: [6,3] Failed Print S1 = “6 3” As Passed expected NullPointerE xception S1: [9,3,2,5] As Print S1 = “9 expected 5” = S1: [3,2,5] As a= expected Failed Passed Passed removeFirst() Validation S1: [] a = removeFirst() a = null NullPointerE xception Failed addLast(Element E) Normal S1: [] addLast(9) S1: [9] Print S1 = “9 ” As expected Passed addLast(Element E) Normal S1: [3,7,9] addLast(6) 10 removeLast() Normal … and a= removeLast() S1: [3,7,9,6] S1: [3,7,9] Failed Print S1 = “3 Print S1 = “3 6” 79” S1: [3,2,5] As Passed a=9 expected 11 removeLast() Validation S1: [] a = removeLast 12 addFirst(Element Normal E) S1: [1,2,3] addFirst(9) 13 removeFirst() Normal … and a removeFirst() 14 removeFirst() Validation S1: [] a = removeFirst() a = null insertMiddle(Ele ment E) Normal S1:[1,2,3,4,5,6] insertMiddle(7) S1:[1,2,3,7,4,5,6] S1:[1,2,7,3,4, Failed Print S1 = “1 5,6] 7456” Print S1 = “1 273456” insertMiddle(Ele ment E) Normal S1: [] insertMiddle(8) S1:[8] Print S1 = “8 ” 15 16 DoublyLin kedList ADT: DoublyLin kedList Question b a = null a=null S1: [9,1,2,3] As Print S1 = “9 expected 3” = S1: [1,2,3] As a= expected As expected S1:[] Print S1 = “” Passed Passed Passed Passed Failed Evaluation The test cases are recorded according to the execution process, so the failure cases have been fixed in the implementation part, the source code is the source code after fixing the errors in the test plan Out of 16 test cases executed, failed, most of the cases were test validation of the operation The data shows that validations often occur when the list is empty which in turn leads to failed functions In the program, the error is found to not perform the empty list condition check, which leads to error cases For example, when a list has no elements, assignments to remove the element cannot be performed, thus creating an Exception NullPointerException which causes the program to stop unexpectedly or cannot perform adding the element leaving the element unchanged 10 Also, in question b, the formula for the middle position is wrong, causing the element to be inserted incorrectly Solution: add conditional statements to check if the list is empty or not, modify the middle formula III Discuss how asymptotic analysis can be used to assess the effectiveness of an algorithm Asymptotic analysis is simply understood as a method of calculating the performance of an algorithm, how it affects the program in time or space, finding a most general formula for all the algorithms program, from which there will be a suitable application for each program It is usually classified into worst case (worst case), average case (average case) and best case (best case) Theta Notation Theta Notation is bounded around the upper and lower bounds of the algorithm (GeeksforGeeks, 2022), so it represents the limits of the algorithm, from which the average running time of the algorithm is calculated From there, it is applied to find the average case of the algorithm Average case is the average time of each run of an algorithm, so Theta Notation is the right choice to analyze the average case of the algorithm Figure 14: Example of Theta notation (GeeksforGeeks, 2022) Big O Notation Like Theta Notation, Big O Notation is also used for asymptotic analysis of algorithms Big O is the method of analyzing the upper limit of an algorithm (GeeksforGeeks, 2022), so it is applied to calculate the worst case for the algorithm This method calculates the maximum number of statements or functions to be executed of an algorithm, thereby figuring out their dependence on the input Finally, give the result of the worst case 11 Figure 15: Example of Big O notation (GeeksforGeeks, 2022) Omega Notation Omega Notation is used to analyze the lower asymptote of an algorithm so it is used for the best case analysis of an algorithm (GeeksforGeeks, 2022) An algorithm with a small best case will have a very fast running speed when entering a good case However, it is not used too much because most developers only care and optimize the worst case Figure 16: Example of Omega Notation (GeeksforGeeks, 2022) Example The Big O Notation asymptotic analysis method will be used to calculate the Worst case of some algorithms a O(1) 12 Figure 17: Example of O(1) A simple algorithm to check that n is divisible by 2, the if statement is executed at most and it is time, even if n = 4, 5, changes, the number of if runs does not change so it does not depend on n We have the worst case of this algorithm as O(1) b O(n) Figure 18: Example of O(n) Algorithm prints numbers from to n, print statement will be run n times, so it depends on n so worst case of this algorithm is O(n) IV Determine two ways in which the efficiency of an algorithm can be measured, illustrating your Selection sort algorithm Figure 19: Selection sort The time complexity of selection sort is calculated as the maximum number of times if(arr[min] > arr[j]) is executed We will consider loops, the first loop runs (4) times, the second loop runs times Hence T(5) = 4*4 = 16 times When input increments from elements to 10 elements 13 Figure 20: Change input from to 10 elements The first loop is run times, the second loop is times T(10) = 9*9 = 81 We see the general formula of T(n) = (n-1) * (n-1) so the Complexity of the selection sort is O(n2) Time complexity will increase with the order of squares for the number of input elements The space complexity of the selection sort will consist of two parts: the number of input cells and the number of generated memory cells Most devices today are 64bit, Ram 8, 16GB, so an int memory cell will occupy 8bytes, bool takes up byte in RAM The input storage capacity of elements is: 5*8bytes = 40 bytes The generated capacity: includes variables i, j, arr.length, arr.length – 1,min, j, temp, boolean variables of the conditional => 58 bytes When element increments from to 10: The input storage capacity of 10 elements is: 10*8bytes = 80 bytes The generated capacity: includes variables i, j, arr.length, arr.length – 1,min, j, temp, boolean variables of the conditional => 58 bytes We see that the generated capacity does not change despite increasing the number of input elements Increased input storage is something that happens in all algorithms, so we'll ignore them So the conclusion is that the Space complexity of the selection sort is O(1) which means it doesn't change even if the input changes V Reference GeeksforGeeks 2022 What are Asymptotic Notations in Complexity Analysis of Algorithms [online] Available at: https://www.geeksforgeeks.org/analysis-of-algorithms-set-3asymptotic-notations/ (Accessed 31 August 2022) 14 Powered by TCPDF (www.tcpdf.org) Index of comments 2.1 - P4: Singly and Doubly Linked-List have been compared and implemented successfully, but the work lacks illustrated figures to clarify these implementations One good thing is a solution has been provided for solving task (b) - P5: A number of test cases have been presented Some potential errors are revealed The testing result, then, has been evaluated - P6: The discussion about asymptotic analysis is presented with some examples - P7: Given an example of selection sort, time complexity & Space complexity have been described Powered by TCPDF (www.tcpdf.org) ... Figure 15: Example of Big O notation (GeeksforGeeks, 20 22) 12 Figure 16: Example of Omega Notation (GeeksforGeeks, 20 22) 12 Figure 17: Example of O(1) 13 Figure... data structures and algorithms Singly linked list and doubly linked list 1.1 The difference of singly linked list and doubly linked list Singly linked list and doubly linked list are both list data. .. (GeeksforGeeks, 20 22) Omega Notation Omega Notation is used to analyze the lower asymptote of an algorithm so it is used for the best case analysis of an algorithm (GeeksforGeeks, 20 22) An algorithm

Ngày đăng: 04/11/2022, 09:53

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

w