CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea Dictionaries. Reading Weiss Chap. 5,...

26
CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea http://www.cs.unt.edu/~rada/CSCE3110 Dictionaries. Reading Weiss Chap. 5, Sec. 10.4.2

description

Course Records Dictionary Member Record keystudent namehw Stan Smith Sue Margolin Billie King Roy Miller39...

Transcript of CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea Dictionaries. Reading Weiss Chap. 5,...

Page 1: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea  Dictionaries. Reading Weiss Chap. 5, Sec. 10.4.2.

CSCE 3110Data Structures & Algorithm Analysis

Rada Mihalceahttp://www.cs.unt.edu/~rada/CSCE3110Dictionaries. Reading Weiss Chap. 5, Sec. 10.4.2

Page 2: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea  Dictionaries. Reading Weiss Chap. 5, Sec. 10.4.2.

Dictionaries

A dictionary is a collection of elements each of which has a unique search key

Uniqueness criteria may be relaxed (multiset) (I.e. do not force uniqueness)

Keep track of current members, with periodic insertions and deletions into the setExamples

Membership in a club, course recordsSymbol table (contains duplicates)Language dictionary (WordSmith, Webster, WordNet)

Similar to database

Page 3: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea  Dictionaries. Reading Weiss Chap. 5, Sec. 10.4.2.

Course RecordsDictionary

MemberRecord

key student name hw1

...123 Stan Smith 49

...124 Sue Margolin 56

...125 Billie King 34

...167 Roy Miller 39

...

Page 4: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea  Dictionaries. Reading Weiss Chap. 5, Sec. 10.4.2.

Dictionary ADT

simple container methods:size()isEmpty()elements()

query methods: findElement(k)findAllElements(k)

update methods: insertItem(k, e)removeElement(k)removeAllElements(k)

special element NO_SUCH_KEY, returned by an unsuccessful search

Page 5: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea  Dictionaries. Reading Weiss Chap. 5, Sec. 10.4.2.

How to Implement a Dictionary?

Sequences / Arraysorderedunordered

Binary Search TreesSkip listsHashtables

Page 6: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea  Dictionaries. Reading Weiss Chap. 5, Sec. 10.4.2.

Recall Arrays …

Unordered array

searching and removing takes O(?) timeinserting takes O(?) timeapplications to log files (frequent insertions, rare searches and removals)

Page 7: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea  Dictionaries. Reading Weiss Chap. 5, Sec. 10.4.2.

Ordered array

searching takes O(log n) time (binary search)inserting and removing takes O(n) timeapplication to look-up tables (frequent searches, rare insertions and removals)

Apply binary search

More Arrays

Page 8: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea  Dictionaries. Reading Weiss Chap. 5, Sec. 10.4.2.

narrow down the search range in stages“high-low” gamefindElement(22)

Binary Searches

Page 9: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea  Dictionaries. Reading Weiss Chap. 5, Sec. 10.4.2.

Implement a dictionary with a BSTA binary search tree is a binary tree T such thateach internal node stores an item (k, e) of a dictionary. keys stored at nodes in the left subtree of v are less than or equal to k.keys stored at nodes in the right subtree of v are greater than or equal to k.

Recall Binary Search Trees…

Page 10: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea  Dictionaries. Reading Weiss Chap. 5, Sec. 10.4.2.

An Alternative to Arrays

Unordered Array:insertion: O(1)search: O(n)

Ordered Arrayinsertion: O(n)search: O(log n)

Skip Lists:insertion: O(log n)search: O(log n)And avoid the fixed-size drawback of arrays!

Page 11: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea  Dictionaries. Reading Weiss Chap. 5, Sec. 10.4.2.

Skip Lists

good implementation for a dictionarya series of lists {S0, S1, …, Sk}each list Si stores a sorted subset of the dictionary D

12 18 25 28 72 74-

18 25 74-

18-

-

S0

S1

S2

S3

Page 12: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea  Dictionaries. Reading Weiss Chap. 5, Sec. 10.4.2.

Skip Lists

list S(i+1) contains items picked at random from S(i)each item has probability 50% of being in the upper level list

like flipping a coinS0 has n elementsS1 has about n/2 elementsS2 has about n/4 elements ….S(i) has about ? elements

