SlideShare ist ein Scribd-Unternehmen logo
1 von 48
Downloaden Sie, um offline zu lesen
Algorithms
Augmenting Data Structures
Dynamic Order Statistics
● We’ve seen algorithms for finding the ith
element of an unordered set in O(n) time
● Next, a structure to support finding the ith
element of a dynamic set in O(lg n) time
■ What operations do dynamic sets usually support?
■ What structure works well for these?
■ How could we use this structure for order statistics?
■ How might we augment it to support efficient
extraction of order statistics?
Order Statistic Trees
● OS Trees augment red-black trees:
■ Associate a size field with each node in the tree
■ x->size records the size of subtree rooted at x,
including x itself:
M
8
C
5
P
2
Q
1
A
1
F
3
D
1
H
1
Selection On OS Trees
M
8
C
5
P
2
Q
1
A
1
F
3
D
1
H
1
How can we use this property
to select the ith element of the set?
OS-Select
OS-Select(x, i)
{
r = size[left[x]] + 1;
if (i == r)
return x;
else if (i < r)
return OS-Select(left[x], i);
else
return OS-Select(right[x], i-r);
}
OS-Select Example
● Example: show OS-Select(root, 5):
M
8
C
5
P
2
Q
1
A
1
F
3
D
1
H
1
OS-Select(x, i)
{
r = size[left[x]] + 1;
if (i == r)
return x;
else if (i < r)
return OS-Select(left[x], i);
else
return OS-Select(right[x], i-r);
}
OS-Select Example
● Example: show OS-Select(root, 5):
M
8
C
5
P
2
Q
1
A
1
F
3
D
1
H
1
i = 5
r = 6
OS-Select(x, i)
{
r = size[left[x]] + 1;
if (i == r)
return x;
else if (i < r)
return OS-Select(left[x], i);
else
return OS-Select(right[x], i-r);
}
OS-Select Example
● Example: show OS-Select(root, 5):
M
8
C
5
P
2
Q
1
A
1
F
3
D
1
H
1
OS-Select(x, i)
{
r = size[left[x]] + 1;
if (i == r)
return x;
else if (i < r)
return OS-Select(left[x], i);
else
return OS-Select(right[x], i-r);
}
i = 5
r = 6
i = 5
r = 2
OS-Select Example
● Example: show OS-Select(root, 5):
M
8
C
5
P
2
Q
1
A
1
F
3
D
1
H
1
i = 5
r = 6
i = 5
r = 2
i = 3
r = 2
OS-Select(x, i)
{
r = size[left[x]] + 1;
if (i == r)
return x;
else if (i < r)
return OS-Select(left[x], i);
else
return OS-Select(right[x], i-r);
}
OS-Select Example
● Example: show OS-Select(root, 5):
M
8
C
5
P
2
Q
1
A
1
F
3
D
1
H
1
i = 5
r = 6
i = 5
r = 2
i = 3
r = 2
i = 1
r = 1
OS-Select(x, i)
{
r = size[left[x]] + 1;
if (i == r)
return x;
else if (i < r)
return OS-Select(left[x], i);
else
return OS-Select(right[x], i-r);
}
OS-Select: A Subtlety
OS-Select(x, i)
{
r = size[left[x]] + 1;
if (i == r)
return x;
else if (i < r)
return OS-Select(left[x], i);
else
return OS-Select(right[x], i-r);
}
● What happens at the leaves?
● How can we deal elegantly with this?
Oops…
OS-Select
● Example: show OS-Select(root, 5):
Note: use a sentinel NIL element at the leaves with
size = 0 to simplify code, avoid testing for NULL
M
8
C
5
P
2
Q
1
A
1
F
3
D
1
H
1
i = 5
r = 6
i = 5
r = 2
i = 3
r = 2
i = 1
r = 1
OS-Select(x, i)
{
r = size[left[x]] + 1;
if (i == r)
return x;
else if (i < r)
return OS-Select(left[x], i);
else
return OS-Select(right[x], i-r);
}
Determining The
Rank Of An Element
M
8
C
5
P
2
Q
1
A
1
F
3
D
1
H
1
What is the rank of this element?
Determining The
Rank Of An Element
M
8
C
5
P
2
Q
1
A
1
F
3
D
1
H
1
Of this one? Why?
Determining The
Rank Of An Element
M
8
C
5
P
2
Q
1
A
1
F
3
D
1
H
1
Of the root? What’s the pattern here?
Determining The
Rank Of An Element
M
8
C
5
P
2
Q
1
A
1
F
3
D
1
H
1
What about the rank of this element?
Determining The
Rank Of An Element
M
8
C
5
P
2
Q
1
A
1
F
3
D
1
H
1
This one? What’s the pattern here?
Determining The
Rank Of An Element
M
8
C
5
P
2
Q
1
A
1
F
3
D
1
H
1
OS-Rank(T, x)
{
r = size[left[x]] + 1;
y = x;
while (y != root[T])
if (y == right[p[y]])
r = r + size[left[p[y]]] + 1;
y = p[y];
return r;
}
Idea: rank of right child x is one
more than its parent’s rank, plus
the size of x’s left subtree
Determining The
Rank Of An Element
M
8
C
5
P
2
Q
1
A
1
F
3
D
1
H
1
Example 1:
find rank of element with key H
y
r = 1
OS-Rank(T, x)
{
r = size[left[x]] + 1;
y = x;
while (y != root[T])
if (y == right[p[y]])
r = r + size[left[p[y]]] + 1;
y = p[y];
return r;
}
Determining The
Rank Of An Element
M
8
C
5
P
2
Q
1
A
1
F
3
D
1
H
1
Example 1:
find rank of element with key H
r = 1
y
r = 1+1+1 = 3
OS-Rank(T, x)
{
r = size[left[x]] + 1;
y = x;
while (y != root[T])
if (y == right[p[y]])
r = r + size[left[p[y]]] + 1;
y = p[y];
return r;
}
Determining The
Rank Of An Element
M
8
C
5
P
2
Q
1
A
1
F
3
D
1
H
1
OS-Rank(T, x)
{
r = x->left->size + 1;
y = x;
while (y != T->root)
if (y == y->p->right)
r = r + y->p->left->size + 1;
y = y->p;
return r;
}
Example 1:
find rank of element with key H
r = 1
r = 3
y
r = 3+1+1 = 5
Determining The
Rank Of An Element
M
8
C
5
P
2
Q
1
A
1
F
3
D
1
H
1
Example 1:
find rank of element with key H
r = 1
r = 3
r = 5
y
r = 5
OS-Rank(T, x)
{
r = size[left[x]] + 1;
y = x;
while (y != root[T])
if (y == right[p[y]])
r = r + size[left[p[y]]] + 1;
y = p[y];
return r;
}
Determining The
Rank Of An Element
M
8
C
5
P
2
Q
1
A
1
F
3
D
1
H
1
Example 2:
find rank of element with key P
y
r = 1
OS-Rank(T, x)
{
r = size[left[x]] + 1;
y = x;
while (y != root[T])
if (y == right[p[y]])
r = r + size[left[p[y]]] + 1;
y = p[y];
return r;
}
Determining The
Rank Of An Element
M
8
C
5
P
2
Q
1
A
1
F
3
D
1
H
1
Example 2:
find rank of element with key P
r = 1
y
r = 1 + 5 + 1 = 7
OS-Rank(T, x)
{
r = size[left[x]] + 1;
y = x;
while (y != root[T])
if (y == right[p[y]])
r = r + size[left[p[y]]] + 1;
y = p[y];
return r;
}
OS-Trees: Maintaining Sizes
● So we’ve shown that with subtree sizes, order
statistic operations can be done in O(lg n) time
● Next step: maintain sizes during Insert() and
Delete() operations
■ How would we adjust the size fields during
insertion on a plain binary search tree?
OS-Trees: Maintaining Sizes
● So we’ve shown that with subtree sizes, order
statistic operations can be done in O(lg n) time
● Next step: maintain sizes during Insert() and
Delete() operations
■ How would we adjust the size fields during
insertion on a plain binary search tree?
■ A: increment sizes of nodes traversed during search
OS-Trees: Maintaining Sizes
● So we’ve shown that with subtree sizes, order
statistic operations can be done in O(lg n) time
● Next step: maintain sizes during Insert() and
Delete() operations
■ How would we adjust the size fields during
insertion on a plain binary search tree?
■ A: increment sizes of nodes traversed during search
■ Why won’t this work on red-black trees?
Maintaining Size Through Rotation
● Salient point: rotation invalidates only x and y
● Can recalculate their sizes in constant time
■ Why?
y
19
x
11
x
19
y
12
rightRotate(y)
leftRotate(x)
6 4
7 6
4 7
Augmenting Data Structures:
Methodology
● Choose underlying data structure
■ E.g., red-black trees
● Determine additional information to maintain
■ E.g., subtree sizes
● Verify that information can be maintained for
operations that modify the structure
■ E.g., Insert(), Delete() (don’t forget rotations!)
● Develop new operations
■ E.g., OS-Rank(), OS-Select()
Review: Methodology For
Augmenting Data Structures
● Choose underlying data structure
● Determine additional information to maintain
● Verify that information can be maintained for
operations that modify the structure
● Develop new operations
Interval Trees
● The problem: maintain a set of intervals
■ E.g., time intervals for a scheduling program:
107
115
84 1815 2321
17 19
i = [7,10]; i low = 7; ihigh = 10
Interval Trees
● The problem: maintain a set of intervals
■ E.g., time intervals for a scheduling program:
■ Query: find an interval in the set that overlaps a
given query interval
○ [14,16]  [15,18]
○ [16,19]  [15,18] or [17,19]
○ [12,14]  NULL
107
115
84 1815 2321
17 19
i = [7,10]; i low = 7; ihigh = 10
Interval Trees
● Following the methodology:
■ Pick underlying data structure
■ Decide what additional information to store
■ Figure out how to maintain the information
■ Develop the desired new operations
Interval Trees
● Following the methodology:
■ Pick underlying data structure
○ Red-black trees will store intervals, keyed on ilow
■ Decide what additional information to store
■ Figure out how to maintain the information
■ Develop the desired new operations
Interval Trees
● Following the methodology:
■ Pick underlying data structure
○ Red-black trees will store intervals, keyed on ilow
■ Decide what additional information to store
○ We will store max, the maximum endpoint in the
subtree rooted at i
■ Figure out how to maintain the information
■ Develop the desired new operations
Interval Trees
[17,19]
[5,11] [21,23]
[4,8] [15,18]
[7,10]
int
max
What are the max fields?









