Queues. Queue ADT. Queue Specification. An ordered sequence A queue supports operations for inserting (enqueuing) and removing (dequeuing) elements.

Similar documents
CSE373 Fall 2013, Second Midterm Examination November 15, 2013

University of Waterloo Department of Electrical and Computer Engineering ECE250 Algorithms and Data Structures Fall 2014

PA3 Design Specification

LECTURE 17 GRAPH TRAVERSALS

CMSC 341 Hashing (Continued) Based on slides from previous iterations of this course

III Data Structures. Dynamic sets

Course Review. Cpt S 223 Fall 2009

INSTITUTE OF AERONAUTICAL ENGINEERING

[1] Strings, Arrays, Pointers, and Hashing

Lecture 4 Stack and Queue

Data Structure Lecture#22: Searching 3 (Chapter 9) U Kang Seoul National University

CS61B Lecture #24: Hashing. Last modified: Wed Oct 19 14:35: CS61B: Lecture #24 1

Linked lists (6.5, 16)

ECE250: Algorithms and Data Structures Midterm Review

CSE 373: Data Structures and Algorithms

CMSC 341 Lecture 16/17 Hashing, Parts 1 & 2

Summer Final Exam Review Session August 5, 2009

data structures and algorithms lecture 6

Hash table basics mod 83 ate. ate. hashcode()

lecture23: Hash Tables

CS 225. Nov. 13 Introduction to Graphs. Data Structures. Wade Fagen-Ulmschneider

Winter 2016 COMP-250: Introduction to Computer Science. Lecture 8, February 4, 2016

Module 2: Classical Algorithm Design Techniques

Section 05: Solutions

Course Review for Finals. Cpt S 223 Fall 2008

Data Structure. Recitation VII

Winter 2016 COMP-250: Introduction to Computer Science. Lecture 8, February 4, 2016

Standard ADTs. Lecture 19 CS2110 Summer 2009

Introduction p. 1 Pseudocode p. 2 Algorithm Header p. 2 Purpose, Conditions, and Return p. 3 Statement Numbers p. 4 Variables p. 4 Algorithm Analysis

Cpt S 223 Fall Cpt S 223. School of EECS, WSU

Computer Science 1 Study Union Practice Problems. What are the arguments to malloc? calloc? realloc? What do they do?

Data Structure. Measuring Input size. Composite Data Structures. Linear data structures. Data Structure is: Abstract Data Type 1/9/2014

CS 225. April 6 Disjoint Sets Implementation. Data Structures. Wade Fagen-Ulmschneider

We've tried to include the common errors and grading standard for every question.

Final Exam Data Structure course. No. of Branches (5)

You must include this cover sheet. Either type up the assignment using theory4.tex, or print out this PDF.

Overview of Data. 1. Array 2. Linked List 3. Stack 4. Queue

CS 1114: Implementing Search. Last time. ! Graph traversal. ! Two types of todo lists: ! Prof. Graeme Bailey.

We don t have much time, so we don t teach them [students]; we acquaint them with things that they can learn. Charles E. Leiserson

Section 05: Midterm Review

Measuring Input size. Last lecture recap.

LIFO : Last In First Out

Linked Structures. See Section 3.2 of the text.

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

COMP 213 Advanced Object-oriented Programming Lecture 8 The Queue ADT (cont.)

Hash Open Indexing. Data Structures and Algorithms CSE 373 SP 18 - KASEY CHAMPION 1

The dictionary problem

DNHI Homework 3 Solutions List, Stacs and Queues

Separate Compilation and Namespaces Week Fall. Computer Programming for Engineers

CMSC 341. Linked Lists, Stacks and Queues. CMSC 341 Lists, Stacks &Queues 1

Assume you are given a Simple Linked List (i.e. not a doubly linked list) containing an even number of elements. For example L = [A B C D E F].

DATA STRUCTURES AND ALGORITHMS

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

DATA STRUCTURES AND ALGORITHMS

CSI33 Data Structures

Faculty of Science FINAL EXAMINATION COMP-250 A Introduction to Computer Science School of Computer Science, McGill University

Course Review. Cpt S 223 Fall 2010

THE UNIVERSITY OF WESTERN AUSTRALIA

CS W3134: Data Structures in Java

Student number: Datenstrukturen & Algorithmen page 1

Postfix (and prefix) notation

1 P age DS & OOPS / UNIT II

Week 6. Data structures

CPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues

CMSC Introduction to Algorithms Spring 2012 Lecture 7

CSE 373 SEPTEMBER 29 STACKS AND QUEUES

Hashing for searching

Linked Lists and Abstract Data Structures A brief comparison

CPSC 331 Term Test #2 March 26, 2007

Module 1: Asymptotic Time Complexity and Intro to Abstract Data Types

Data Structure. A way to store and organize data in order to support efficient insertions, queries, searches, updates, and deletions.

Basic Data Structures

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)

The Stack ADT. Stacks and Queues. Stack: Illustration. What is a Stack? Accessors top size isempty

Computer Programming II Python

CSCA48 Summer 2018 Week 3: Priority Queue, Linked Lists. Marzieh Ahmadzadeh University of Toronto Scarborough

UNIT 6A Organizing Data: Lists. Last Two Weeks

