SlideShare uma empresa Scribd logo
1 de 25
Baixar para ler offline
CSE 326
Randomized Data Structures
David Kaplan
Dept of Computer Science & Engineering
Autumn 2001
Randomized Data
Structures
CSE 326 Autumn 2001
2
Motivation for
Randomized Data Structures
We’ve seen many data structures with good average
case performance on random inputs, but bad behavior
on particular inputs
 e.g. Binary Search Trees
 Instead of randomizing the input (since we cannot!),
consider randomizing the data structure
 No bad inputs, just unlucky random numbers
 Expected case good behavior on any input
Randomized Data
Structures
CSE 326 Autumn 2001
3
Average vs. Expected Time
Average (1/N) • ∑ xi
Expectation ∑ Pr(xi) • xi
Deterministic with good average time
 If your application happens to always (or often) use the
“bad” case, you are in big trouble!
Randomized with good expected time
 Once in a while you will have an expensive operation, but no
inputs can make this happen all the time
Like an insurance policy for your algorithm!
Randomized Data
Structures
CSE 326 Autumn 2001
4
Randomized Data Structures
 Define a property (or subroutine) in an algorithm
 Sample or randomly modify the property
 Use altered property as if it were the true property
Can transform average case runtimes into expected
runtimes (remove input dependency).
Sometimes allows substantial speedup in exchange for
probabilistic unsoundness.
Randomized Data
Structures
CSE 326 Autumn 2001
5
Randomization in Action
 Quicksort
 Randomized data structures
 Treaps
 Randomized skip lists
 Random number generation
 Primality testing
Randomized Data
Structures
CSE 326 Autumn 2001
6
Treap
Dictionary Data Structure
Treap is a BST
 binary tree property
 search tree property
Treap is also a heap
 heap-order property
 random priorities
15
12
10
30
9
15
7
8
4
18
6
7
2
9
priority
key
Randomized Data
Structures
CSE 326 Autumn 2001
7
Treap Insert
 Choose a random priority
 Insert as in normal BST
 Rotate up until heap order is restored
6
7
insert(15)
7
8
2
9
14
12
6
7
7
8
2
9
14
12
9
15
6
7
7
8
2
9
9
15
14
12
Randomized Data
Structures
CSE 326 Autumn 2001
8
Tree + Heap… Why Bother?
 Insert data in sorted order into a treap …
What shape tree comes out?
6
7
insert(7)
6
7
insert(8)
7
8
6
7
insert(9)
7
8
2
9
6
7
insert(12)
7
8
2
9
15
12
Treap Delete
 Find the key
 Increase its value to ∞
 Rotate it to the fringe
 Snip it off
delete(9)
6
7
7
8
2⇒∞
9
9
15
15
12
7
8
6
7
∞
9
9
15
15
12
7
8
6
7
9
15
∞
9
15
12
7
8
6
7
9
15
15
12
∞
9
7
8
6
7
9
15
15
12
Randomized Data
Structures
CSE 326 Autumn 2001
10
Treap Summary
 Implements Dictionary ADT
 insert in expected O(log n) time
 delete in expected O(log n) time
 find in expected O(log n) time
 but worst case O(n)
 Memory use
 O(1) per node
 about the cost of AVL trees
 Very simple to implement
 little overhead – less than AVL trees
Randomized Data
Structures
CSE 326 Autumn 2001
11
Perfect Binary Skip List
 Sorted linked list
 # of links of a node is its height
 The height i link of each node (that has one)
links to the next node of height i or greater
8
2
11
10
19
13
20
22
29
23
Randomized Data
Structures
CSE 326 Autumn 2001
12
Find in a Perfect Binary Skip List
 Start i at the maximum height
 Until the node is found or i is one and the next
node is too large:
 If the next node along the i link is less than the
target, traverse to the next node
 Otherwise, decrease i by one
Runtime?
Randomized Data
Structures
CSE 326 Autumn 2001
13
Randomized Skip List Intuition
It’s far too hard to insert into a perfect skip list
But is perfection necessary?
What matters in a skip list?
 We want way fewer tall nodes than short ones
 Make good progress through the list with each
high traverse
Randomized Data
Structures
CSE 326 Autumn 2001
14
Randomized Skip List
 Sorted linked list
 # of links of a node is its height
 The height i link of each node (that has one) links to
the next node of height i or greater
 There should be about 1/2 as many height i+1 nodes
as height i nodes
2
19
23
8
13
29
20
10
22
11
Randomized Data
Structures
CSE 326 Autumn 2001
15
Find in a RSL
 Start i at the maximum height
 Until the node is found or i is one and the
next node is too large:
 If the next node along the i link is less than the
target, traverse to the next node
 Otherwise, decrease i by one
Same as for a perfect skip list!
Runtime?
Randomized Data
Structures
CSE 326 Autumn 2001
16
Insertion in a RSL
 Flip a coin until it comes up heads; that takes
i flips. Make the new node’s height i.
 Do a find, remembering nodes where we go
down
 Put the node at the spot where the find ends
 Point all the nodes where we went down (up
to the new node’s height) at the new node
 Point the new node’s links where those
redirected pointers were pointing
RSL Insert Example
2
19
23
8
13
29
20
10
11
insert(22)
with 3 flips
2
19
23
8
13
29
20
10
22
11
Runtime?
Randomized Data
Structures
CSE 326 Autumn 2001
18
Range Queries and Iteration
Range query: search for everything that falls between
two values
 find start point
 walk list at the bottom
 output each node until the end point
Iteration: successively return (in order) each element in
the structure
 start at beginning, walk list to end
 Just like a linked list!
