You are on page 1of 3

Analysis of Algorithms

1. Consider a function that reads a matrix of numbers and outputs the sum of all
the entries. What do you think could be the worst-case complexity (running
time) of this program?
The worst-case complexity is O (M*N) for a 2-Dimensional matrix with M-rows
and N-columns.
2. Make guesstimates of average-case complexity when the matrix represents the
consumption of various vegetables for each month of the year.
O(n) for 1-Dimensional matrix for a single month. For 12 months it would be
O(12n).
3. For the case when the matrix represents the number of days of the year for
which an Infoscion reports to another Infoscion directly. Did you need to
make any assumptions about internal data structures?
We need to plan for the matrix size and dimension for number of employees and
the number of days each one contacting to others.
4. Which of the following statements is/are true?
Q: A square matrix algorithm that is O(n2), where n is the “height” of the
matrix is O(m) where m is the total number of elements in the matrix-false
Q: An O(n2) algorithm is an O(n3) algorithm is an O(n4) algorithm-false
Q: An O(1034n) algorithm is an O(n) algorithm-false
Q: An O(log2n) algorithm is an O(logen) algorithm is an O(log10n)
Algorithm-false
Q: Beyond a threshold problem size, an O(n2) algorithm always performs
better than an O(n3) one-true
Q: Beyond a threshold problem size, an O(nk) algorithm always performs
better than an O(2n) one-false
5. A list of employee records sometimes needs to be accessed in increasing order
of salary, and sometimes in the reverse order. What is the complexity of
reversing a linked list? A doubly linked list?
Time complexity of reversing a doubly linked list is O(n)
Time complexity of reversing a linked list is also O(n)
6. What is the worst-case complexity of finding a given element in a binary
search tree?
worst-case complexity of finding a given element in a binary
search tree is n log n.
7. Compute the worst-case complexity of Euclid’s algorithm for computing the
greatest common divisor. (Note: The algorithm works by continually
computing remainders until 0 is reached. For example, if numbers given are
2340 and 1761, then the sequence of remainders is 579, 24, 3, and 0. Thus, 3
is the greatest common divisor)
worst-case complexity of Euclid’s algorithm for computing the
greatest common divisor is O(n).
8. Plot the actual running time of your implementation of bubble sort, insertion
and selection sort. Take an array size of 100 for the following kinds of input:
90% sorted array given, 90% reverse sorted array given, array with elements
alternating (10, 5, 8, 6, 20, 14,…), and array with randomly generated
elements.
Since the algorithm does not know whether the input is been sorted or not, it will
do the sorted inputs also in the same way like unsorted input.
Bubble Sort Insertion Sort Selection Sort
Worst Best Worst Best Worst Best
90% O(n )=10000 O(n)=10 O(n )=10000 O(n)=10 O(n )=10000 O(n2)=10000
2 2 2

sorted 0 0
array
90% O(n2)=10000 O(n)=10 O(n2)=10000 O(n)=10 O(n2)=10000 O(n2)=10000
reverse 0 0
sorted
array
Alternated O(n2)=10000 O(n)=10 O(n2)=10000 O(n)=10 O(n2)=10000 O(n2)=10000
elements 0 0
Random O(n2)=10000 O(n)=10 O(n2)=10000 O(n)=10 O(n2)=10000 O(n2)=10000
elements 0 0

9. Implement Fibonacci Series using Iteration and Recursion. Identify the


drawbacks, if any, of either of these implementations.
Time complexity for Recursion is-O(n log n)
Time complexity for Iteration is-O(n),so using iteration is better.
10. Given two sorted arrays A and B and an auxiliary array C, find an O(n)
algorithm (where n is the number of elements in A and B) for merging the
contents of A and B into C such that the elements in C are sorted
Bubble sort and insertion sort takes O (n) time to sort contents in an array.
In this particular case it will take O (m+n) where m is number of items in array A
and n is number of items in array B.
11. Tower of Hanoi Problem: We are given n disks of different sizes and three
poles. Initially all these n disks are mounted on the first pole in order of size,
the largest at the bottom and the smallest at the top. The problem is to move
these disks to the third pole using the second pole as an auxiliary. At any
given time we can move only disk with a constraint that a larger disk cannot
be placed on top of a smaller disk. Solve the Tower of Hanoi problem and analyze
the complexity of your algorithm.
#include <conio.h>
#include <stdio.h>
void main()
{
void hanoi(char,char,char,int);
char t1='A',t2='B',t3='C';
int n;
clrscr();
printf("\n Enter the no. of disks on Tower A:");
scanf("%d",&n);
if(n<1)
{
printf("\n Nothing to move");
}
else
hanoi(t1,t2,t3,n);
getch();
}
void hanoi(char t1,char t2,char t3,int n)
{
static int step=0;
step++;
printf("\n %c %c %c %d",t1,t2,t3,n);
if(n==1)
{
printf("\n Move top disk from Tower %c ----> %c",t1,t2);
return;
}
hanoi(t1,t3,t2,n-1);
printf("\n %c %c %c %d",t1,t2,t3,n);
printf("\n Move top disk from Tower %c ----> %c",t1,t2);
printf("\n %c %c %c %d",t1,t2,t3,n);
hanoi(t3,t2,t1,n-1);
printf("\n %c %c %c %d steps=%d",t1,t2,t3,n,step);
}

Time complexity is O(2n-1)

You might also like