170 likes | 220 Views
Data Structures -1 st exam-. 授課教授:李錫智. [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) <= cg(n) for all n >= n 0 . Using this definition, show that a.[5]9n 3 + 100n + 1 = O(n 3 ) Ans : 9n 3 + 100n + 1<=cn 3
E N D
Data Structures-1st exam- 授課教授:李錫智
[10] We define f(n) = O(g(n)) if and only if there is a real constant c>0 and an integer constant n0>0, such that f(n) <= cg(n) for all n >= n0. Using this definition, show that a.[5]9n3+ 100n + 1 = O(n3) Ans: 9n3+ 100n + 1<=cn3 取c=10, n0=119n3 + 100n + 1 = O(n3) b.[5] 2n + 100000n = O(2n) Ans: 2n+ 100000n<=c2n 取c=2, n0=222n + 100000n = O(2n) PS. 係數不需要一樣,只要滿足條件
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的過程
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 do n sum sum + A[i]; n return sum; 1 Total = 2n+2 Time complexity= O(n) PS.只有答案不給分,需要有Justify的過程
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;
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
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
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;
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.
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 ’+’
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
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+/
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;
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:
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:
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:
7. [20] Given an array Sof nintegers and another integer z, a. [10] Design an algorithm that determines whether there exist two elements, xand y, in Ssuch that x+ y= z. Describe your algorithm in Chinese, English, or pseudocode. Ans: Algorithm judge(S,z) #operations for i0 to n-1 do n for j0 to n-1 do n2 if (S[j]=z-S[i]) n2 return TRUE 1 return FALSE 1 b. [10] What is the time complexity of your algorithm? Explain it briefly. Ans: Total= 2n2+n+2 Time complexity=O(n2)PS.需要說明,否則不給分