Runtimes?
Randomized Data
Structures
CSE 326 Autumn 2001
19
Randomized Skip List
Summary
 Implements Dictionary ADT
 insert in expected O(log n)
 find in expected O(log n)
 Memory use
 expected constant memory per node
 about double a linked list
 Less overhead than search trees for iteration
over range queries
Randomized Data
Structures
CSE 326 Autumn 2001
20
Random Number Generation
Linear congruential generator
 xi+1 = Axi mod M
Choose A, M to minimize repetitions
 M is prime
 (Axi mod M) cycles through {1, … , M-1} before repeating
 Good choices:
M = 231
– 1 = 2,147,483,647
A = 48,271
Must handle overflow!
Randomized Data
Structures
CSE 326 Autumn 2001
21
Some Properties of Primes
If:
 P is prime
 0 < X < P
Then:
 XP-1
= 1 (mod P)
 the only solutions to X2
= 1 (mod P) are:
X = 1 and X = P - 1
Randomized Data
Structures
CSE 326 Autumn 2001
22
Checking Non-Primality
Exponentiation (pow function, Weiss 2.4.4) can be
modified to detect non-primality (composite-ness)
// Computes x^n mod(M)
HugeInt pow(HugeInt x, HugeInt n, HugeInt M) {
if (n == 0) return 1;
if (n == 1) return x;
HugeInt squared = x * x % M;
// 1 < x < M - 1 && squared == 1 --> M isn’t prime!
if (isEven(n)) return pow(squared, n/2, M);
else return (pow(squared, n/2, M) * x);
}
Randomized Data
Structures
CSE 326 Autumn 2001
23
Checking Primality
Systematic algorithm
For prime P, for all A such that 0 < A < P
Calculate AP-1
mod P using pow
Check at each step of pow and at end for primality conditions
Randomized algorithm
Use one random A
 If the randomized algorithm reports failure, then P isn’t prime.
 If the randomized algorithm reports success, then P might be prime.
 At most (P-9)/4 values of A fool this prime check
 if algorithm reports success, then P is prime with probability > ¾
Each new A has independent probability of false positive
 We can make probability of false positive arbitrarily small
Randomized Data
Structures
CSE 326 Autumn 2001
24
Evaluating
Randomized Primality Testing
Your probability of being struck by lightning this year:
0.00004%
Your probability that a number that tests prime 11 times
in a row is actually not prime: 0.00003%
Your probability of winning a lottery of 1 million people
five times in a row: 1 in 2100
Your probability that a number that tests prime 50 times
in a row is actually not prime: 1 in 2100
Randomized Data
Structures
CSE 326 Autumn 2001
25
Cycles
Prime numbers
Random numbers
Randomized
algorithms

Mais conteúdo relacionado

Mais procurados

Register allocation and assignment
Register allocation and assignmentRegister allocation and assignment
Register allocation and assignmentKarthi Keyan
 
Quick sort-Data Structure
Quick sort-Data StructureQuick sort-Data Structure
Quick sort-Data StructureJeanie Arnoco
 
13. Query Processing in DBMS
13. Query Processing in DBMS13. Query Processing in DBMS
13. Query Processing in DBMSkoolkampus
 
Basic blocks and control flow graphs
Basic blocks and control flow graphsBasic blocks and control flow graphs
Basic blocks and control flow graphsTilakpoudel2
 
Lecture 21 problem reduction search ao star search
Lecture 21 problem reduction search ao star searchLecture 21 problem reduction search ao star search
Lecture 21 problem reduction search ao star searchHema Kashyap
 
12. Indexing and Hashing in DBMS
12. Indexing and Hashing in DBMS12. Indexing and Hashing in DBMS
12. Indexing and Hashing in DBMSkoolkampus
 
Topological Sorting
Topological SortingTopological Sorting
Topological SortingShahDhruv21
 
Support vector machines (svm)
Support vector machines (svm)Support vector machines (svm)
Support vector machines (svm)Sharayu Patil
 
RECURSIVE DESCENT PARSING
RECURSIVE DESCENT PARSINGRECURSIVE DESCENT PARSING
RECURSIVE DESCENT PARSINGJothi Lakshmi
 
Lecture 17 Iterative Deepening a star algorithm
Lecture 17 Iterative Deepening a star algorithmLecture 17 Iterative Deepening a star algorithm
Lecture 17 Iterative Deepening a star algorithmHema Kashyap
 
Stressen's matrix multiplication
Stressen's matrix multiplicationStressen's matrix multiplication
Stressen's matrix multiplicationKumar
 
Transaction management
Transaction managementTransaction management
Transaction managementrenuka_a
 
Code optimization in compiler design
Code optimization in compiler designCode optimization in compiler design
Code optimization in compiler designKuppusamy P
 

Mais procurados (20)

Register allocation and assignment
Register allocation and assignmentRegister allocation and assignment
Register allocation and assignment
 
Quick sort-Data Structure
Quick sort-Data StructureQuick sort-Data Structure
Quick sort-Data Structure
 
13. Query Processing in DBMS
13. Query Processing in DBMS13. Query Processing in DBMS
13. Query Processing in DBMS
 
Chapter 5 Syntax Directed Translation
Chapter 5   Syntax Directed TranslationChapter 5   Syntax Directed Translation
Chapter 5 Syntax Directed Translation
 
Basic blocks and control flow graphs
Basic blocks and control flow graphsBasic blocks and control flow graphs
Basic blocks and control flow graphs
 
Dynamic Itemset Counting
Dynamic Itemset CountingDynamic Itemset Counting
Dynamic Itemset Counting
 