1 Probability Review. CS 124 Section #8 Hashing, Skip Lists 3/20/17. Expectation (weighted average): the expectation of a random quantity X is:

Hash tables. hashing -- idea collision resolution. hash function Java hashcode() for HashMap and HashSet big-o time bounds applications

Algorithm Design and Analysis

Data Structures and Algorithms

Solution for Data Structure

LECTURE 11 TREE TRAVERSALS

Basic Data Structures 1 / 24

University of Palestine. Final Exam 2 nd semester 2014/2015 Total Grade: 50

How to Win Coding Competitions: Secrets of Champions. Week 2: Computational complexity. Linear data structures Lecture 5: Stack. Queue.

CS61BL Summer 2013 Midterm 2

SEQUENCES, ARRAYS, AND LINKED LISTS

Quiz 1 Solutions. (a) f(n) = n g(n) = log n Circle all that apply: f = O(g) f = Θ(g) f = Ω(g)

Question Q1 Q2 Q3 Q4 Q5 Q6 Q7 Total

Stack and Queue. Stack:

CSE373: Data Structures & Algorithms Lecture 28: Final review and class wrap-up. Nicki Dell Spring 2014

8. Fundamental Data Structures

More on Hashing: Collisions. See Chapter 20 of the text.

Hash Tables. Gunnar Gotshalks. Maps 1

You must print this PDF and write your answers neatly by hand. You should hand in the assignment before recitation begins.

CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues. Lauren Milne Summer 2015

Algorithms and Data Structures

CS 225. April 9 Graphs Intro. Data Structures. Wade Fagen-Ulmschneider

Lecture 17. Improving open-addressing hashing. Brent s method. Ordered hashing CSE 100, UCSD: LEC 17. Page 1 of 19

Transcription:

Queues Look!up tables Hash tables An ordered sequence A queue supports operations for inserting (enqueuing) and removing (dequeuing) elements. Maps When dequeuing, the element that has been in the queue for the longest time is removed. Lecture 11 Queues MasterMind Riley 7.6-7.8 + 8 Cormen 11.1, 11.2. 11.4 (not the analytical parts) Queue ADT 2 Queue Specification 3 4

An example Determining whether a string is a palindrome or not. A string is a palindrome if (and only if) it reads the same forwards and backwards. Focus on letters and digits; skip delimiters and blank space, and treat lower-case and upper-case as equals. Can be defined recursively: The empty string and a 1-character string is a palindrome. Longer strings are palindromes if the first and last characters are equal and the substring in-between the first and the last is a palindrome. Examples Ni talar bra latin. (that is, NITALARBRALATIN ) A Toyota!! ( ATOYOTA ) Won't lovers revolt now? Madam, I help Martin. I trample him --- Adam etc 5 6 Implementations of Queues A list with two iterators; one at the beginning and one at the end. Keep the list sorted at all time so that the element at the beginning has spent longest time in the queue. Dequeue at the beginning. Enqueue at the end. 7 8

Using Stacks Efficiency A queue can be implemented using two stacks. The stack solution consumes more time. However, the amount of work is still constant per element. Call them S1 and S2. Enqueue pushes elements on S1. Dequeue pops elements from S2. If S2 is empty, all elements in S1 are moved over to S2 one element at a time by popping them from S1 and pushing them onto S2. Then dequeue pops the top of S2 unless it is still empty, in which case an exception should be thrown. You can t dequeue something from an empty queue. A push onto S1, a pop from S1, a push onto S2, and a pop from S2. This assumes a reasonable implementation of the stacks. All operations take constant time. So, asymptotically, using a list or two stacks leads to the same time complexity: O(1) per element. A kind of analysis called amortized. 9 10 11 12 Bounded Queues Can be implemented with an array and in the form of a Ring Buffer.

Maps Also known as look-up tables functions A set of pairs {(keyi,valuei)} and methods to create, change, inspect and search such sets. A key occurs in at most one pair. Example: lookupvalue(k) returns a value v such that (k,v) is a pair in the look-up table (or it fails if there is no such pair). 13 14 16 Simple Implementations A list. An array. Arbitrary size Access via linear search Fixed size Instant access if consequtive keys from an ordered universe Easy update insert/delete could be a problem 15

Hashing Hash table implemented using an array. Good to use a size that is a prime number. The array contains buckets with references. A hash function h(x):key->index Should preferrably give different results when applied to different keys. Should be easy to compute. The pair (k,v) is stored in the bucket with index h(k). The hash function is used to scatter the pairs within the array. The load factor is the percentage of the buckets in use. 17 18 20 Issues How do we obtain a key? hashcode() for objects in Java. What about collisions!? What do we do when two pairs are hashed to the same bucket? Open hashing: Collect all pairs that fall into a bucket in a list (or some other data structure)! lookupvalue: First find the right bucket using the hash function. Then traverse the list. Closed hashing: Find an empty bucket somewhere else in the array! 19

Variants of closed hashing Linear probing: Start at the occupied bucket and move on through the array one step at a time (with wrap-around) until an empty bucket is found. Double hashing: Like linear probing but use a step length determined by another function. The step length and the size of the hash table (the array) should be relative primes (don t share any prime factors). 21 22 23 24

- Beware of bugs in the above code; I have only proved it correct, not tried it. Donald E. Knuth Professor Emeritus of The Art of Computer Programming (at Stanford) 25 26