week 6
play

Week 6 Oliver Kullmann Dynamic sets Data structures Simple - PowerPoint PPT Presentation

CS 270 Algorithms Week 6 Oliver Kullmann Dynamic sets Data structures Simple implementa- tion Dynamic sets 1 Special cases Simple implementation 2 Stacks Implementatio Special cases 3 Queues Implementatio Stacks 4 Tutorial


  1. CS 270 Algorithms Week 6 Oliver Kullmann Dynamic sets Data structures Simple implementa- tion Dynamic sets 1 Special cases Simple implementation 2 Stacks Implementatio Special cases 3 Queues Implementatio Stacks 4 Tutorial Implementation 5 Queues 6 Implementation 7 Tutorial 8

  2. 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 The introduction to Part III on using sets on a computer. Tutorial Chapter 10, Section 10.1.

  3. CS 270 Sets Algorithms The most fundamental mathematical notion is that of a set: Oliver Kullmann We have the possibility to determine the elements of a set. Dynamic And we can form sets, either by some set-defining property, sets Simple or by using already given sets (e.g., unions, intersections, implementa- tion differences). Special cases Now to bring the eternal and infinite world of mathematics to a Stacks computer, we need to take care of Implementatio construction of “objects” Queues destruction of “objects” Implementatio naming (basically of functions) Tutorial order issues (sets are unordered, but in computation there is always order). For this, CLRS uses the (generic) ADT of “dynamic sets”. ADT: “abstract data type” — values (like “sets”) and how to operate with them.

  4. CS 270 Elements of a dynamic sets Algorithms A “dynamic set” might contain Oliver Kullmann pointers (or iterators) to objects, Dynamic or the objects themselves (in Java this can be only integers sets Simple and other primitive types, in C++ this is possible for every implementa- type of object). tion 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 For insertion into a dynamic set, we must be given the Queues object itself, and typically we obtain the pointer (iterator, Implementatio handle) to the copy of that object in the dynamic set back. Tutorial For deletion from a dynamic set, we typically have the pointer of an object (already) in the dynamic set, and we want to delete that (very specific) object. For searching, we typically have only given some “key information”, and we want to search for some element in the dynamic set, which fits this (key) information.

  5. CS 270 Keys Algorithms 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 Often they keys are used for sorting. Implementatio Tutorial For example for that database of personal attributes, we might sort it according to alphabetical sorting of names.

  6. CS 270 Dynamic sets: elementship and search Algorithms 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 either a pointer (iterator) to an object in S with key k , or Special cases NIL if there is no such object. Stacks Implementatio We require the ability to extract the key from an object in S , Queues and to compare keys for equality. Implementatio 1 Storing S via an array (or a list), SEARCH can be Tutorial 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.

  7. CS 270 Dynamic sets: modifying operations Algorithms Oliver With the following operations we can build and change dynamic Kullmann sets: Dynamic sets INSERT ( S , x ) inserts an object into dynamic set S , where Simple implementa- x is either a pointer or the object itself. tion Special DELETE ( S , x ) deletes an object from dynamic set S , cases where here x is a pointer (iterator) into S . Stacks 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 The most important application of INSERT is for creating a dynamic set: To create a set S of size n , call INSERT ( S , − ) n -times. We always have INSERT , while we might not have DELETE .

  8. CS 270 Dynamic sets: using order Algorithms Oliver Kullmann Often it is assumed that a linear order is given on the keys: Dynamic So besides “ k == k ′ ?” sets we now can ask “ k ≤ k ′ ?”. Simple implementa- tion In practice using strict orders “ < ” is more common, however Special cases this creates some (necessary) technical complications, which in Stacks this module we won’t be much concerned about (we discuss Implementatio issues when the need arises). Queues 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 ).)

  9. 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 PREDECESSOR ( S , x ), where x is a pointer (iterator) Tutorial into S , returns the previous element in S . Operations for computing successors and predecessors can fail (there is no successor resp. predecessor iff we are already at the end resp. beginning), and in such cases we return NIL .

  10. CS 270 Using sorting algorithms: static case Algorithms Oliver Dynamic sets can be realised using sorting, where we have to Kullmann assume a linear order on the keys. Dynamic sets If the set S with n elements is to be built only once, at the Simple beginning, from a sequence of elements, then storing the implementa- tion elements in an array and sorting them, using for example Special MERGE-SORT with time complexity O ( n · log n ), is a good cases Stacks option: Implementatio SEARCH then takes time O (log n ) (using binary search) Queues Implementatio while each of MINIMUM , MAXIMUM , Tutorial SUCCESSOR , PREDECESSOR takes constant time. However we are concerned here with the dynamic case, where insertions and deletions are used in unpredictable ways. If we not assume that building the set is done (once and for all) at the beginning, but we want to have insertion and deletion, then the case is much less favourable.

  11. CS 270 Using sorting algorithms: dynamic case Algorithms Oliver Now INSERTION and DELETION take time Kullmann O (log( n ) + n ) = O ( n ), searching first for the right Dynamic insertion/deletion place, and then shifting the other sets Simple elements appropriately, implementa- tion while the five other (non-modifying) operations still take Special logarithmic resp. constant time. cases Stacks For practical applications, the linear complexity of insertion and Implementatio deletion is not acceptable. And we also see that most of the Queues intelligence of sophisticated searching algorithms is blown out of Implementatio the window, and only some form of INSERTION-SORT (in Tutorial combination with binary search) survived. It could be said that data structures for dynamic sets try to introduce some of the intelligent methods into the dynamic framework, making insertion and deletion more efficient (necessarily at the cost of making the other operations (somewhat) less efficient).

  12. CS 270 Special cases of dynamic sets Algorithms Oliver Often not all of the operations for dynamic sets are needed, Kullmann opening up the possibilities of specialised and more efficient Dynamic implementations. Three important cases are as follows: sets Simple implementa- Buffer Only INSERTION , tion SHOW-SELECTED-ELEMENT (like Special cases MINIMUM and MAXIMUM , but not Stacks necessarily related to some order) and Implementatio DELETE-SELECTED-ELEMENT ; special Queues cases are “stacks” and “queues”. Implementatio Priority queue Only INSERTION , MINIMUM resp. Tutorial MAXIMUM (for min- resp. max-priority queues) and DELETE-MIN resp. DELETE-MAX (special queues, where the selected element is given by a linear order on the keys). Dictionary Only INSERTION , DELETION and SEARCH (i.e., no order-requirements).

Recommend


More recommend