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.
Trang 1
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 DOUBLY LINKED-LIST’S OPERATIONS: 5
3 IMPLEMENT SINGLY AND DOUBLY LINKED-LIST: 6
3.1 Singly linked-list: 6
3.2 Doubly linked-list: 11
4 INSERT AN ELEMENT IN THE MIDDLE OF A LINKED-LIST: 16
III IMPLEMENT ERROR HANDLING AND REPORT TEST RESULTS: 17
1 TESTING PLAN: 17
2 EVALUATION: 20
IV: DISCUSS HOW ASYMPTOTIC ANALYSIS CAN BE USED TO ASSESS THE EFFECTIVENESS OF AN ALGORITHM: 20
1 Θ NOTATION: 20
2 BIG O NOTATION: 21
3 Ω NOTATION: 21
V DETERMINE TWO WAYS IN WHICH THE EFFICIENCY OF AN ALGORITHM CAN BE MEASURED, ILLUSTRATING YOUR ANSWER WITH AN EXAMPLE: 22
1 SPACE COMPLEXITY: 22
2 TIME COMPLEXITY: 23
VI CONCLUSION: 24
REFERENCES 25
Trang 2LIST OF FIGURE:
Figure 1 Class Node 6
Figure 2 Add element at the end 7
Figure 3 The process of adding the element at the end 7
Figure 4 Add element at the beginning 8
Figure 5 The process of adding the element at the beginning 8
Figure 6 Delete the element at the beginning 9
Figure 7 The process of deleting the element at the beginning 9
Figure 8 Delete the element at the end 10
Figure 9 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
Trang 3I INTRODUCTION:
This report will implement a complex ADT and algorithm in an executable programminglanguage to solve a well-defined problem Perform error handling and report the test results Discuss howasymptotic 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 COMPLEX DATA STRUCTURES AND ALGORITHMS:
1 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 has nodes with only a data field
and next link field
Doubly linked list has nodes with a data field, aprevious link field and a next link field
In Singly linked list, the traversal can be done
using the next node link only
In Doubly linked list, the traversal can be doneusing the previous node link or the next nodelink
The Singly linked list occupies less memory than
Doubly linked list as it has only 2 fields
The Doubly linked list occupies more memorythan Singly linked list as it has 3 fields
Less efficient access to elements More efficient access to elements
2 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.
Trang 4 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.
3 IMPLEMENT SINGLY AND DOUBLY LINKED-LIST:
3.1 Singly linked-list:
Figure 1 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.
Trang 5Figure 2 Add element at the end.
Figure 3 The process of adding the element at the end.
Before:
After adding:
Trang 6First, 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 1 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 4 Add element at the beginning.
Figure 5 The process of adding the element at the beginning.
Before:
After adding:
Trang 7First, 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 1 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 6 Delete the element at the beginning.
Figure 7 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
Trang 8Figure 8 Delete the element at the end.
Figure 9 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)
Trang 9Figure 10 toString()
The toString () method returns the string representation of the object If any object is printed, theJava compiler calls the toString () method So override the toString () method, which returns the desiredoutput, 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.
Trang 10Figure 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.
Trang 11Figure 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.
Trang 12Figure 16 Delete the first element in Doubly.
Figure 17 The process of deleting the first element in Doubly.
Trang 13Figure 18 Delete the last element in Doubly.
Figure 19 The process of deleting the last element in Doubly.
Trang 14Figure 20 toString()
The toString () method returns the string representation of the object If any object is printed, theJava compiler calls the toString () method So override the toString () method, which returns the desiredoutput, it can be the state of an object
4 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.
Trang 15Figure 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 0 and
1 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 1 and 3 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 ERROR HANDLING AND REPORT TEST RESULTS:
1 TESTING PLAN:
No Scope Operation Testing
type Input
Expected Output
Actual Output Status
Passed
Trang 16LinkedList S1
add(10) S1: [9, 5, 10]
The sameas
expectedoutput
Passed
addFirst(50) S1: [50]
The sameas
expectedoutput
Passed
addFirst(10) S1: [10, 9, 5]
The sameas
expectedoutput
S1: []; Failed
The sameas
expectedoutput
Passed
S1: [];
Print an error message
The sameas
expectedoutput
expectedoutput
Passed
Trang 17The sameas
expectedoutput
Passed
add(10) S2: [99, 4, 10]
The sameas
expectedoutput
Passed
S1: [];
Print an error message
S1: [];
Print:
“Can’tdelete”
Passed
The sameas
expectedoutput
Passed
S2: [];
Print an error message
S1: [];
Print:
“Can’tdelete”
Passed
S2: [2, 3];
Print an error message
The sameas
expectedoutput
S3: [ ];
addMiddle(99) S3: [99]
The sameas
expectedoutput
The sameas
expectedoutput
Passed
Trang 18IV: 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 InAsymptotic Analysis, we evaluate the performance of an algorithm in terms of input size (we don’tmeasure the actual running time) We calculate, how the time (or space) taken by an algorithm increaseswith the input size
1 Θ NOTATION:
The theta notation bounds a function from above and below, so it defines exact asymptoticbehavior 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 0 <= c1*g(n) <= f(n) <= c2*g(n) for all n >= n0}
The above definition means, 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, 2021).
Trang 19Figure 23 Θ NOTATION (geeksforgeeks)
2 BIG O NOTATION:
The Big O notation defines an upper bound of an algorithm, it bounds a function only from above.The Big O notation is useful when we only have upper bound on time complexity of an algorithm Many
times we easily find an upper bound by simply looking at the algorithm (geeksforgeeks, 2021)
O(g(n)) = { f(n): there exist positive constants c and n0 such that 0 <= f(n) <= c*g(n) for all n >= n0}
Figure 24 BIG O NOTATION (geeksforgeeks)
3 Ω NOTATION:
Just as Big O notation provides an asymptotic upper bound on a function, Ω notation provides an
asymptotic lower bound Ω Notation can be useful when we have lower bound on time complexity of an
algorithm For a given function g(n), we denote by Ω(g(n)) the set of functions (geeksforgeeks, 2021)
Ω (g(n)) = {f(n): there exist positive constants c and n0 such that 0 <= c*g(n) <= f(n) for all n >= n0}.
Figure 25 Ω NOTATION (tutorialspoint)
4 Notations relate to the ideas of best, average and worst-case performance
Worst Case Analysis (Usually Done):
Trang 20For Linear Search, the worst case occurs when the lookup element (x in the code above) is not inthe array In the absence of x, the search () function compares it with all the elements of arr [] one by one.
Therefore, the worst case time complexity of linear search will be Θ (n) (geeksforgeeks, 2020).
Average Case Analysis (Sometimes done):
For the linear search problem, let's assume that all cases are equally distributed (even if x is not
present in the array) So we sum all the cases and divide the sum by (n + 1) (geeksforgeeks, 2020).
Best Case Analysis (Bogus):
In the linear search problem, the best case occurs when x is present at the first location Thenumber of operations in the best case is constant (not dependent on n) So time complexity in the best
case would be Θ(1) (geeksforgeeks, 2020).
V DETERMINE TWO WAYS IN WHICH THE EFFICIENCY OF AN ALGORITHM CAN BE MEASURED, ILLUSTRATING YOUR ANSWER WITH AN EXAMPLE:
1 SPACE COMPLEXITY:
Space complexity of an algorithm represents the amount of memory space needed the algorithm
in its life cycle Space needed by an algorithm is equal to the sum of the following two components Afixed part that is a space required to store certain data and variables (i.e simple variables and constants,program size etc.), that are not dependent of the size of the problem A variable part is a space required
by variables, whose size is totally dependent on the size of the problem For example, recursion stack
space, dynamic memory allocation etc (tutorialspoint, 2021).
Space complexity S(p) of any algorithm p is S(p) = A + Sp(I) Where A is treated as the fixed part and S(I) is treated as the variable part of the algorithm which depends on instance characteristic I
(tutorialspoint, 2021).
Figure 26 Space complexity example