Presentation is loading. Please wait.

Presentation is loading. Please wait.

授課教授:李錫智 Data Structures -1 st exam-. 1.[10] We define f(n) = O(g(n)) if and only if there is a real constant c>0 and an integer constant n 0 >0, such.

Similar presentations


Presentation on theme: "授課教授:李錫智 Data Structures -1 st exam-. 1.[10] We define f(n) = O(g(n)) if and only if there is a real constant c>0 and an integer constant n 0 >0, such."— Presentation transcript:

1 授課教授:李錫智 Data Structures -1 st exam-

2 1.[10] We define f(n) = O(g(n)) if and only if there is a real constant c>0 and an integer constant n 0 >0, such that f(n) = n 0. Using this definition, show that a.[5]9n 3 + 100n + 1 = O(n 3 ) Ans: 9n 3 + 100n + 1<=cn 3 取 c=10, n 0 =11  9n 3 + 100n + 1 = O(n 3 ) b.[5] 2 n + 100000n = O(2 n ) Ans: 2 n + 100000n<=c2 n 取 c=2, n 0 =22  2 n + 100000n = O(2 n ) PS. 係數不需要一樣,只要滿足條件

3 2. [15] Suppose we have the following program: Algorithm Sum(A, n)#cells sum = 0; 1 for i  0 to n-1 do 1 sum  sum + A[i]; 0 return sum; a. [5] What is the space complexity of the program in terms of the big-Oh notation? Please express the complexity as simple as possible and justify your answer. Note that the input array A is stored in the calling function, not in the Sum function itself. Ans: Total = 2 = O(1) Space complexity= O(1) PS. 只有答案不給分,需要有 Justify 的過程

4 b. [5] What is the time complexity of the program in terms of the big-Oh notation? Please express the complexity as simple as possible and justify your answer. Ans: Algorithm Sum(A, n)#operations sum = 0;1 for i  0 to n-1 don sum  sum + A[i]; n return sum;1 Total = 2n+2 Time complexity= O(n) PS. 只有答案不給分,需要有 Justify 的過程

5 c. [5] Suppose A contains eight integers 25, 20, 5, 10, 50, 55, 15, 80. What is the returned value for the call Sum(A, 8)? Ans: 260 A=25, 20, 5, 10, 50, 55, 15, 80 Algorithm Sum(A, n) sum = 0; for i  0 to n-1 do sum  sum + A[i]; return sum;

6 3. [10] Suppose we use S[5] to implement a stack, and let sptr be the stack pointer indicating the index of the last item pushed in the stack. a. [2] What value is sptr initialized to? Ans: sptr= -1 b. [5] What does S contain after the following operations in sequence: push(50), push(40), push(60), pop(), push(30), pop(), pop(), push(60), pop(), push(40)? Ans: c.[3] What is the value of sptr after the above operations? Ans: sptr= 1 40 50

7 60403070 4. [10] Suppose we use Q[5] to implement a queue in a circular fashion. Let qfront and qrear be the queue pointers indicating the index of the earliest item enqueued into and the index of the empty cell following the latest item enqueued into the queue, respectively. a. [2] What values are qfront and qrear initialized to? Ans: qfront=0, qrear=0 b. [5] What does Q contain after the following operations in sequence: enqueue(60), enqueue(40), enqueue(50), dequeue(), enqueue(30), dequeue(), enqueue(70), enqueue(60), dequeue (), enqueue(40)? Ans: c. [3] What are the values of qfront and qrear after the above operations? Ans: qfront=3, qrear=2

8 5. [20] Suppose we have the following program: Algorithm nsysu(A, n) B  a new empty stack; for i  0 to n-1 do if (A[i] is an integer) then Print out A[i]; continue; if (A[i] is ‘(‘) then B.push(A[i]); continue; if (A[i] is ‘)’) then while (B.top() != ‘(‘) do Print out B.pop(); B.pop(); continue;

9 If (B.isEmpty() or (precedence of A[i] > precedence of B.top())) then B.push(A[i]); continue; while (!B.isEmpty() and (precedence of A[i] <= precedence of B.top())) do Print out B.pop(); B.push(A[i]); while(!B.isEmpty()) do Print out B.pop(); Note that +, - *, / have a higher precedence over (, * and / have a higher precedence over + and -, * and / have equal precedence, and + and – have equal precedence.

10 Suppose array A contains is an array containing the elements (, 2, +, (, 4, +, 6, ), *, 3, *, 2, -, 7, ), /, 5, -, 5, /, (, 2, +, 3, and ). Let’s run nsysu(A,25). a. [4] What is the content of B immediately after the first ‘*‘ is processed? Ans: b. [4] What will be done when the second ‘)’ is read in? Ans: print out pop ‘- ’ print out pop ’+’ * + (

11 Suppose array A contains is an array containing the elements (, 2, +, (, 4, +, 6, ), *, 3, *, 2, -, 7, ), /, 5, -, 5, /, (, 2, +, 3, and ). Let’s run nsysu(A,25). c. [4] What is the content of B immediately after the first ‘/’ is processed? Ans: d. [4] What is the content of B immediately after the last ‘+’ is processed? Ans: or / + ( / - + ( /

12 Suppose array A contains is an array containing the elements (, 2, +, (, 4, +, 6, ), *, 3, *, 2, -, 7, ), /, 5, -, 5, /, (, 2, +, 3, and ). Let’s run nsysu(A,25). c. [4] What is printed out after this function call? Ans: 246+3*2*7-+5/523+/- or 246+3*27-+5/523+/

13 6. [15] Suppose we have the following program: Algorithm EE(A, n) S  a new array of n integers; B  a new empty stack; for i  0 to n-1 do while(!B.isEmpty() and (A[B.top()] <= A[i])) do B.pop(); if (B.isEmpty()) then S[i]  i + 1; else S[i]  i – B.top(); B.push(i); return S;

14 Suppose A is an array containing 90, 30, 100, 40, 80, 120, 60, 55, and 70. Let’s run EE(A,9). a. [3] What is the content of B immediately after 100 is processed? Ans: b. [3] What is the content of B immediately after 80 is processed? Ans: 2 4 2

15 Suppose A is an array containing 90, 30, 100, 40, 80, 120, 60, 55, and 70. Let’s run EE(A,9). c. [3] What is the content of B immediately after 60 is processed? Ans: d. [3] What is the content of B immediately after 70 is processed? Ans: 6 5 8 5

16 Suppose A is an array containing 90, 30, 100, 40, 80, 120, 60, 55, and 70. Let’s run EE(A,9). e. [3] What is the content of S returned by the function call? Ans: 113126113

17 7. [20] Given an array S of n integers and another integer z, a. [10] Design an algorithm that determines whether there exist two elements, x and y, in S such that x + y = z. Describe your algorithm in Chinese, English, or pseudocode. Ans:Algorithm judge(S,z)#operations for i  0 to n-1 don for j  0 to n-1 don 2 if (S[j]=z-S[i])n 2 return TRUE1 return FALSE1 b. [10] What is the time complexity of your algorithm? Explain it briefly. Ans: Total= 2n 2 +n+2 Time complexity=O(n 2 ) PS. 需要說明,否則不給分


Download ppt "授課教授:李錫智 Data Structures -1 st exam-. 1.[10] We define f(n) = O(g(n)) if and only if there is a real constant c>0 and an integer constant n 0 >0, such."

Similar presentations


Ads by Google