06 B: Hashing and Priority Queues CS1102S: Data Structures and - - PowerPoint PPT Presentation

06 b hashing and priority queues
SMART_READER_LITE
LIVE PREVIEW

06 B: Hashing and Priority Queues CS1102S: Data Structures and - - PowerPoint PPT Presentation

Hashing Priority Queues Puzzlers 06 B: Hashing and Priority Queues CS1102S: Data Structures and Algorithms Martin Henz February 26, 2010 Generated on Friday 26 th February, 2010, 09:23 CS1102S: Data Structures and Algorithms 06 B: Hashing


slide-1
SLIDE 1

Hashing Priority Queues Puzzlers

06 B: Hashing and Priority Queues

CS1102S: Data Structures and Algorithms

Martin Henz

February 26, 2010

Generated on Friday 26th February, 2010, 09:23 CS1102S: Data Structures and Algorithms 06 B: Hashing and Priority Queues 1

slide-2
SLIDE 2

Hashing Priority Queues Puzzlers

1

Hashing

2

Priority Queues

3

Puzzlers

CS1102S: Data Structures and Algorithms 06 B: Hashing and Priority Queues 2

slide-3
SLIDE 3

Hashing Priority Queues Puzzlers Collision Resolution Strategies Double Hashing A Detail: Removal from Hash Table Hash Tables in the Java API

1

Hashing Collision Resolution Strategies Double Hashing A Detail: Removal from Hash Table Hash Tables in the Java API

2

Priority Queues

3

Puzzlers

CS1102S: Data Structures and Algorithms 06 B: Hashing and Priority Queues 3

slide-4
SLIDE 4

Hashing Priority Queues Puzzlers Collision Resolution Strategies Double Hashing A Detail: Removal from Hash Table Hash Tables in the Java API

Recap: Main Ideas

Implement set as array Store values in array; compute index using a hash function. Spread The hash function should “spread” the hash keys evenly over the available hash values Collision Hash table implementations differ in their strategies of collision resolution: Two hash keys mapping to the same hash value

CS1102S: Data Structures and Algorithms 06 B: Hashing and Priority Queues 4

slide-5
SLIDE 5

Hashing Priority Queues Puzzlers Collision Resolution Strategies Double Hashing A Detail: Removal from Hash Table Hash Tables in the Java API

Separate Chaining

CS1102S: Data Structures and Algorithms 06 B: Hashing and Priority Queues 5

slide-6
SLIDE 6

Hashing Priority Queues Puzzlers Collision Resolution Strategies Double Hashing A Detail: Removal from Hash Table Hash Tables in the Java API

Hash Tables without Linked Lists

Idea Store items directly into array; use alternative cells if a collision

  • ccurs

More formally Try cells h0(x), h1(x), h2(x), . . . until an empty cell is found. How to define hi? hi(x) = (hash(x) + f(i)) mod TableSize where f(0) = 0. Load factor, λ Ratio of number of elements in hash table to table size.

CS1102S: Data Structures and Algorithms 06 B: Hashing and Priority Queues 6

slide-7
SLIDE 7

Hashing Priority Queues Puzzlers Collision Resolution Strategies Double Hashing A Detail: Removal from Hash Table Hash Tables in the Java API

Linear Probing

Conflict resolution f(i) = i Clustering As the load factor λ increases, occupied areas in the array tend to occur in clusters, leading to frequent unsuccessful insertion tries.

CS1102S: Data Structures and Algorithms 06 B: Hashing and Priority Queues 7

slide-8
SLIDE 8

Hashing Priority Queues Puzzlers Collision Resolution Strategies Double Hashing A Detail: Removal from Hash Table Hash Tables in the Java API

Quadratic Probing

Conflict resolution f(i) = i2 Theorem If quadratic probing is used, and the table size is prime, then a new element can always be inserted if the table is at least half empty.

CS1102S: Data Structures and Algorithms 06 B: Hashing and Priority Queues 8

slide-9
SLIDE 9

Hashing Priority Queues Puzzlers Collision Resolution Strategies Double Hashing A Detail: Removal from Hash Table Hash Tables in the Java API

Double Hashing

Idea Use a second hash function to find the jump distance Formally f(i) = i · hash2(x) Attention The function hash2 must never return 0. Why?

CS1102S: Data Structures and Algorithms 06 B: Hashing and Priority Queues 9

slide-10
SLIDE 10

Hashing Priority Queues Puzzlers Collision Resolution Strategies Double Hashing A Detail: Removal from Hash Table Hash Tables in the Java API

Double Hashing: Example

hash1(x) = x mod 10 hash2(x) = 7 − (x mod 7) hi(x) = hashi(x) + i · hash2(x)

