21: Advanced Data Structures
Transcript of 21: Advanced Data Structures
![Page 1: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/1.jpg)
Advanced DataStructures
![Page 2: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/2.jpg)
2
30-2 6
-1 4
![Page 3: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/3.jpg)
22
30-2 6
-1 4
30-2 6
-1 4
![Page 4: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/4.jpg)
4
22
30-2 6
-1 4
30-2 6
-1
![Page 5: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/5.jpg)
4
3
4
22
30-2 6
-1
0-2 6
-1
![Page 6: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/6.jpg)
3
4
3
4
22
0-2 6
-1
0-2 6
-1
![Page 7: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/7.jpg)
2
3
4
3
4
2
0-2 6
-1
0-2 6
-1
![Page 8: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/8.jpg)
2
-1
2
3
4
3
4
0-2 6
-1
0-2 6
![Page 9: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/9.jpg)
-1
-2
2
-1
2
3
4
3
4
0-2 60 6
![Page 10: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/10.jpg)
-2
-1
-2
2
-1
2
3
4
3
4
0 60 6
![Page 11: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/11.jpg)
Insertion Order Matters
● Suppose we create a BST of numbers in this order:
4, 2, 1, 3, 6, 5, 7
4
2
1 3
6
5 7
![Page 12: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/12.jpg)
Insertion Order Matters
● Suppose we create a BST of numbers in this order:
1, 2, 3, 4, 5, 6, 7
12
34
56
7
![Page 13: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/13.jpg)
Tree Terminology
● The height of a tree is the number of nodes in the longest path from the root to a leaf.
4
2
1 3
6
5 7
![Page 14: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/14.jpg)
Tree Terminology
● The height of a tree is the number of nodes in the longest path from the root to a leaf.
12
34
56
7
![Page 15: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/15.jpg)
Keeping the Height Low
● Almost all BST operations have time complexity based on height:● Insertion: O(h)● Search: O(h)● Deletion: O(h)
● Keeping the height low will make these operations much more efficient.
● How do we do this?
![Page 16: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/16.jpg)
Tree Rotations
● One common way of keeping tree heights low is to reshape the BST when it gets too high.
● One way to accomplish this is a tree rotation, which locally rearranges nodes.
![Page 17: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/17.jpg)
Tree Rotations
B
A
>B
<A>A<B
B
A
>B
<A
>A<B
Rotate Right
Rotate Left
![Page 18: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/18.jpg)
1
2
3
4
5
6
![Page 19: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/19.jpg)
1
2
3
4
5
6
![Page 20: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/20.jpg)
1
2
3
4
5
6
![Page 21: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/21.jpg)
1
2
3
4
5
6
![Page 22: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/22.jpg)
1
2
3
4
5
6
![Page 23: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/23.jpg)
1
2
3
4
5
6
![Page 24: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/24.jpg)
1
2
3
4
5
6
![Page 25: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/25.jpg)
1
2
3
4
5
6
![Page 26: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/26.jpg)
1
2
3
4
5
6
![Page 27: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/27.jpg)
1
2
3
4
5
6
![Page 28: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/28.jpg)
1
2
3
4
5
6
![Page 29: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/29.jpg)
1
2
3
4
5
6
![Page 30: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/30.jpg)
Let's Code it Up!
![Page 31: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/31.jpg)
When to Rotate?
● The actual code for rotations is not too complex.
● Deciding when and where to rotate the tree, on the other hand, is a bit involved.
● There are many schemes we can use to determine this:● AVL trees maintain balance information in each
node, then rotate when the balance is off.● Red/Black trees assign each node a color, then
rotate when certain color combinations occur.
![Page 32: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/32.jpg)
An Interesting Observation
![Page 33: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/33.jpg)
Random Binary Search Trees
● If we build a binary search tree with totally random values, the resulting tree is (with high probability) within a constant factor of balanced.● Approximately 4.3 ln n
● Moreover, the average depth of a given node is often very low.● Approximately 2 ln n.
● If we structure the BST as if it were a random tree, we get (with high probability) a very good data structure!
![Page 34: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/34.jpg)
Treaps
● A treap is a data structure that combines a binary search tree and a binary heap.
● Each node stores two pieces of information:● The piece of information that we actually want to
store, and● A random real number.
● The tree is stored such that● The nodes are a binary search tree when looking up
the information, and● The nodes are a binary heap with respect to the
random real number.
![Page 35: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/35.jpg)
Ibex
Dikdik
Cat
Sloth
Dog Puppy
Kitty
314
106
4 42
137
178
271
![Page 36: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/36.jpg)
Treaps are Wonderful
● With very high probability, the height of an n-node treap is O(log n).
● Insertion is surprisingly simple once we have code for tree rotations.
● Deletion is straightforward once we have code for tree rotations.
![Page 37: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/37.jpg)
Inserting into a Treap
● Insertion into a treap is a combination of normal BST insertion and heap insertion.
● First, insert the node doing a normal BST insertion. This places the value into the right place.
● Next, bubble the node upward in the tree by rotating it with its parent until its value is smaller than its parent.
![Page 38: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/38.jpg)
Ibex
Dikdik
Cat
Sloth
Dog Puppy
314
106
4 42 178
271
![Page 39: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/39.jpg)
Ibex
Dikdik
Cat
Sloth
Dog Puppy
Kitty
314
106
4 42
571
178
271
![Page 40: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/40.jpg)
Ibex
Dikdik
Cat
Sloth
Dog
Puppy
Kitty
314
106
4 42 571
178
271
![Page 41: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/41.jpg)
Ibex
Dikdik
Cat SlothDog
Puppy
Kitty
314
106
4 42
571
178
271
![Page 42: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/42.jpg)
Ibex
Dikdik
Cat
Sloth
DogPuppy
Kitty
314
106
4 42
571
178
271
![Page 43: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/43.jpg)
Let's Code it Up!
![Page 44: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/44.jpg)
Removing from a Treap
● In general, removing a node from a BST is quite difficult because we have to make sure not to lose any nodes.
● For example, how do you remove the root of this tree?
● However, removing leaves is very easy, since they have no children.
4
2
1 3
6
5 7
![Page 45: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/45.jpg)
Removing from a Treap
● It would seem that, since a treap has extra structure on top of that of a BST, that removing from a treap would be extremely hard.
● However, it's actually quite simple:● Keep rotating the node to delete with its
larger child until it becomes a leaf.● Once the node is a leaf, delete it.
![Page 46: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/46.jpg)
Ibex
Dikdik
Cat
Sloth
DogPuppy
Kitty
314
106
4 42
571
178
271
![Page 47: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/47.jpg)
Ibex
Dikdik
Cat SlothDog
Puppy
Kitty
314
106
4 42
571
178
271
![Page 48: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/48.jpg)
Ibex
Dikdik
Cat
Sloth
Dog
Puppy
Kitty
314
106
4 42 571
178
271
![Page 49: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/49.jpg)
Ibex
Dikdik
Cat
Sloth
Dog Puppy
Kitty
314
106
4 42
571
178
271
![Page 50: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/50.jpg)
Ibex
Dikdik
Cat
Sloth
Dog Puppy
314
106
4 42 178
271
![Page 51: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/51.jpg)
Summary of Treaps
● Treaps give a (reasonably) straightforward way to guarantee that the height of a BST is not too great.
● Insertion into a treap is similar to insertion into a BST followed by insertion into a binary heap.
● Deletion from a treap is similar to the bubble-down step from a heap.
● All operations run in expected O(log n) time.
![Page 52: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/52.jpg)
A Survey of Other Data Structures
![Page 53: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/53.jpg)
Data Structures so Far
● We have seen many data structures over the past few weeks:● Dynamic arrays.● Linked lists.● Hash tables.● Tries.● Binary search trees (and treaps).● Binary heaps.
● These are the most-commonly-used data structures for general data storage.
![Page 54: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/54.jpg)
Specialized Data Structures
● For applications that manipulate specific types of data, other data structures exist that make certain operations surprisingly fast and efficient.
● Many critical applications of computers would be impossible without these data structures.
![Page 55: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/55.jpg)
k-d Trees
![Page 56: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/56.jpg)
Suppose that you want to efficientlystore points in k-dimensional space.
How might you organize thedata to efficiently query for
points within a region?
![Page 57: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/57.jpg)
(3, 1, 4)
(2, 3, 7) (4, 3, 4)
(2, 1, 3) (2, 4, 5) (6, 1, 4)
(5, 2, 5)(1, 4, 4) (0, 5, 7)
(4, 0, 6) (7, 1, 6)
![Page 58: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/58.jpg)
(3, 1, 4)
(2, 3, 7) (4, 3, 4)
(2, 1, 3) (2, 4, 5) (6, 1, 4)
(5, 2, 5)(1, 4, 4) (0, 5, 7)
(4, 0, 6) (7, 1, 6)
Search for (1, 4, 4)
![Page 59: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/59.jpg)
(3, 1, 4)
(2, 3, 7) (4, 3, 4)
(2, 1, 3) (2, 4, 5) (6, 1, 4)
(5, 2, 5)(1, 4, 4) (0, 5, 7)
(4, 0, 6) (7, 1, 6)
Search for (1, 4, 4)
![Page 60: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/60.jpg)
(3, 1, 4)
(2, 3, 7) (4, 3, 4)
(2, 1, 3) (2, 4, 5) (6, 1, 4)
(5, 2, 5)(1, 4, 4) (0, 5, 7)
(4, 0, 6) (7, 1, 6)
Search for (1, 4, 4)
![Page 61: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/61.jpg)
(3, 1, 4)
(2, 3, 7) (4, 3, 4)
(2, 1, 3) (2, 4, 5) (6, 1, 4)
(5, 2, 5)(1, 4, 4) (0, 5, 7)
(4, 0, 6) (7, 1, 6)
Search for (1, 4, 4)
![Page 62: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/62.jpg)
(3, 1, 4)
(2, 3, 7) (4, 3, 4)
(2, 1, 3) (2, 4, 5) (6, 1, 4)
(5, 2, 5)(1, 4, 4) (0, 5, 7)
(4, 0, 6) (7, 1, 6)
Search for (1, 4, 4)
![Page 63: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/63.jpg)
The Intuition
![Page 64: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/64.jpg)
The Intuition
2
-1 4
-2 0 63
![Page 65: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/65.jpg)
The Intuition
2
-1 4
-2 0 63
0
![Page 66: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/66.jpg)
The Intuition
2
-1 4
-2 0 63
0
![Page 67: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/67.jpg)
The Intuition
2
-1 4
-2 0 63
0
![Page 68: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/68.jpg)
The Intuition
2
-1 4
-2 0 63
0
Values less than two Values greater than two
![Page 69: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/69.jpg)
The Intuition
2
-1 4
-2 0 63
0
Values less than two Values greater than two
![Page 70: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/70.jpg)
Key Idea: Split Space in Half
![Page 71: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/71.jpg)
![Page 72: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/72.jpg)
![Page 73: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/73.jpg)
![Page 74: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/74.jpg)
Upper half-space
Lower half-space
![Page 75: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/75.jpg)
![Page 76: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/76.jpg)
![Page 77: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/77.jpg)
![Page 78: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/78.jpg)
x < x
0 x ≥ x0
![Page 79: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/79.jpg)
![Page 80: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/80.jpg)
![Page 81: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/81.jpg)
![Page 82: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/82.jpg)
![Page 83: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/83.jpg)
![Page 84: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/84.jpg)
Nearest-Neighbor Lookup
![Page 85: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/85.jpg)
![Page 86: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/86.jpg)
![Page 87: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/87.jpg)
![Page 88: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/88.jpg)
![Page 89: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/89.jpg)
![Page 90: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/90.jpg)
![Page 91: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/91.jpg)
y = y0
(x1, y
1)
(x
2, y
2)
r 1
r 2
![Page 92: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/92.jpg)
y = y0
(x1, y
1)
(x
2, y
2)
r 1
r 2
![Page 93: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/93.jpg)
y = y0
(x1, y
1)
(x
2, y
2)
r 1
r 2
|y1 - y
0|
|y2 – y
0|
![Page 94: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/94.jpg)
k-d Trees
● Assuming the points are nicely distributed, nearest-neighbor searches in k-d trees can run faster than O(n) time.
● Applications in computational geometry (collision detection), machine learning (nearest-neighbor classification), and many other places.
![Page 95: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/95.jpg)
Suffix Trees
![Page 96: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/96.jpg)
String Processing
● In computational biology, strings are enormously useful for storing DNA and RNA.
● Many important questions in biology can be addressed through string processing:● What is the most plausible evolutionary
history of the following genomes?● Are there particular gene sequences that
appear with high frequency within a genome?
![Page 97: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/97.jpg)
Suffix Trees
● A suffix tree is a (slightly modified) trie that stores all suffixes of a string S.
● Here is the suffix tree for “dikdik;” the $ is a marker for “end-of-string.”
dik
$ dik$
ik
$ dik$
k
$ dik$
![Page 98: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/98.jpg)
Suffix Trees
● Important, nontrivial, nonobvious fact: A suffix tree for a string of n characters can be built in time O(n).
● Given a string of length m, we can determine whether it is a substring of the original string in time O(m).
dik
$ dik$
ik
$ dik$
k
$ dik$
![Page 99: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/99.jpg)
Suffix Trees
● Other applications of suffix trees:● Searching for one genome within another
allowing for errors, insertions, and deletions in time O(n + m).
● Finding the longest common substring of two sequences in time O(n + m).
● Improving the performance of data compression routines by finding long repeated strings efficiently.
![Page 100: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/100.jpg)
Bloom Filters
![Page 101: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/101.jpg)
Distributing Data
● Websites like Google and Facebook deal with enormous amounts of data.
● Probably measured in hundreds of millions of gigabytes (hundreds of petabytes).
● There is absolutely no way to store this on one computer.
● Instead, data must be stored on multiple computers networked together.
![Page 102: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/102.jpg)
Looking up Data
● Suppose you are at Google implementing search.
● When you get a search query, you have to be able to know which computer knows what pages to display for that query.
● Network latency is, say, 2ms between you and each computer.
● If you have one thousand computers to search, you can't just query each one and ask.
![Page 103: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/103.jpg)
Bloom Filters
● A Bloom filter is a data structure similar to a set backed by a hash table.
● Stores a set of values in a way that may lead to false positives:● If the Bloom filter says that an object is not
present, it is definitely not present.● If the Bloom filter says that an object is
present, it may actually not be present.
![Page 104: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/104.jpg)
Bloom Filters
N N N N N N N N N N N N N N
![Page 105: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/105.jpg)
Bloom Filters
N N N N N N N N N N N N N N
ValueOne
![Page 106: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/106.jpg)
Bloom Filters
N N N N N N N N N N N N N N
ValueOne
![Page 107: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/107.jpg)
Bloom Filters
N N N Y N N Y N Y N N Y N Y
ValueOne
![Page 108: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/108.jpg)
Bloom Filters
N N N Y N N Y N Y N N Y N Y
![Page 109: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/109.jpg)
Bloom Filters
N N N Y N N Y N Y N N Y N Y
ValueTwo
![Page 110: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/110.jpg)
Bloom Filters
N N N Y N N Y N Y N N Y N Y
ValueTwo
![Page 111: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/111.jpg)
Bloom Filters
N Y N Y N Y Y N Y N N Y N Y
ValueTwo
![Page 112: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/112.jpg)
Bloom Filters
N Y N Y N Y Y N Y N N Y N Y
![Page 113: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/113.jpg)
Bloom Filters
N Y N Y N Y Y N Y N N Y N Y
ValueOne
![Page 114: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/114.jpg)
Bloom Filters
N Y N Y N Y Y N Y N N Y N Y
ValueOne
![Page 115: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/115.jpg)
Bloom Filters
N Y N Y N Y Y N Y N N Y N Y
![Page 116: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/116.jpg)
Bloom Filters
N Y N Y N Y Y N Y N N Y N Y
ValueTwo
![Page 117: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/117.jpg)
Bloom Filters
N Y N Y N Y Y N Y N N Y N Y
ValueTwo
![Page 118: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/118.jpg)
Bloom Filters
N Y N Y N Y Y N Y N N Y N Y
![Page 119: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/119.jpg)
Bloom Filters
N Y N Y N Y Y N Y N N Y N Y
ValueThree
![Page 120: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/120.jpg)
Bloom Filters
N Y N Y N Y Y N Y N N Y N Y
ValueThree
![Page 121: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/121.jpg)
Bloom Filters
N Y N Y N Y Y N Y N N Y N Y
![Page 122: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/122.jpg)
Bloom Filters
N Y N Y N Y Y N Y N N Y N Y
ValueFour
![Page 123: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/123.jpg)
Bloom Filters
N Y N Y N Y Y N Y N N Y N Y
ValueFour
![Page 124: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/124.jpg)
Bloom Filters and Networks
● Bloom filters can be used to mitigate the networking problem from earlier.
● Have each computer store a Bloom filter of what's stored on each other computer.
● To determine which computer has some data:● Look up that value in each Bloom filter.● Call up just the computers that might have it.
● Since Bloom filter lookup is substantially faster than a network query (probably 1000-10,000x), this solution is used extensively in practice.
![Page 125: 21: Advanced Data Structures](https://reader030.fdocuments.in/reader030/viewer/2022020213/5868f5871a28ab52568be265/html5/thumbnails/125.jpg)
Data structures make it possible tosolve important problems at scale.
You get to decide which problemswe'll be using them for.