Lecture 21 problem reduction search ao star search
Lecture 21 problem reduction search ao star searchLecture 21 problem reduction search ao star search
Lecture 21 problem reduction search ao star search
 
Hashing
HashingHashing
Hashing
 
Recognition-of-tokens
Recognition-of-tokensRecognition-of-tokens
Recognition-of-tokens
 
12. Indexing and Hashing in DBMS
12. Indexing and Hashing in DBMS12. Indexing and Hashing in DBMS
12. Indexing and Hashing in DBMS
 
Topological Sorting
Topological SortingTopological Sorting
Topological Sorting
 
AI Lecture 3 (solving problems by searching)
AI Lecture 3 (solving problems by searching)AI Lecture 3 (solving problems by searching)
AI Lecture 3 (solving problems by searching)
 
Support vector machines (svm)
Support vector machines (svm)Support vector machines (svm)
Support vector machines (svm)
 
RECURSIVE DESCENT PARSING
RECURSIVE DESCENT PARSINGRECURSIVE DESCENT PARSING
RECURSIVE DESCENT PARSING
 
Lecture 17 Iterative Deepening a star algorithm
Lecture 17 Iterative Deepening a star algorithmLecture 17 Iterative Deepening a star algorithm
Lecture 17 Iterative Deepening a star algorithm
 
ER-Model-ER Diagram
ER-Model-ER DiagramER-Model-ER Diagram
ER-Model-ER Diagram
 
Stressen's matrix multiplication
Stressen's matrix multiplicationStressen's matrix multiplication
Stressen's matrix multiplication
 
Transaction management
Transaction managementTransaction management
Transaction management
 
B and B+ tree
B and B+ treeB and B+ tree
B and B+ tree
 
Code optimization in compiler design
Code optimization in compiler designCode optimization in compiler design
Code optimization in compiler design
 

Destaque

Faster and smaller inverted indices with Treaps Research Paper
Faster and smaller inverted indices with Treaps Research PaperFaster and smaller inverted indices with Treaps Research Paper
Faster and smaller inverted indices with Treaps Research Papersameiralk
 
5.4 mining sequence patterns in biological data
5.4 mining sequence patterns in biological data5.4 mining sequence patterns in biological data
5.4 mining sequence patterns in biological dataKrish_ver2
 
Stanford online courses 2014
Stanford online courses 2014Stanford online courses 2014
Stanford online courses 2014Neeraj Mandhana
 
Chapter - 8.4 Data Mining Concepts and Techniques 2nd Ed slides Han & Kamber
Chapter - 8.4 Data Mining Concepts and Techniques 2nd Ed slides Han & KamberChapter - 8.4 Data Mining Concepts and Techniques 2nd Ed slides Han & Kamber
Chapter - 8.4 Data Mining Concepts and Techniques 2nd Ed slides Han & Kambererror007
 
1.9 b trees eg 03
1.9 b trees eg 031.9 b trees eg 03
1.9 b trees eg 03Krish_ver2
 
160607 14 sw교육_강의안
160607 14 sw교육_강의안160607 14 sw교육_강의안
160607 14 sw교육_강의안Choi Man Dream
 
5.4 randamized algorithm
5.4 randamized algorithm5.4 randamized algorithm
5.4 randamized algorithmKrish_ver2
 
trabajo de cultural
trabajo de culturaltrabajo de cultural
trabajo de culturalargelures
 
CV Belinda Wahl 2015
CV Belinda Wahl 2015CV Belinda Wahl 2015
CV Belinda Wahl 2015Belinda Wahl
 
5.2 divede and conquer 03
5.2 divede and conquer 035.2 divede and conquer 03
5.2 divede and conquer 03Krish_ver2
 
평범한 이야기[Intro: 2015 의기제]
평범한 이야기[Intro: 2015 의기제]평범한 이야기[Intro: 2015 의기제]
평범한 이야기[Intro: 2015 의기제]대호 이
 
5.4 randomized datastructures
5.4 randomized datastructures5.4 randomized datastructures
5.4 randomized datastructuresKrish_ver2
 
문제는 한글이 잘 구현되는가?
문제는 한글이 잘 구현되는가?문제는 한글이 잘 구현되는가?
문제는 한글이 잘 구현되는가?Choi Man Dream
 

Destaque (20)

Faster and smaller inverted indices with Treaps Research Paper
Faster and smaller inverted indices with Treaps Research PaperFaster and smaller inverted indices with Treaps Research Paper
Faster and smaller inverted indices with Treaps Research Paper
 
Algorithms
AlgorithmsAlgorithms
Algorithms
 
5.4 mining sequence patterns in biological data
5.4 mining sequence patterns in biological data5.4 mining sequence patterns in biological data
5.4 mining sequence patterns in biological data
 
Stanford online courses 2014
Stanford online courses 2014Stanford online courses 2014
Stanford online courses 2014
 
Data structures
Data structuresData structures
Data structures
 
Chapter - 8.4 Data Mining Concepts and Techniques 2nd Ed slides Han & Kamber
Chapter - 8.4 Data Mining Concepts and Techniques 2nd Ed slides Han & KamberChapter - 8.4 Data Mining Concepts and Techniques 2nd Ed slides Han & Kamber
Chapter - 8.4 Data Mining Concepts and Techniques 2nd Ed slides Han & Kamber
 
1.9 b trees eg 03
1.9 b trees eg 031.9 b trees eg 03
1.9 b trees eg 03
 
160607 14 sw교육_강의안
160607 14 sw교육_강의안160607 14 sw교육_강의안
160607 14 sw교육_강의안
 
