Professional Documents
Culture Documents
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.
Grading grid
P4 P5 P6 P7 M4 M5 D3 D4
Summative Feedback: Resubmission Feedback:
2.1
IV Signature:
Table of Contents
1. Introduction .......................................................................................................................................................... 4
2. Design and implementation of Stack ADT and Queue ADT (P4) .......................................................................... 4
2.1 Stack .............................................................................................................................................................. 4
2.2 queue .................................................................................................................................................................. 7
3. Application .............................................................................................................................................................. 10
4. Implement error handling and report test results (P5) ...................................................................................... 10
4.1 testing ............................................................................................................................................................... 10
4.2 Evaluation ......................................................................................................................................................... 12
5. Discuss how asymptotic analysis can be used to assess the effectiveness of an algorithm (P6) ....................... 12
6.1 Time complexity ................................................................................................................................................ 16
6.2 Space complexity ............................................................................................................................................. 18
7. References .......................................................................................................................................................... 20
Table of figure
Figure 1 LIFO ................................................................................................................................................................. 4
Figure 2 FIFO ................................................................................................................................................................. 7
Figure 3 test table ....................................................................................................................................................... 12
Figure 4 Big Oh Notation, Ο ........................................................................................................................................ 13
Figure 5 function f(n) 1 ............................................................................................................................................... 13
Figure 6 Omega Notation, Ω ....................................................................................................................................... 14
Figure 7 function f(n) 2 ............................................................................................................................................... 14
Figure 8 Theta Notation, θ .......................................................................................................................................... 14
Figure 9 function f(n) 3 ............................................................................................................................................... 14
Figure 10 number of items in the collection............................................................................................................... 15
Figure 11 example O(n) – Linear time complexity ...................................................................................................... 16
Figure 12 example O(n2) – Quadratic time complexity .............................................................................................. 16
Figure 13 exmaple time complexity ............................................................................................................................ 17
Figure 14 example Space complexity.......................................................................................................................... 19
Figure 15 example Space complexity 2....................................................................................................................... 19
List of table
Table 1 Common Asymptotic Notations ..................................................................................................................... 15
1. Introduction
A data structure is a programmatic way of storing data so that it can be used efficiently. Almost every
enterprise application uses various types of data structures in one way or another. This tutorial will give
you a great understanding of Data Structures needed to understand complexity of enterprise-grade
applications and the needs of algorithms and data structures. In this report, I have reused ADT such as
Queues and Stacks.
Figure 1 LIFO
package demo;
succ = current.nextNode;
current.nextNode = pre;
pre = current;
current = succ;
}
head = pre;
}
current is not null, then succ is equal to current and current is connected to pre instead of current
because pre is the beginning of head, so it will run backwards.
s.print();
System.out.println(s.size());
s.push(2,99);
s.print();
System.out.println(s.size());
s.push(8);
s.print();
System.out.println(s.size());
s.removeLast();
s.print();
System.out.println(s.size());
s.reverse();
s.print();
System.out.println(s.size());
s.pop();
s.print();
}
}
2.2 queue
FIFO(First in First out) With this method, the first number of the input row will be the first number
output.
Figure 2 FIFO
package demo;
public void enqueue(T data) {// add a new node into the end of the list
Node aNode = new Node(data);
if(head == null) {
head = aNode;
tail = aNode;
}
else {
//connect the new node to tail
tail.nextNode = aNode;
aNode.prev = tail;
tail = aNode;
}
}
tail connects to aNode aNode connects prev and tail with anode so it will be printed at the end
public void enqueue (int index, T data ) {
Node aNode = new Node(data);
int count = 0;
if(head != null) {
Node current = head;
current = tail;
while(current.prev != null && count < index - 1) {
current = current.prev;
count++;
}
aNode.prev = current.prev;
current.prev = aNode;
}
}
we take p as tail if p is not null then it will print until null then stop
public static void main(String[] args) {
Myqueue<Integer> s = new Myqueue<>();
s.enqueue(2);
s.enqueue(7);
s.enqueue(4);
s.enqueue(5);
s.enqueue(8);
s.enqueue(9);
s.enqueue(1);
s.print();
System.out.println(s.size());
s.enqueue(4,55);
s.print();
System.out.println(s.size());
s.dequeue();
s.print();
System.out.println(s.size());
s.dequeue(2);
s.print();
System.out.println(s.size());
s.queuelast();
s.print();
System.out.println(s.size());
}
}
3. Application
reverse a Stack: we let current ,pre and head, current connects to the next current, pre connects to null.
Current is not null, then succ connects to current, current with pre finally replaces pre with head.
succ = current.nextNode;
current.nextNode = pre;
pre = current;
current = succ;
}
head = pre;
}
using recursion will help the computer to calculate faster, but the algorithm will be more complicated,
reverse the unused stack so that it is easier to understand for coders, Although this is not as optimal as
recursion, but I still prefer to use this method over not using it to reverse stack.
4.2 Evaluation
I have tested 14 cases all of the above gives the expected pop() result and Stack ADT push is also
properly executed queue ADT. My code is basically completed but there are still a few flaws such as the
interface is still hard to see, the code is not fully optimized, and there are a few cases like changing the
number in the stack. I will try to fix it in the future.
Asymptotic analysis is input bound i.e., if there's no input to the algorithm, it is concluded to work in a
constant time. Other than the "input" all other factors are considered constant.
Asymptotic analysis refers to computing the running time of any operation in mathematical units of
computation. For example, the running time of one operation is computed as f(n) and may be for
another operation it is computed as g(n2). This means the first operation running time will increase
linearly with the increase in n and the running time of the second operation will increase exponentially
when n increases. Similarly, the running time of both operations will be nearly the same if n is
significantly small (Anon., 2021).
Following are the commonly used asymptotic notations to calculate the running time complexity of
an algorithm.
Big-O Notation (O-notation): The notation Ο(n) is the formal way to express the upper bound of an
algorithm's running time. It measures the worst case time complexity or the longest amount of time
an algorithm can possibly take to complete.
Omega Notation, (Ω-notation): The notation Ω(n) is the formal way to express the lower bound of an
algorithm's running time. It measures the best case time complexity or the best amount of time an
algorithm can possibly take to complete.
Figure 6 Omega Notation, Ω
Theta Notation, (θ-notation): The notation θ(n) is the formal way to express both the lower bound and
the upper bound of an algorithm's running time. It is represented as follows
constant − Ο(1)
logarithmic − Ο(log n)
linear − Ο(n)
n log n − Ο(n log n)
quadratic − Ο(n2)
cubic − Ο(n3)
polynomial − nΟ(1)
exponential − 2Ο(n)
this function prints all the numbers from 0 to n-1. As n grows, execution of print statement increases as
well. For n=0, it will not execute at all, for n=1, it will execute once, for n=2, it executes two times and for
n=n, it executes n times. So, the loop is directly proportional to the value of n, hence complexity of loop
is O(n).
Typical quadratic complexity function has the format, a loop with an in the loop and both loops depends
on the size of the input
Time complexity of an algorithm signifies the total time required by the program to run till its
completion.
The time complexity of algorithms is most commonly expressed using the big O notation. It's an
asymptotic notation to represent the time complexity. We will study about it in detail in the next
tutorial.
Time Complexity is most commonly estimated by counting the number of elementary steps performed
by any algorithm to finish execution. Like in the example above, for the first code the loop will run n
number of times, so the time complexity will be n atleast and as the value of n will increase the time
taken will also increase. While for the second code, time complexity is constant, because it will never be
dependent on the value of n, it will always give the result in 1 step.
And since the algorithm's performance may vary with different types of input data, hence for an
algorithm we usually use the worst-case Time complexity of an algorithm because that is the maximum
time taken for any input size (Anon., 2021).
In above code “Hello World!!!” print only once on a screen. So, time complexity is constant: O(1) i.e.
every time constant amount of time require to execute code, no matter which operating system or
which machine configurations you are using.
𝑁∗(𝑁−1)
Total number of times count++ will run is 0+1+2+…+(N-1)= . So the time complexity will be
2
O(N2).
The term Space Complexity is misused for Auxiliary Space at many places. Following are the correct
definitions of Auxiliary Space and Space Complexity. Auxiliary Space is the extra space or temporary
space used by an algorithm. Space Complexity of an algorithm is total space taken by the algorithm with
respect to the input size. Space complexity includes both Auxiliary space and space used by input. For
example, if we want to compare standard sorting algorithms on the basis of space, then Auxiliary Space
would be a better criteria than Space Complexity. Merge Sort uses O(n) auxiliary space, Insertion sort
and Heap Sort use O(1) auxiliary space. Space complexity of all these sorting algorithms is O(n) though.
Space complexity is a parallel concept to time complexity. If we need to create an array of size n, this will
require O(n) space. If we create a two dimensional array of size n*n, this will require O(n2) space
(hritikrommie, 2021).
Each of these calls is added to call stack and takes up actual memory. So it take O(n) space. However just
because you have n calls total doesn’t mean it takes O(n) space.
It’s necessary to mention that space complexity depends on a variety of things such as the programming
language, the compiler, or even the machine running the algorithm.
7. References
Anon., 2021. studytonight. [Online]
Available at: https://www.studytonight.com/data-structures/time-complexity-of-algorithms
[Accessed 4 7 2021].
2.1 - P4: Most useful operations of Stack and Queue ADT have been implemented. The solution for size() in both Stack
and Queue can be better (i.e. O(1)).
One solution is provided for task (c), however this is not a good one since by doing that way, the concept
"Encapsulation" won't be met.
- P5: Most operations of Stack and Queue have been tested.
- P6: Some places in the discussion should be rewritten in your own words. Fortunately, the work has provided
examples to illustrate two common complexities.
- P7: When discussing two ways analyzing an algorithms, a number of examples are shown. Although they should
be rewritten in Java, fortunately their time and space complexity analyses are proper.