SlideShare a Scribd company logo
1 of 11
Skip Lists 1© 2004 Goodrich, Tamassia
Skip Lists
+∞−∞
S0
S1
S2
S3
+∞−∞ 10 362315
+∞−∞ 15
+∞−∞ 2315
Skip Lists 2© 2004 Goodrich, Tamassia
What is a Skip List
A skip list for a set S of distinct (key, element) items is a series of
lists S0, S1, … , Sh such that
 Each list Si contains the special keys +∞ and −∞
 List S0 contains the keys of S in nondecreasing order
 Each list is a subsequence of the previous one, i.e.,
S0 ⊇S1 ⊇ … ⊇ Sh
 List Shcontains only the two special keys
We show how to use a skip list to implement the dictionary ADT
56 64 78 +∞31 34 44−∞ 12 23 26
+∞−∞
+∞31−∞
64 +∞31 34−∞ 23
S0
S1
S2
S3
Skip Lists 3© 2004 Goodrich, Tamassia
Search
We search for a key x in a a skip list as follows:
 We start at the first position of the top list
 At the current position p, we compare x with y ← key(next(p))
x = y: we return element(next(p))
x > y: we “scan forward”
x < y: we “drop down”
 If we try to drop down past the bottom list, we return null
Example: search for 78
+∞−∞
S0
S1
S2
S3
+∞31−∞
64 +∞31 34−∞ 23
56 64 78 +∞31 34 44−∞ 12 23 26
Skip Lists 4© 2004 Goodrich, Tamassia
Randomized Algorithms
A randomized algorithm
performs coin tosses (i.e.,
uses random bits) to control
its execution
It contains statements of the
type
b ← random()
if b = 0
do A …
else { b = 1}
do B …
Its running time depends on
the outcomes of the coin
tosses
We analyze the expected
running time of a
randomized algorithm under
the following assumptions
 the coins are unbiased, and
 the coin tosses are
independent
The worst-case running time
of a randomized algorithm is
often large but has very low
probability (e.g., it occurs
when all the coin tosses give
“heads”)
We use a randomized
algorithm to insert items into
a skip list
Skip Lists 5© 2004 Goodrich, Tamassia
To insert an entry (x, o) into a skip list, we use a randomized
algorithm:
 We repeatedly toss a coin until we get tails, and we denote with i
the number of times the coin came up heads
 If i ≥ h, we add to the skip list new lists Sh+1, … , Si+1, each containing
only the two special keys
 We search for x in the skip list and find the positions p0, p1, …, piof
the items with largest key less than x in each list S0, S1, … , Si
 For j ← 0, …, i, we insert item (x, o) into list Sj after position pj
Example: insert key 15, with i = 2
Insertion
+∞−∞ 10 36
+∞−∞
23
23 +∞−∞
S0
S1
S2
+∞−∞
S0
S1
S2
S3
+∞−∞ 10 362315
+∞−∞ 15
+∞−∞ 2315
p0
p1
p2
Skip Lists 6© 2004 Goodrich, Tamassia
Deletion
To remove an entry with key x from a skip list, we proceed as
follows:
 We search for x in the skip list and find the positions p0, p1 , …, pi of
the items with key x, where position pj is in list Sj
 We remove positions p0, p1 , …, pi from the lists S0, S1, … , Si
 We remove all but one list containing only the two special keys
Example: remove key 34
−∞ +∞4512
−∞ +∞
23
23−∞ +∞
S0
S1
S2
−∞ +∞
S0
S1
S2
S3
−∞ +∞4512 23 34
−∞ +∞34
−∞ +∞23 34
p0
p1
p2
Skip Lists 7© 2004 Goodrich, Tamassia
Implementation
We can implement a skip list
with quad-nodes
A quad-node stores:
 entry
 link to the node prev
 link to the node next
 link to the node below
 link to the node above
Also, we define special keys
PLUS_INF and MINUS_INF,
and we modify the key
comparator to handle them
x
quad-node
Skip Lists 8© 2004 Goodrich, Tamassia
Space Usage
The space used by a skip list
depends on the random bits
used by each invocation of the
insertion algorithm
We use the following two basic
probabilistic facts:
Fact 1: The probability of getting i
consecutive heads when
flipping a coin is 1/2i
Fact 2: If each of n entries is
present in a set with probability
p, the expected size of the set
is np
Consider a skip list with n
entries
 By Fact 1, we insert an entry
in list Si with probability 1/2i
 By Fact 2, the expected size