CS1102S: Data Structures and Algorithms 06 B: Hashing and Priority Queues 10

slide-11
SLIDE 11

Hashing Priority Queues Puzzlers Collision Resolution Strategies Double Hashing A Detail: Removal from Hash Table Hash Tables in the Java API

A Detail: Removal from Hash Table

Removal from separate chaining hash table Straightforward: remove item from respective linked list (if it is there) Removal from Probing Hash Table: First idea Set the respective table entry back to null Problem This operation interrupts probing chains; elements can be “lost”

CS1102S: Data Structures and Algorithms 06 B: Hashing and Priority Queues 11

slide-12
SLIDE 12

Hashing Priority Queues Puzzlers Collision Resolution Strategies Double Hashing A Detail: Removal from Hash Table Hash Tables in the Java API

Solution

private static class HashEntry<AnyType> { public AnyType element ; public boolean i s A c t i v e ; public HashEntry ( AnyType e ) { this (e , true ) ; } public HashEntry ( AnyType e , boolean i ) { element = e ; i s A c t i v e = i ; } } public void remove ( AnyType x ) { int currentPos = findPos ( x ) ; i f ( i s A c t i v e ( currentPos ) ) array [ currentPos ] . i s A c t i v e = false ; }

CS1102S: Data Structures and Algorithms 06 B: Hashing and Priority Queues 12

slide-13
SLIDE 13

Hashing Priority Queues Puzzlers Collision Resolution Strategies Double Hashing A Detail: Removal from Hash Table Hash Tables in the Java API

Remember Sets?

Idea A Set (interface) is a Collection (interface) that does not allow duplicate entries. HashSet A HashSet is a hash table implementation of Set. class HashSet<E> implements Set<E>

CS1102S: Data Structures and Algorithms 06 B: Hashing and Priority Queues 13

slide-14
SLIDE 14

Hashing Priority Queues Puzzlers Motivation Binary Heaps Basic Heap Operations Priority Queues in Standard Library

1

Hashing

2

Priority Queues Motivation Binary Heaps Basic Heap Operations Priority Queues in Standard Library

3

Puzzlers

CS1102S: Data Structures and Algorithms 06 B: Hashing and Priority Queues 14

slide-15
SLIDE 15

Hashing Priority Queues Puzzlers Motivation Binary Heaps Basic Heap Operations Priority Queues in Standard Library

Motivation

Operations on queues add(e): enter new element into queue remove(): remove the element that has been entered first A slight variation Priority should not be implicit, using the time of entry, but explicit, using an ordering Operations on priority queues insert(e): enter new element into queue deleteMin(): remove the smallest element

CS1102S: Data Structures and Algorithms 06 B: Hashing and Priority Queues 15

slide-16
SLIDE 16

Hashing Priority Queues Puzzlers Motivation Binary Heaps Basic Heap Operations Priority Queues in Standard Library

Application Examples

Printer queue: use number of pages as “priority” Discrete event simulation: use simulation time as “priority” Network routing: give priority to packets with strictest quality-of-service requirements

CS1102S: Data Structures and Algorithms 06 B: Hashing and Priority Queues 16

slide-17
SLIDE 17

Hashing Priority Queues Puzzlers Motivation Binary Heaps Basic Heap Operations Priority Queues in Standard Library

Simple Implementations

Unordered list: insert(e): O(1), deleteMin(): O(N) Ordered list: insert(e): O(N), deleteMin(): O(1) Search tree: insert(e): O(log N), deleteMin(): O(log N)

CS1102S: Data Structures and Algorithms 06 B: Hashing and Priority Queues 17

slide-18
SLIDE 18

Hashing Priority Queues Puzzlers Motivation Binary Heaps Basic Heap Operations Priority Queues in Standard Library

Binary Heaps

Rough Idea Keep a binary tree whose root contains the smallest element insert(e) and deleteMin() need to restore this property Completeness Keep binary tree complete, which means completely filled, with the possible exception of the bottom level, which is filled from left to right. Heap-order For every node X, the key in the parent of X is smaller than or equal to the key in X, with the exception of the root

CS1102S: Data Structures and Algorithms 06 B: Hashing and Priority Queues 18

slide-19
SLIDE 19

Hashing Priority Queues Puzzlers Motivation Binary Heaps Basic Heap Operations Priority Queues in Standard Library

Order in Binary Heap

Tree on the left is a binary heap; tree on the right is not!

CS1102S: Data Structures and Algorithms 06 B: Hashing and Priority Queues 19

slide-20
SLIDE 20

