This lectue Complexity o binay seach Answes to inomal execise Abstact data types Stacks ueues ADTs, Stacks, ueues 1 binayseach(int[] a, int value) { while (ight >= let) { { i (value < a[middle]) ight = middle-1; let = middle+1; etun alse; ADTs, Stacks, ueues 2 Complexity o binay seach Complexity o binay seach binayseach(int[] a, int value) { Let n be the size o aay while (ight >= let) { How many times is the loop executed? { i (value < a[middle]) ight = middle-1; let = middle+1; etun alse; ADTs, Stacks, ueues 3 binayseach(int[] a, int value) { Let n be the size o aay while (ight let) { How many times is the loop executed? n { n/2 (n/2)/2 i (value < a[middle]) ight = middle-1; ((n/2)/2)/2 let = middle+1; In the wost case, until ((n/2) )/2 = 1 x times, whee 2 x n and 2 x-1 < n etun alse; x is log 2 n, ounded up to an intege ADTs, Stacks, ueues 4 Example: n = 10, value = 19 Complexity o binay seach 0 1 2 3 4 5 6 7 8 9 let = 0, binayseach(int[] a, int value) { Let n be the size o aay ight= 9, 13 15 17 19 21 23 25 27 29 31 middle = 4 constant time let = 0, opeations: c 1 13 15 17 19 ight= 3, middle = 1 while (ight >= let) { loop executed log 2 n times let = 2, 17 19 ight= 3, middle = 2 { constant time 19 let = 3, i (value < a[middle]) ight = middle-1; opeations: c 2 ight= 3, let = middle+1; middle = 3 n = 10, log 2 10 ounded up to an intege is 4. t(n) = c 1 + c 2 log 2 n+ c 3 Hint: given n, look o the next lagest powe o 2 etun alse; constant time opeation: c 3 and take log 2 o that. ADTs, Stacks, ueues 5 ADTs, Stacks, ueues 6 1
Finally Pove that c 1 + c 2 log 2 n+ c 3 O(log 2 n). Need to ind n 0 and c such that o all n n 0, c 1 + c 2 log 2 n+ c 3 c log 2 n Let n 0 be 2; then o all n n 0, log 2 n 1. So, o all n n 0, c 1 + c 2 log 2 n+ c 3 c 1 log 2 n+ c 2 log 2 n+ c 3 log 2 n and o all n n 0, c 1 + c 2 log 2 n+ c 3 (c 1 + c 2 + c 3 ) log 2 n Set n 0 = 2 and c = c 1 + c 2 + c 3. n O(n 2 ) Pove that n is in O(n 2 ). Need to ind n 0 and c such that o all n n 0, n c n 2 Let n 0 be 1; then o all n n 0, log 2 n n and n n 2 So, o all n n 0, n 7n 2 + 10 n 2 + n 2 and o all n n 0, n (7 + 10 + 1) n 2 Set n 0 = 1 and c = 18. ADTs, Stacks, ueues 7 ADTs, Stacks, ueues 8 n O(n) I we wee to pove that n is in O(n), we d need to ind n 0 and c such that o all n n 0, n c n Fo all such n 0 and c it will also hold that o all n n 0, 7n 2 + 10n c n (note o n 1, log 2 n is positive) 7n + 10 c n (c-10)/7 Thee is obviously no such n 0 and c: whateve values o n 0 and c we ix, we can also ind n n 0 which is lage than (c-10)/7. O(log a n) = O(log b n) Suppose (n) is in O(log a n). We pove that it is in O(log b n). I (n) is in O(log a n), thee exist n 0 and c such that o all n n 0, (n) c log a n. We know that log a n = log a b log b n (see Math to eview) Fo example, log 2 64 = log 2 8 log 8 64 6 = 3 2 So, i (n) c log a n, then (n) c log a b log b n log a b is a constant acto, so we can deine a new constant c =clog a b and we have: o all n n 0, (n) c log b n, hence (n) is in O(log b n). ADTs, Stacks, ueues 9 ADTs, Stacks, ueues 10 Abstact Data Types (ADTs) Stacks An abstact data type (ADT) is an abstaction o a data stuctue An ADT speciies: Data stoed Opeations on the data Eo conditions associated with opeations Example: ADT modeling a simple stock tading system The data stoed ae buy/sell odes The opeations suppoted ae ode buy(stock, shaes, pice) ode sell(stock, shaes, pice) void cancel(ode) Eo conditions: Buy/sell a nonexistent stock Cancel a nonexistent ode ADTs, Stacks, ueues 11 ADTs, Stacks, ueues 12 2
The Stack ADT ( 4.2) The Stack ADT stoes abitay objects Insetions and deletions ollow the last-in ist-out scheme Think o a sping-loaded plate dispense Main stack opeations: push(object): insets an element object pop(): emoves and etuns the last inseted element Auxiliay stack opeations: object top(): etuns the last inseted element without emoving it intege size(): etuns the numbe o elements stoed boolean isempty(): indicates whethe no elements ae stoed Stack Inteace in Java Java inteace coesponding to ou Stack ADT Requies the deinition o class EmptyStackException Dieent om the built-in Java class java.util.stack public inteace Stack { public int size(); public boolean isempty(); public Object top() thows EmptyStackException; public void push(object o); public Object pop() thows EmptyStackException; ADTs, Stacks, ueues 13 ADTs, Stacks, ueues 14 Exceptions Attempting the execution o an opeation o ADT may sometimes cause an eo condition, called an exception Exceptions ae said to be thown by an opeation that cannot be executed In the Stack ADT, opeations pop and top cannot be peomed i the stack is empty Attempting the execution o pop o top on an empty stack thows an EmptyStackException Applications o Stacks Diect applications Page-visited histoy in a Web bowse Undo sequence in a text edito Chain o method calls in the Java Vitual Machine Indiect applications Auxiliay data stuctue o algoithms Component o othe data stuctues ADTs, Stacks, ueues 15 ADTs, Stacks, ueues 16 Method Stack in the JVM The Java Vitual Machine (JVM) keeps tack o the chain o active methods with a stack When a method is called, the JVM pushes on the stack a ame containing Local vaiables and etun value Pogam counte, keeping tack o the statement being executed When a method ends, its ame is popped om the stack and contol is passed to the method on top o the stack Allows o ecusion main() { int i = 5; oo(i); oo(int j) { int k; k = j+1; ba(k); ba(int m) { ba PC = 1 m = 6 oo PC = 3 j = 5 k = 6 main PC = 2 i = 5 ADTs, Stacks, ueues 17 Aay-based Stack A simple way o implementing the Stack ADT uses an aay We add elements om let to ight A vaiable keeps tack o the index o the top element S Algoithm size() etun t +1 Algoithm pop() i isempty() then thow EmptyStackException t t 1 etun S[t +1] 0 1 2 t ADTs, Stacks, ueues 18 3
Aay-based Stack (cont.) The aay stoing the stack elements may become ull A push opeation will then thow a FullStackException Limitation o the aaybased implementation Not intinsic to the Stack ADT S Algoithm push(o) i t = S.length 1 then thow FullStackException t t +1 S[t] o 0 1 2 t ADTs, Stacks, ueues 19 Peomance and Limitations Peomance Let n be the numbe o elements in the stack The space used is O(n) Each opeation uns in time O(1) Limitations The maximum size o the stack must be deined a pioi and cannot be changed Tying to push a new element into a ull stack causes an implementation-speciic exception ADTs, Stacks, ueues 20 Aay-based Stack in Java Paentheses Matching public class AayStack implements Stack { // holds the stack elements pivate Object S[ ]; // index to top element pivate int top = -1; // constucto public AayStack(int capacity) { S = new Object[capacity]); public Object pop() thows EmptyStackException { i isempty() thow new EmptyStackException ( Empty stack: cannot pop ); Object temp = S[top]; // acilitates gabage collection S[top] = null; top = top 1; etun temp; Each (, {, o [ must be paied with a matching ),, o [ coect: ( )(( )){([( )]) coect: ((( )(( )){([( )]) incoect: )(( )){([( )]) incoect: ({[ ]) incoect: ( ADTs, Stacks, ueues 21 ADTs, Stacks, ueues 22 Paentheses Matching Algoithm Algoithm PaenMatch(X,n): Input: An aay X o n tokens, each o which is eithe a gouping symbol, a vaiable, an aithmetic opeato, o a numbe Output: tue i and only i all the gouping symbols in X match Let S be an empty stack o i=0 to n-1 do i X[i] is an opening gouping symbol then S.push(X[i]) i X[i] is a closing gouping symbol then i S.isEmpty() then etun alse {nothing to match with i S.pop() does not match the type o X[i] then etun alse {wong type i S.isEmpty() then etun tue {evey symbol matched etun alse {some symbols wee neve matched ueues ADTs, Stacks, ueues 23 ADTs, Stacks, ueues 24 4
The ueue ADT ( 4.3) The ueue ADT stoes abitay objects Insetions and deletions ollow the ist-in ist-out scheme Insetions ae at the ea o the queue and emovals ae at the ont o the queue Main queue opeations: enqueue(object): insets an element at the end o the queue object dequeue(): emoves and etuns the element at the ont o the queue Auxiliay queue opeations: object ont(): etuns the element at the ont without emoving it intege size(): etuns the numbe o elements stoed boolean isempty(): indicates whethe no elements ae stoed Exceptions Attempting the execution o dequeue o ont on an empty queue thows an EmptyueueException ueue Example Opeation Output enqueue(5) (5) enqueue(3) (5, 3) dequeue() 5 (3) enqueue(7) (3, 7) dequeue() 3 (7) ont() 7 (7) dequeue() 7 () dequeue() eo () isempty() tue () enqueue(9) (9) enqueue(7) (9, 7) size() 2 (9, 7) enqueue(3) (9, 7, 3) enqueue(5) (9, 7, 3, 5) dequeue() 9 (7, 3, 5) ADTs, Stacks, ueues 25 ADTs, Stacks, ueues 26 Applications o ueues Diect applications Waiting lists, bueaucacy Access to shaed esouces (e.g., pinte) Multipogamming Indiect applications Auxiliay data stuctue o algoithms Component o othe data stuctues ADTs, Stacks, ueues 27 Aay-based ueue Use an aay o size N in a cicula ashion Two vaiables keep tack o the ont and ea index o the ont element index immediately past the ea element Aay location is kept empty nomal coniguation wapped-aound coniguation 012 ADTs, Stacks, ueues 28 ueue Opeations We use the modulo opeato (emainde o division) Algoithm size() etun (N + ) mod N Algoithm isempty() etun ( = ) ueue Opeations (cont.) Opeation enqueue thows an exception i the aay is ull This exception is implementationdependent Algoithm enqueue(o) i size() = N 1 then thow FullueueException [] o ( + 1) mod N 012 012 ADTs, Stacks, ueues 29 ADTs, Stacks, ueues 30 5
ueue Opeations (cont.) Opeation dequeue thows an exception i the queue is empty This exception is speciied in the queue ADT 012 Algoithm dequeue() i isempty() then thow EmptyueueException o [] ( + 1) mod N etun o ueue Inteace in Java Java inteace coesponding to ou ueue ADT Requies the deinition o class EmptyueueException No coesponding built-in Java class public inteace ueue { public int size(); public boolean isempty(); public Object ont() thows EmptyueueException; public void enqueue(object o); public Object dequeue() thows EmptyueueException; ADTs, Stacks, ueues 31 ADTs, Stacks, ueues 32 Application: Round Robin Schedules We can implement a ound obin schedule using a queue,, by epeatedly peoming the ollowing steps: 1. e =.dequeue() 2. Sevice element e 3..enqueue(e) The ueue 1. Deque the next element 2. Sevice the next element 3. Enqueue the seviced element Shaed Sevice ADTs, Stacks, ueues 33 6