of list Si is n/2i
The expected number of
nodes used by the skip list is
nn
n h
i
i
h
i
i
2
2
1
2 00
<= ∑∑
==
Thus, the expected space
usage of a skip list with n
items is O(n)
Skip Lists 9© 2004 Goodrich, Tamassia
Height
The running time of the
search an insertion
algorithms is affected by the
height h of the skip list
We show that with high
probability, a skip list with n
items has height O(log n)
We use the following
additional probabilistic fact:
Fact 3: If each of n events has
probability p, the probability
that at least one event
occurs is at most np
Consider a skip list with n
entires
 By Fact 1, we insert an entry
in list Si with probability 1/2i
 By Fact 3, the probability that
list Si has at least one item is
at most n/2i
By picking i = 3log n, we have
that the probability that S3logn has
at least one entry is
at most
n/23logn
= n/n3
= 1/n2
Thus a skip list with n entries
has height at most 3log n with
probability at least 1 − 1/n2
Skip Lists 10© 2004 Goodrich, Tamassia
Search and Update Times
The search time in a skip list
is proportional to
 the number of drop-down
steps, plus
 the number of scan-forward
steps
The drop-down steps are
bounded by the height of the
skip list and thus are O(log n)
with high probability
To analyze the scan-forward
steps, we use yet another
probabilistic fact:
Fact 4: The expected number of
coin tosses required in order
to get tails is 2
When we scan forward in a
list, the destination key does
not belong to a higher list
 A scan-forward step is
associated with a former coin
toss that gave tails
By Fact 4, in each list the
expected number of scan-
forward steps is 2
Thus, the expected number of
scan-forward steps is O(log n)
We conclude that a search in a
skip list takes O(log n)
expected time
The analysis of insertion and
deletion gives similar results
Skip Lists 11© 2004 Goodrich, Tamassia
Summary
A skip list is a data
structure for dictionaries
that uses a randomized
insertion algorithm
In a skip list with n
entries
 The expected space
used is O(n)
 The expected search,
insertion and deletion
time is O(log n)
Using a more complex
probabilistic analysis,
one can show that
these performance
bounds also hold with
high probability
Skip lists are fast and
simple to implement in
practice

More Related Content

Viewers also liked

5.2 divede and conquer 03
5.2 divede and conquer 035.2 divede and conquer 03
5.2 divede and conquer 03Krish_ver2
 
1.9 b trees eg 03
1.9 b trees eg 031.9 b trees eg 03
1.9 b trees eg 03Krish_ver2
 
문제는 한글이 잘 구현되는가?
문제는 한글이 잘 구현되는가?문제는 한글이 잘 구현되는가?
문제는 한글이 잘 구현되는가?Choi Man Dream
 
2.4 mst prim &kruskal demo
2.4 mst  prim &kruskal demo2.4 mst  prim &kruskal demo
2.4 mst prim &kruskal demoKrish_ver2
 
nhận thiết kế clip quảng cáo giá tốt
nhận thiết kế clip quảng cáo giá tốtnhận thiết kế clip quảng cáo giá tốt
nhận thiết kế clip quảng cáo giá tốtraul110
 
1.9 b trees 02
1.9 b trees 021.9 b trees 02
1.9 b trees 02Krish_ver2
 
CV Belinda Wahl 2015
CV Belinda Wahl 2015CV Belinda Wahl 2015
CV Belinda Wahl 2015Belinda Wahl
 
5.3 dyn algo-i
5.3 dyn algo-i5.3 dyn algo-i
5.3 dyn algo-iKrish_ver2
 
2.4 mst kruskal’s
2.4 mst  kruskal’s 2.4 mst  kruskal’s
2.4 mst kruskal’s Krish_ver2
 
trabajo de cultural
trabajo de culturaltrabajo de cultural
trabajo de culturalargelures
 
5.1 greedyyy 02
5.1 greedyyy 025.1 greedyyy 02
5.1 greedyyy 02Krish_ver2
 
Madhumita Project Coordinator/Business Analyst
Madhumita Project Coordinator/Business AnalystMadhumita Project Coordinator/Business Analyst
Madhumita Project Coordinator/Business AnalystMadhumita Bairagi
 

Viewers also liked (20)

5.2 divede and conquer 03
5.2 divede and conquer 035.2 divede and conquer 03
5.2 divede and conquer 03
 
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
 
4.4 hashing02
4.4 hashing024.4 hashing02
4.4 hashing02
 