Hashing Priority Queues Puzzlers Motivation Binary Heaps Basic Heap Operations Priority Queues in Standard Library

Representation as Array

CS1102S: Data Structures and Algorithms 06 B: Hashing and Priority Queues 20

slide-21
SLIDE 21

Hashing Priority Queues Puzzlers Motivation Binary Heaps Basic Heap Operations Priority Queues in Standard Library

insert

Idea Add “hole” at bottom and “percolate” the hole up to the right place for insertion

CS1102S: Data Structures and Algorithms 06 B: Hashing and Priority Queues 21

slide-22
SLIDE 22

Hashing Priority Queues Puzzlers Motivation Binary Heaps Basic Heap Operations Priority Queues in Standard Library

Example: insert(14)

CS1102S: Data Structures and Algorithms 06 B: Hashing and Priority Queues 22

slide-23
SLIDE 23

Hashing Priority Queues Puzzlers Motivation Binary Heaps Basic Heap Operations Priority Queues in Standard Library

Example: insert(14), continued

CS1102S: Data Structures and Algorithms 06 B: Hashing and Priority Queues 23

slide-24
SLIDE 24

Hashing Priority Queues Puzzlers Motivation Binary Heaps Basic Heap Operations Priority Queues in Standard Library

Analysis

Worst case O(log N) Average 2.607 comparisons

CS1102S: Data Structures and Algorithms 06 B: Hashing and Priority Queues 24

slide-25
SLIDE 25

Hashing Priority Queues Puzzlers Motivation Binary Heaps Basic Heap Operations Priority Queues in Standard Library

deleteMin

Idea Remove root, leaving “hole” at top. “Percolate” the hole down to a correct place for insertion of bottom element

CS1102S: Data Structures and Algorithms 06 B: Hashing and Priority Queues 25

slide-26
SLIDE 26

Hashing Priority Queues Puzzlers Motivation Binary Heaps Basic Heap Operations Priority Queues in Standard Library

Example: deleteMin()

CS1102S: Data Structures and Algorithms 06 B: Hashing and Priority Queues 26

slide-27
SLIDE 27

Hashing Priority Queues Puzzlers Motivation Binary Heaps Basic Heap Operations Priority Queues in Standard Library

Example: deleteMin(), continued

CS1102S: Data Structures and Algorithms 06 B: Hashing and Priority Queues 27

slide-28
SLIDE 28

Hashing Priority Queues Puzzlers Motivation Binary Heaps Basic Heap Operations Priority Queues in Standard Library

Example: deleteMin(), continued

CS1102S: Data Structures and Algorithms 06 B: Hashing and Priority Queues 28

slide-29
SLIDE 29

Hashing Priority Queues Puzzlers Motivation Binary Heaps Basic Heap Operations Priority Queues in Standard Library

Analysis

Worst case O(log N) Average log N

CS1102S: Data Structures and Algorithms 06 B: Hashing and Priority Queues 29

slide-30
SLIDE 30

Hashing Priority Queues Puzzlers Motivation Binary Heaps Basic Heap Operations Priority Queues in Standard Library

buildHeap

Initial setup Build a heap from a given (unordered) collection of elements Idea “Percolate” every inner node down the tree

CS1102S: Data Structures and Algorithms 06 B: Hashing and Priority Queues 30

slide-31
SLIDE 31

Hashing Priority Queues Puzzlers Motivation Binary Heaps Basic Heap Operations Priority Queues in Standard Library

Example: buildHeap, percolateDown(7)

CS1102S: Data Structures and Algorithms 06 B: Hashing and Priority Queues 31

slide-32
SLIDE 32

Hashing Priority Queues Puzzlers Motivation Binary Heaps Basic Heap Operations Priority Queues in Standard Library

Example: buildHeap, percolateDown(6), ..(5)

CS1102S: Data Structures and Algorithms 06 B: Hashing and Priority Queues 32

slide-33
SLIDE 33

Hashing Priority Queues Puzzlers Motivation Binary Heaps Basic Heap Operations Priority Queues in Standard Library

Example: buildHeap, percolateDown(4), ..(3)

CS1102S: Data Structures and Algorithms 06 B: Hashing and Priority Queues 33

slide-34
SLIDE 34

Hashing Priority Queues Puzzlers Motivation Binary Heaps Basic Heap Operations Priority Queues in Standard Library

Example: buildHeap, percolateDown(2), ..(1)

CS1102S: Data Structures and Algorithms 06 B: Hashing and Priority Queues 34

slide-35
SLIDE 35

Hashing Priority Queues Puzzlers Motivation Binary Heaps Basic Heap Operations Priority Queues in Standard Library

Analysis