max
maxmaxmax
rightx
leftx
highx
x
Interval Trees
[17,19]
23
[5,11]
18
[21,23]
23
[4,8]
8
[15,18]
18
[7,10]
10
int
max
Note that:
Interval Trees
● Following the methodology:
■ Pick underlying data structure
○ Red-black trees will store intervals, keyed on ilow
■ Decide what additional information to store
○ Store the maximum endpoint in the subtree rooted at i
■ Figure out how to maintain the information
○ How would we maintain max field for a BST?
○ What’s different?
■ Develop the desired new operations
Interval Trees
● What are the new max values for the subtrees?
[11,35]
35
[6,20]
20
[6,20]
???
[11,35]
???
rightRotate(y)
leftRotate(x)
…
14
…
19
…
30
…
???
…
???
…
???
Interval Trees
● What are the new max values for the subtrees?
● A: Unchanged
● What are the new max values for x and y?
[11,35]
35
[6,20]
20
[6,20]
???
[11,35]
???
rightRotate(y)
leftRotate(x)
…
14
…
19
…
30
…
14
…
19
…
30
Interval Trees
● What are the new max values for the subtrees?
● A: Unchanged
● What are the new max values for x and y?
● A: root value unchanged, recompute other
[11,35]
35
[6,20]
20
[6,20]
35
[11,35]
35
rightRotate(y)
leftRotate(x)
…
14
…
19
…
30
…
14
…
19
…
30
Interval Trees
● Following the methodology:
■ Pick underlying data structure
○ Red-black trees will store intervals, keyed on ilow
■ Decide what additional information to store
○ Store the maximum endpoint in the subtree rooted at i
■ Figure out how to maintain the information
○ Insert: update max on way down, during rotations
○ Delete: similar
■ Develop the desired new operations
Searching Interval Trees
IntervalSearch(T, i)
{
x = T->root;
while (x != NULL && !overlap(i, x->interval))
if (x->left != NULL && x->left->max  i->low)
x = x->left;
else
x = x->right;
return x
}
● What will be the running time?
IntervalSearch() Example
● Example: search for interval
overlapping [14,16]
[17,19]
23
[5,11]
18
[21,23]
23
[4,8]
8
[15,18]
18
[7,10]
10
IntervalSearch(T, i)
{
x = T->root;
while (x != NULL && !overlap(i, x->interval))
if (x->left != NULL && x->left->max  i->low)
x = x->left;
else
x = x->right;
return x
}
IntervalSearch() Example
● Example: search for interval
overlapping [12,14]
[17,19]
23
[5,11]
18
[21,23]
23
[4,8]
8
[15,18]
18
[7,10]
10
IntervalSearch(T, i)
{
x = T->root;
while (x != NULL && !overlap(i, x->interval))
if (x->left != NULL && x->left->max  i->low)
x = x->left;
else
x = x->right;
return x
}
Correctness of IntervalSearch()
● Key idea: need to check only 1 of node’s 2
children
■ Case 1: search goes right
○ Show that  overlap in right subtree, or no overlap at all
■ Case 2: search goes left
○ Show that  overlap in left subtree, or no overlap at all
Correctness of IntervalSearch()
● Case 1: if search goes right,  overlap in the right
subtree or no overlap in either subtree
■ If  overlap in right subtree, we’re done
■ Otherwise:
○ xleft = NULL, or x  left  max < x  low (Why?)
○ Thus, no overlap in left subtree!
while (x != NULL && !overlap(i, x->interval))
if (x->left != NULL && x->left->max  i->low)
x = x->left;
else
x = x->right;
return x;
Correctness of IntervalSearch()
● Case 2: if search goes left,  overlap in the left
subtree or no overlap in either subtree
■ If  overlap in left subtree, we’re done
■ Otherwise:
○ i low  x left max, by branch condition
○ x left max = y high for some y in left subtree
○ Since i and y don’t overlap and i low  y high,
i high < y low
○ Since tree is sorted by low’s, i high < any low in right subtree
○ Thus, no overlap in right subtree
while (x != NULL && !overlap(i, x->interval))
if (x->left != NULL && x->left->max  i->low)
x = x->left;
else
x = x->right;
return x;

