Notes 1
Notes 1
Module-I
For any given problem the running time of an algorithm is assumed to be the
number of time steps. The space used by an algorithm is assumed to be the number
of RAM memory cells.
Order of Growth
Order of growth in algorithm means how the time for computation increases when
you increase the input size. It really matters when your input size is very large.
Order of growth provides only a crude description of the behavior of a process.It
considers leading term and ignores lower order terms since it is insignificant for
large values of input. It also ignores the constant coefficient of leading terms.
// Here c is a constant
for (int i = 1; i <= c; i++) {
// some O(1) expressions
}
3) O(nc): Time complexity of nested loops is equal to the number of times the
innermost statement is executed. For example the following sample loops have
O(n2) time complexity
int fun(int n)
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j < n; j += i)
{
// Some O(1) task
}
}
}
Analysis:
Solution: In this example, the search process begins from the end of a. The
successful Searches:
The Best case time complexities: When x number found in the position a[n].
Therefore, the time complexities in the best case will be:
TBSeqSearch(n)=4=Θ(1)
The Worst case time complexities: when x found in the first position a[1], So:
TWSeqSearch(n)= 2+2n = Θ(n)
The Average case time complexities: It is the average of complexities for all cases:
TFSeqSearch(n)=n+1= Θ(n)
Insertion sort
Insertion sort works just like how you arrange a pack of playing cards in your
hand.
1.Start with an empty hand with a pack of cards(faced down) on a table.
2.Pick one card at a time from the table and insert it in the correct position in your
hand.
3.To find the correct position of the card, compare it with each card in the hand
from right to left.
(Notice that cards in the hand are already sorted)
Algorithmically :
Insertion Sort(A)
1. for i = 2 to length(A) // Start with the 2nd element because the first element
is trivially sorted
2. x=A[i] // x is the element you want to insert in right place into the already
sorted set of elements
3. j=i-1 // Last index of the already sorted elements because thats where
you want to start comparing x
4. while j>0 and A[j]>x // Whenever there is an element greater than x
5. A[j+1]=A[j] // shift it to the right
6. j = j-1
7. end while
8. A[j+1]=x // The correct position to insert x
9. end for
Average case :
1. Outer for loop runs for (n-1) times
4. On average the inner while loop runs for (n-2)/2 times // Eg - Sorted set : 2
4 10 12, Element to insert : 9
1/2+2/2+3/2+4/2+....(n-1)/2 = (1+2+3+...+(n-1))/2
= ((n-1)*n)/4 => Θ(n2)
Worst case :
1. Outer for loop runs for (n-1) times
4. In worst case the inner while loop runs for (n-2) times // Eg - Sorted set
: 10 11 12 13, Element to insert : 9
1+2+3+...+(n-1) = ((n-1)*n)/2 => O(n2)
Solution:
The successful cases:
The Best case time complexities: When a(1) is the max number. That mean, there is
no need to entered in if block (if condition is never be true):
1.…………1
2.…………(n-2+1)+1=n
3.…………n-1
4.…………0
7……………1
TBArrayMax(n)=2n+1=Θ(n)
The Worst case time complexities: when array a is sorted in increasing form, max
element is a(n), So:
1.…………1
2.…………(n-2+1)+1=n
3.…………n-1
4.…………n-1
7……………1
TWArrayMax(n)= 3n = Θ(n)
The Average case time complexities: It is the average of complexities for all cases
from the best to the worst one:
Recurrence Equations
Recurrence equation have a general condition, breaking the problem into smaller
and smaller pieces, and the base condition (initial or boundary condition) that
terminate the recursion.
Solution of Recurrence Equations
Iteration method
Recursion tree method
Master’s theorem
Iteration Method
In the iteration method we iteratively “unfold” the recurrence until we “see the
pattern”. The idea is to expand (iterate) the recurrence and express it as a
summation of terms dependent only on n and the initial conditions. Techniques for
evaluating summations can then be used to provide bounds on the solution. The
iteration method does not require making a good guess like the substitution method
(but it is often more involved than using induction).
Recursion Tree Method
A recursion tree is a tree where each node represents the cost of a certain recursive
subproblem. In this method, we draw a recurrence tree and calculate the time taken
by every level of tree. Finally, we sum the work done at all levels. To draw the
recurrence tree, we start from the given recurrence and keep drawing till we find a
pattern among levels. The pattern is typically a arithmetic or geometric series.