5.4 randamized algorithm
5.4 randamized algorithm5.4 randamized algorithm
5.4 randamized algorithm
 
trabajo de cultural
trabajo de culturaltrabajo de cultural
trabajo de cultural
 
CV Belinda Wahl 2015
CV Belinda Wahl 2015CV Belinda Wahl 2015
CV Belinda Wahl 2015
 
4.4 hashing02
4.4 hashing024.4 hashing02
4.4 hashing02
 
5.2 divede and conquer 03
5.2 divede and conquer 035.2 divede and conquer 03
5.2 divede and conquer 03
 
평범한 이야기[Intro: 2015 의기제]
평범한 이야기[Intro: 2015 의기제]평범한 이야기[Intro: 2015 의기제]
평범한 이야기[Intro: 2015 의기제]
 
РЕКЛАМНЫЕ МОДЕЛИ
РЕКЛАМНЫЕ МОДЕЛИРЕКЛАМНЫЕ МОДЕЛИ
РЕКЛАМНЫЕ МОДЕЛИ
 
5.4 randomized datastructures
5.4 randomized datastructures5.4 randomized datastructures
5.4 randomized datastructures
 
Salario minimo basico
Salario minimo basicoSalario minimo basico
Salario minimo basico
 
문제는 한글이 잘 구현되는가?
문제는 한글이 잘 구현되는가?문제는 한글이 잘 구현되는가?
문제는 한글이 잘 구현되는가?
 
4.2 bst 03
4.2 bst 034.2 bst 03
4.2 bst 03
 
RESUME-ARITRA BHOWMIK
RESUME-ARITRA BHOWMIKRESUME-ARITRA BHOWMIK
RESUME-ARITRA BHOWMIK
 

Semelhante a 5.4 randomized datastructures

Scalable Simple Random Sampling Algorithms
Scalable Simple Random Sampling AlgorithmsScalable Simple Random Sampling Algorithms
Scalable Simple Random Sampling AlgorithmsXiangrui Meng
 
Chapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for printChapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for printAbdii Rashid
 
Asymptotic Notation and Data Structures
Asymptotic Notation and Data StructuresAsymptotic Notation and Data Structures
Asymptotic Notation and Data StructuresAmrinder Arora
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithmsJulie Iskander
 
Basics in algorithms and data structure
Basics in algorithms and data structure Basics in algorithms and data structure
Basics in algorithms and data structure Eman magdy
 
Stevens-Benchmarking Sorting Algorithms
Stevens-Benchmarking Sorting AlgorithmsStevens-Benchmarking Sorting Algorithms
Stevens-Benchmarking Sorting AlgorithmsJames Stevens
 
Uwe Friedrichsen – Extreme availability and self-healing data with CRDTs - No...
Uwe Friedrichsen – Extreme availability and self-healing data with CRDTs - No...Uwe Friedrichsen – Extreme availability and self-healing data with CRDTs - No...
Uwe Friedrichsen – Extreme availability and self-healing data with CRDTs - No...NoSQLmatters
 
lecture 11
lecture 11lecture 11
lecture 11sajinsc
 
Lecture 02: Preliminaries of Data structure
Lecture 02: Preliminaries of Data structureLecture 02: Preliminaries of Data structure
Lecture 02: Preliminaries of Data structureNurjahan Nipa
 

Semelhante a 5.4 randomized datastructures (20)

Bioinformatics t4-alignments v2014
Bioinformatics t4-alignments v2014Bioinformatics t4-alignments v2014
Bioinformatics t4-alignments v2014
 
Bioinformatica 27-10-2011-t4-alignments
Bioinformatica 27-10-2011-t4-alignmentsBioinformatica 27-10-2011-t4-alignments
Bioinformatica 27-10-2011-t4-alignments
 
lecture4.pdf
lecture4.pdflecture4.pdf
lecture4.pdf
 
12 Sorting Techniques.pptx
12 Sorting Techniques.pptx12 Sorting Techniques.pptx
12 Sorting Techniques.pptx
 
Scalable Simple Random Sampling Algorithms
Scalable Simple Random Sampling AlgorithmsScalable Simple Random Sampling Algorithms
Scalable Simple Random Sampling Algorithms
 
Chapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for printChapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for print
 
Asymptotic Notation and Data Structures
Asymptotic Notation and Data StructuresAsymptotic Notation and Data Structures
Asymptotic Notation and Data Structures
 
Cis435 week06
Cis435 week06Cis435 week06
Cis435 week06
 
Quick sort.pptx
Quick sort.pptxQuick sort.pptx
Quick sort.pptx
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
Basics in algorithms and data structure
Basics in algorithms and data structure Basics in algorithms and data structure
Basics in algorithms and data structure
 
Stevens-Benchmarking Sorting Algorithms
Stevens-Benchmarking Sorting AlgorithmsStevens-Benchmarking Sorting Algorithms
Stevens-Benchmarking Sorting Algorithms
 
Uwe Friedrichsen – Extreme availability and self-healing data with CRDTs - No...
Uwe Friedrichsen – Extreme availability and self-healing data with CRDTs - No...Uwe Friedrichsen – Extreme availability and self-healing data with CRDTs - No...
Uwe Friedrichsen – Extreme availability and self-healing data with CRDTs - No...
 
Data Structures 6
Data Structures 6Data Structures 6
Data Structures 6
 
Sorting2
Sorting2Sorting2
Sorting2
 
Data structures
Data structuresData structures
Data structures
 
Data Structure (MC501)
Data Structure (MC501)Data Structure (MC501)
Data Structure (MC501)
 
s4_quick_sort.ppt
s4_quick_sort.ppts4_quick_sort.ppt
s4_quick_sort.ppt
 