Bound The runtime is bounded by the sum of all heights of all nodes Theorem For perfect binary tree of height h, containing 2h+1 − 1 nodes, sum of heights of nodes is 2h+1 − 1 − (h + 1). Worst case O(N)

CS1102S: Data Structures and Algorithms 06 B: Hashing and Priority Queues 35

slide-36
SLIDE 36

Hashing Priority Queues Puzzlers Motivation Binary Heaps Basic Heap Operations Priority Queues in Standard Library

Other Heap Operations

decreaseKey(p,∆) Lowers the value of item at position p by a positive amount ∆. Implementation: Percolate up increaseKey(p,∆) Increases the value of item at position p by a positive amount ∆. Implementation: Percolate down delete(p) Remove value at position p Implementation: decreaseKey(p,∞), then deleteMin()

CS1102S: Data Structures and Algorithms 06 B: Hashing and Priority Queues 36

slide-37
SLIDE 37

Hashing Priority Queues Puzzlers Motivation Binary Heaps Basic Heap Operations Priority Queues in Standard Library

Priority Queues in Standard Library

class PriorityQueue<E> { boolean add (E e ) { . . . } / / add element E p o l l ( ) { . . . } / / remove smallest }

CS1102S: Data Structures and Algorithms 06 B: Hashing and Priority Queues 37

slide-38
SLIDE 38

Hashing Priority Queues Puzzlers Last Puzzler: “It’s Elementary” New Puzzler: The Last Laugh

1

Hashing

2

Priority Queues

3

Puzzlers Last Puzzler: “It’s Elementary” New Puzzler: The Last Laugh

CS1102S: Data Structures and Algorithms 06 B: Hashing and Priority Queues 38

slide-39
SLIDE 39

Hashing Priority Queues Puzzlers Last Puzzler: “It’s Elementary” New Puzzler: The Last Laugh

Last Puzzler: It’s Elementary

What does the following program print? public class Elementary { public static void main ( String [ ] args ) { System . out . p r i n t l n (12345 + 5432 l ) ; } }

CS1102S: Data Structures and Algorithms 06 B: Hashing and Priority Queues 39

slide-40
SLIDE 40

Hashing Priority Queues Puzzlers Last Puzzler: “It’s Elementary” New Puzzler: The Last Laugh

Scary...

I’m so scared when try running these codes on

  • Eclipse. When I run the file downloaded from

the module homepage, the result is 17777. But when I type it myself , the result is 66666. Maybe the number in teacher’s file is not the normal number, right ?

CS1102S: Data Structures and Algorithms 06 B: Hashing and Priority Queues 40

slide-41
SLIDE 41

Hashing Priority Queues Puzzlers Last Puzzler: “It’s Elementary” New Puzzler: The Last Laugh

The “Fine” Print

CS1102S: Data Structures and Algorithms 06 B: Hashing and Priority Queues 41

slide-42
SLIDE 42

Hashing Priority Queues Puzzlers Last Puzzler: “It’s Elementary” New Puzzler: The Last Laugh

Constant Numbers in Java

(see Java Language Specification) 12345: int constant in decimal notation 0xff: int constant in hexadecimal notation 077: int constant in octal notation 45.23: double constant 5432l: long constant in decimal notation 0xffL: long constant in hexadecimal notation 077L: long constant in octal notation

CS1102S: Data Structures and Algorithms 06 B: Hashing and Priority Queues 42

slide-43
SLIDE 43

Hashing Priority Queues Puzzlers Last Puzzler: “It’s Elementary” New Puzzler: The Last Laugh

Useful Habit

Use “L ” (and not “l”) to indicate long literals: public class Elementary { public static void main ( String [ ] args ) { System . out . p r i n t l n (12345 + 5432L ) ; } }

CS1102S: Data Structures and Algorithms 06 B: Hashing and Priority Queues 43

slide-44
SLIDE 44

Hashing Priority Queues Puzzlers Last Puzzler: “It’s Elementary” New Puzzler: The Last Laugh

New Puzzler: The Last Laugh

What does the following program print? public class LastLaugh { public static void main ( String [ ] args ) { System . out . p r i n t l n ( ”H” + ” a ” ) ; System . out . p r i n t l n ( ’H ’ + ’a ’ ) ; } }

CS1102S: Data Structures and Algorithms 06 B: Hashing and Priority Queues 44

slide-45
SLIDE 45

Hashing Priority Queues Puzzlers Last Puzzler: “It’s Elementary” New Puzzler: The Last Laugh

Next Week

Sorting, sorting, and more sorting!

CS1102S: Data Structures and Algorithms 06 B: Hashing and Priority Queues 45