Weitere ähnliche Inhalte

Was ist angesagt?

Minimization of DFA
Minimization of DFAMinimization of DFA
Minimization of DFAkunj desai
 
Insertion sort bubble sort selection sort
Insertion sort bubble sort  selection sortInsertion sort bubble sort  selection sort
Insertion sort bubble sort selection sortUmmar Hayat
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sortingKaushal Shah
 
Lecture 5 sorting and searching
Lecture 5   sorting and searchingLecture 5   sorting and searching
Lecture 5 sorting and searchingNada G.Youssef
 
NLP_KASHK:Parsing with Context-Free Grammar
NLP_KASHK:Parsing with Context-Free Grammar NLP_KASHK:Parsing with Context-Free Grammar
NLP_KASHK:Parsing with Context-Free Grammar Hemantha Kulathilake
 
Circle drawing algo.
Circle drawing algo.Circle drawing algo.
Circle drawing algo.Mohd Arif
 
Artificial Intelligence Notes Unit 1
Artificial Intelligence Notes Unit 1 Artificial Intelligence Notes Unit 1
Artificial Intelligence Notes Unit 1 DigiGurukul
 
Regular Expression to Finite Automata
Regular Expression to Finite AutomataRegular Expression to Finite Automata
Regular Expression to Finite AutomataArchana Gopinath
 
Heap Sort || Heapify Method || Build Max Heap Algorithm
Heap Sort || Heapify Method || Build Max Heap AlgorithmHeap Sort || Heapify Method || Build Max Heap Algorithm
Heap Sort || Heapify Method || Build Max Heap AlgorithmLearning Courses Online
 
Insertion sort
Insertion sortInsertion sort
Insertion sortalmaqboli
 
Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)Shuvongkor Barman
 
Mca ii dfs u-1 introduction to data structure
Mca ii dfs u-1 introduction to data structureMca ii dfs u-1 introduction to data structure
Mca ii dfs u-1 introduction to data structureRai University
 
NFA or Non deterministic finite automata
NFA or Non deterministic finite automataNFA or Non deterministic finite automata
NFA or Non deterministic finite automatadeepinderbedi
 

Was ist angesagt? (20)

Minimization of DFA
Minimization of DFAMinimization of DFA
Minimization of DFA
 
Insertion sort bubble sort selection sort
Insertion sort bubble sort  selection sortInsertion sort bubble sort  selection sort
Insertion sort bubble sort selection sort
 
pushdown automata
pushdown automatapushdown automata
pushdown automata
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sorting
 
Heaps
HeapsHeaps
Heaps
 
Lecture 5 sorting and searching
Lecture 5   sorting and searchingLecture 5   sorting and searching
Lecture 5 sorting and searching
 
Insertion sort
Insertion sort Insertion sort
Insertion sort
 
