Anda di halaman 1dari 9

1.

Show that the following equalities are correct: a) 5n^2 - 6n = Theta(n^2) By definition, 0 <= c1(n^2) <= 5n^2-6n <= c2(n^2) Divide both sides by n^2 0<= c1 <= 5-6/n <= c2(n^2) 5-6/n >= 0 n=6/5 hence for n0 =2 and c1=2 c2=5 the above codtion is true, hence 5n^2 - 6n = Theta(n^2) is proved.

b) n! = O(n^n) when n > = 1 0 < 1*2*3*4*5*n-1*n <= n*n*n*n*n*n**n(n times) 0 < n! < n^n Hence for n0 = 1 and c=1 N! =O(n^n) c) 2 n^2 2^n + n log n = Theta(n^ 2 + 2^n) d) 33 n^3 + 4 n^2 = Omega(n^2) by definition 0 <= c1(n^2) <= 33 n^3 + 4 n^2 Dividing n^2 0 <= c1 <= 33 n + 4

33 n + 4 >=0 n >= -44/33 i.e >= 1 hence for c1=37 and n0 =1

e) 33 n^3 + 4 n^2 = Omega(n^3)

by definition

0 <= c1 n^3 <= 33 n^3 + 4 n^2

Dividing by n^3

0 <= c1 <= 33 + 4/n

As 33 + 4/n >= 0

As n tends to infinity Hence for n0 = 1 and C1=33 33 n^3 + 4 n^2 = Omega(n^3)

And show that the following are incorrect: f) 10 n^2 + 9 = O(n) f(N)= 10 n^2 + 9 g(n)=n f(n)/g(n)(as n tends to infinity)= hence 10 n^2 + 9 O(n) b) n^2 log n = Theta(n^2) f(N)= n^2 log n g(n)= n^2 f(n)/g(n)(as n tends to infinity)= n^2 log n Theta(n^2)

infinity

infinity

2. (5 points) Order the following functions by their asymptotic growth rates. log(log n) ; 2^(log n) ; n^2 ; n! ; (log n)! ; (3/2)^n ; n^3; 2^2^n; n; n log n; sqrt(log n); log n Ans: assuming that the log is to the base 2.

Log log n < SQRT log n < 2 ^ log n = log n < (log n)! < n log n < n^2 < n^3 < n! < 3/2^n < 2^2^n

3. Fibonacci numbers. Implement in two separate files,called fibonacci1.c and fibonacci2.c, the iterative and the recursive methods for computing Fibonacci numbers. The programs should read from the standard input the value of N, and output the corresponding Fibonacci value. Write down in a table the time (according to your system clock) that it takes for each program to complete, for each of the following N values: 1, 2, 4, 8, 16, 32, 64. Ans: 1st program i.e iterative method File name: fibonacci1.c

Functionality 1. Assign variable a,b and c as 0,0 and 1. 2. Include time.h to get the system time. 3. Start the clock. 4. Take the value of N from user. 5. A while loop which runs N times and prints c. a. In each iteration,the value of i. a is changed to b. ii. b is changed to c. iii. And c is changed to a+b. 6. Stop the clock. 7. Print the time taken.

Time taken by the system

N Time Taken(sec)

1 0.00000 0

2 0.00000 0

4 0.00000 0

8 0.00100 0

16 0.00100 0

32 0.003000

64 0.028000

Instructions on how to run the file

1. In UNIX a. cc fibonacci1.c b. ./a.out c. Put the value of N. d. If you want to enter more, enter Y or y or enter anything else to exit the program. 2. In WINDOWS a. Compile and run using any C compiler like devc++,turboc etc. b. Put the value of N. c. If you want to enter more, enter Y or y or enter anything else to exit the program. 2nd program i.e recursive method File name: fibonacci2.c

Functionality 8. Assign variable a and b as 0 and 1.. 9. Include time.h to get the system time. 10.Start the clock. 11.Take the value of N from user. 12.Call the recursive function with arguments as (a,b,N) a. Print b and call the same function with arguments (b,a+b,N-1) b. If N==0, execute the return statement. 13.Stop the clock. 14.Print the time taken.

Time taken by the system

N Time Taken(sec)

1 0.00000 0

2 0.00100 0

4 0.00200 0

8 0.01700 0

16 0.02200 0

32 0.046000

64 0.093000

Instructions on how to run the file

3. In UNIX a. cc fibonacci2.c b. ./a.out c. Put the value of N. d. If you want to enter more, enter Y or y or enter anything else to exit the program. 4. In WINDOWS a. Compile and run using any C compiler like devc++,turboc etc. b. Put the value of N. c. If you want to enter more, enter Y or y or enter anything else to exit the program.

4. Write a function to add two polynomials. Do not destroy the input. Use a linked list implementation. If the polynomials have N and M terms, what is the time complexity of your program? Save the program as pAddition.c

File name:

pAddition.c

Functionality 1. A structure poly is created i.e struct poly {int coeff; int pow; struct poly *next; }; 2. The first polynomial in x is created using create_poly function. 3. Similarly the second polynomial in x is created using create_poly function. 4. Both the polynomials are printed using the print function. 5. Add function is called with both the polynomials as arguments. a. Each element of the first polynomial is compared with each polynomial of the second polynomial , and if the powers are same, then both the coefficients are added and are stored in a new list. 6.Again print function is called using sum as an argument.

CONCLUSION
The time complexity of the program is O(NM),if N and m are the no of terms in the polynomial.

Instructions on how to run the file

5. In UNIX a. cc pAddition.c b. ./a.out c. Enter the number of elements in the 1st polynomial. d. Enter the coefficients and powers followed by space or enter. e. Similarly enter the second polynomial.

6. In WINDOWS a. Compile and run using any C compiler like devc++,turboc etc. b. Enter the number of elements in the 1st polynomial. c. Enter the coefficients and powers followed by space or enter. d. Similarly enter the second polynomial.

5. (10 points) Write a nonrecursive function to reverse a singly linked list in O(N) time. Save the program as reverseList.c ans: File name: reverseList.c

Functionality 1.Create a list using create_list finction. 2. print function is called with head as an argument 3. call the reverse function. A.Here the first three nodes of the list are holded to variables as temp ,cur and p While(cur!=NULL) cur->next=temp; temp=cur; cur=p; p=cur->next; B. once the while loop ends First->next is made NULL c.Temp is returned. 4. print function is called with the new head as an argument.

CONCLUSION

Since the list is traversed once, the time complexity of the program is O(N).

Instructions on how to run the file

7. In UNIX a. cc reverseList.c b. ./a.out c. Enter the number of elements in the list. d. Enter the elemetns followed by space or enter. 8. In WINDOWS a. Compile and run using any C compiler like devc++,turboc etc. b. Enter the number of elements in the list. c. Enter the elemetns followed by space or enter.

Anda mungkin juga menyukai