1.9 b trees eg 03
1.9 b trees eg 031.9 b trees eg 03
1.9 b trees eg 03
 
РЕКЛАМНЫЕ МОДЕЛИ
РЕКЛАМНЫЕ МОДЕЛИРЕКЛАМНЫЕ МОДЕЛИ
РЕКЛАМНЫЕ МОДЕЛИ
 
Top Forex Brokers
Top Forex BrokersTop Forex Brokers
Top Forex Brokers
 
문제는 한글이 잘 구현되는가?
문제는 한글이 잘 구현되는가?문제는 한글이 잘 구현되는가?
문제는 한글이 잘 구현되는가?
 
4.2 bst 02
4.2 bst 024.2 bst 02
4.2 bst 02
 
Online Trading Concepts
Online Trading ConceptsOnline Trading Concepts
Online Trading Concepts
 
2.4 mst prim &kruskal demo
2.4 mst  prim &kruskal demo2.4 mst  prim &kruskal demo
2.4 mst prim &kruskal demo
 
nhận thiết kế clip quảng cáo giá tốt
nhận thiết kế clip quảng cáo giá tốtnhận thiết kế clip quảng cáo giá tốt
nhận thiết kế clip quảng cáo giá tốt
 
1.9 b trees 02
1.9 b trees 021.9 b trees 02
1.9 b trees 02
 
Salario minimo basico
Salario minimo basicoSalario minimo basico
Salario minimo basico
 
CV Belinda Wahl 2015
CV Belinda Wahl 2015CV Belinda Wahl 2015
CV Belinda Wahl 2015
 
5.3 dyn algo-i
5.3 dyn algo-i5.3 dyn algo-i
5.3 dyn algo-i
 
2.4 mst kruskal’s
2.4 mst  kruskal’s 2.4 mst  kruskal’s
2.4 mst kruskal’s
 
trabajo de cultural
trabajo de culturaltrabajo de cultural
trabajo de cultural
 
5.1 greedyyy 02
5.1 greedyyy 025.1 greedyyy 02
5.1 greedyyy 02
 
Madhumita Project Coordinator/Business Analyst
Madhumita Project Coordinator/Business AnalystMadhumita Project Coordinator/Business Analyst
Madhumita Project Coordinator/Business Analyst
 

Similar to 5.4 randamized algorithm

Skip lists (Advance Data structure)
Skip lists (Advance Data structure)Skip lists (Advance Data structure)
Skip lists (Advance Data structure)Shubham Shukla
 
computer notes - Data Structures - 34
computer notes - Data Structures - 34computer notes - Data Structures - 34
computer notes - Data Structures - 34ecomputernotes
 
Computer notes - Binary Search
Computer notes - Binary SearchComputer notes - Binary Search
Computer notes - Binary Searchecomputernotes
 
computer notes - Data Structures - 32
computer notes - Data Structures - 32computer notes - Data Structures - 32
computer notes - Data Structures - 32ecomputernotes
 
Skip list vinay khimsuriya_200430723005
Skip list vinay khimsuriya_200430723005Skip list vinay khimsuriya_200430723005
Skip list vinay khimsuriya_200430723005vinaykhimsuriya1
 
Lecture12,13,14.pdf
Lecture12,13,14.pdfLecture12,13,14.pdf
Lecture12,13,14.pdfzainab278016
 
In three of the exercises below there is given a code of a method na.pdf
In three of the exercises below there is given a code of a method na.pdfIn three of the exercises below there is given a code of a method na.pdf
In three of the exercises below there is given a code of a method na.pdffeelinggift
 
2Bytesprog2 course_2014_c6_single linked list
2Bytesprog2 course_2014_c6_single linked list2Bytesprog2 course_2014_c6_single linked list
2Bytesprog2 course_2014_c6_single linked listkinan keshkeh
 
LR(1) and SLR(1) parsing
LR(1) and SLR(1) parsingLR(1) and SLR(1) parsing
LR(1) and SLR(1) parsingR Islam
 
time_complexity_list_02_04_2024_22_pages.pdf
time_complexity_list_02_04_2024_22_pages.pdftime_complexity_list_02_04_2024_22_pages.pdf
time_complexity_list_02_04_2024_22_pages.pdfSrinivasaReddyPolamR
 

Similar to 5.4 randamized algorithm (13)

Skip lists (Advance Data structure)
Skip lists (Advance Data structure)Skip lists (Advance Data structure)
Skip lists (Advance Data structure)
 