lecture 11
lecture 11lecture 11
lecture 11
 
Lecture 02: Preliminaries of Data structure
Lecture 02: Preliminaries of Data structureLecture 02: Preliminaries of Data structure
Lecture 02: Preliminaries of Data structure
 

Mais de Krish_ver2

5.5 back tracking
5.5 back tracking5.5 back tracking
5.5 back trackingKrish_ver2
 
5.5 back track
5.5 back track5.5 back track
5.5 back trackKrish_ver2
 
5.5 back tracking 02
5.5 back tracking 025.5 back tracking 02
5.5 back tracking 02Krish_ver2
 
5.3 dynamic programming 03
5.3 dynamic programming 035.3 dynamic programming 03
5.3 dynamic programming 03Krish_ver2
 
5.3 dynamic programming
5.3 dynamic programming5.3 dynamic programming
5.3 dynamic programmingKrish_ver2
 
5.3 dyn algo-i
5.3 dyn algo-i5.3 dyn algo-i
5.3 dyn algo-iKrish_ver2
 
5.2 divede and conquer 03
5.2 divede and conquer 035.2 divede and conquer 03
5.2 divede and conquer 03Krish_ver2
 
5.2 divide and conquer
5.2 divide and conquer5.2 divide and conquer
5.2 divide and conquerKrish_ver2
 
5.1 greedyyy 02
5.1 greedyyy 025.1 greedyyy 02
5.1 greedyyy 02Krish_ver2
 
4.4 hashing ext
4.4 hashing  ext4.4 hashing  ext
4.4 hashing extKrish_ver2
 
4.4 external hashing
4.4 external hashing4.4 external hashing
4.4 external hashingKrish_ver2
 
4.1 sequentioal search
4.1 sequentioal search4.1 sequentioal search
4.1 sequentioal searchKrish_ver2
 
3.9 external sorting
3.9 external sorting3.9 external sorting
3.9 external sortingKrish_ver2
 
3.8 quick sort
3.8 quick sort3.8 quick sort
3.8 quick sortKrish_ver2
 

Mais de Krish_ver2 (20)

5.5 back tracking
5.5 back tracking5.5 back tracking
5.5 back tracking
 
5.5 back track
5.5 back track5.5 back track
5.5 back track
 
5.5 back tracking 02
5.5 back tracking 025.5 back tracking 02
5.5 back tracking 02
 
5.3 dynamic programming 03
5.3 dynamic programming 035.3 dynamic programming 03
5.3 dynamic programming 03
 
5.3 dynamic programming
5.3 dynamic programming5.3 dynamic programming
5.3 dynamic programming
 
5.3 dyn algo-i
5.3 dyn algo-i5.3 dyn algo-i
5.3 dyn algo-i
 
5.2 divede and conquer 03
5.2 divede and conquer 035.2 divede and conquer 03
5.2 divede and conquer 03
 
5.2 divide and conquer
5.2 divide and conquer5.2 divide and conquer
5.2 divide and conquer
 
5.1 greedyyy 02
5.1 greedyyy 025.1 greedyyy 02
5.1 greedyyy 02
 
5.1 greedy
5.1 greedy5.1 greedy
5.1 greedy
 
5.1 greedy 03
5.1 greedy 035.1 greedy 03
5.1 greedy 03
 
4.4 hashing
4.4 hashing4.4 hashing
4.4 hashing
 
4.4 hashing ext
4.4 hashing  ext4.4 hashing  ext
4.4 hashing ext
 
4.4 external hashing
4.4 external hashing4.4 external hashing
4.4 external hashing
 
4.2 bst
4.2 bst4.2 bst
4.2 bst
 
4.2 bst 02
4.2 bst 024.2 bst 02
4.2 bst 02
 
4.1 sequentioal search
4.1 sequentioal search4.1 sequentioal search
4.1 sequentioal search
 
3.9 external sorting
3.9 external sorting3.9 external sorting
3.9 external sorting
 
3.8 quicksort
3.8 quicksort3.8 quicksort
3.8 quicksort
 
3.8 quick sort
3.8 quick sort3.8 quick sort
3.8 quick sort
 

Último

A gentle introduction to Artificial Intelligence
A gentle introduction to Artificial IntelligenceA gentle introduction to Artificial Intelligence
A gentle introduction to Artificial IntelligenceApostolos Syropoulos
 
5 charts on South Africa as a source country for international student recrui...
5 charts on South Africa as a source country for international student recrui...5 charts on South Africa as a source country for international student recrui...
5 charts on South Africa as a source country for international student recrui...CaraSkikne1
 
3.26.24 Race, the Draft, and the Vietnam War.pptx
3.26.24 Race, the Draft, and the Vietnam War.pptx3.26.24 Race, the Draft, and the Vietnam War.pptx
3.26.24 Race, the Draft, and the Vietnam War.pptxmary850239
 
Protein Structure - threading Protein modelling pptx
Protein Structure - threading Protein modelling pptxProtein Structure - threading Protein modelling pptx
Protein Structure - threading Protein modelling pptxvidhisharma994099
 
Work Experience for psp3 portfolio sasha
Work Experience for psp3 portfolio sashaWork Experience for psp3 portfolio sasha
Work Experience for psp3 portfolio sashasashalaycock03
 
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRA
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRADUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRA
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRATanmoy Mishra
 
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...Nguyen Thanh Tu Collection
 
P4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdf
P4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdfP4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdf
P4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdfYu Kanazawa / Osaka University
 
