14
Computer Science CS 330: Algorithms Elementary Data Elementary Data Structures, Structures, Dictionaries Dictionaries Gene Itkis

CS 330: Algorithms Elementary Data Structures, Dictionaries

Embed Size (px)

DESCRIPTION

CS 330: Algorithms Elementary Data Structures, Dictionaries. Gene Itkis. Primitive DS. Linked lists (singly- and doubly- linked) “Stretchable”, efficient, but… Sequential access Arrays Even more efficient, but not “stretchable” Random Access. Simple Data Structures. Methods for all: - PowerPoint PPT Presentation

Citation preview

Page 1: CS 330: Algorithms Elementary Data Structures, Dictionaries

Computer Science

CS 330: AlgorithmsElementary Data Elementary Data

Structures,Structures,DictionariesDictionaries

Gene Itkis

Page 2: CS 330: Algorithms Elementary Data Structures, Dictionaries

Computer Science

CS-330: Algorithms, Fall 2004 Gene Itkis 2

Primitive DS

Linked lists (singly- and doubly- linked) “Stretchable”, efficient, but… Sequential access

Arrays Even more efficient, but not

“stretchable” Random Access

Page 3: CS 330: Algorithms Elementary Data Structures, Dictionaries

Computer Science

CS-330: Algorithms, Fall 2004 Gene Itkis 3

Simple Data Structures

Stacks

lastlast inserted

Queues

firstfirst inserted

Priority Queues

highest priority

MethodsMethodsfor all:for all:

isEmpty()isEmpty() size()size()

insert(insert(elem))

remove()remove() which element?

Page 4: CS 330: Algorithms Elementary Data Structures, Dictionaries

Computer Science

DictionariesDictionaries

Search/Retrieve by by keykey

Page 5: CS 330: Algorithms Elementary Data Structures, Dictionaries

Computer Science

CS-330: Algorithms, Fall 2004 Gene Itkis 5

Dictionary Examples

Phone directory Name is key; phone # is the info Reverse lookup: phone # is key

Student records Possible keys: name, ID#

Credit cards DB; PKI Certificate Revocation Lists (CRLs) E.g. check that the given credit card / certificate

is valid Extra: “Authenticate” the result

ETC.

Page 6: CS 330: Algorithms Elementary Data Structures, Dictionaries

Computer Science

CS-330: Algorithms, Fall 2004 Gene Itkis 6

Dictionary Interface

isEmpty()

size()

findfind(key)(key)

insert(insert(elem))

remove(remove(key))

Dynamic

Page 7: CS 330: Algorithms Elementary Data Structures, Dictionaries

Computer Science

CS-330: Algorithms, Fall 2004 Gene Itkis 7

Implementations

Simple/naïve

ListsLists

Better implementations

Hash TablesHash Tables

Probabilistic methods, Expected values

Ordered treesOrdered trees

“Good enough” approximations; Augmenting (order stats)

Other

Skip-lists

Page 8: CS 330: Algorithms Elementary Data Structures, Dictionaries

Computer Science

CS-330: Algorithms, Fall 2004 Gene Itkis 8

Naïve: Lists

Unordered List insert & delete

O(1) – fast find

O(n) – slow Ordered list

Linked-List find & insert

O(n) – slow Array

find Binary search: O(lg n)O(lg n) – pretty fast

insert O(n) – slow

Page 9: CS 330: Algorithms Elementary Data Structures, Dictionaries

Computer Science

CS-330: Algorithms, Fall 2004 Gene Itkis 9

Ordered Trees

Order x< y, z : min at root – heap () y < x < zy < x < z : search

Depth Shallow Balanced

There might be exceptions: e.g., Leftist heaps

“Strong” balance

Approximate E.g., depth of leaves within factor of 2 (R-B

trees)

heap

x

zy

Page 10: CS 330: Algorithms Elementary Data Structures, Dictionaries

Computer Science

CS-330: Algorithms, Fall 2004 Gene Itkis 10

Ordered Trees

Searching Easy

Insert/Delete Naïve: destroys balance Fix balance

How? AVL trees

Nodes keep children heightsRotate when needed: when children heights are >1

apart

RedRed--BlackBlack / 2-3-42-3-4 trees

“Almost balanced”

Page 11: CS 330: Algorithms Elementary Data Structures, Dictionaries

Computer Science

CS-330: Algorithms, Fall 2004 Gene Itkis 11

Hash tables

Example Find professors by office number Find tools in a tool-box

Might not work for everyone

Idea “Figure” info location from the key

Page 12: CS 330: Algorithms Elementary Data Structures, Dictionaries

Computer Science

CS-330: Algorithms, Fall 2004 Gene Itkis 12

Hash Tables: Idea

Hash function H(key)=i

If it works… find

O(1) insert & delete

O(1)

Problem? Collisions:

H(key’)=H(key)

key

H

Hashtable

ikeyinfo

key’

Page 13: CS 330: Algorithms Elementary Data Structures, Dictionaries

Computer Science

CS-330: Algorithms, Fall 2004 Gene Itkis 13

Hash Table: Issues

Good Hash functions Minimize collision chances

“Random looking”

Collision Resolution Chaining Open Addressing

Linear Probing: d=1 Quadratic Probing: d=i2

Double Hashing: d=H2(key’)

keyinfo

key

H

Hashtable key’

key’info

keyinfo

key’info

d

key”

key”info

d

Page 14: CS 330: Algorithms Elementary Data Structures, Dictionaries

Computer Science

CS-330: Algorithms, Fall 2004 Gene Itkis 14

Collision Resolution Methods Comparison

Chaining Requires extra space As with linked list

Stretchable Can degenerate to linked-list search

Open Addressing No extra space needed Has size limit Also can degenerate to unordered list

search Perfect Hashing