Chapter 21 - Standard Template Library 1 (STL)

Total Page:16

File Type:pdf, Size:1020Kb

Chapter 21 - Standard Template Library 1 (STL) Chapter 21 - Standard Template Library 1 (STL) Outline 21.1 Introduction to the Standard Template Library (STL) 21.1.1 Introduction to Containers 21.1.2 Introduction to Iterators 21.1.3 Introduction to Algorithms 21.2 Sequence Containers 21.2.1 vector Sequence Container 21.2.2 list Sequence Container 21.2.3 deque Sequence Container 21.3 Associative Containers 21.3.1 multiset Associative Container 21.3.2 set Associative Container 21.3.3 multimap Associative Container 21.3.4 map Associative Container 21.4 Container Adapters 21.4.1 stack Adapter 21.4.2 queue Adapter 21.4.3 priority_queue Adapter 2003 Prentice Hall, Inc. All rights reserved. Chapter 21 - Standard Template Library 2 (STL) 21.5 Algorithms 21.5.1 fill, fill_n, generate and generate_n 21.5.2 equal, mismatch and lexicographical_compare 21.5.3 remove, remove_if, remove_copy and remove_copy_if 21.5.4 replace, replace_if, replace_copy and replace_copy_if 21.5.5 Mathematical Algorithms 21.5.6 Basic Searching and Sorting Algorithms 21.5.7 swap, iter_swap and swap_ranges 21.5.8 copy_backward, merge, unique and reverse 21.5.9 inplace_merge, unique_copy and reverse_copy 21.5.10 Set Operations 21.5.11 lower_bound, upper_bound and equal_range 21.5.12 Heapsort 21.5.13 min and max 21.5.14 Algorithms Not Covered in This Chapter 21.6 Class bitset 21.7 Function Objects 2003 Prentice Hall, Inc. All rights reserved. 3 21.1 Introduction to the Standard Template Library (STL) • STL – Powerful, template-based components • Containers: template data structures • Iterators: like pointers, access elements of containers • Algorithms: data manipulation, searching, sorting, etc. – Object- oriented programming: reuse, reuse, reuse – Only an introduction to STL, a huge class library 2003 Prentice Hall, Inc. All rights reserved. 4 21.1.1 Introduction to Containers • Three types of containers – Sequence containers • Linear data structures (vectors, linked lists) • First-class container – Associative containers • Non-linear, can find elements quickly • Key/value pairs • First-class container – Container adapters • Near containers – Similar to containers, with reduced functionality • Containers have some common functions 2003 Prentice Hall, Inc. All rights reserved. 5 STL Container Classes (Fig. 21.1) • Sequence containers – vector – deque – list • Associative containers – set – multiset – map – multimap • Container adapters – stack – queue – priority_queue 2003 Prentice Hall, Inc. All rights reserved. 6 Common STL Member Functions (Fig. 21.2) • Member functions for all containers – Default constructor, copy constructor, destructor – empty – max_size, size – = < <= > >= == != – swap • Functions for first-class containers – begin, end – rbegin, rend – erase, clear 2003 Prentice Hall, Inc. All rights reserved. 7 Common STL typedefs (Fig. 21.4) • typedefs for first-class containers – value_type – reference – const_reference – pointer – iterator – const_iterator – reverse_iterator – const_reverse_iterator – difference_type – size_type 2003 Prentice Hall, Inc. All rights reserved. 8 21.1.2 Introduction to Iterators • Iterators similar to pointers – Point to first element in a container – Iterator operators same for all containers • * dereferences • ++ points to next element • begin() returns iterator to first element • end() returns iterator to last element – Use iterators with sequences (ranges) • Containers • Input sequences: istream_iterator • Output sequences: ostream_iterator 2003 Prentice Hall, Inc. All rights reserved. 9 21.1.2 Introduction to Iterators • Usage – std::istream_iterator< int > inputInt( cin ) • Can read input from cin • *inputInt – Dereference to read first int from cin • ++inputInt – Go to next int in stream – std::ostream_iterator< int > outputInt(cout) • Can output ints to cout • *outputInt = 7 – Outputs 7 to cout • ++outputInt – Advances iterator so we can output next int 2003 Prentice Hall, Inc. All rights reserved. 10 1 // Fig. 21.5: fig21_05.cpp Outline 2 // Demonstrating input and output with iterators. 3 #include <iostream> 4 fig21_05.cpp 5 using std::cout; (1 of 2) 6 using std::cin; 7 using std::endl; 8 Note creation of 9 #include <iterator> // ostream_iteratoristream_iterator and istream_iterator. For 10 compilation reasons, we use 11 int main() std:: rather than a using 12 { statement. 13 cout << "Enter two integers: "; Access and assign the iterator 14 like a pointer. 15 // create istream_iterator for reading int values from cin 16 std::istream_iterator< int > inputInt( cin ); 17 18 int number1 = *inputInt; // read int from standard input 19 ++inputInt; // move iterator to next input value 20 int number2 = *inputInt; // read int from standard input 21 2003 Prentice Hall, Inc. All rights reserved. 11 22 // create ostream_iterator for writing int values to cout Outline 23 std::ostream_iterator< int > outputInt( cout ); 24 25 cout << "The sum is: "; fig21_05.cpp 26 *outputInt = number1 + number2; // output result to cout (2 of 2) 27 cout << endl; 28 fig21_05.cpp 29 return 0; output (1 of 1) 30 31 } // end main Enter two integers: 12 25 The sum is: 37 Create an ostream_iterator is similar. Assigning to this iterator outputs to cout. 2003 Prentice Hall, Inc. All rights reserved. 12 Iterator Categories (Fig. 21.6) • Input – Read elements from container, can only move forward • Output – Write elements to container, only forward • Forward – Combines input and output, retains position – Multi-pass (can pass through sequence twice) • Bidirectional – Like forward, but can move backwards as well • Random access – Like bidirectional, but can also jump to any element 2003 Prentice Hall, Inc. All rights reserved. 13 Iterator Types Supported (Fig. 21.8) • Sequence containers – vector: random access – deque: random access – list: bidirectional • Associative containers (all bidirectional) – set – multiset – Map – multimap • Container adapters (no iterators supported) – stack – queue – priority_queue 2003 Prentice Hall, Inc. All rights reserved. 14 Iterator Operations (Fig. 21.10) • All – ++p, p++ • Input iterators – *p – p = p1 – p == p1, p != p1 • Output iterators – *p – p = p1 • Forward iterators – Have functionality of input and output iterators 2003 Prentice Hall, Inc. All rights reserved. 15 Iterator Operations (Fig. 21.10) • Bidirectional – --p, p-- • Random access – p + i, p += i – p - i, p -= i – p[i] – p < p1, p <= p1 – p > p1, p >= p1 2003 Prentice Hall, Inc. All rights reserved. 16 21.1.3 Introduction to Algorithms • STL has algorithms used generically across containers – Operate on elements indirectly via iterators – Often operate on sequences of elements • Defined by pairs of iterators • First and last element – Algorithms often return iterators • find() • Returns iterator to element, or end() if not found – Premade algorithms save programmers time and effort 2003 Prentice Hall, Inc. All rights reserved. 17 21.2 Sequence Containers • Three sequence containers – vector - based on arrays – deque - based on arrays – list - robust linked list 2003 Prentice Hall, Inc. All rights reserved. 18 21.2.1 vector Sequence Container • vector – <vector> – Data structure with contiguous memory locations • Access elements with [] – Use when data must be sorted and easily accessible • When memory exhausted – Allocates larger, contiguous area of memory – Copies itself there – Deallocates old memory • Has random access iterators 2003 Prentice Hall, Inc. All rights reserved. 19 21.2.1 vector Sequence Container • Declarations – std::vector <type> v; • type: int, float, etc. • Iterators – std::vector<type>::const_iterator iterVar; • const_iterator cannot modify elements – std::vector<type>::reverse_iterator iterVar; • Visits elements in reverse order (end to beginning) • Use rbegin to get starting point • Use rend to get ending point 2003 Prentice Hall, Inc. All rights reserved. 20 21.2.1 vector Sequence Container • vector functions – v.push_back(value) • Add element to end (found in all sequence containers). – v.size() • Current size of vector – v.capacity() • How much vector can hold before reallocating memory • Reallocation doubles size – vector<type> v(a, a + SIZE) • Creates vector v with elements from array a up to (not including) a + SIZE 2003 Prentice Hall, Inc. All rights reserved. 21 21.2.1 vector Sequence Container • vector functions – v.insert(iterator, value ) • Inserts value before location of iterator – v.insert(iterator, array , array + SIZE) • Inserts array elements (up to, but not including array + SIZE) into vector – v.erase( iterator ) • Remove element from container – v.erase( iter1, iter2 ) • Remove elements starting from iter1 and up to (not including) iter2 – v.clear() • Erases entire container 2003 Prentice Hall, Inc. All rights reserved. 22 21.2.1 vector Sequence Container • vector functions operations – v.front(), v.back() • Return first and last element – v.[elementNumber] = value; • Assign value to an element – v.at[elementNumber] = value; • As above, with range checking • out_of_bounds exception 2003 Prentice Hall, Inc. All rights reserved. 23 21.2.1 vector Sequence Container • ostream_iterator – std::ostream_iterator< type > Name ( outputStream, separator ); • type: outputs values of a certain type • outputStream: iterator output location • separator: character separating outputs • Example – std::ostream_iterator< int > output( cout, " " ); – std::copy( iterator1, iterator2, output ); • Copies elements from iterator1 up to (not including) iterator2 to output, an ostream_iterator 2003 Prentice Hall, Inc. All rights reserved. 24 1 // Fig.
Recommended publications
  • An Alternative to Fibonacci Heaps with Worst Case Rather Than Amortized Time Bounds∗
    Relaxed Fibonacci heaps: An alternative to Fibonacci heaps with worst case rather than amortized time bounds¤ Chandrasekhar Boyapati C. Pandu Rangan Department of Computer Science and Engineering Indian Institute of Technology, Madras 600036, India Email: [email protected] November 1995 Abstract We present a new data structure called relaxed Fibonacci heaps for implementing priority queues on a RAM. Relaxed Fibonacci heaps support the operations find minimum, insert, decrease key and meld, each in O(1) worst case time and delete and delete min in O(log n) worst case time. Introduction The implementation of priority queues is a classical problem in data structures. Priority queues find applications in a variety of network problems like single source shortest paths, all pairs shortest paths, minimum spanning tree, weighted bipartite matching etc. [1] [2] [3] [4] [5] In the amortized sense, the best performance is achieved by the well known Fibonacci heaps. They support delete and delete min in amortized O(log n) time and find min, insert, decrease key and meld in amortized constant time. Fast meldable priority queues described in [1] achieve all the above time bounds in worst case rather than amortized time, except for the decrease key operation which takes O(log n) worst case time. On the other hand, relaxed heaps described in [2] achieve in the worst case all the time bounds of the Fi- bonacci heaps except for the meld operation, which takes O(log n) worst case ¤Please see Errata at the end of the paper. 1 time. The problem that was posed in [1] was to consider if it is possible to support both decrease key and meld simultaneously in constant worst case time.
    [Show full text]
  • Lecture 04 Linear Structures Sort
    Algorithmics (6EAP) MTAT.03.238 Linear structures, sorting, searching, etc Jaak Vilo 2018 Fall Jaak Vilo 1 Big-Oh notation classes Class Informal Intuition Analogy f(n) ∈ ο ( g(n) ) f is dominated by g Strictly below < f(n) ∈ O( g(n) ) Bounded from above Upper bound ≤ f(n) ∈ Θ( g(n) ) Bounded from “equal to” = above and below f(n) ∈ Ω( g(n) ) Bounded from below Lower bound ≥ f(n) ∈ ω( g(n) ) f dominates g Strictly above > Conclusions • Algorithm complexity deals with the behavior in the long-term – worst case -- typical – average case -- quite hard – best case -- bogus, cheating • In practice, long-term sometimes not necessary – E.g. for sorting 20 elements, you dont need fancy algorithms… Linear, sequential, ordered, list … Memory, disk, tape etc – is an ordered sequentially addressed media. Physical ordered list ~ array • Memory /address/ – Garbage collection • Files (character/byte list/lines in text file,…) • Disk – Disk fragmentation Linear data structures: Arrays • Array • Hashed array tree • Bidirectional map • Heightmap • Bit array • Lookup table • Bit field • Matrix • Bitboard • Parallel array • Bitmap • Sorted array • Circular buffer • Sparse array • Control table • Sparse matrix • Image • Iliffe vector • Dynamic array • Variable-length array • Gap buffer Linear data structures: Lists • Doubly linked list • Array list • Xor linked list • Linked list • Zipper • Self-organizing list • Doubly connected edge • Skip list list • Unrolled linked list • Difference list • VList Lists: Array 0 1 size MAX_SIZE-1 3 6 7 5 2 L = int[MAX_SIZE]
    [Show full text]
  • Priority Queues and Binary Heaps Chapter 6.5
    Priority Queues and Binary Heaps Chapter 6.5 1 Some animals are more equal than others • A queue is a FIFO data structure • the first element in is the first element out • which of course means the last one in is the last one out • But sometimes we want to sort of have a queue but we want to order items according to some characteristic the item has. 107 - Trees 2 Priorities • We call the ordering characteristic the priority. • When we pull something from this “queue” we always get the element with the best priority (sometimes best means lowest). • It is really common in Operating Systems to use priority to schedule when something happens. e.g. • the most important process should run before a process which isn’t so important • data off disk should be retrieved for more important processes first 107 - Trees 3 Priority Queue • A priority queue always produces the element with the best priority when queried. • You can do this in many ways • keep the list sorted • or search the list for the minimum value (if like the textbook - and Unix actually - you take the smallest value to be the best) • You should be able to estimate the Big O values for implementations like this. e.g. O(n) for choosing the minimum value of an unsorted list. • There is a clever data structure which allows all operations on a priority queue to be done in O(log n). 107 - Trees 4 Binary Heap Actually binary min heap • Shape property - a complete binary tree - all levels except the last full.
    [Show full text]
  • Assignment 3: Kdtree ______Due June 4, 11:59 PM
    CS106L Handout #04 Spring 2014 May 15, 2014 Assignment 3: KDTree _________________________________________________________________________________________________________ Due June 4, 11:59 PM Over the past seven weeks, we've explored a wide array of STL container classes. You've seen the linear vector and deque, along with the associative map and set. One property common to all these containers is that they are exact. An element is either in a set or it isn't. A value either ap- pears at a particular position in a vector or it does not. For most applications, this is exactly what we want. However, in some cases we may be interested not in the question “is X in this container,” but rather “what value in the container is X most similar to?” Queries of this sort often arise in data mining, machine learning, and computational geometry. In this assignment, you will implement a special data structure called a kd-tree (short for “k-dimensional tree”) that efficiently supports this operation. At a high level, a kd-tree is a generalization of a binary search tree that stores points in k-dimen- sional space. That is, you could use a kd-tree to store a collection of points in the Cartesian plane, in three-dimensional space, etc. You could also use a kd-tree to store biometric data, for example, by representing the data as an ordered tuple, perhaps (height, weight, blood pressure, cholesterol). However, a kd-tree cannot be used to store collections of other data types, such as strings. Also note that while it's possible to build a kd-tree to hold data of any dimension, all of the data stored in a kd-tree must have the same dimension.
    [Show full text]
  • Rethinking Host Network Stack Architecture Using a Dataflow Modeling Approach
    DISS.ETH NO. 23474 Rethinking host network stack architecture using a dataflow modeling approach A thesis submitted to attain the degree of DOCTOR OF SCIENCES of ETH ZURICH (Dr. sc. ETH Zurich) presented by Pravin Shinde Master of Science, Vrije Universiteit, Amsterdam born on 15.10.1982 citizen of Republic of India accepted on the recommendation of Prof. Dr. Timothy Roscoe, examiner Prof. Dr. Gustavo Alonso, co-examiner Dr. Kornilios Kourtis, co-examiner Dr. Andrew Moore, co-examiner 2016 Abstract As the gap between the speed of networks and processor cores increases, the software alone will not be able to handle all incoming data without additional assistance from the hardware. The network interface controllers (NICs) evolve and add supporting features which could help the system increase its scalability with respect to incoming packets, provide Quality of Service (QoS) guarantees and reduce the CPU load. However, modern operating systems are ill suited to both efficiently exploit and effectively manage the hardware resources of state-of-the-art NICs. The main problem is the layered architecture of the network stack and the rigid interfaces. This dissertation argues that in order to effectively use the diverse and complex NIC hardware features, we need (i) a hardware agnostic representation of the packet processing capabilities of the NICs, and (ii) a flexible interface to share this information with different layers of the network stack. This work presents the Dataflow graph based model to capture both the hardware capabilities for packet processing of the NIC and the state of the network stack, in order to enable automated reasoning about the NIC features in a hardware-agnostic way.
    [Show full text]
  • Priorityqueue
    CSE 373 Java Collection Framework, Part 2: Priority Queue, Map slides created by Marty Stepp http://www.cs.washington.edu/373/ © University of Washington, all rights reserved. 1 Priority queue ADT • priority queue : a collection of ordered elements that provides fast access to the minimum (or maximum) element usually implemented using a tree structure called a heap • priority queue operations: add adds in order; O(log N) worst peek returns minimum value; O(1) always remove removes/returns minimum value; O(log N) worst isEmpty , clear , size , iterator O(1) always 2 Java's PriorityQueue class public class PriorityQueue< E> implements Queue< E> Method/Constructor Description Runtime PriorityQueue< E>() constructs new empty queue O(1) add( E value) adds value in sorted order O(log N ) clear() removes all elements O(1) iterator() returns iterator over elements O(1) peek() returns minimum element O(1) remove() removes/returns min element O(log N ) Queue<String> pq = new PriorityQueue <String>(); pq.add("Stuart"); pq.add("Marty"); ... 3 Priority queue ordering • For a priority queue to work, elements must have an ordering in Java, this means implementing the Comparable interface • many existing types (Integer, String, etc.) already implement this • if you store objects of your own types in a PQ, you must implement it TreeSet and TreeMap also require Comparable types public class Foo implements Comparable<Foo> { … public int compareTo(Foo other) { // Return > 0 if this object is > other // Return < 0 if this object is < other // Return 0 if this object == other } } 4 The Map ADT • map : Holds a set of unique keys and a collection of values , where each key is associated with one value.
    [Show full text]
  • Programmatic Testing of the Standard Template Library Containers
    Programmatic Testing of the Standard Template Library Containers y z Jason McDonald Daniel Ho man Paul Stro op er May 11, 1998 Abstract In 1968, McIlroy prop osed a software industry based on reusable comp onents, serv- ing roughly the same role that chips do in the hardware industry. After 30 years, McIlroy's vision is b ecoming a reality. In particular, the C++ Standard Template Library STL is an ANSI standard and is b eing shipp ed with C++ compilers. While considerable attention has b een given to techniques for developing comp onents, little is known ab out testing these comp onents. This pap er describ es an STL conformance test suite currently under development. Test suites for all of the STL containers have b een written, demonstrating the feasi- bility of thorough and highly automated testing of industrial comp onent libraries. We describ e a ordable test suites that provide go o d co de and b oundary value coverage, including the thousands of cases that naturally o ccur from combinations of b oundary values. We showhowtwo simple oracles can provide fully automated output checking for all the containers. We re ne the traditional categories of black-b ox and white-b ox testing to sp eci cation-based, implementation-based and implementation-dep endent testing, and showhow these three categories highlight the key cost/thoroughness trade- o s. 1 Intro duction Our testing fo cuses on container classes |those providing sets, queues, trees, etc.|rather than on graphical user interface classes. Our approach is based on programmatic testing where the number of inputs is typically very large and b oth the input generation and output checking are under program control.
    [Show full text]
  • Parallelization of Bulk Operations for STL Dictionaries
    Parallelization of Bulk Operations for STL Dictionaries Leonor Frias1?, Johannes Singler2 [email protected], [email protected] 1 Dep. de Llenguatges i Sistemes Inform`atics,Universitat Polit`ecnicade Catalunya 2 Institut f¨urTheoretische Informatik, Universit¨atKarlsruhe Abstract. STL dictionaries like map and set are commonly used in C++ programs. We consider parallelizing two of their bulk operations, namely the construction from many elements, and the insertion of many elements at a time. Practical algorithms are proposed for these tasks. The implementation is completely generic and engineered to provide best performance for the variety of possible input characteristics. It features transparent integration into the STL. This can make programs profit in an easy way from multi-core processing power. The performance mea- surements show the practical usefulness on real-world multi-core ma- chines with up to eight cores. 1 Introduction Multi-core processors bring parallel computing power to the customer at virtu- ally no cost. Where automatic parallelization fails and OpenMP loop paralleliza- tion primitives are not strong enough, parallelized algorithms from a library are a sensible choice. Our group pursues this goal with the Multi-Core Standard Template Library [6], a parallel implementation of the C++ STL. To allow best benefit from the parallel computing power, as many operations as possible need to be parallelized. Sequential parts could otherwise severely limit the achievable speedup, according to Amdahl’s law. Thus, it may be profitable to parallelize an operation even if the speedup is considerably less than the number of threads. The STL contains four kinds of generic dictionary types, namely set, map, multiset, and multimap.
    [Show full text]
  • Readings Findmin Problem Priority Queue
    Readings • Chapter 6 Priority Queues & Binary Heaps › Section 6.1-6.4 CSE 373 Data Structures Winter 2007 Binary Heaps 2 FindMin Problem Priority Queue ADT • Quickly find the smallest (or highest priority) • Priority Queue can efficiently do: item in a set ›FindMin() • Applications: • Returns minimum value but does not delete it › Operating system needs to schedule jobs according to priority instead of FIFO › DeleteMin( ) › Event simulation (bank customers arriving and • Returns minimum value and deletes it departing, ordered according to when the event › Insert (k) happened) • In GT Insert (k,x) where k is the key and x the value. In › Find student with highest grade, employee with all algorithms the important part is the key, a highest salary etc. “comparable” item. We’ll skip the value. › Find “most important” customer waiting in line › size() and isEmpty() Binary Heaps 3 Binary Heaps 4 List implementation of a Priority BST implementation of a Priority Queue Queue • What if we use unsorted lists: • Worst case (degenerate tree) › FindMin and DeleteMin are O(n) › FindMin, DeleteMin and Insert (k) are all O(n) • In fact you have to go through the whole list • Best case (completely balanced BST) › Insert(k) is O(1) › FindMin, DeleteMin and Insert (k) are all O(logn) • What if we used sorted lists • Balanced BSTs › FindMin and DeleteMin are O(1) › FindMin, DeleteMin and Insert (k) are all O(logn) • Be careful if we want both Min and Max (circular array or doubly linked list) › Insert(k) is O(n) Binary Heaps 5 Binary Heaps 6 1 Better than a speeding BST Binary Heaps • A binary heap is a binary tree (NOT a BST) that • Can we do better than Balanced Binary is: Search Trees? › Complete: the tree is completely filled except • Very limited requirements: Insert, possibly the bottom level, which is filled from left to FindMin, DeleteMin.
    [Show full text]
  • FORSCHUNGSZENTRUM JÜLICH Gmbh Programming in C++ Part II
    FORSCHUNGSZENTRUM JÜLICH GmbH Jülich Supercomputing Centre D-52425 Jülich, Tel. (02461) 61-6402 Ausbildung von Mathematisch-Technischen Software-Entwicklern Programming in C++ Part II Bernd Mohr FZJ-JSC-BHB-0155 1. Auflage (letzte Änderung: 19.09.2003) Copyright-Notiz °c Copyright 2008 by Forschungszentrum Jülich GmbH, Jülich Supercomputing Centre (JSC). Alle Rechte vorbehalten. Kein Teil dieses Werkes darf in irgendeiner Form ohne schriftliche Genehmigung des JSC reproduziert oder unter Verwendung elektronischer Systeme verarbeitet, vervielfältigt oder verbreitet werden. Publikationen des JSC stehen in druckbaren Formaten (PDF auf dem WWW-Server des Forschungszentrums unter der URL: <http://www.fz-juelich.de/jsc/files/docs/> zur Ver- fügung. Eine Übersicht über alle Publikationen des JSC erhalten Sie unter der URL: <http://www.fz-juelich.de/jsc/docs> . Beratung Tel: +49 2461 61 -nnnn Auskunft, Nutzer-Management (Dispatch) Das Dispatch befindet sich am Haupteingang des JSC, Gebäude 16.4, und ist telefonisch erreich- bar von Montag bis Donnerstag 8.00 - 17.00 Uhr Freitag 8.00 - 16.00 Uhr Tel.5642oder6400, Fax2810, E-Mail: [email protected] Supercomputer-Beratung Tel. 2828, E-Mail: [email protected] Netzwerk-Beratung, IT-Sicherheit Tel. 6440, E-Mail: [email protected] Rufbereitschaft Außerhalb der Arbeitszeiten (montags bis donnerstags: 17.00 - 24.00 Uhr, freitags: 16.00 - 24.00 Uhr, samstags: 8.00 - 17.00 Uhr) können Sie dringende Probleme der Rufbereitschaft melden: Rufbereitschaft Rechnerbetrieb: Tel. 6400 Rufbereitschaft Netzwerke: Tel. 6440 An Sonn- und Feiertagen gibt es keine Rufbereitschaft. Fachberater Tel. +49 2461 61 -nnnn Fachgebiet Berater Telefon E-Mail Auskunft, Nutzer-Management, E.
    [Show full text]
  • Chapter C4: Heaps and Binomial / Fibonacci Heaps
    Chapter C4: Heaps and Binomial / Fibonacci Heaps C4.1 Heaps A heap is the standard implementation of a priority queue. A min-heap stores values in nodes of a tree such that heap-order property: for each node, its value is smaller than or equal to its children’s So the minimum is on top. (A max-heap can be defined similarly.) The standard heap uses a binary tree. Specifically, it uses a complete binary tree, which we define here to be a binary tree where each level except the last is full, and in the last level nodes are added left to right. Here is an example: 7 24 19 25 56 68 40 29 31 58 Now, any binary tree can be stored in an array using level numbering. The root is in cell 0, cells 1 and cells 2 are for its children, cells 3 through 6 are for its grandchildren, and so on. Note that this means a nice formula: if a node is in cell x,thenitschildren are in 2x+1 and 2x+2. Such storage can be (very) wasteful for a general binary tree; but the definition of complete binary tree means the standard heap is stored in consecutive cells of the array. C4.2 Heap Operations The idea for insertion is to: Add as last leaf, then bubble up value until heap-order property re-established. Insert(v) add v as next leaf while v<parent(v) { swapElements(v,parent(v)) v=parent(v) } One uses a “hole” to reduce data movement. Here is an example of inserting the value 12: 7 7 24 19 12 19 ) 25 56 68 40 25 24 68 40 29 31 58 12 29 31 58 56 The idea for extractMin is to: Replace with value from last leaf, delete last leaf, and bubble down value until heap-order property re-established.
    [Show full text]
  • Nearest Neighbor Searching and Priority Queues
    Nearest neighbor searching and priority queues Nearest Neighbor Search • Given: a set P of n points in Rd • Goal: a data structure, which given a query point q, finds the nearest neighbor p of q in P or the k nearest neighbors p q Variants of nearest neighbor • Near neighbor (range search): find one/all points in P within distance r from q • Spatial join: given two sets P,Q, find all pairs p in P, q in Q, such that p is within distance r from q • Approximate near neighbor: find one/all points p’ in P, whose distance to q is at most (1+ε) times the distance from q to its nearest neighbor Solutions Depends on the value of d: • low d: graphics, GIS, etc. • high d: – similarity search in databases (text, images etc) – finding pairs of similar objects (e.g., copyright violation detection) Nearest neighbor search in documents • How could we represent documents so that we can define a reasonable distance between two documents? • Vector of word frequency occurrences – Probably want to get rid of useless words that occur in all documents – Probably need to worry about synonyms and other details from language – But basically, we get a VERY long vector • And maybe we ignore the frequencies and just idenEfy with a “1” the words that occur in some document. Nearest neighbor search in documents • One reasonable measure of distance between two documents is just a count of words they share – this is just the point wise product of the two vectors when we ignore counts.
    [Show full text]