How to Add a New Field in Existing Kanban View in Odoo 17
How to Add a New Field in Existing Kanban View in Odoo 17How to Add a New Field in Existing Kanban View in Odoo 17
How to Add a New Field in Existing Kanban View in Odoo 17Celine George
 
EBUS5423 Data Analytics and Reporting Bl
EBUS5423 Data Analytics and Reporting BlEBUS5423 Data Analytics and Reporting Bl
EBUS5423 Data Analytics and Reporting BlDr. Bruce A. Johnson
 
SOLIDE WASTE in Cameroon,,,,,,,,,,,,,,,,,,,,,,,,,,,.pptx
SOLIDE WASTE in Cameroon,,,,,,,,,,,,,,,,,,,,,,,,,,,.pptxSOLIDE WASTE in Cameroon,,,,,,,,,,,,,,,,,,,,,,,,,,,.pptx
SOLIDE WASTE in Cameroon,,,,,,,,,,,,,,,,,,,,,,,,,,,.pptxSyedNadeemGillANi
 
Patient Counselling. Definition of patient counseling; steps involved in pati...
Patient Counselling. Definition of patient counseling; steps involved in pati...Patient Counselling. Definition of patient counseling; steps involved in pati...
Patient Counselling. Definition of patient counseling; steps involved in pati...raviapr7
 
Education and training program in the hospital APR.pptx
Education and training program in the hospital APR.pptxEducation and training program in the hospital APR.pptx
Education and training program in the hospital APR.pptxraviapr7
 
AUDIENCE THEORY -- FANDOM -- JENKINS.pptx
AUDIENCE THEORY -- FANDOM -- JENKINS.pptxAUDIENCE THEORY -- FANDOM -- JENKINS.pptx
AUDIENCE THEORY -- FANDOM -- JENKINS.pptxiammrhaywood
 
Unveiling the Intricacies of Leishmania donovani: Structure, Life Cycle, Path...
Unveiling the Intricacies of Leishmania donovani: Structure, Life Cycle, Path...Unveiling the Intricacies of Leishmania donovani: Structure, Life Cycle, Path...
Unveiling the Intricacies of Leishmania donovani: Structure, Life Cycle, Path...Dr. Asif Anas
 
The Stolen Bacillus by Herbert George Wells
The Stolen Bacillus by Herbert George WellsThe Stolen Bacillus by Herbert George Wells
The Stolen Bacillus by Herbert George WellsEugene Lysak
 
HED Office Sohayok Exam Question Solution 2023.pdf
HED Office Sohayok Exam Question Solution 2023.pdfHED Office Sohayok Exam Question Solution 2023.pdf
HED Office Sohayok Exam Question Solution 2023.pdfMohonDas
 

Último (20)

A gentle introduction to Artificial Intelligence
A gentle introduction to Artificial IntelligenceA gentle introduction to Artificial Intelligence
A gentle introduction to Artificial Intelligence
 
5 charts on South Africa as a source country for international student recrui...
5 charts on South Africa as a source country for international student recrui...5 charts on South Africa as a source country for international student recrui...
5 charts on South Africa as a source country for international student recrui...
 
3.26.24 Race, the Draft, and the Vietnam War.pptx
3.26.24 Race, the Draft, and the Vietnam War.pptx3.26.24 Race, the Draft, and the Vietnam War.pptx
3.26.24 Race, the Draft, and the Vietnam War.pptx
 
Protein Structure - threading Protein modelling pptx
Protein Structure - threading Protein modelling pptxProtein Structure - threading Protein modelling pptx
Protein Structure - threading Protein modelling pptx
 
Work Experience for psp3 portfolio sasha
Work Experience for psp3 portfolio sashaWork Experience for psp3 portfolio sasha
Work Experience for psp3 portfolio sasha
 
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRA
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRADUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRA
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRA
 
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...
 
P4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdf
P4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdfP4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdf
P4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdf
 
How to Add a New Field in Existing Kanban View in Odoo 17
How to Add a New Field in Existing Kanban View in Odoo 17How to Add a New Field in Existing Kanban View in Odoo 17
How to Add a New Field in Existing Kanban View in Odoo 17
 
Personal Resilience in Project Management 2 - TV Edit 1a.pdf
Personal Resilience in Project Management 2 - TV Edit 1a.pdfPersonal Resilience in Project Management 2 - TV Edit 1a.pdf
Personal Resilience in Project Management 2 - TV Edit 1a.pdf
 
EBUS5423 Data Analytics and Reporting Bl
EBUS5423 Data Analytics and Reporting BlEBUS5423 Data Analytics and Reporting Bl
EBUS5423 Data Analytics and Reporting Bl
 
March 2024 Directors Meeting, Division of Student Affairs and Academic Support
March 2024 Directors Meeting, Division of Student Affairs and Academic SupportMarch 2024 Directors Meeting, Division of Student Affairs and Academic Support
March 2024 Directors Meeting, Division of Student Affairs and Academic Support
 
SOLIDE WASTE in Cameroon,,,,,,,,,,,,,,,,,,,,,,,,,,,.pptx
SOLIDE WASTE in Cameroon,,,,,,,,,,,,,,,,,,,,,,,,,,,.pptxSOLIDE WASTE in Cameroon,,,,,,,,,,,,,,,,,,,,,,,,,,,.pptx
SOLIDE WASTE in Cameroon,,,,,,,,,,,,,,,,,,,,,,,,,,,.pptx
 
Patient Counselling. Definition of patient counseling; steps involved in pati...
Patient Counselling. Definition of patient counseling; steps involved in pati...Patient Counselling. Definition of patient counseling; steps involved in pati...
Patient Counselling. Definition of patient counseling; steps involved in pati...
 