Problem solving
Problem solvingProblem solving
Problem solving
 
NLP_KASHK:Parsing with Context-Free Grammar
NLP_KASHK:Parsing with Context-Free Grammar NLP_KASHK:Parsing with Context-Free Grammar
NLP_KASHK:Parsing with Context-Free Grammar
 
Circle drawing algo.
Circle drawing algo.Circle drawing algo.
Circle drawing algo.
 
Artificial Intelligence Notes Unit 1
Artificial Intelligence Notes Unit 1 Artificial Intelligence Notes Unit 1
Artificial Intelligence Notes Unit 1
 
Clipping
ClippingClipping
Clipping
 
Regular Expression to Finite Automata
Regular Expression to Finite AutomataRegular Expression to Finite Automata
Regular Expression to Finite Automata
 
Heap Sort || Heapify Method || Build Max Heap Algorithm
Heap Sort || Heapify Method || Build Max Heap AlgorithmHeap Sort || Heapify Method || Build Max Heap Algorithm
Heap Sort || Heapify Method || Build Max Heap Algorithm
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)
 
Mca ii dfs u-1 introduction to data structure
Mca ii dfs u-1 introduction to data structureMca ii dfs u-1 introduction to data structure
Mca ii dfs u-1 introduction to data structure
 
Quick sort
Quick sortQuick sort
Quick sort
 
NFA or Non deterministic finite automata
NFA or Non deterministic finite automataNFA or Non deterministic finite automata
NFA or Non deterministic finite automata
 
Best,worst,average case .17581556 045
Best,worst,average case .17581556 045Best,worst,average case .17581556 045
Best,worst,average case .17581556 045
 

Andere mochten auch

Medians and order statistics
Medians and order statisticsMedians and order statistics
Medians and order statisticsRajendran
 
Medians and Order Statistics
Medians and Order StatisticsMedians and Order Statistics
Medians and Order StatisticsHoa Nguyen
 
median and order statistics
median and order statisticsmedian and order statistics
median and order statisticsShashank Singh
 
07 Analysis of Algorithms: Order Statistics
07 Analysis of Algorithms: Order Statistics07 Analysis of Algorithms: Order Statistics
07 Analysis of Algorithms: Order StatisticsAndres Mendez-Vazquez
 
Introduction, features and environment of ms front page 2003
Introduction, features and environment of ms front page 2003Introduction, features and environment of ms front page 2003
Introduction, features and environment of ms front page 2003Ann Alcid
 

Andere mochten auch (7)

Medians and order statistics
Medians and order statisticsMedians and order statistics
Medians and order statistics
 
Medians and Order Statistics
Medians and Order StatisticsMedians and Order Statistics
Medians and Order Statistics
 
median and order statistics
median and order statisticsmedian and order statistics
median and order statistics
 
Sufficient statistics
Sufficient statisticsSufficient statistics
Sufficient statistics
 
07 Analysis of Algorithms: Order Statistics
07 Analysis of Algorithms: Order Statistics07 Analysis of Algorithms: Order Statistics
07 Analysis of Algorithms: Order Statistics
 
Chapter 15
Chapter 15Chapter 15
Chapter 15
 
Introduction, features and environment of ms front page 2003
Introduction, features and environment of ms front page 2003Introduction, features and environment of ms front page 2003
Introduction, features and environment of ms front page 2003
 

Ähnlich wie Augmenting Data Structures

Lec 1 Ds
Lec 1 DsLec 1 Ds
Lec 1 DsQundeel
 
Data Structure
Data StructureData Structure
Data Structuresheraz1
 
Lec 1 Ds
Lec 1 DsLec 1 Ds
Lec 1 DsQundeel
 
Don’t optimize my queries, optimize my data!
Don’t optimize my queries, optimize my data!Don’t optimize my queries, optimize my data!
Don’t optimize my queries, optimize my data!Julian Hyde
 
III_Data Structure_Module_1.pptx
III_Data Structure_Module_1.pptxIII_Data Structure_Module_1.pptx
III_Data Structure_Module_1.pptxshashankbhadouria4
 
III_Data Structure_Module_1.ppt
III_Data Structure_Module_1.pptIII_Data Structure_Module_1.ppt
III_Data Structure_Module_1.pptshashankbhadouria4
 
Lazy beats Smart and Fast
Lazy beats Smart and FastLazy beats Smart and Fast
Lazy beats Smart and FastJulian Hyde
 
R programming slides
R  programming slidesR  programming slides
R programming slidesPankaj Saini
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithmsJulie Iskander
 
Week2-stacks-queues.pptx
Week2-stacks-queues.pptxWeek2-stacks-queues.pptx
Week2-stacks-queues.pptxVandanaBharti21
 
Advance data structure & algorithm
Advance data structure & algorithmAdvance data structure & algorithm
Advance data structure & algorithmK Hari Shankar
 
Basic terminologies & asymptotic notations
Basic terminologies & asymptotic notationsBasic terminologies & asymptotic notations
Basic terminologies & asymptotic notationsRajendran
 
R programming & Machine Learning
R programming & Machine LearningR programming & Machine Learning
R programming & Machine LearningAmanBhalla14
 
python-numpyandpandas-170922144956 (1).pptx
python-numpyandpandas-170922144956 (1).pptxpython-numpyandpandas-170922144956 (1).pptx
python-numpyandpandas-170922144956 (1).pptxAkashgupta517936
 
19. Data Structures and Algorithm Complexity
19. Data Structures and Algorithm Complexity19. Data Structures and Algorithm Complexity
19. Data Structures and Algorithm ComplexityIntro C# Book
 

Ähnlich wie Augmenting Data Structures (20)

Lec 1 Ds
Lec 1 DsLec 1 Ds
Lec 1 Ds
 
Data Structure
Data StructureData Structure
Data Structure
 
Lec 1 Ds
Lec 1 DsLec 1 Ds
Lec 1 Ds
 
