Vous êtes sur la page 1sur 4

ANALYSIS OF ALGORITHMS Assignment Chapter 1 - 3 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 for a 2-Dimensional matrix with m-rows and n-columns is O (m*n) 2. Make guesstimates of average-case complexity when the matrix represents the consumption of various vegetables for each month of the year. Average-case complexity of 1-Dimensional matrix for a single month is O (n) and 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? Assumptions for matrix size and dimension is needed 4. Which of the following statements is/are true? a. 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 b. An O (n2) algorithm is an O (n3) algorithm is an O (n4) algorithm -FALSE c. An O (1034n) algorithm is an O (n) algorithm -FALSE d. An O (log2n) algorithm is an O (logen) algorithm is an O (log10n) algorithm -FALSE e. Beyond a threshold problem size, an O (n2) algorithm always performs better than an O (n3) one -TRUE f. Beyond a threshold problem size, an O (nk) algorithm always performs better than an O (2n) one -FALSE

Assignment Chapter 4 - 5 1. 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 linked list O (n) Time complexity of reversing a doubly linked list O (n) 2. What is the worst-case complexity of finding a given element in a binary search tree? The worst-case complexity of finding a given element in a binary search tree isO(nlogn) 3. Compute the worst-case complexity of Euclids 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) The worst-case complexity of Euclids algorithm for computing the greatest common divisor is O(n) 4. 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 Bubble sort Worst O (n2) =10000 O (n2) =10000 O (n2) =10000 O (n2) =10000 Insertion sort Worst O (n2) =10000 O (n2) =10000 O (n2) =10000 O (n2) =10000 Selection sort Worst O (n2) =10000 O (n2) =10000 O (n2) =10000 O (n2) =10000

Best O(n)=100 O(n)=100 O(n)=100 O(n)=100

Best O(n)=100 O(n)=100 O(n)=100 O(n)=100

90% sorted array 90%reverse sorted array alternated elements Random elements

Best O (n2) =10000 O (n2) =10000 O (n2) =10000 O (n2) =10000

5. Implement Fibonacci Series using Iteration and Recursion. Identify the drawbacks, if any, of either of these implementations. The Time Complexity of implementing Fibonacci series using recursion is O(nlogn) where as in case of implementing in iteration the Time complexity is O(n) and so Implementing Fibonacci series using iteration is better 6. 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 -number of items in array A n -number of items in array B 7. 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<stdio.h> #include<conio.h> #include<math.h> void hanoi (int x, char s, char d, char i) { if (x==1) { printf("move disk %d from %c to %c\n",x,s,d);

} else { hanoi (x-1, s, i, d); printf("move disk %d from %c to %c \n",x,s, d); hanoi (x-1, i,d, s); } } void main() { int disk; int moves; clrscr(); printf("enter the number of disks:"); scanf("%d", &disk); moves=pow(2,disk)-1; printf("\n The no. of moves required is %d\n\n",moves); hanoi (disk,'s','d','i'); getch(); }

Time complexity is O (2n-1)

Vous aimerez peut-être aussi