Education and training program in the hospital APR.pptx
Education and training program in the hospital APR.pptxEducation and training program in the hospital APR.pptx
Education and training program in the hospital APR.pptx
 
Finals of Kant get Marx 2.0 : a general politics quiz
Finals of Kant get Marx 2.0 : a general politics quizFinals of Kant get Marx 2.0 : a general politics quiz
Finals of Kant get Marx 2.0 : a general politics quiz
 
AUDIENCE THEORY -- FANDOM -- JENKINS.pptx
AUDIENCE THEORY -- FANDOM -- JENKINS.pptxAUDIENCE THEORY -- FANDOM -- JENKINS.pptx
AUDIENCE THEORY -- FANDOM -- JENKINS.pptx
 
Unveiling the Intricacies of Leishmania donovani: Structure, Life Cycle, Path...
Unveiling the Intricacies of Leishmania donovani: Structure, Life Cycle, Path...Unveiling the Intricacies of Leishmania donovani: Structure, Life Cycle, Path...
Unveiling the Intricacies of Leishmania donovani: Structure, Life Cycle, Path...
 
The Stolen Bacillus by Herbert George Wells
The Stolen Bacillus by Herbert George WellsThe Stolen Bacillus by Herbert George Wells
The Stolen Bacillus by Herbert George Wells
 
HED Office Sohayok Exam Question Solution 2023.pdf
HED Office Sohayok Exam Question Solution 2023.pdfHED Office Sohayok Exam Question Solution 2023.pdf
HED Office Sohayok Exam Question Solution 2023.pdf
 

5.4 randomized datastructures

  • 1. CSE 326 Randomized Data Structures David Kaplan Dept of Computer Science & Engineering Autumn 2001
  • 2. Randomized Data Structures CSE 326 Autumn 2001 2 Motivation for Randomized Data Structures We’ve seen many data structures with good average case performance on random inputs, but bad behavior on particular inputs  e.g. Binary Search Trees  Instead of randomizing the input (since we cannot!), consider randomizing the data structure  No bad inputs, just unlucky random numbers  Expected case good behavior on any input
  • 3. Randomized Data Structures CSE 326 Autumn 2001 3 Average vs. Expected Time Average (1/N) • ∑ xi Expectation ∑ Pr(xi) • xi Deterministic with good average time  If your application happens to always (or often) use the “bad” case, you are in big trouble! Randomized with good expected time  Once in a while you will have an expensive operation, but no inputs can make this happen all the time Like an insurance policy for your algorithm!
  • 4. Randomized Data Structures CSE 326 Autumn 2001 4 Randomized Data Structures  Define a property (or subroutine) in an algorithm  Sample or randomly modify the property  Use altered property as if it were the true property Can transform average case runtimes into expected runtimes (remove input dependency). Sometimes allows substantial speedup in exchange for probabilistic unsoundness.
  • 5. Randomized Data Structures CSE 326 Autumn 2001 5 Randomization in Action  Quicksort  Randomized data structures  Treaps  Randomized skip lists  Random number generation  Primality testing
  • 6. Randomized Data Structures CSE 326 Autumn 2001 6 Treap Dictionary Data Structure Treap is a BST  binary tree property  search tree property Treap is also a heap  heap-order property  random priorities 15 12 10 30 9 15 7 8 4 18 6 7 2 9 priority key
  • 7. Randomized Data Structures CSE 326 Autumn 2001 7 Treap Insert  Choose a random priority  Insert as in normal BST  Rotate up until heap order is restored 6 7 insert(15) 7 8 2 9 14 12 6 7 7 8 2 9 14 12 9 15 6 7 7 8 2 9 9 15 14 12
  • 8. Randomized Data Structures CSE 326 Autumn 2001 8 Tree + Heap… Why Bother?  Insert data in sorted order into a treap … What shape tree comes out? 6 7 insert(7) 6 7 insert(8) 7 8 6 7 insert(9) 7 8 2 9 6 7 insert(12) 7 8 2 9 15 12
  • 9. Treap Delete  Find the key  Increase its value to ∞  Rotate it to the fringe  Snip it off delete(9) 6 7 7 8 2⇒∞ 9 9 15 15 12 7 8 6 7 ∞ 9 9 15 15 12 7 8 6 7 9 15 ∞ 9 15 12 7 8 6 7 9 15 15 12 ∞ 9 7 8 6 7 9 15 15 12
  • 10. Randomized Data Structures CSE 326 Autumn 2001 10 Treap Summary  Implements Dictionary ADT  insert in expected O(log n) time  delete in expected O(log n) time  find in expected O(log n) time  but worst case O(n)  Memory use  O(1) per node  about the cost of AVL trees  Very simple to implement  little overhead – less than AVL trees
  • 11. Randomized Data Structures CSE 326 Autumn 2001 11 Perfect Binary Skip List  Sorted linked list  # of links of a node is its height  The height i link of each node (that has one) links to the next node of height i or greater 8 2 11 10 19 13 20 22 29 23
  • 12. Randomized Data Structures CSE 326 Autumn 2001 12 Find in a Perfect Binary Skip List  Start i at the maximum height  Until the node is found or i is one and the next node is too large:  If the next node along the i link is less than the target, traverse to the next node  Otherwise, decrease i by one Runtime?
  • 13. Randomized Data Structures CSE 326 Autumn 2001 13 Randomized Skip List Intuition It’s far too hard to insert into a perfect skip list But is perfection necessary? What matters in a skip list?  We want way fewer tall nodes than short ones  Make good progress through the list with each high traverse
  • 14. Randomized Data Structures CSE 326 Autumn 2001 14 Randomized Skip List  Sorted linked list  # of links of a node is its height  The height i link of each node (that has one) links to the next node of height i or greater  There should be about 1/2 as many height i+1 nodes as height i nodes 2 19 23 8 13 29 20 10 22 11
  • 15. Randomized Data Structures CSE 326 Autumn 2001 15 Find in a RSL  Start i at the maximum height  Until the node is found or i is one and the next node is too large:  If the next node along the i link is less than the target, traverse to the next node  Otherwise, decrease i by one Same as for a perfect skip list! Runtime?
  • 16. Randomized Data Structures CSE 326 Autumn 2001 16 Insertion in a RSL  Flip a coin until it comes up heads; that takes i flips. Make the new node’s height i.  Do a find, remembering nodes where we go down  Put the node at the spot where the find ends  Point all the nodes where we went down (up to the new node’s height) at the new node  Point the new node’s links where those redirected pointers were pointing
  • 17. RSL Insert Example 2 19 23 8 13 29 20 10 11 insert(22) with 3 flips 2 19 23 8 13 29 20 10 22 11 Runtime?
  • 18. Randomized Data Structures CSE 326 Autumn 2001 18 Range Queries and Iteration Range query: search for everything that falls between two values  find start point  walk list at the bottom  output each node until the end point Iteration: successively return (in order) each element in the structure  start at beginning, walk list to end  Just like a linked list! Runtimes?
  • 19. Randomized Data Structures CSE 326 Autumn 2001 19 Randomized Skip List Summary  Implements Dictionary ADT  insert in expected O(log n)  find in expected O(log n)  Memory use  expected constant memory per node  about double a linked list  Less overhead than search trees for iteration over range queries
  • 20. Randomized Data Structures CSE 326 Autumn 2001 20 Random Number Generation Linear congruential generator  xi+1 = Axi mod M Choose A, M to minimize repetitions  M is prime  (Axi mod M) cycles through {1, … , M-1} before repeating  Good choices: M = 231 – 1 = 2,147,483,647 A = 48,271 Must handle overflow!
  • 21. Randomized Data Structures CSE 326 Autumn 2001 21 Some Properties of Primes If:  P is prime  0 < X < P Then:  XP-1 = 1 (mod P)  the only solutions to X2 = 1 (mod P) are: X = 1 and X = P - 1
  • 22. Randomized Data Structures CSE 326 Autumn 2001 22 Checking Non-Primality Exponentiation (pow function, Weiss 2.4.4) can be modified to detect non-primality (composite-ness) // Computes x^n mod(M) HugeInt pow(HugeInt x, HugeInt n, HugeInt M) { if (n == 0) return 1; if (n == 1) return x; HugeInt squared = x * x % M; // 1 < x < M - 1 && squared == 1 --> M isn’t prime! if (isEven(n)) return pow(squared, n/2, M); else return (pow(squared, n/2, M) * x); }
  • 23. Randomized Data Structures CSE 326 Autumn 2001 23 Checking Primality Systematic algorithm For prime P, for all A such that 0 < A < P Calculate AP-1 mod P using pow Check at each step of pow and at end for primality conditions Randomized algorithm Use one random A  If the randomized algorithm reports failure, then P isn’t prime.  If the randomized algorithm reports success, then P might be prime.  At most (P-9)/4 values of A fool this prime check  if algorithm reports success, then P is prime with probability > ¾ Each new A has independent probability of false positive  We can make probability of false positive arbitrarily small
  • 24. Randomized Data Structures CSE 326 Autumn 2001 24 Evaluating Randomized Primality Testing Your probability of being struck by lightning this year: 0.00004% Your probability that a number that tests prime 11 times in a row is actually not prime: 0.00003% Your probability of winning a lottery of 1 million people five times in a row: 1 in 2100 Your probability that a number that tests prime 50 times in a row is actually not prime: 1 in 2100
  • 25. Randomized Data Structures CSE 326 Autumn 2001 25 Cycles Prime numbers Random numbers Randomized algorithms

