1649 data structures algorithms greenwich asm2

22 58 3
1649 data structures  algorithms greenwich asm2

Đ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

Table of Contents LIST OF FIGURE 4 I INTRODUCTION 5 II IMPLEMENT COMPLEX DATA STRUCTURES AND ALGORITHMS 5 1 HIGHLIGHT SOME DIFFERENCES BETWEEN SINGLY AND DOUBLY LINKED LIST 5 2 DESCRIBE SINGLY AND DOU.

Table of Contents LIST OF FIGURE: I INTRODUCTION: .5 II IMPLEMENT COMPLEX DATA STRUCTURES AND ALGORITHMS: .5 HIGHLIGHT SOME DIFFERENCES BETWEEN SINGLY AND DOUBLY LINKED-LIST: DESCRIBE SINGLY AND DOUBLY LINKED-LIST’S OPERATIONS: .5 IMPLEMENT SINGLY AND DOUBLY LINKED-LIST: 3.1 Singly linked-list: .6 3.2 Doubly linked-list: .11 INSERT AN ELEMENT IN THE MIDDLE OF A LINKED-LIST: .16 III IMPLEMENT ERROR HANDLING AND REPORT TEST RESULTS: 17 TESTING PLAN: .17 EVALUATION: 20 IV: DISCUSS HOW ASYMPTOTIC ANALYSIS CAN BE USED TO ASSESS THE EFFECTIVENESS OF AN ALGORITHM: 20 Θ NOTATION: 20 BIG O NOTATION: 21 Ω NOTATION: 21 V DETERMINE TWO WAYS IN WHICH THE EFFICIENCY OF AN ALGORITHM CAN BE MEASURED, ILLUSTRATING YOUR ANSWER WITH AN EXAMPLE: 22 SPACE COMPLEXITY: .22 TIME COMPLEXITY: .23 VI CONCLUSION: .24 REFERENCES .25 LIST OF FIGURE: Figure Class Node Figure Add element at the end Figure The process of adding the element at the end Figure Add element at the beginning Figure The process of adding the element at the beginning Figure Delete the element at the beginning .9 Figure The process of deleting the element at the beginning Figure Delete the element at the end .10 Figure The process of removing the element at the end .10 Figure 10 toString() 11 Figure 11 class Node (Doubly) 11 Figure 12 Add elements to the beginning of Doubly .12 Figure 13 The process of adding elements to the beginning in Doubly 12 Figure 14 Add the element at the end of Doubly .13 Figure 15 The process of adding the element at the end in Doubly 13 Figure 16 Delete the first element in Doubly 14 Figure 17 The process of deleting the first element in Doubly 14 Figure 18 Delete the last element in Doubly 15 Figure 19 The process of deleting the last element in Doubly 15 Figure 20 toString() 16 Figure 21 Add the element to the center 16 Figure 22 The process of add the element to the center 17 Figure 23 Θ NOTATION (geeksforgeeks) 21 Figure 24 BIG O NOTATION (geeksforgeeks) 21 Figure 25 Ω NOTATION (tutorialspoint) 22 Figure 26 Space complexity example 23 Figure 27 Time complexity (tutorialspoint, 2021) 24 Figure 28 Time complexity example .24 I INTRODUCTION: This report will implement a complex ADT and algorithm in an executable programming language to solve a well-defined problem Perform error handling and report the test results Discuss how asymptotic analysis can be used to evaluate the effectiveness of an algorithm Identify two possible ways of measuring the effectiveness of an algorithm, illustrate with an example II IMPLEMENT ALGORITHMS: COMPLEX DATA STRUCTURES AND HIGHLIGHT SOME DIFFERENCES BETWEEN SINGLY AND DOUBLY LINKED-LIST: SINGLY LINKED LIST : A singly linked list is a set of nodes where each node has two fields ‘data’ and ‘link’ The ‘data’ field stores actual piece of information and ‘link’ field is used to point to next node Basically ‘link’ field is nothing but address only (geeksforgeeks, 2019) DOUBLY LINKED LIST : A Doubly Linked List (DLL) contains an extra pointer, typically called previous pointer, together with next pointer and data which are there in singly linked list (geeksforgeeks, 2019) Some differences between singly and doubly linked-list (geeksforgeeks, 2019): SINGLY LINKED LIST DOUBLY LINKED LIST Singly linked list has nodes with only a data field Doubly linked list has nodes with a data field, a and next link field In Singly linked list, the traversal can be done using the next node link only previous link field and a next link field In Doubly linked list, the traversal can be done using the previous node link or the next node link The Singly linked list occupies less memory than The Doubly linked list occupies more memory Doubly linked list as it has only fields than Singly linked list as it has fields Less efficient access to elements More efficient access to elements DESCRIBE SINGLY AND DOUBLY LINKED-LIST’S OPERATIONS: Basic Operations on Linked List (AfterAcademy, 2020):  TRAVERSAL: To traverse all the nodes one after another  INSERTION: To add a node at the given position  DELETION: To delete a node  SEARCHING: To search an element(s) by value  UPDATING: To update a node  SORTING: To arrange nodes in a linked list in a specific order  MERGING: To merge two linked lists into one Basic Operations on Linked List (AfterAcademy, 2020):  INSERTION: Adds an element at the beginning of the list  DELETION: Deletes an element at the beginning of the list  INSERT LAST: Adds an element at the end of the list  DELETE LAST: Deletes an element from the end of the list  INSERT AFTER: Adds an element after an item of the list  DELETE: Deletes an element from the list using the key  DISPLAY FORWARD: Displays the complete list in a forward manner  DISPLAY BACKWARD: Displays the complete list in a backward manner IMPLEMENT SINGLY AND DOUBLY LINKED-LIST: 3.1 Singly linked-list: Figure Class Node The data field will be stored between the value and next will be a pointer to point to its next straight Head is the first value and tail is the last value Initially, they are both null Figure Add element at the end Figure The process of adding the element at the end Before: After adding: First, allocate a new node and input the data After that, we will consider two cases If the initial value is empty (i.e the linked list is empty), assign the aNode value to head and tail At this point, the linked list has element If the linked list is not empty, we will create a variable current and start looking for the last node using the while loop as shown After the loop is finished, we have found the last node Start assigning it to the tail At this point, the tail is the newly added aNode element Figure Add element at the beginning Figure The process of adding the element at the beginning Before: After adding: First, allocate a new node and input the data After that, we will consider two cases If the initial value is empty (i.e the linked list is empty), assign the aNode value to head At this point, the linked list has element If the linked list is not empty, direct the next cursor to the head Start assigning it to the head At this point, the head is the newly added aNode element Figure Delete the element at the beginning Figure The process of deleting the element at the beginning Before: After remove: This is the function that removes the top value of the linked list If the list is not empty, create a temp variable Change the head value and use the variable temp to disconnect the first element from the linked list Figure Delete the element at the end Figure The process of removing the element at the end Before: After remove: This is the function that removes the last value of the linked list The first is to find the second element from the bottom of the linked list Create a current variable as shown and use the while loop to find the second element from the bottom of the linked list After finding that element, assign a null value to the element after it (The second element from the bottom of the linked list is the last element) Figure 10 toString() The toString () method returns the string representation of the object If any object is printed, the Java compiler calls the toString () method So override the toString () method, which returns the desired output, it can be the state of an object 3.2 Doubly linked-list: Figure 11 class Node (Doubly) The data field will be stored between the value and next will be the pointer to point to its next guy, and prev will be the pointer to point to the next one in front of it Similar to singly linked list, Head is the first value and last is the last value Initially, they are both null Figure 12 Add elements to the beginning of Doubly Figure 13 The process of adding elements to the beginning in Doubly Before: After adding: First, allocate a new node and input the data After that, we will consider two cases If head = NULL, we give both head and last = new_Node If head! = NULL, we will update the new head as new_Node We need to link the current head to new_Node before we give new_Node with the new head Figure 14 Add the element at the end of Doubly Figure 15 The process of adding the element at the end in Doubly Before: After adding: First, allocate a new node and input the data After that, we will consider two cases If head = NULL, we give both head and last = new_node If head! = NULL, we will update last to new_node We need to make a link between the current last and new_Node before we give new_node with the new last Figure 16 Delete the first element in Doubly Figure 17 The process of deleting the first element in Doubly Before: After adding: First, we will consider three cases If head = = NULL, there is nothing to delete The program will say "Can't delete" If head! = NULL, we have two cases If head = = last (ie the linked list has only element), we will give head and last at the same time as null If the linked list has more than one element, give the new head the next one and correct its prev with null These are the actions to break the link with the old head element Figure 18 Delete the last element in Doubly Figure 19 The process of deleting the last element in Doubly Before: After adding: First, we will consider three cases If head = = NULL, there is nothing to delete The program will say "Can't delete" If head! = NULL, we have two cases If head = = last (ie the linked list has only element), we will give head and last at the same time as null If the linked list has more than one element, give the new last with the previous one and correct its next with null These are the actions to break the link with the old last element Figure 20 toString() The toString () method returns the string representation of the object If any object is printed, the Java compiler calls the toString () method So override the toString () method, which returns the desired output, it can be the state of an object INSERT AN ELEMENT IN THE MIDDLE OF A LINKED-LIST: Code for class addMiddle (Insert an element in the middle): Figure 21 Add the element to the center Figure 22 The process of add the element to the center First, we will check if the linked list is empty If it's empty, we proceed to add the element as usual If the sequence is not empty, we create two buttons: slow = head and fast = head.next Use while loop to find the middle position of linked list As shown in Figure 23, slow and fast are at positions and After checking the condition of the loop, the linked list is satisfied Proceed to assign new values to slow and fast At this point, the new slow and the fast are at positions and Continuing to test the condition, slow and fast are no longer satisfied The loop stops Finally, proceed to connect the new node to the linked list III IMPLEMENT RESULTS: ERROR HANDLING AND REPORT TEST TESTING PLAN: No Scope Operation Singly add(item i) LinkedList ADT: MySingly Testing type Normal Input S1: [ ]; add(10) Expected Actual Output Output S1: [10] Status The same Passed as expected output The same add(item i) Normal S1: [9, 5]; add(10) S1: [9, 5, 10] as expected Passed output The same addFirst(item i) Normal S1: [ ]; addFirst(50) S1: [50] as expected Passed output The same addFirst(item i) Normal S1: [9, 5]; addFirst(10) S1: [10, 9, 5] as expected Passed output LinkedList S1 S1: []; remove() Normal S1: [] remove() Print an error S1: []; Failed message The same S1: [3, 2] remove() remove() S1:[2] as expected Passed output S1: [] removeLast() removeLast() S1: [2, 3, 6] removeLast() removeLast() S1: []; Print an error message S1: [2, 3]; Print an error message LinkedList ADT: addFirst(item i) Normal S2: [ ]; addFirst (10) S2: [10] LinkedList S2 Failed The same as expected Passed output as expected Passed output MyDoubly 10 error The same Doubly Program addFirst(item i) Normal S2: [2, 5]; addFirst (10) S2: [10, 2, 5] The same Passed as expected output The same 11 add(item i) Normal S2: [ ]; add(2) as S2: [2] expected Passed output The same 12 add(item i) Normal S2: [99, 4]; add(10) S2: [99, 4, 10] as expected Passed output 13 removeFirst() Normal S2: [] removeFirst() S1: []; S1: []; Print an error message Print: “Can’t Passed delete” The same 14 removeFirst() S2: [3, 6, 2] removeFirst() S1:[6, 2] as expected Passed output 15 removeLast() 16 removeLast() S2: [] removeLast() S2: [2, 3, 6] removeLast() S1: []; S2: []; Print an error message S2: [2, 3]; Print an error message Print: “Can’t Passed delete” The same as expected Passed output The same 17 insertMiddle addMiddle S3: [ ]; (item i) addMiddle(99) S3: [99] (Item i, S3) expected Passed output The same MyLinkedList 18 as addMiddle S3: [1, 3, 5, 4] S3: [1, 3, 10, 5, (item i) add(10) 4] as expected output Passed EVALUATION: In all, 18 test cases were performed There are cases for Singly, cases for Doubly and cases for adding elements to the middle of the linked list There are two cases of deletion in Singly that fails When the linked list is empty, the two delete functions can only produce an empty linked list According to the expected output, the program must send one more message to indicate that this linked list is empty The program will soon be revised and added conditions for the functions "remove ()" and "removeLast ()" in Singly IV: DISCUSS HOW ASYMPTOTIC ANALYSIS CAN BE USED TO ASSESS THE EFFECTIVENESS OF AN ALGORITHM: Asymptotic Analysis is the big idea that handles above issues in analyzing algorithms In Asymptotic Analysis, we evaluate the performance of an algorithm in terms of input size (we don’t measure the actual running time) We calculate, how the time (or space) taken by an algorithm increases with the input size Θ NOTATION: The theta notation bounds a function from above and below, so it defines exact asymptotic behavior A simple way to get Theta notation of an expression is to drop low order terms and ignore leading constants (geeksforgeeks, 2021) Θ(g(n)) = {f(n): there exist positive constants c1, c2 and n0 such that

Ngày đăng: 01/04/2023, 18:41

Tài liệu cùng người dùng

Tài liệu liên quan