Don’t optimize my queries, optimize my data!
Don’t optimize my queries, optimize my data!Don’t optimize my queries, optimize my data!
Don’t optimize my queries, optimize my data!
 
III_Data Structure_Module_1.pptx
III_Data Structure_Module_1.pptxIII_Data Structure_Module_1.pptx
III_Data Structure_Module_1.pptx
 
III_Data Structure_Module_1.ppt
III_Data Structure_Module_1.pptIII_Data Structure_Module_1.ppt
III_Data Structure_Module_1.ppt
 
Practical data analysis with wine
Practical data analysis with winePractical data analysis with wine
Practical data analysis with wine
 
Lazy beats Smart and Fast
Lazy beats Smart and FastLazy beats Smart and Fast
Lazy beats Smart and Fast
 
R programming slides
R  programming slidesR  programming slides
R programming slides
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
Data Structures 6
Data Structures 6Data Structures 6
Data Structures 6
 
Week2-stacks-queues.pptx
Week2-stacks-queues.pptxWeek2-stacks-queues.pptx
Week2-stacks-queues.pptx
 
Advance data structure & algorithm
Advance data structure & algorithmAdvance data structure & algorithm
Advance data structure & algorithm
 
Basic terminologies & asymptotic notations
Basic terminologies & asymptotic notationsBasic terminologies & asymptotic notations
Basic terminologies & asymptotic notations
 
R programming & Machine Learning
R programming & Machine LearningR programming & Machine Learning
R programming & Machine Learning
 
python-numpyandpandas-170922144956 (1).pptx
python-numpyandpandas-170922144956 (1).pptxpython-numpyandpandas-170922144956 (1).pptx
python-numpyandpandas-170922144956 (1).pptx
 
sort search in C
 sort search in C  sort search in C
sort search in C
 
Lecture 9.pptx
Lecture 9.pptxLecture 9.pptx
Lecture 9.pptx
 
Data Structures 7
Data Structures 7Data Structures 7
Data Structures 7
 
19. Data Structures and Algorithm Complexity
19. Data Structures and Algorithm Complexity19. Data Structures and Algorithm Complexity
19. Data Structures and Algorithm Complexity
 

Mehr von Dr Sandeep Kumar Poonia

An improved memetic search in artificial bee colony algorithm
An improved memetic search in artificial bee colony algorithmAn improved memetic search in artificial bee colony algorithm
An improved memetic search in artificial bee colony algorithmDr Sandeep Kumar Poonia
 
Modified position update in spider monkey optimization algorithm
Modified position update in spider monkey optimization algorithmModified position update in spider monkey optimization algorithm
Modified position update in spider monkey optimization algorithmDr Sandeep Kumar Poonia
 
Enhanced local search in artificial bee colony algorithm
Enhanced local search in artificial bee colony algorithmEnhanced local search in artificial bee colony algorithm
Enhanced local search in artificial bee colony algorithmDr Sandeep Kumar Poonia
 
Memetic search in differential evolution algorithm
Memetic search in differential evolution algorithmMemetic search in differential evolution algorithm
Memetic search in differential evolution algorithmDr Sandeep Kumar Poonia
 
Improved onlooker bee phase in artificial bee colony algorithm
Improved onlooker bee phase in artificial bee colony algorithmImproved onlooker bee phase in artificial bee colony algorithm
Improved onlooker bee phase in artificial bee colony algorithmDr Sandeep Kumar Poonia
 
Comparative study of_hybrids_of_artificial_bee_colony_algorithm
Comparative study of_hybrids_of_artificial_bee_colony_algorithmComparative study of_hybrids_of_artificial_bee_colony_algorithm
Comparative study of_hybrids_of_artificial_bee_colony_algorithmDr Sandeep Kumar Poonia
 
A novel hybrid crossover based abc algorithm
A novel hybrid crossover based abc algorithmA novel hybrid crossover based abc algorithm
A novel hybrid crossover based abc algorithmDr Sandeep Kumar Poonia
 
Multiplication of two 3 d sparse matrices using 1d arrays and linked lists
Multiplication of two 3 d sparse matrices using 1d arrays and linked listsMultiplication of two 3 d sparse matrices using 1d arrays and linked lists
Multiplication of two 3 d sparse matrices using 1d arrays and linked listsDr Sandeep Kumar Poonia
 
Sunzip user tool for data reduction using huffman algorithm
Sunzip user tool for data reduction using huffman algorithmSunzip user tool for data reduction using huffman algorithm
Sunzip user tool for data reduction using huffman algorithmDr Sandeep Kumar Poonia
 
New Local Search Strategy in Artificial Bee Colony Algorithm
New Local Search Strategy in Artificial Bee Colony Algorithm New Local Search Strategy in Artificial Bee Colony Algorithm
New Local Search Strategy in Artificial Bee Colony Algorithm Dr Sandeep Kumar Poonia
 
Performance evaluation of different routing protocols in wsn using different ...
Performance evaluation of different routing protocols in wsn using different ...Performance evaluation of different routing protocols in wsn using different ...
Performance evaluation of different routing protocols in wsn using different ...Dr Sandeep Kumar Poonia
 
Performance evaluation of diff routing protocols in wsn using difft network p...
Performance evaluation of diff routing protocols in wsn using difft network p...Performance evaluation of diff routing protocols in wsn using difft network p...
Performance evaluation of diff routing protocols in wsn using difft network p...Dr Sandeep Kumar Poonia
 

Mehr von Dr Sandeep Kumar Poonia (20)

Soft computing
Soft computingSoft computing
Soft computing
 
An improved memetic search in artificial bee colony algorithm
An improved memetic search in artificial bee colony algorithmAn improved memetic search in artificial bee colony algorithm
An improved memetic search in artificial bee colony algorithm
 
Modified position update in spider monkey optimization algorithm
Modified position update in spider monkey optimization algorithmModified position update in spider monkey optimization algorithm
Modified position update in spider monkey optimization algorithm
 
