授課教授:李錫智 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.

Slides:



Advertisements
Similar presentations
Stack and Queues using Linked Structures Kruse and Ryba Ch 4.
Advertisements

Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Queues1 Part-B2 Queues. Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out scheme.
If f(n) and g(n) are both O(h(n)), then f(n) + g(n) is O(h(2n)). False! From the Rule of Sum, f(n) + g(n) => max(O(h(n)), O(h(n))) => O(h(n))
Stacks  a data structure which stores data in a Last-in First-out manner (LIFO)  has a pointer called TOP  can be implemented by either Array or Linked.
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
CHAPTER 7 Queues.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 18 Stacks.
 Abstract Data Type Abstract Data Type  What is the difference? What is the difference?  Stacks Stacks  Stack operations Stack operations  Parsing.
Stacks, Queues, and Deques
授課教授:李錫智 Data Structures -2 nd exam-. 1.[15] A simple way for implementing the vector ADT is to use an array A, where A[i] stores the element at rank.
授課教授:李錫智 Data Structures -3 rd exam-. 1.[5] Refer to the two trees in Fig. 1. Let’s define the balance factor BF of a node to be the absolute value of.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Analysis of Algorithms Algorithm Input Output. Analysis of Algorithms2 Outline and Reading Running time (§1.1) Pseudo-code (§1.1) Counting primitive operations.
CS 206 Introduction to Computer Science II 03 / 06 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 17 / 2008 Instructor: Michael Eckmann.
Introduction to Lock-free Data-structures and algorithms Micah J Best May 14/09.
Main Index Contents 11 Main Index Contents Model for a Queue Model for a Queue The Queue The Queue ADTQueue ADT (3 slides) Queue ADT Radix Sort Radix Sort.
Queues.
Analysis of Algorithms1 Estimate the running time Estimate the memory space required. Time and space depend on the input size.
Part-B1 Stacks. Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations.
CS 206 Introduction to Computer Science II 03 / 16 / 2009 Instructor: Michael Eckmann.
Part-B1 Stacks. Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations.
© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Algorithm/Running Time Analysis. Running Time Why do we need to analyze the running time of a program? Option 1: Run the program and time it –Why is this.
Implementing a Queue as a Linked Structure CS 308 – Data Structures.
CS 206 Introduction to Computer Science II 10 / 28 / 2009 Instructor: Michael Eckmann.
Chapter 1 Introduction Definition of Algorithm An algorithm is a finite sequence of precise instructions for performing a computation or for solving.
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
Objectives of these slides:
Stacks. week 2a2 Outline and Reading The Stack ADT (§4.1) Applications of Stacks Array-based implementation (§4.1.2) Growable array-based stack Think.
Stack and Queue.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
CSC 201 Analysis and Design of Algorithms Lecture 03: Introduction to a CSC 201 Analysis and Design of Algorithms Lecture 03: Introduction to a lgorithms.
Data Structures -1 st test- March 30, 2015 授課教授:李錫智.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Due: 2007/11/12. Problem 1 Rewrite function Push and Pop (Program 3.10 and 3.12) using an additional variable lastOp as discussed on Page 146. The queue.
CSC 211 Data Structures Lecture 13
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 3.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Data Structures -3 rd test- 2015/06/01 授課教授:李錫智. Question 1 Suppose we use an array to implement a sorted list. Please answer the following questions.
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
Data Structures -3 st exam- 授課教師 : 李錫智 教授. 1. [5] Assume we have a binary tree which is implemented in a pointer-based scheme. Describe how to know the.
Advanced Higher Computing Science Stacks Queues and linked lists.
Week 5 - Wednesday.  What did we talk about last time?  Recursion  Definitions: base case, recursive case  Recursive methods in Java.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
Queue. Avoid confusion Britain Italy 6 Applications of Queues Direct applications –Waiting lists, bureaucracy –Access to shared resources (e.g.,
Data Structures -2 nd exam- 授課教師 : 李錫智 教授 1. 1.[10] Please answer the following questions about stack: What is the response of the statement “(new stack()).isEmpty()”?
Sum of Arithmetic Sequences. Definitions Sequence Series.
CSE373: Data Structures & Algorithms Lecture 8: Amortized Analysis Dan Grossman Fall 2013.
Data Structures -3 rd exam- 授課教授:李錫智. 1.[10] Suppose we want to implement a queue by using the following List operations: getLength(); remove(position);
Lecture 21 Data Structures, Algorithms and Complexity Stacks and Queues GRIFFITH COLLEGE DUBLIN.
Data Structures -1 st exam- 授課教師 : 李錫智 教授. 1.[10] Suppose we have the ADT Bag which has the following operations: bool isEmpty(): tests whether Bag is.
Data Structures -3 rd exam- 授課教師 : 李錫智 教授. 1.[10] Please answer the following questions about queue: [ 2] What is the response of the statement: “(new.
Group 2 Block 胡貴蓉 Project 2 JMVC code tracing.
 In general, Queue is line of person waiting for their turn at some service counter like ticket window at cinema hall, at bus stand or at railway station.
Review Array Array Elements Accessing array elements
COSC160: Data Structures: Lists and Queues
Stacks and Queues.
CMSC 341 Lecture 5 Stacks, Queues
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Queues 11/16/2018 4:19 AM Queues 11/16/2018 4:19 AM Queues.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Queues Jyh-Shing Roger Jang (張智星)
Computing Spans Given an an array X, the span S[i] of X[i] is
Presentation transcript:

授課教授:李錫智 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 that f(n) = n 0. Using this definition, show that a.[5]9n n + 1 = O(n 3 ) Ans: 9n n + 1<=cn 3 取 c=10, n 0 =11  9n n + 1 = O(n 3 ) b.[5] 2 n n = O(2 n ) Ans: 2 n n<=c2 n 取 c=2, n 0 =22  2 n n = O(2 n ) 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 don 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=

[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: 2 4 2

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 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. 需要說明,否則不給分