Download presentation

Presentation is loading. Please wait.

Published byAngie Pagan Modified over 2 years ago

1
Homework – Chapter 1 作業解答

2
Problem 1 Given the Fibonacci number as 1 2 3 5 8 13 21 34… where the next Fibonacci number will be the sum of its previous two Fibonacci number. –Example: The number of 8 is the sum of 3 and 5. The next number will be 55 since it is the sum of 21 and 34.

3
Problem 1 1.Write a recursive algorithm to generate the Fibonacci number N. int Fibonacci (int N) { if (N <= 2) return N; return Fibonacci(N-1) + Fibonacci(N-2); }

4
Problem 1 2.Write a non-recursive algorithm using loop structure to generate the Fibonacci number N. int Fibonacci2 (int N) { if (N <= 2) return N; Let N1 = 1, N2 = 2, Total = 0; for (i = 3 to N) { Total = N1 + N2; N1 = N2; N2 = Total; } return Total; }

5
Problem 1 3.Discuss the execution efficiency of these two programs. –The process of the recursive Fibonacci() can be illustrated by the following tree structure. –Space complexity of Fibonacci(): At most N-1 stack frames (the depth of the tree) are pushed into function call stack when Fibonacci() is called. Therefore, the complexity is O(N). –Time complexity of Fibonacci(): Therefore, the time complexity depends on how many nodes the tree has, which is O(2 N ).

6
–Space complexity of Fibonacci2(): Only a fix number of automatic variables are required. Therefore, the complexity is O(1). –Time complexity of Fibonacci2(): The for-loop runs in N-2 times. Its body perform addition in O(1) for each iteration. Therefore, the total computing time is O(N). Conclusion: –Fibonacci2() is more efficient than Fibonacci(). Its space complexity and time complexity are relatively lower.

7
Problem 2 Ordering by asymptotic growth rates: (A)log(n!) (B)4 logn (C)(n-1)! (D)n ． 2 n (E)(logn) logn. –Please write computation!

8
Solution (A)log(n!) log(n!) < log(n n ) = O(n logn) (B)4 logn Let S = 4 logn logS = log4 logn = log2 2logn = 2logn = logn 2 ∴ S = n 2 = O(n 2 ) (C)(n-1)! = O(n!) (D)n ． 2 n < 2 n ． 2 n = 2 2n = O(2 n ) ∴ (A) < (B) < (D) < (C)

9
Solution (E)(logn) logn Let S = (logn) logn log S = (logn)(loglogn) = log(n loglogn ) S = n loglogn < n logn < 2 n 2 n is growing faster than n logn. ∴ (A) < (B) < (E) < (D) < (C) nn logn 2n2n 112 10 1024 10010 4 2 100 ………

Similar presentations

OK

General Computer Science for Engineers CISC 106 Lecture 07 James Atlas Computer and Information Sciences 9/18/2009.

General Computer Science for Engineers CISC 106 Lecture 07 James Atlas Computer and Information Sciences 9/18/2009.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Public speaking for kids ppt on batteries Ppt on eye osce Ppt on chemical change and physical change Ppt on breastfeeding advantages Ppt on latest technology in automobile Free ppt on self development Ppt on tunnel diode oscillator Ppt on dc motor working animation Ppt on dth service free download Moving message display ppt on tv