Enhanced local search in artificial bee colony algorithm
Enhanced local search in artificial bee colony algorithmEnhanced local search in artificial bee colony algorithm
Enhanced local search in artificial bee colony algorithm
 
RMABC
RMABCRMABC
RMABC
 
Memetic search in differential evolution algorithm
Memetic search in differential evolution algorithmMemetic search in differential evolution algorithm
Memetic search in differential evolution algorithm
 
Improved onlooker bee phase in artificial bee colony algorithm
Improved onlooker bee phase in artificial bee colony algorithmImproved onlooker bee phase in artificial bee colony algorithm
Improved onlooker bee phase in artificial bee colony algorithm
 
Comparative study of_hybrids_of_artificial_bee_colony_algorithm
Comparative study of_hybrids_of_artificial_bee_colony_algorithmComparative study of_hybrids_of_artificial_bee_colony_algorithm
Comparative study of_hybrids_of_artificial_bee_colony_algorithm
 
A novel hybrid crossover based abc algorithm
A novel hybrid crossover based abc algorithmA novel hybrid crossover based abc algorithm
A novel hybrid crossover based abc algorithm
 
Multiplication of two 3 d sparse matrices using 1d arrays and linked lists
Multiplication of two 3 d sparse matrices using 1d arrays and linked listsMultiplication of two 3 d sparse matrices using 1d arrays and linked lists
Multiplication of two 3 d sparse matrices using 1d arrays and linked lists
 
Sunzip user tool for data reduction using huffman algorithm
Sunzip user tool for data reduction using huffman algorithmSunzip user tool for data reduction using huffman algorithm
Sunzip user tool for data reduction using huffman algorithm
 
New Local Search Strategy in Artificial Bee Colony Algorithm
New Local Search Strategy in Artificial Bee Colony Algorithm New Local Search Strategy in Artificial Bee Colony Algorithm
New Local Search Strategy in Artificial Bee Colony Algorithm
 
A new approach of program slicing
A new approach of program slicingA new approach of program slicing
A new approach of program slicing
 
Performance evaluation of different routing protocols in wsn using different ...
Performance evaluation of different routing protocols in wsn using different ...Performance evaluation of different routing protocols in wsn using different ...
Performance evaluation of different routing protocols in wsn using different ...
 
Enhanced abc algo for tsp
Enhanced abc algo for tspEnhanced abc algo for tsp
Enhanced abc algo for tsp
 
Database aggregation using metadata
Database aggregation using metadataDatabase aggregation using metadata
Database aggregation using metadata
 
Performance evaluation of diff routing protocols in wsn using difft network p...
Performance evaluation of diff routing protocols in wsn using difft network p...Performance evaluation of diff routing protocols in wsn using difft network p...
Performance evaluation of diff routing protocols in wsn using difft network p...
 
Lecture28 tsp
Lecture28 tspLecture28 tsp
Lecture28 tsp
 
Lecture27 linear programming
Lecture27 linear programmingLecture27 linear programming
Lecture27 linear programming
 
Lecture26
Lecture26Lecture26
Lecture26
 

Kürzlich hochgeladen

Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 

Kürzlich hochgeladen (20)

Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 

