CS 270 CS 270 General remarks Algorithms Algorithms Week 6 Oliver Oliver Kullmann Kullmann Dynamic Dynamic sets sets Data structures Simple Simple We start considering Part III “Data Structures” from implementa- implementa- tion tion CLRS . Dynamic sets 1 Special Special cases cases As a first example we consider two special cases of Simple implementation 2 Stacks Stacks “buffers”, namely stacks and queues . Implementatio Implementatio Special cases 3 Queues Queues Reading from CLRS for week 5 Implementatio Implementatio Stacks 4 The introduction to Part III on using sets on a computer. Tutorial Tutorial Implementation Chapter 10, Section 10.1. 5 Queues 6 Implementation 7 Tutorial 8 CS 270 CS 270 Sets Elements of a dynamic sets Algorithms Algorithms The most fundamental mathematical notion is that of a set: A “dynamic set” might contain Oliver Oliver Kullmann Kullmann We have the possibility to determine the elements of a set. pointers (or iterators) to objects, Dynamic Dynamic sets or the objects themselves (in Java this can be only integers sets And we can form sets, either by some set-defining property, Simple Simple or by using already given sets (e.g., unions, intersections, and other primitive types, in C++ this is possible for every implementa- implementa- tion type of object). tion differences). Special Special Whatever the objects in a set are, access to them (especially for cases cases Now to bring the eternal and infinite world of mathematics to a Stacks Stacks changing them) is only possible via a pointer (or iterator). computer, we need to take care of Implementatio Implementatio construction of “objects” For insertion into a dynamic set, we must be given the Queues Queues destruction of “objects” object itself, and typically we obtain the pointer (iterator, Implementatio Implementatio naming (basically of functions) handle) to the copy of that object in the dynamic set back. Tutorial Tutorial order issues (sets are unordered, but in computation there is For deletion from a dynamic set, we typically have the always order). pointer of an object (already) in the dynamic set, and we want to delete that (very specific) object. For this, CLRS uses the (generic) ADT of “dynamic sets”. For searching, we typically have only given some “key ADT: “abstract data type” — information”, and we want to search for some element in the values (like “sets”) and how to operate with them. dynamic set, which fits this (key) information.
CS 270 CS 270 Keys Dynamic sets: elementship and search Algorithms Algorithms Oliver Oliver Kullmann 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 Dynamic sets sets Besides objects (which become “elements” once they are in the the operation Simple Simple set) and pointers, CLRS uses the notion of a key to identify an implementa- implementa- SEARCH ( S , k ) for key k and dynamic set S , returning tion tion object: either a pointer (iterator) to an object in S with key k , or Special Special cases cases If the object is for example a record of personal attributes, NIL if there is no such object. Stacks Stacks then the name or some form of ID can be used as a key. Implementatio Implementatio We require the ability to extract the key from an object in S , Queues Queues and to compare keys for equality. Often they keys are used for sorting. Implementatio Implementatio Tutorial 1 Storing S via an array (or a list), SEARCH can be Tutorial For example for that database of personal attributes, performed in O ( | S | ) time (that is, linear time ) by simple we might sort it according to alphabetical sorting of names. 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 CS 270 Dynamic sets: modifying operations Dynamic sets: using order Algorithms Algorithms Oliver Oliver With the following operations we can build and change dynamic Kullmann Kullmann Often it is assumed that a linear order is given on the keys: sets: Dynamic Dynamic So besides “ k == k ′ ?” sets sets INSERT ( S , x ) inserts an object into dynamic set S , where Simple we now can ask “ k ≤ k ′ ?”. Simple implementa- implementa- x is either a pointer or the object itself. tion tion Special In practice using strict orders “ < ” is more common, however Special DELETE ( S , x ) deletes an object from dynamic set S , cases cases this creates some (necessary) technical complications, which in where here x is a pointer (iterator) into S . Stacks Stacks this module we won’t be much concerned about (we discuss Implementatio Implementatio Note that the “ x ” in DELETE is of a different nature than the issues when the need arises). Queues Queues “ x ” in INSERT : It is a pointer (iterator!) into the (dynamic) Implementatio Implementatio (These complications have to do with the notion of “equality”, since set (and thus these two x are of different type). Tutorial Tutorial “ < ” includes “not equal”. Considering Java, recall that there (lacking The most important application of INSERT is for creating a operator overloading and lacking the ability to distinguish between dynamic set: “object” and “pointer”) you have two operations for checking “equality”: “ == ” for object-identity and “ .equals() ” for To create a set S of size n , call INSERT ( S , − ) n -times. object-equality, and now we needed appropriate object-equality ( consistent with the algorithms ).) We always have INSERT , while we might not have DELETE .
CS 270 CS 270 Dynamic sets: four further operations Using sorting algorithms: static case Algorithms Algorithms Oliver Oliver Dynamic sets can be realised using sorting, where we have to Kullmann Kullmann Given a linear order on the objects (to be put into the set), we assume a linear order on the keys. Dynamic Dynamic have the following additional operations: sets sets If the set S with n elements is to be built only once, at the Simple Simple MINIMUM ( S ) returns a pointer (iterator) to the element implementa- beginning, from a sequence of elements, then storing the implementa- tion tion with the smallest key elements in an array and sorting them, using for example Special Special cases MERGE-SORT with time complexity O ( n · log n ), is a good cases MAXIMUM ( S ) returns a pointer (iterator) to the element Stacks Stacks option: with the largest key Implementatio Implementatio SUCCESSOR ( S , x ), where x is a pointer (iterator) into SEARCH then takes time O (log n ) (using binary search) Queues Queues S , returns the next element in S w.r.t. the order on the keys Implementatio Implementatio while each of MINIMUM , MAXIMUM , PREDECESSOR ( S , x ), where x is a pointer (iterator) Tutorial Tutorial SUCCESSOR , PREDECESSOR takes constant time. into S , returns the previous element in S . However we are concerned here with the dynamic case, where Operations for computing successors and predecessors can fail insertions and deletions are used in unpredictable ways. If we (there is no successor resp. predecessor iff we are already at the not assume that building the set is done (once and for all) at the end resp. beginning), and in such cases we return NIL . beginning, but we want to have insertion and deletion, then the case is much less favourable. CS 270 CS 270 Using sorting algorithms: dynamic case Special cases of dynamic sets Algorithms Algorithms Oliver Oliver Often not all of the operations for dynamic sets are needed, Now INSERTION and DELETION take time Kullmann Kullmann opening up the possibilities of specialised and more efficient O (log( n ) + n ) = O ( n ), searching first for the right Dynamic Dynamic implementations. Three important cases are as follows: insertion/deletion place, and then shifting the other sets sets elements appropriately, Simple Simple implementa- implementa- Buffer Only INSERTION , tion tion while the five other (non-modifying) operations still take SHOW-SELECTED-ELEMENT (like Special Special logarithmic resp. constant time. cases cases MINIMUM and MAXIMUM , but not Stacks Stacks necessarily related to some order) and For practical applications, the linear complexity of insertion and Implementatio Implementatio DELETE-SELECTED-ELEMENT ; special deletion is not acceptable. And we also see that most of the Queues Queues cases are “stacks” and “queues”. intelligence of sophisticated searching algorithms is blown out of Implementatio Implementatio Priority queue Only INSERTION , MINIMUM resp. the window, and only some form of INSERTION-SORT (in Tutorial Tutorial MAXIMUM (for min- resp. max-priority queues) combination with binary search) survived. and DELETE-MIN resp. DELETE-MAX It could be said that data structures for dynamic sets try to (special queues, where the selected element is introduce some of the intelligent methods into the dynamic given by a linear order on the keys). framework, making insertion and deletion more efficient Dictionary Only INSERTION , DELETION and (necessarily at the cost of making the other operations SEARCH (i.e., no order-requirements). (somewhat) less efficient).
Recommend
More recommend