Академический Документы
Профессиональный Документы
Культура Документы
Algorithms
Week 6 Oliver
Kullmann
Dynamic
sets
Data structures Simple
implementa-
tion
1 Dynamic sets Special
cases
Implementatio
3 Special cases Queues
Implementatio
4 Stacks Tutorial
5 Implementation
6 Queues
7 Implementation
8 Tutorial
CS 270
General remarks Algorithms
Oliver
Kullmann
Dynamic
sets
Simple
We start considering Part III “Data Structures” from implementa-
tion
CLRS. Special
cases
As a first example we consider two special cases of
Stacks
“buffers”, namely stacks and queues. Implementatio
Queues
Reading from CLRS for week 5 Implementatio
Simple
and other primitive types, in C++ this is possible for every implementa-
tion
type of object).
Special
Whatever the objects in a set are, access to them (especially for cases
Stacks
changing them) is only possible via a pointer (or iterator).
Implementatio
handle) to the copy of that object in the dynamic set back. Tutorial
Oliver
Kullmann
Dynamic
sets
Besides objects (which become “elements” once they are in the
Simple
set) and pointers, CLRS uses the notion of a key to identify an implementa-
tion
object:
Special
cases
If the object is for example a record of personal attributes, Stacks
then the name or some form of ID can be used as a key. Implementatio
Queues
Tutorial
Oliver
With sets S we can “ask” whether “x ∈ S” is true. This is the Kullmann
most basic set operation, and the equivalent for dynamic sets is Dynamic
sets
the operation
Simple
implementa-
SEARCH(S, k) for key k and dynamic set S, returning tion
Implementatio
We require the ability to extract the key from an object in S, Queues
and to compare keys for equality. Implementatio
Tutorial
1 Storing S via an array (or a list), SEARCH can be
performed in O(|S|) time (that is, linear time) by simple
sequential search.
2 To do faster than this, typically in time O(log(|S|))
(logarithmic time), under various circumstances, is a major
aim of data structures for dynamic sets.
CS 270
Dynamic sets: modifying operations Algorithms
Oliver
With the following operations we can build and change dynamic Kullmann
sets: Dynamic
sets
Special
DELETE(S, x) deletes an object from dynamic set S, cases
Implementatio
Note that the “x” in DELETE is of a different nature than the Queues
“x” in INSERT: It is a pointer (iterator!) into the (dynamic) Implementatio
set (and thus these two x are of different type). Tutorial
Oliver
Kullmann
Often it is assumed that a linear order is given on the keys:
Dynamic
So besides “k == k ′ ?” sets
Simple
we now can ask “k ≤ k ′ ?”. implementa-
tion
Special
In practice using strict orders “<” is more common, however cases
this creates some (necessary) technical complications, which in Stacks
this module we won’t be much concerned about (we discuss Implementatio
Implementatio
(These complications have to do with the notion of “equality”, since
Tutorial
“<” includes “not equal”. Considering Java, recall that there (lacking
operator overloading and lacking the ability to distinguish between
“object” and “pointer”) you have two operations for checking
“equality”: “= =” for object-identity and “.equals()” for
object-equality, and now we needed appropriate object-equality
(consistent with the algorithms).)
CS 270
Dynamic sets: four further operations Algorithms
Oliver
Kullmann
Given a linear order on the objects (to be put into the set), we
Dynamic
have the following additional operations: sets
Simple
MINIMUM(S) returns a pointer (iterator) to the element implementa-
tion
with the smallest key Special
cases
MAXIMUM(S) returns a pointer (iterator) to the element
Stacks
with the largest key Implementatio
SUCCESSOR(S, x), where x is a pointer (iterator) into Queues
S, returns the next element in S w.r.t. the order on the keys Implementatio
Oliver
Dynamic sets can be realised using sorting, where we have to Kullmann
Implementatio
while each of MINIMUM, MAXIMUM,
Tutorial
SUCCESSOR, PREDECESSOR takes constant time.
Oliver
Now INSERTION and DELETION take time Kullmann
Simple
elements appropriately, implementa-
tion
while the five other (non-modifying) operations still take
Special
logarithmic resp. constant time. cases
Stacks
deletion is not acceptable. And we also see that most of the Queues
Oliver
Often not all of the operations for dynamic sets are needed, Kullmann
Simple
implementa-
Buffer Only INSERTION, tion
Oliver
Kullmann
Dynamic
In other words: sets
Simple
implementa-
Buffers (stacks, queues, and priority queues) have the tion
but they have no order on the elements (and thus also no Implementatio
Oliver
Kullmann
Dynamic
sets
Simple
A stack is a buffer where the selected element implementa-
tion
is the element last entered.
Special
cases
Stacks
Queues
“last in – first out” LIFO.
Implementatio
One could also say “first in – last out” (FILO). Tutorial
Oliver
Kullmann
Simple
And the DELETE operation (which here deletes a implementa-
tion
selected element) is called POP.
Special
The operation for returning the selected element is called cases
Stacks
TOP.
Implementatio
Queues
In the book the operation POP combines the above POP (just
Implementatio
deleting the selected element) and the above TOP (just
Tutorial
returning the selected element), which is the old style for a
stack, which has been shown to have severe disadvantages:
Oliver
Kullmann
If we have a stack, and perform (in this order)
Dynamic
sets
PUSH(1), PUSH(2), PUSH(3), Simple
implementa-
tion
then TOP yields 3. Special
cases
1 After POP then TOP yields 2. Stacks
Queues
3 A final POP yields an empty stack. Implementatio
Tutorial
Note that we have precisely as many PUSH as POP
instructions (that holds for every buffer).
Oliver
Kullmann
Dynamic
In order to check whether the stack is empty, the fourth sets
Special
It depends on the concrete stack-implementation (i.e., on cases
Stacks
the interface) what happens in case of an error.
Implementatio
There are two principle error possibilities: stack overflow Queues
and stack underflow. Implementatio
stack-capacity.
Underflow means the use of a top- or pop-operation on an
empty stack.
CS 270
Implementation via an array Algorithms
Oliver
Kullmann
Dynamic
sets
Using an array we can easily implement the ADT Stack: Simple
implementa-
tion
1 The class (Java or, say, C++) contains as data member
Special
(“instance variable”) a fixed-size array. cases
Implementatio
3 An index-variable points to the currently open slot in the
Queues
array. Implementatio
4 Popping an element from the stack just means Tutorial
Oliver
Kullmann
c l a s s Stack {
Dynamic
private f i n a l int [ ] stack ; sets
Special
cases
public Stack ( f i n a l i n t N ) { Stacks
// S t a n d a r d e x c e p t i o n s r a i s e d i f N < 0 Implementatio
// o r N i s t o o b i g f o r a v a i l a b l e memory . Queues
N = N ; Implementatio
s t a c k = new i n t [ N ] ; Tutorial
n = 0;
}
Oliver
p u b l i c boolean empty ( ) { r e t u r n n==0; } Kullmann
Dynamic
sets
public i n t top ( ) {
Simple
// S t a n d a r d e x c e p t i o n r a i s e d i f n == 0 . implementa-
tion
a s s e r t ( s t a c k != n u l l && s t a c k . l e n g t h==N) ;
Special
a s s e r t ( n >= 0 && n <= N) ; cases
} Implementatio
Queues
Remark: assert is here used for conditions which are enforced Implementatio
by the internal logic of the class (and thus a violation would Tutorial
Stacks
p u b l i c boolean pop ( ) {
Implementatio
a s s e r t ( n >= 0 ) ;
Queues
a s s e r t ( n <= N) ;
Implementatio
i f ( n == 0 ) r e t u r n f a l s e ; Tutorial
−−n ;
return true ;
}
Remarks: push and pop don’t throw (exceptions). Both
operations can fail (if we have already N elements, or there is no
element left), in which case they return false .
CS 270
Class Stack (cont.) Algorithms
Oliver
Kullmann
// a d d i t i o n a l f u n c t i o n a l i t y :
public int s i z e () { return n ; } Dynamic
sets
public i n t max size ( ) { return N; } Simple
implementa-
tion
p u b l i c boolean e q u a l s ( S t a c k S ) { Special
i f ( n != S . n ) r e t u r n f a l s e ; cases
i f ( s t a c k [ i ] != S . s t a c k [ i ] ) Implementatio
Queues
return f a l s e ;
Implementatio
return true ;
Tutorial
}
Oliver
Kullmann
Implementatio
} Tutorial
Oliver
Kullmann
Dynamic
sets
Special
cases
Stacks
Thus a queue is characterised by the slogan Implementatio
“first in – first out” FIFO. Queues
One could also say “last in – last out” (“LILO”), but that Implementatio
Oliver
Kullmann
Dynamic
sets
Tutorial
However our choice is more popular with programming
languages.
CS 270
Examples Algorithms
Oliver
Kullmann
Dynamic
sets
If we have a queue, and perform (in this order)
Simple
implementa-
tion
PUSH(1), PUSH(2), PUSH(3),
Special
cases
then FRONT yields 1. Stacks
Implementatio
1 After POP then FRONT yields 2.
Queues
2 And after another POP then FRONT yields 3. Implementatio
Oliver
Kullmann
Using an array we can (relatively) easily implement the ADT
Queue: Dynamic
sets
1 The class (Java or, say, C++) contains as data member Simple
implementa-
(instance variable) a fixed-size array. tion
Special
2 We have furthermore index-variables for the left (“head”) cases
Implementatio
3 PUSH moves forward the right end, POP the left end.
Queues
4 We do not need to re-allocate the elements, when we reach Implementatio
the right boundary of the array while actually not the whole Tutorial
array is filled, since we can “wrap around” the part of the
array used for the queue.
5 For a real implementation, one needs to get the details
right, but the basic idea is very simple.
Oliver
Kullmann
c l a s s Queue { Dynamic
p r i v a t e f i n a l i n t [ ] queue ; sets
Implementatio
p u b l i c Queue ( f i n a l i n t N ) { Queues
// S t a n d a r d e x c e p t i o n s r a i s e d i f N < 0 o r Implementatio
N i s t o o b i g f o r a v a i l a b l e memory . Tutorial
N = N ;
queue = new i n t [ N ] ;
n = a = b = 0;
}
CS 270
Class Queue (cont.) Algorithms
Oliver
Kullmann
Dynamic
sets
} Tutorial
CS 270
Class Queue (cont.) Algorithms
Oliver
Kullmann
Dynamic
p u b l i c boolean push ( f i n a l i n t x ) { sets
Simple
a s s e r t ( n >= 0 && n <= N) ; implementa-
tion
i f ( n == N) r e t u r n f a l s e ;
Special
a s s e r t ( queue != n u l l && queue . l e n g t h==N) ; cases
a s s e r t ( b >= 0 && b < N) ; Stacks
queue [ b ] = x ; Implementatio
++n ; Implementatio
}
CS 270
Class Queue (cont.) Algorithms
Oliver
Kullmann
Dynamic
sets
i f ( n == 0 ) r e t u r n f a l s e ; Special
cases
a s s e r t ( a >= 0 && a < N) ; Stacks
i f ( a == N−1) a = 0 ; e l s e ++a ; Implementatio
−−n ; Queues
return true ; Implementatio
} Tutorial
CS 270
Class Queue (cont.) Algorithms
Oliver
// a d d i t i o n a l f u n c t i o n a l i t y : Kullmann
Simple
implementa-
p u b l i c boolean e q u a l s ( Queue S ) { tion
Special
i f ( n != S . n ) r e t u r n f a l s e ; cases
f o r ( i n t i=a , j=S . a , c =0; Stacks
c<n ; Implementatio
++c ) Implementatio
return f a l s e ;
return true ;
}
Oliver
Kullmann
Dynamic
public String toString () { sets
Simple
S t r i n g o u t = ” [ ” + n + ” , ” + N + ” ] \ n” ; implementa-
tion
for ( int i = a , c = 0;
Special
c < n; cases
i = ( i==N−1) ? 0 : i +1, ++c ) Stacks
o u t += queue [ i ] + ” ” ; Implementatio
} Implementatio
Tutorial
}
CS 270
Pushing and popping Algorithms
Oliver
Kullmann
Dynamic
From [Sedgewick, Exercise 4.6]: sets
Simple
A letter means push and an asterisk means pop in the following implementa-
tion
sequence. Give the sequence of values returned by the pop Special
operations when this sequence of operations is performed on an cases
Implementatio
Queues
EAS*Y*QUE***ST***IO*N***
Implementatio
Tutorial
Solution:
SYEUQTSAONIE
CS 270
Pushing and popping Algorithms
Oliver
Kullmann
Dynamic
From [Sedgewick, Exercise 4.6]: sets
Simple
A letter means push and an asterisk means pop in the following implementa-
tion
sequence. Give the sequence of values returned by the pop Special
operations when this sequence of operations is performed on an cases
Implementatio
Queues
EAS*Y*QUE***ST***IO*N***
Implementatio
Tutorial
Solution:
SYEUQTSAONIE
CS 270
Checking balancedness Algorithms
Oliver
Develop the idea for a program that reads in a sequence of Kullmann
Simple
For example implementa-
tion
Special
( [ { ( [ ] ) ( [ ] ) } ] ) cases
Stacks
Queues
( ( [ { } { } [ ] ) ) ) Implementatio
Tutorial
is not.
Solution:
When you read an opening bracket-symbol, push it onto the
stack, when you read a closing bracket-symbol, check whether
it’s the top-symbol, and pop it.
CS 270
Checking balancedness Algorithms
Oliver
Develop the idea for a program that reads in a sequence of Kullmann
Simple
For example implementa-
tion
Special
( [ { ( [ ] ) ( [ ] ) } ] ) cases
Stacks
Queues
( ( [ { } { } [ ] ) ) ) Implementatio
Tutorial
is not.
Solution:
When you read an opening bracket-symbol, push it onto the
stack, when you read a closing bracket-symbol, check whether
it’s the top-symbol, and pop it.
CS 270
Pushing and popping again Algorithms
Oliver
Kullmann
Dynamic
A letter means enqueue and an asterisk means dequeue in the sets
sequence Simple
implementa-
tion
EAS*Y*QUE***ST***IO*N*** Special
cases
Stacks
Tutorial
Solution:
EASYQUESTION
CS 270
Pushing and popping again Algorithms
Oliver
Kullmann
Dynamic
A letter means enqueue and an asterisk means dequeue in the sets
sequence Simple
implementa-
tion
EAS*Y*QUE***ST***IO*N*** Special
cases
Stacks
Tutorial
Solution:
EASYQUESTION
CS 270
A queue via two stacks Algorithms
Oliver
Kullmann
Dynamic
Can we implement a queue using two stacks sets
Special
Solution: the basic idea is − · − = + cases
Stacks
1 Use two stacks, IN and OUT. Implementatio
2 The queue is empty iff both stacks are empty. Queues
Tutorial
4 front (): if OUT is not-empty, perform OUT.top(),
otherwise pop everything from IN, push it to OUT, and
then perform OUT.top().
5 pop(): The same as with front , only replacing OUT.top()
with OUT.pop().
CS 270
A queue via two stacks Algorithms
Oliver
Kullmann
Dynamic
Can we implement a queue using two stacks sets
Special
Solution: the basic idea is − · − = + cases
Stacks
1 Use two stacks, IN and OUT. Implementatio
2 The queue is empty iff both stacks are empty. Queues
Tutorial
4 front (): if OUT is not-empty, perform OUT.top(),
otherwise pop everything from IN, push it to OUT, and
then perform OUT.top().
5 pop(): The same as with front , only replacing OUT.top()
with OUT.pop().