computer notes - Data Structures - 34
computer notes - Data Structures - 34computer notes - Data Structures - 34
computer notes - Data Structures - 34
 
Computer notes - Binary Search
Computer notes - Binary SearchComputer notes - Binary Search
Computer notes - Binary Search
 
computer notes - Data Structures - 32
computer notes - Data Structures - 32computer notes - Data Structures - 32
computer notes - Data Structures - 32
 
Skip list vinay khimsuriya_200430723005
Skip list vinay khimsuriya_200430723005Skip list vinay khimsuriya_200430723005
Skip list vinay khimsuriya_200430723005
 
Lecture12,13,14.pdf
Lecture12,13,14.pdfLecture12,13,14.pdf
Lecture12,13,14.pdf
 
In three of the exercises below there is given a code of a method na.pdf
In three of the exercises below there is given a code of a method na.pdfIn three of the exercises below there is given a code of a method na.pdf
In three of the exercises below there is given a code of a method na.pdf
 
Sortsearch
SortsearchSortsearch
Sortsearch
 
Sorting
SortingSorting
Sorting
 
Chapter-2.pptx
Chapter-2.pptxChapter-2.pptx
Chapter-2.pptx
 
2Bytesprog2 course_2014_c6_single linked list
2Bytesprog2 course_2014_c6_single linked list2Bytesprog2 course_2014_c6_single linked list
2Bytesprog2 course_2014_c6_single linked list
 
LR(1) and SLR(1) parsing
LR(1) and SLR(1) parsingLR(1) and SLR(1) parsing
LR(1) and SLR(1) parsing
 
time_complexity_list_02_04_2024_22_pages.pdf
time_complexity_list_02_04_2024_22_pages.pdftime_complexity_list_02_04_2024_22_pages.pdf
time_complexity_list_02_04_2024_22_pages.pdf
 

More from 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.4 randomized datastructures
5.4 randomized datastructures5.4 randomized datastructures
5.4 randomized datastructuresKrish_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.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
 
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
 
3.8 quicksort 04
3.8 quicksort 043.8 quicksort 04
3.8 quicksort 04Krish_ver2
 

More from 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.4 randomized datastructures
5.4 randomized datastructures5.4 randomized datastructures
5.4 randomized datastructures
 
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.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 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.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
 
3.8 quicksort 04
3.8 quicksort 043.8 quicksort 04
3.8 quicksort 04
 
3.7 heap sort
3.7 heap sort3.7 heap sort
3.7 heap sort
 

Recently uploaded

Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Planning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxPlanning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxLigayaBacuel1
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayMakMakNepo
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 

Recently uploaded (20)

Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Planning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxPlanning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptx
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up Friday
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 