Page 13: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea  Dictionaries. Reading Weiss Chap. 5, Sec. 10.4.2.

Traversing Positions in a Skip List

Assume a node P in the skip listafter(p)before(p)below(p)above(p)

Running time of each operation?

Page 14: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea  Dictionaries. Reading Weiss Chap. 5, Sec. 10.4.2.

Operations in a Skip List

Use skip lists to implement dictionaries Need to deal with

SearchInsert Remove

Page 15: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea  Dictionaries. Reading Weiss Chap. 5, Sec. 10.4.2.

Searching

Search for key KStart with p = the top-most, left position node in the skip listtwo steps:

1. if below(p) is null then stopwe are at the bottom

2. while key(p) < K move to the right go back to 1

Page 16: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea  Dictionaries. Reading Weiss Chap. 5, Sec. 10.4.2.

Searching

Search for 27

12 18 25 28 72 74-

18 25 74-

18-

-

S0

S1

S2

S3

Page 17: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea  Dictionaries. Reading Weiss Chap. 5, Sec. 10.4.2.

More Searching

Search for 74

12 18 25 28 72 74-

18 25 74-

18-

-

S0

S1

S2

S3

Page 18: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea  Dictionaries. Reading Weiss Chap. 5, Sec. 10.4.2.

Pseudocode for Searching

Algorithm SkipSearch(k) Input: Search key k Output: Position p in S such that p has

the largest key less than or equal to k p = top-most, left node in S while below(p) != null do p below(p) while(key (after(p)) k do p after(p) return p

Page 19: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea  Dictionaries. Reading Weiss Chap. 5, Sec. 10.4.2.

Running Time Analysis

log n levels O(log n) for going down in the skip listat each level, O(1) for moving forward

why? works like a binary searchin skip lists, the elements in list S(i+1) play the role of search dividers for elements in S(i)(in binary search: mid-list elements to divide the search)

total running time: O(log n)

Page 20: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea  Dictionaries. Reading Weiss Chap. 5, Sec. 10.4.2.

Insertion in Skip Lists

First: identify the place to insert new key k node p in S0 with largest key less or equal than k

Insert new item(k,e) after pwith probability 50%, the new item is inserted in list S1

with probability 25% , the new item is inserted in list S2

• with probability 12.5% , the new item is inserted in list S3

– with probability 6.25% , the new item is inserted in list S4– ….

Page 21: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea  Dictionaries. Reading Weiss Chap. 5, Sec. 10.4.2.

Insertion in Skip Lists

Insert 29

12 18 25 28 72 74-

18 25 74-

18-

-

S0

S1

S2

S3

29

29

Page 22: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea  Dictionaries. Reading Weiss Chap. 5, Sec. 10.4.2.

Pseudocode for Insertion

Algorithm SkipInsert(k,e) Input: Item (k,e) Output: - p SkipSearch(k) q insertAfterAbove(p, null, Item (k,e)) while random( ) 50% do while(above(p) == null) do p before(p) p above(p) q insertAfterAbove(p, q, Item(k,e))

Page 23: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea  Dictionaries. Reading Weiss Chap. 5, Sec. 10.4.2.

Running Time for Insertion?

Search position for new item(k,e)O(log n)

InsertionO(1)

Total running timeO(log n)

Page 24: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea  Dictionaries. Reading Weiss Chap. 5, Sec. 10.4.2.

Removal from Skip Lists

Easier than insertion

Locate item with key k to be removedif no such element, return NO SUCH KEYotherwise, remove Item(k,e)remove all items found with above(Item(k,e))

Page 25: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea  Dictionaries. Reading Weiss Chap. 5, Sec. 10.4.2.

Removal from Skip Lists

Remove 18Running time?

12 18 25 28 72 74-

18 25 74-

18-

-

S0

S1

S2

S3

Page 26: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea  Dictionaries. Reading Weiss Chap. 5, Sec. 10.4.2.

Efficient Implementation of Skip Lists

use DoublyLinkedList implementation+ two additional pointers

abovebelow

For a LinkedList provide pointer to headFor a DoublyLinkedList provide pointers to head and tailFor a SkipList ??