ASM2 - 1649 - TRẦN HỮU VƯƠNG
ASM2 - 1649 - TRẦN HỮU VƯƠNG
Unit number and title Unit 19: Data Structures and Algorithms
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:
IV Signature:
Contents
P4 .................................................................................................................................................................................. 3
Linked list ................................................................................................................................................................ 3
how it works: ........................................................................................................................................................... 4
Explain: .................................................................................................................................................................... 5
ArrayList ADT ............................................................................................................................................................ 8
How it work? ........................................................................................................................................................... 9
Explain ................................................................................................................................................................... 10
3. Implement error handling and report test results (P5) ..................................................................................... 12
3.1. Testing plan .................................................................................................................................................... 12
3.2 Evaluation ........................................................................................................................................................ 13
P6 ................................................................................................................................................................................ 13
Asymptotic Notations: ......................................................................................................................................... 13
P7 ................................................................................................................................................................................ 16
Time Complexity ................................................................................................................................................... 16
Space Complexity.................................................................................................................................................. 17
+ Input space ......................................................................................................................................................... 17
+ Auxiliary space................................................................................................................................................... 17
Ex01 ............................................................................................................................................................................ 18
EX02 ........................................................................................................................................................................... 19
REFERENCES.......................................................................................................................................................... 20
P4
Linked list
A singly linked list is a data structure consisting of a sequence of elements where each
element points to the next one in the sequence. Unlike arrays, linked lists do not have a
fixed size in memory and elements can be efficiently inserted or removed from any
position in the list. However, accessing elements in a linked list can be less efficient
compared to arrays because you have to traverse the list from the beginning to reach a
specific element.
In a singly linked list, each element is called a "node." Each node contains two parts: the
data and a reference (or pointer) to the next node in the sequence. The first node in the list
is called the "head," and the last node typically has a reference to null to indicate the end of
the list.
how it works:
Initialization: To create a singly linked list, you typically start by creating a "head" node.
This node serves as the starting point of the list.
Adding Nodes: To add new elements to the list, you create a new node with the desired
data and adjust the pointers to incorporate the new node into the existing list. Typically,
you add nodes to the end of the list, but you can also insert them at the beginning or in the
middle.
Traversal: To access or manipulate elements in the list, you traverse the list by following
the pointers from one node to the next until you reach the desired node. This traversal
process allows you to search for specific elements, retrieve data, delete nodes, or perform
other operations.
Accessing Elements: Unlike arrays, where elements can be accessed directly using an
index, accessing elements in a linked list usually requires traversing the list from the head
node to the desired node. This traversal process can be less efficient than array indexing,
especially for large lists.
Removing Nodes: To remove nodes from the list, you adjust the pointers of the
neighboring nodes to bypass the node you want to delete, effectively removing it from the
sequence. Once the node is no longer referenced by any other node, it can be garbage-
collected to free up memory.
Termination: Singly linked lists typically terminate with a node whose next pointer is set to
null or None, indicating the end of the list.
Overall, singly linked lists provide dynamic memory allocation, efficient insertion and
deletion operations (especially at the beginning or end of the list), and flexibility in
managing sequential data. However, they may not be as efficient for random access or
searching as arrays due to the need for sequential traversal.
Explain:
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
Befor
After
Befor
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 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
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.
Befor
After
Befor
After adding:
First, allocate a new node and input the data. After that , we will consider two cases. If the
initial value 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
Befor
After
Befor
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
Befor
After
Befor
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 toString() method return 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.
ArrayList ADT
An ArrayList is a dynamic array implementation of the List interface in Java. It's part of
the Java Collections Framework and provides resizable arrays. ArrayLists automatically
resize themselves as more elements are added to them. They are similar to arrays but offer
more functionality and flexibility. ArrayLists also allow you to add, remove, access, and
manipulate elements easily.
Random Access: Elements within an ArrayList can be accessed directly using their index,
much like arrays. This allows for fast access to elements at any position.
Insertion and Deletion: Adding and removing elements from ArrayLists is relatively
efficient. When an element is inserted or removed, the other elements are shifted
accordingly to accommodate the change.
Implementation of List Interface: ArrayLists implement the List interface, which means
they support all the operations defined by List, such as adding elements, removing
elements, checking if an element exists, getting the size of the list, etc.
Ordered Collection: Elements in an ArrayList are ordered and maintain their insertion
order. This means that elements are stored in the order they were added and can be
accessed in that same order.
Resizable: Unlike arrays, ArrayLists are resizable. They can grow or shrink in size as
needed, making them convenient for situations where the number of elements is not known
in advance.
The underlying data structure of an ArrayList is an array of objects. When you create an
ArrayList, it internally creates an array to store the elements. As elements are added to the
ArrayList and its capacity is reached, it creates a new array with a larger capacity, typically
doubling the size of the previous array, and copies all elements from the old array to the
new one. This process is known as "resizing" and happens transparently to the user.
How it work?
Initialization: When you create an ArrayList, it starts with an initial capacity (typically 10).
An underlying array of this initial capacity is created to hold the elements.
Adding Elements: When you add elements to the ArrayList using the add() method, the
ArrayList checks if there is enough space in its internal array to accommodate the new
element. If there is sufficient space, the element is simply added to the end of the array.
Resizing: If the internal array is not large enough to hold the new element, the ArrayList
creates a new array with a larger capacity. The usual practice is to double the size of the
current array. Then, it copies all the elements from the old array to the new one.
Adding Element after Resizing: After resizing and creating a new array, the new element is
added to the end of the resized array.
Accessing Elements: You can access elements in an ArrayList using their index. Since
ArrayLists use an array internally, accessing elements by index is fast and efficient.
Removing Elements: When you remove an element from an ArrayList, it shifts all
subsequent elements to the left by one position to fill the gap left by the removed element.
This operation can be relatively costly for large ArrayLists, especially if the element being
removed is near the beginning of the list, as it may require shifting a large number of
elements.
Memory Management: ArrayLists consume more memory than arrays because they
maintain extra space to accommodate potential growth. However, they offer flexibility and
convenience in exchange for this extra memory overhead.
Explain
Befor
First I have a list that looks like 5 positions, and I'll add the color red to the first position
After add
After adding red color in the first position. Next I will add blue in the 2nd position
Next I'll add orange in the 3rd position
After the first 3 positions have been added, for the 4th position I will add pink
3.2 Evaluation
There are two test cases and each test case has a failure
+ Arrays list has a test case that fails because the element you want to delete exceeds the
number of elements in the array. Specifically, you want to delete the 10th element but there
are only 4 elements in the array.
The solution is to request deletion of elements that do not exceed the number of elements
in the array
+ Linked list has a test case that fails because the element you want to delete exceeds the
number of elements in the array. Specifically, you want to delete the 10th element but there
are only 5 elements in the array.
The solution is to request deletion of elements that do not exceed the number of elements
in the array
P6
It's a method used in computer science and mathematics to describe the behavior of
functions as their input values become very large. It's particularly useful in analyzing
algorithms, as it helps us understand how their performance scales with the size of the
input. Typically, we express the complexity of an algorithm using big O notation, which
gives us an upper bound on the growth rate of the algorithm's runtime or space usage.
Asymptotic Notations:
Falling leaf notation serves as a mathematical tool for evaluating algorithm performance by
examining how their efficiency scales with increasing input size. It offers a concise method
to assess time or space complexity as input size approaches infinity. Instead of directly
comparing algorithms, the focus is on understanding the relative growth rates of
complexity. This approach abstracts away constant factors and machine-specific details,
emphasizing trends. By utilizing symbols like Big O, Big Omega, and Big Theta,
algorithms can be classified based on their worst-case, best-case, or average-case
complexity, offering valuable insights into their effectiveness.
Theta notation encompasses both the upper and lower limits of a function. It's employed to
evaluate the average-case complexity of an algorithm by summing the running times for all
potential input combinations and calculating their average.
Example
Big-O notation is utilized to describe the upper limit of an algorithm's runtime, serving as
an indicator of its worst-case complexity. This notation is extensively employed in
asymptotic analysis and signifies the upper boundary of a function. It denotes the
maximum time an algorithm may require, encapsulating its worst-case time complexity. In
essence, Big-O represents the highest potential output value for a given input, particularly
emphasizing the scenario where an algorithm takes the longest time to execute its
statements.
Figure 2Big-O Notation (O-notation):
Example
P7
Time Complexity
Time complexity, in computer science, refers to the amount of time an algorithm takes to
run based on the size of its input. It describes how the runtime of an algorithm changes as
the input data grows. Time complexity is commonly expressed using Big O notation,
which gives an upper limit on how the algorithm's runtime grows with input size.
Essentially, it helps us understand how much computational time an algorithm needs as the
input size increases, aiding in analyzing and comparing algorithm efficiency.
Space Complexity
Space complexity refers to the amount of memory an algorithm requires to execute and
produce a result. While it was once a critical consideration due to limited memory
resources, modern machines typically have ample memory, reducing the immediate
concern for most users. However, in scenarios where memory is constrained or monitored
closely, estimating space complexity remains essential. It's important to note that space
complexity can vary based on factors like the programming language, compiler, and the
specific hardware running the algorithm.
+ Input space
Input space, within the realm of machine learning and data science, denotes the entirety of
potential inputs or features that a model can receive. It encompasses the wide range of
values or configurations that the model may encounter during its training or when
processing data. Simply put, it includes all the various types of information that describe
each instance in a dataset. Recognizing the input space is vital for developing and training
machine learning models, as it guides the selection of relevant features and influences the
model's complexity and ability to generalize to new data.
+ Auxiliary space
The auxiliary space in machine learning pertains to extra or supporting information utilized
to assist in learning. This supplementary data, while not directly linked to the main task,
can offer valuable context or aid in enhancing the model's performance. It encompasses
various kinds of data, like side information or additional features, which help the model
better grasp the primary data or improve its predictive abilities. Effectively incorporating
auxiliary space can bolster the resilience and precision of machine learning models,
particularly in situations where the primary data might be restricted or nois
Ex01
Time Complexity:
The time complexity of this function also depends on the size of the input array arr. Let's
denote the size of the array as n.
The for loop iterates through each element of the array once. So, the loop has a time
complexity of O(n).
Inside the loop, the operations are constant time, such as comparisons and assignments.
Space Complexity:
The space complexity here is also O(1) because we're only using a constant amount of
extra space (maxElement), regardless of the size of the input array.
To summarize:
Time Complexity:
The time complexity of this recursive factorial function depends on the value of n.
In each recursive call, the function decrements n by 1 until n becomes 0 or 1. So, the
number of recursive calls is proportional to n.
Space Complexity:
The space complexity here is O(n) because, for each recursive call, a new stack frame is
created to store the local variables and function call information.
To summarize:
Mitchell, J.A. and Thomson, M. (2017) A guide to citation.3rd edn. London: London
Publishings
Mitchell, J.A. ‘How citation changed the research world’, The Mendeley, 62(9), p70-81.
Mitchell, J.A. ‘How citation changed the research world’, The Mendeley, 62(9)
[online]. Available at: https://www.mendeley.com/reference-management/reference-
manager (Accessed: 05 May 2024).