5.4 randamized algorithm

  • 1. Skip Lists 1© 2004 Goodrich, Tamassia Skip Lists +∞−∞ S0 S1 S2 S3 +∞−∞ 10 362315 +∞−∞ 15 +∞−∞ 2315
  • 2. Skip Lists 2© 2004 Goodrich, Tamassia What is a Skip List A skip list for a set S of distinct (key, element) items is a series of lists S0, S1, … , Sh such that  Each list Si contains the special keys +∞ and −∞  List S0 contains the keys of S in nondecreasing order  Each list is a subsequence of the previous one, i.e., S0 ⊇S1 ⊇ … ⊇ Sh  List Shcontains only the two special keys We show how to use a skip list to implement the dictionary ADT 56 64 78 +∞31 34 44−∞ 12 23 26 +∞−∞ +∞31−∞ 64 +∞31 34−∞ 23 S0 S1 S2 S3
  • 3. Skip Lists 3© 2004 Goodrich, Tamassia Search We search for a key x in a a skip list as follows:  We start at the first position of the top list  At the current position p, we compare x with y ← key(next(p)) x = y: we return element(next(p)) x > y: we “scan forward” x < y: we “drop down”  If we try to drop down past the bottom list, we return null Example: search for 78 +∞−∞ S0 S1 S2 S3 +∞31−∞ 64 +∞31 34−∞ 23 56 64 78 +∞31 34 44−∞ 12 23 26
  • 4. Skip Lists 4© 2004 Goodrich, Tamassia Randomized Algorithms A randomized algorithm performs coin tosses (i.e., uses random bits) to control its execution It contains statements of the type b ← random() if b = 0 do A … else { b = 1} do B … Its running time depends on the outcomes of the coin tosses We analyze the expected running time of a randomized algorithm under the following assumptions  the coins are unbiased, and  the coin tosses are independent The worst-case running time of a randomized algorithm is often large but has very low probability (e.g., it occurs when all the coin tosses give “heads”) We use a randomized algorithm to insert items into a skip list
  • 5. Skip Lists 5© 2004 Goodrich, Tamassia To insert an entry (x, o) into a skip list, we use a randomized algorithm:  We repeatedly toss a coin until we get tails, and we denote with i the number of times the coin came up heads  If i ≥ h, we add to the skip list new lists Sh+1, … , Si+1, each containing only the two special keys  We search for x in the skip list and find the positions p0, p1, …, piof the items with largest key less than x in each list S0, S1, … , Si  For j ← 0, …, i, we insert item (x, o) into list Sj after position pj Example: insert key 15, with i = 2 Insertion +∞−∞ 10 36 +∞−∞ 23 23 +∞−∞ S0 S1 S2 +∞−∞ S0 S1 S2 S3 +∞−∞ 10 362315 +∞−∞ 15 +∞−∞ 2315 p0 p1 p2
  • 6. Skip Lists 6© 2004 Goodrich, Tamassia Deletion To remove an entry with key x from a skip list, we proceed as follows:  We search for x in the skip list and find the positions p0, p1 , …, pi of the items with key x, where position pj is in list Sj  We remove positions p0, p1 , …, pi from the lists S0, S1, … , Si  We remove all but one list containing only the two special keys Example: remove key 34 −∞ +∞4512 −∞ +∞ 23 23−∞ +∞ S0 S1 S2 −∞ +∞ S0 S1 S2 S3 −∞ +∞4512 23 34 −∞ +∞34 −∞ +∞23 34 p0 p1 p2
  • 7. Skip Lists 7© 2004 Goodrich, Tamassia Implementation We can implement a skip list with quad-nodes A quad-node stores:  entry  link to the node prev  link to the node next  link to the node below  link to the node above Also, we define special keys PLUS_INF and MINUS_INF, and we modify the key comparator to handle them x quad-node
  • 8. Skip Lists 8© 2004 Goodrich, Tamassia Space Usage The space used by a skip list depends on the random bits used by each invocation of the insertion algorithm We use the following two basic probabilistic facts: Fact 1: The probability of getting i consecutive heads when flipping a coin is 1/2i Fact 2: If each of n entries is present in a set with probability p, the expected size of the set is np Consider a skip list with n entries  By Fact 1, we insert an entry in list Si with probability 1/2i  By Fact 2, the expected size of list Si is n/2i The expected number of nodes used by the skip list is nn n h i i h i i 2 2 1 2 00 <= ∑∑ == Thus, the expected space usage of a skip list with n items is O(n)
  • 9. Skip Lists 9© 2004 Goodrich, Tamassia Height The running time of the search an insertion algorithms is affected by the height h of the skip list We show that with high probability, a skip list with n items has height O(log n) We use the following additional probabilistic fact: Fact 3: If each of n events has probability p, the probability that at least one event occurs is at most np Consider a skip list with n entires  By Fact 1, we insert an entry in list Si with probability 1/2i  By Fact 3, the probability that list Si has at least one item is at most n/2i By picking i = 3log n, we have that the probability that S3logn has at least one entry is at most n/23logn = n/n3 = 1/n2 Thus a skip list with n entries has height at most 3log n with probability at least 1 − 1/n2
  • 10. Skip Lists 10© 2004 Goodrich, Tamassia Search and Update Times The search time in a skip list is proportional to  the number of drop-down steps, plus  the number of scan-forward steps The drop-down steps are bounded by the height of the skip list and thus are O(log n) with high probability To analyze the scan-forward steps, we use yet another probabilistic fact: Fact 4: The expected number of coin tosses required in order to get tails is 2 When we scan forward in a list, the destination key does not belong to a higher list  A scan-forward step is associated with a former coin toss that gave tails By Fact 4, in each list the expected number of scan- forward steps is 2 Thus, the expected number of scan-forward steps is O(log n) We conclude that a search in a skip list takes O(log n) expected time The analysis of insertion and deletion gives similar results
  • 11. Skip Lists 11© 2004 Goodrich, Tamassia Summary A skip list is a data structure for dictionaries that uses a randomized insertion algorithm In a skip list with n entries  The expected space used is O(n)  The expected search, insertion and deletion time is O(log n) Using a more complex probabilistic analysis, one can show that these performance bounds also hold with high probability Skip lists are fast and simple to implement in practice