Analysis of Algorithm: Space Complexity
Analysis of Algorithm: Space Complexity
SPACE COMPLEXITY
Definition
This is essentially the number of memory cells which an algorithm needs. A good algorithm keeps
this number as small as possible.
What is Space complexity?
If any algorithm requires a fixed amount of space for all input values
then that space complexity is said to be Constant Space Complexity
Constant Space Complexity
Int square(int a)
{
return a*a;
}
In above piece of code, it requires 2 bytes of memory to store variable 'a' and
another 2 bytes of memory is used for return value.
That means, totally it requires 4 bytes of memory to complete its execution. And
this 4 bytes of memory is fixed for any input value of 'a'. This space complexity is
said to be Constant Space Complexity.
Linear Space Complexity
That means, totally it requires '2n+8' bytes of memory to complete its execution.
Here, the amount of memory depends on the input value of 'n'. This space
complexity is said to be Linear Space Complexity.
Time complexity
REG NO .2143533
What is the complexity of the algorithm?
Legend has it that there were three diamond needles set into the floor of
the temple of Brahma in Hanoi.
Stacked upon the leftmost needle were 64 golden disks, each a different
size, stacked in concentric order:
A Legend (Ct’d)
The priests were to transfer the disks from the first needle to the second
needle, using the third as necessary.
But they could only move one disk at a time, and could never put a larger
disk on top of a smaller one.
When they completed this task, the world would end!
To Illustrate
For simplicity, suppose there were just 3 disks, and we’ll refer to the three
needles as A, B, and C...
Since we can only move one disk at a time, we move the top disk from A
to B.
Example
For simplicity, suppose there were just 3 disks, and we’ll refer to the three
needles as A, B, and C...
For simplicity, suppose there were just 3 disks, and we’ll refer to the three
needles as A, B, and C...
For simplicity, suppose there were just 3 disks, and we’ll refer to the three
needles as A, B, and C...
For simplicity, suppose there were just 3 disks, and we’ll refer to the three
needles as A, B, and C...
For simplicity, suppose there were just 3 disks, and we’ll refer to the three
needles as A, B, and C...
For simplicity, suppose there were just 3 disks, and we’ll refer to the three
needles as A, B, and C...
For simplicity, suppose there were just 3 disks, and we’ll refer to the three
needles as A, B, and C...
Introduct
The Travelling Salesman Problem (often called TSP) is a classic
algorithmic problem in the field of computer science.
In this context better solution often means a solution that is cheaper.
TSP is a mathematical problem. ion:-
Problem
You have to travel from one location to the other from the shortest path in less
time.
Finding solution:
Select a current city/location.
Select desired city/location.
Measure the distance in between each city.
Add distance of each route.
Select the route whose sum of distance is the shortest and less time consuming.
DIAGRAM
Restriction & Advantages:
You only visit each city once and you can't pass through any traversed
path.
Introduction.
Problem.
Finding Solution.
Diagram.
Restrictions.
Introduction:-
3
Simulation
173 256 548 326 753 478 222 144 721 875
321
0 1 2 3 4 5 6 7 8 59
Simulation
0 1 2 3 4 5 6 7 8 9
321
326 478
0 1 2 3 4 5 6 7 8 69
Simulation
326 478
0 1 2 3 4 5 6 7 8 9
321
144 173 222 256 326 478 548 721 753 875
Bubble Sort Definition
The pass through the list is repeated until no swaps are needed,
which indicates that the list is sorted.
Example: 5, 12, 3, 9, 16
● 5, 12, 3, 9, 16
○ The list stays the same because 5 is less than 12.
● 5, 3, 12, 9, 16
○ 3 and 12 are switched because 3 is less than 12
● 5, 3, 9, 12, 16
○ 9 and 12 are switched since 9 is less than 12
● 5, 3, 9, 12, 16
○ 12 and 16 do not switch because 12 is less than 16
Example:
3, 5, 9, 12, 16
3 is less than 5, so they do not switch
3, 5, 9, 12, 16
5 is less than 9 so they remain in the same places
3, 5, 9, 12, 16
12 is greater than 9 so they do not switch places
3, 5, 9, 12, 16
12 and 16 are in numerical order so they don't switch
Running Time :
● The bubble sort requires very little memory other than that
which the array or list itself occupies.
int main ()
{
int n = 9;
printf("%d", fib(n));
getchar();
return 0;
}
We can observe that this implementation does a lot of
repeated work (see the following recursion tree). So this
is a bad implementation for nth Fibonacci number.
fib(5)
/
fib(4) fib(3)
/ /
fib(3) fib(2) fib(2) fib(1)
/ / /
fib(2) fib(1) fib(1) fib(0) fib(1) fib(0)
/
fib(1) fib(0)
How to solve Dynamic Programming
1) Identify if it is a DP problem
2) Decide a state expression with least parameters
3) Formulate state relationship
4) Do tabulation (or add memoization)
Identify if it is a DP problem
DP problems are all about state and their transition. This is the most basic
step which must be done very carefully because the state transition
depends on the choice of state definition you make. So, let’s see what do
we mean by the term “state”.
State:
A state can be defined as the set of parameters that can uniquely identify a
certain position or standing in the given problem. This set of parameters
should be as small as possible to reduce state space
Formulate state relationship
This part is the hardest part of for solving a DP problem and requires a lots
of intuition, observation and practice.
Let’s understand it by considering a sample problem
Example:
1+1+1+1+1+1
1+1+1+3
1+1+3+1
1+3+1+1
3+1+1+1
3+3
1+5
5+1
Do tabulation (or add memoization)
1. Max Heap
2. Min Heap
Max Heap
Min Heap
Sheraz sher
2143174
What is graph ?
Types of graph
Directed / undirected
Weighted / unweighted
Cyclic / acyclic
Breadth-first search (BFS)
It is an algorithm for traversing or
searching tree or graph data structures. It starts at the tree
root sometimes referred to as a 'search key') and explores
the neighbor nodes first, before moving to the next level
neighbors.
Bfs use a FIFO queue.
Example
Complexity
Breadth first search will never get trapped exploring the useless path
forever.
If there is more than one solution then BFS can find the minimal one
that requires less number of steps.
DISADVANTAGES OF BREADTH-FIRST SEARCH
If the solution is farther away from the root, breath first search
will consume lot of time.
DEPTH FOR SEARCH
2.When all of vertices edges have been explored, the search “back-tracks” to
explore edges leaving the vertex from which vertex was discovered.
3.The process continues until we have discovered all the vertices that are
reachable from the original source vertex.
4.If any undiscovered vertices remain, then one of them is selected as a new
source vertex.
• where insertionPoint is the index where the element would have been, if it had been in the array in sorted order.
• To insert the value into the array, negate insertionPoint + 1
statement1;
statement2;
statement3;