Augmenting Data Structures

  • 2. Dynamic Order Statistics ● We’ve seen algorithms for finding the ith element of an unordered set in O(n) time ● Next, a structure to support finding the ith element of a dynamic set in O(lg n) time ■ What operations do dynamic sets usually support? ■ What structure works well for these? ■ How could we use this structure for order statistics? ■ How might we augment it to support efficient extraction of order statistics?
  • 3. Order Statistic Trees ● OS Trees augment red-black trees: ■ Associate a size field with each node in the tree ■ x->size records the size of subtree rooted at x, including x itself: M 8 C 5 P 2 Q 1 A 1 F 3 D 1 H 1
  • 4. Selection On OS Trees M 8 C 5 P 2 Q 1 A 1 F 3 D 1 H 1 How can we use this property to select the ith element of the set?
  • 5. OS-Select OS-Select(x, i) { r = size[left[x]] + 1; if (i == r) return x; else if (i < r) return OS-Select(left[x], i); else return OS-Select(right[x], i-r); }
  • 6. OS-Select Example ● Example: show OS-Select(root, 5): M 8 C 5 P 2 Q 1 A 1 F 3 D 1 H 1 OS-Select(x, i) { r = size[left[x]] + 1; if (i == r) return x; else if (i < r) return OS-Select(left[x], i); else return OS-Select(right[x], i-r); }
  • 7. OS-Select Example ● Example: show OS-Select(root, 5): M 8 C 5 P 2 Q 1 A 1 F 3 D 1 H 1 i = 5 r = 6 OS-Select(x, i) { r = size[left[x]] + 1; if (i == r) return x; else if (i < r) return OS-Select(left[x], i); else return OS-Select(right[x], i-r); }
  • 8. OS-Select Example ● Example: show OS-Select(root, 5): M 8 C 5 P 2 Q 1 A 1 F 3 D 1 H 1 OS-Select(x, i) { r = size[left[x]] + 1; if (i == r) return x; else if (i < r) return OS-Select(left[x], i); else return OS-Select(right[x], i-r); } i = 5 r = 6 i = 5 r = 2
  • 9. OS-Select Example ● Example: show OS-Select(root, 5): M 8 C 5 P 2 Q 1 A 1 F 3 D 1 H 1 i = 5 r = 6 i = 5 r = 2 i = 3 r = 2 OS-Select(x, i) { r = size[left[x]] + 1; if (i == r) return x; else if (i < r) return OS-Select(left[x], i); else return OS-Select(right[x], i-r); }
  • 10. OS-Select Example ● Example: show OS-Select(root, 5): M 8 C 5 P 2 Q 1 A 1 F 3 D 1 H 1 i = 5 r = 6 i = 5 r = 2 i = 3 r = 2 i = 1 r = 1 OS-Select(x, i) { r = size[left[x]] + 1; if (i == r) return x; else if (i < r) return OS-Select(left[x], i); else return OS-Select(right[x], i-r); }
  • 11. OS-Select: A Subtlety OS-Select(x, i) { r = size[left[x]] + 1; if (i == r) return x; else if (i < r) return OS-Select(left[x], i); else return OS-Select(right[x], i-r); } ● What happens at the leaves? ● How can we deal elegantly with this? Oops…
  • 12. OS-Select ● Example: show OS-Select(root, 5): Note: use a sentinel NIL element at the leaves with size = 0 to simplify code, avoid testing for NULL M 8 C 5 P 2 Q 1 A 1 F 3 D 1 H 1 i = 5 r = 6 i = 5 r = 2 i = 3 r = 2 i = 1 r = 1 OS-Select(x, i) { r = size[left[x]] + 1; if (i == r) return x; else if (i < r) return OS-Select(left[x], i); else return OS-Select(right[x], i-r); }
  • 13. Determining The Rank Of An Element M 8 C 5 P 2 Q 1 A 1 F 3 D 1 H 1 What is the rank of this element?
  • 14. Determining The Rank Of An Element M 8 C 5 P 2 Q 1 A 1 F 3 D 1 H 1 Of this one? Why?
  • 15. Determining The Rank Of An Element M 8 C 5 P 2 Q 1 A 1 F 3 D 1 H 1 Of the root? What’s the pattern here?
  • 16. Determining The Rank Of An Element M 8 C 5 P 2 Q 1 A 1 F 3 D 1 H 1 What about the rank of this element?
  • 17. Determining The Rank Of An Element M 8 C 5 P 2 Q 1 A 1 F 3 D 1 H 1 This one? What’s the pattern here?
  • 18. Determining The Rank Of An Element M 8 C 5 P 2 Q 1 A 1 F 3 D 1 H 1 OS-Rank(T, x) { r = size[left[x]] + 1; y = x; while (y != root[T]) if (y == right[p[y]]) r = r + size[left[p[y]]] + 1; y = p[y]; return r; } Idea: rank of right child x is one more than its parent’s rank, plus the size of x’s left subtree
  • 19. Determining The Rank Of An Element M 8 C 5 P 2 Q 1 A 1 F 3 D 1 H 1 Example 1: find rank of element with key H y r = 1 OS-Rank(T, x) { r = size[left[x]] + 1; y = x; while (y != root[T]) if (y == right[p[y]]) r = r + size[left[p[y]]] + 1; y = p[y]; return r; }
  • 20. Determining The Rank Of An Element M 8 C 5 P 2 Q 1 A 1 F 3 D 1 H 1 Example 1: find rank of element with key H r = 1 y r = 1+1+1 = 3 OS-Rank(T, x) { r = size[left[x]] + 1; y = x; while (y != root[T]) if (y == right[p[y]]) r = r + size[left[p[y]]] + 1; y = p[y]; return r; }
  • 21. Determining The Rank Of An Element M 8 C 5 P 2 Q 1 A 1 F 3 D 1 H 1 OS-Rank(T, x) { r = x->left->size + 1; y = x; while (y != T->root) if (y == y->p->right) r = r + y->p->left->size + 1; y = y->p; return r; } Example 1: find rank of element with key H r = 1 r = 3 y r = 3+1+1 = 5
  • 22. Determining The Rank Of An Element M 8 C 5 P 2 Q 1 A 1 F 3 D 1 H 1 Example 1: find rank of element with key H r = 1 r = 3 r = 5 y r = 5 OS-Rank(T, x) { r = size[left[x]] + 1; y = x; while (y != root[T]) if (y == right[p[y]]) r = r + size[left[p[y]]] + 1; y = p[y]; return r; }
  • 23. Determining The Rank Of An Element M 8 C 5 P 2 Q 1 A 1 F 3 D 1 H 1 Example 2: find rank of element with key P y r = 1 OS-Rank(T, x) { r = size[left[x]] + 1; y = x; while (y != root[T]) if (y == right[p[y]]) r = r + size[left[p[y]]] + 1; y = p[y]; return r; }
  • 24. Determining The Rank Of An Element M 8 C 5 P 2 Q 1 A 1 F 3 D 1 H 1 Example 2: find rank of element with key P r = 1 y r = 1 + 5 + 1 = 7 OS-Rank(T, x) { r = size[left[x]] + 1; y = x; while (y != root[T]) if (y == right[p[y]]) r = r + size[left[p[y]]] + 1; y = p[y]; return r; }
  • 25. OS-Trees: Maintaining Sizes ● So we’ve shown that with subtree sizes, order statistic operations can be done in O(lg n) time ● Next step: maintain sizes during Insert() and Delete() operations ■ How would we adjust the size fields during insertion on a plain binary search tree?
  • 26. OS-Trees: Maintaining Sizes ● So we’ve shown that with subtree sizes, order statistic operations can be done in O(lg n) time ● Next step: maintain sizes during Insert() and Delete() operations ■ How would we adjust the size fields during insertion on a plain binary search tree? ■ A: increment sizes of nodes traversed during search
  • 27. OS-Trees: Maintaining Sizes ● So we’ve shown that with subtree sizes, order statistic operations can be done in O(lg n) time ● Next step: maintain sizes during Insert() and Delete() operations ■ How would we adjust the size fields during insertion on a plain binary search tree? ■ A: increment sizes of nodes traversed during search ■ Why won’t this work on red-black trees?
  • 28. Maintaining Size Through Rotation ● Salient point: rotation invalidates only x and y ● Can recalculate their sizes in constant time ■ Why? y 19 x 11 x 19 y 12 rightRotate(y) leftRotate(x) 6 4 7 6 4 7
  • 29. Augmenting Data Structures: Methodology ● Choose underlying data structure ■ E.g., red-black trees ● Determine additional information to maintain ■ E.g., subtree sizes ● Verify that information can be maintained for operations that modify the structure ■ E.g., Insert(), Delete() (don’t forget rotations!) ● Develop new operations ■ E.g., OS-Rank(), OS-Select()
  • 30. Review: Methodology For Augmenting Data Structures ● Choose underlying data structure ● Determine additional information to maintain ● Verify that information can be maintained for operations that modify the structure ● Develop new operations
  • 31. Interval Trees ● The problem: maintain a set of intervals ■ E.g., time intervals for a scheduling program: 107 115 84 1815 2321 17 19 i = [7,10]; i low = 7; ihigh = 10
  • 32. Interval Trees ● The problem: maintain a set of intervals ■ E.g., time intervals for a scheduling program: ■ Query: find an interval in the set that overlaps a given query interval ○ [14,16]  [15,18] ○ [16,19]  [15,18] or [17,19] ○ [12,14]  NULL 107 115 84 1815 2321 17 19 i = [7,10]; i low = 7; ihigh = 10
  • 33. Interval Trees ● Following the methodology: ■ Pick underlying data structure ■ Decide what additional information to store ■ Figure out how to maintain the information ■ Develop the desired new operations
  • 34. Interval Trees ● Following the methodology: ■ Pick underlying data structure ○ Red-black trees will store intervals, keyed on ilow ■ Decide what additional information to store ■ Figure out how to maintain the information ■ Develop the desired new operations
  • 35. Interval Trees ● Following the methodology: ■ Pick underlying data structure ○ Red-black trees will store intervals, keyed on ilow ■ Decide what additional information to store ○ We will store max, the maximum endpoint in the subtree rooted at i ■ Figure out how to maintain the information ■ Develop the desired new operations
  • 36. Interval Trees [17,19] [5,11] [21,23] [4,8] [15,18] [7,10] int max What are the max fields?
  • 38. Interval Trees ● Following the methodology: ■ Pick underlying data structure ○ Red-black trees will store intervals, keyed on ilow ■ Decide what additional information to store ○ Store the maximum endpoint in the subtree rooted at i ■ Figure out how to maintain the information ○ How would we maintain max field for a BST? ○ What’s different? ■ Develop the desired new operations
  • 39. Interval Trees ● What are the new max values for the subtrees? [11,35] 35 [6,20] 20 [6,20] ??? [11,35] ??? rightRotate(y) leftRotate(x) … 14 … 19 … 30 … ??? … ??? … ???
  • 40. Interval Trees ● What are the new max values for the subtrees? ● A: Unchanged ● What are the new max values for x and y? [11,35] 35 [6,20] 20 [6,20] ??? [11,35] ??? rightRotate(y) leftRotate(x) … 14 … 19 … 30 … 14 … 19 … 30
  • 41. Interval Trees ● What are the new max values for the subtrees? ● A: Unchanged ● What are the new max values for x and y? ● A: root value unchanged, recompute other [11,35] 35 [6,20] 20 [6,20] 35 [11,35] 35 rightRotate(y) leftRotate(x) … 14 … 19 … 30 … 14 … 19 … 30
  • 42. Interval Trees ● Following the methodology: ■ Pick underlying data structure ○ Red-black trees will store intervals, keyed on ilow ■ Decide what additional information to store ○ Store the maximum endpoint in the subtree rooted at i ■ Figure out how to maintain the information ○ Insert: update max on way down, during rotations ○ Delete: similar ■ Develop the desired new operations
  • 43. Searching Interval Trees IntervalSearch(T, i) { x = T->root; while (x != NULL && !overlap(i, x->interval)) if (x->left != NULL && x->left->max  i->low) x = x->left; else x = x->right; return x } ● What will be the running time?
  • 44. IntervalSearch() Example ● Example: search for interval overlapping [14,16] [17,19] 23 [5,11] 18 [21,23] 23 [4,8] 8 [15,18] 18 [7,10] 10 IntervalSearch(T, i) { x = T->root; while (x != NULL && !overlap(i, x->interval)) if (x->left != NULL && x->left->max  i->low) x = x->left; else x = x->right; return x }
  • 45. IntervalSearch() Example ● Example: search for interval overlapping [12,14] [17,19] 23 [5,11] 18 [21,23] 23 [4,8] 8 [15,18] 18 [7,10] 10 IntervalSearch(T, i) { x = T->root; while (x != NULL && !overlap(i, x->interval)) if (x->left != NULL && x->left->max  i->low) x = x->left; else x = x->right; return x }
  • 46. Correctness of IntervalSearch() ● Key idea: need to check only 1 of node’s 2 children ■ Case 1: search goes right ○ Show that  overlap in right subtree, or no overlap at all ■ Case 2: search goes left ○ Show that  overlap in left subtree, or no overlap at all
  • 47. Correctness of IntervalSearch() ● Case 1: if search goes right,  overlap in the right subtree or no overlap in either subtree ■ If  overlap in right subtree, we’re done ■ Otherwise: ○ xleft = NULL, or x  left  max < x  low (Why?) ○ Thus, no overlap in left subtree! while (x != NULL && !overlap(i, x->interval)) if (x->left != NULL && x->left->max  i->low) x = x->left; else x = x->right; return x;
  • 48. Correctness of IntervalSearch() ● Case 2: if search goes left,  overlap in the left subtree or no overlap in either subtree ■ If  overlap in left subtree, we’re done ■ Otherwise: ○ i low  x left max, by branch condition ○ x left max = y high for some y in left subtree ○ Since i and y don’t overlap and i low  y high, i high < y low ○ Since tree is sorted by low’s, i high < any low in right subtree ○ Thus, no overlap in right subtree while (x != NULL && !overlap(i, x->interval)) if (x->left != NULL && x->left->max  i->low) x = x->left; else x = x->right; return x;