Notas do Editor

  1. Notice that it doesn’t matter what order the input comes in. The shape of the tree is fully specified by what the keys are and what their random priorities are! So, there’s no bad inputs, only bad random numbers! That’s the difference between average time and expected time. Which one is better?
  2. Sorry about how packed this slide is. Basically, rotate the node down to the fringe and then cut it off. This is pretty simple as long as you have rotate, as well. However, you do need to find the smaller child as you go!
  3. The big advantage of this is that it’s simple compared to AVL or even splay. There’s no zig-zig vs. zig-zag vs. zig. Unfortunately, it doesn’t give worst case or even amortized O(log n) performance. It gives expected O(log n) performance.
  4. Now, onto something completely different.
  5. How many times can we go down? Log n. How many times can we go right? Also log n. So, the runtime is log n.
  6. Expected time here is O(log n). I won’t prove it, but the intuition is that we’ll probably cover enough distance at each level.
  7. To insert, we just need to decide what level to make the node, and the rest is pretty straightforward. How do we decide the height? Each time we flip a coin, we have a 1/2 chance of heads. So count the # of flips before a heads and put the node at that height. Let’s look at an example.
  8. The bold lines and boxes are the ones we traverse. How long does this take? The same as find (asymptotically), so expected O(log n).
  9. Now, one interesting thing here is how easy it is to do these two things. To range query: find the start point then walk along the linked list at the bottom outputting each node on the way until the end point.. Takes O(log n + m) where m is the final number of nodes in the query. Iteration is similar except we know the bounds: the start and end. It’s the same as a linked list! (O(n)). In other words, these support range queries and iteration easily. Search trees support them, but not always easily.