Hop Jump and Leap Dhruv Matani and Gaurav

  • Slides: 48
Download presentation
Hop, Jump, and Leap Dhruv Matani and Gaurav Menghani

Hop, Jump, and Leap Dhruv Matani and Gaurav Menghani

Hop, Jump, and Leap* Dhruv Matani and Gaurav Menghani * In O(lg n) with

Hop, Jump, and Leap* Dhruv Matani and Gaurav Menghani * In O(lg n) with high probability

Hop, Jump, and Leap • Skip Lists are a simple and powerful datastructure. •

Hop, Jump, and Leap • Skip Lists are a simple and powerful datastructure. • Augmenting skip-lists to achieve decent running times for most problems. • By decent, we mean O(lg n) w. h. p

Hop, Jump, and Leap In class, we saw: Order Maintenance Static RMQ O(1) Amortized

Hop, Jump, and Leap In class, we saw: Order Maintenance Static RMQ O(1) Amortized Insert O(n) preprocessing O(1) Query

Hop, Jump, and Leap Today, we shall see: Order Maintenance O(lg n) w. h.

Hop, Jump, and Leap Today, we shall see: Order Maintenance O(lg n) w. h. p. Query Dynamic RMQ O(lg n) w. h. p. Insert O(lg n) w. h. p. Query

Dynamic Range Minimum Query using Skip Lists

Dynamic Range Minimum Query using Skip Lists

7 1 8 5 19 2 8 3 19 2 7 1 26 2

7 1 8 5 19 2 8 3 19 2 7 1 26 2 30 1 26 1 13 1 8 1 24 1 19 1 27 1

Find element at Index 4

Find element at Index 4

Find the element at index 4 7 1 8 5 19 2 8 3

Find the element at index 4 7 1 8 5 19 2 8 3 19 2 7 1 26 2 30 1 26 1 13 1 8 1 24 1 19 1 27 1

Find the element at index 4 6 7 1 7 1 26 2 30

Find the element at index 4 6 7 1 7 1 26 2 30 1 26 1 13 1 8 5 19 2 8 3 19 2 24 1 19 1 27 1

Find the element at index 4 7 1 8 5 19 2 8 3

Find the element at index 4 7 1 8 5 19 2 8 3 19 2 1 7 1 7 1 26 2 30 1 26 1 13 1 8 1 24 1 19 1 27 1

Find the element at index 4 7 1 6 7 1 7 1 26

Find the element at index 4 7 1 6 7 1 7 1 26 2 30 1 26 1 13 1 8 5 19 2 8 3 19 2 24 1 19 1 27 1

Find the element at index 4 7 1 8 5 19 2 8 3

Find the element at index 4 7 1 8 5 19 2 8 3 19 2 1 7 1 26 2 30 1 26 1 13 1 8 1 24 1 19 1 27 1

Find the element at index 4 7 1 8 5 19 2 8 3

Find the element at index 4 7 1 8 5 19 2 8 3 19 2 7 1 3 26 2 30 1 26 1 13 1 8 1 24 1 19 1 27 1

Find the element at index 4 7 1 8 5 19 2 8 3

Find the element at index 4 7 1 8 5 19 2 8 3 19 2 7 1 6 26 2 30 1 26 1 13 1 8 1 24 1 19 1 27 1

Find the element at index 4 7 1 8 5 19 2 8 3

Find the element at index 4 7 1 8 5 19 2 8 3 19 2 7 1 26 2 3 7 1 30 1 26 1 13 1 8 1 24 1 19 1 27 1

Find the element at index 4 7 1 8 5 19 2 8 3

Find the element at index 4 7 1 8 5 19 2 8 3 19 2 7 1 26 2 30 1 26 1 4 13 1 8 1 24 1 19 1 27 1

Find minimum in range [3, 7]

Find minimum in range [3, 7]

Find min [3, 7] 7 1 8 5 19 2 8 3 19 2

Find min [3, 7] 7 1 8 5 19 2 8 3 19 2 7 1 26 2 30 1 26 1 13 1 8 1 24 1 19 1 27 1

Find min [3, 7] 7 1 8 5 19 2 8 3 19 2

Find min [3, 7] 7 1 8 5 19 2 8 3 19 2 7 1 26 2 30 1 26 1 13 1 8 1 24 1 19 1 27 1

Find min [3, 7] = [3, 6], [7, 7] 7 1 7 1 26

Find min [3, 7] = [3, 6], [7, 7] 7 1 7 1 26 2 30 1 26 1 13 1 8 5 19 2 8 3 19 2 24 1 19 1 27 1

Find min [3, 7] [7, 7] 7 1 [3, 6] 7 1 7 1

Find min [3, 7] [7, 7] 7 1 [3, 6] 7 1 7 1 26 2 30 1 26 1 13 1 8 5 19 2 8 3 19 2 24 1 19 1 27 1

Find min [3, 7] [7, 7] 7 1 [3, 6] 7 1 7 1

Find min [3, 7] [7, 7] 7 1 [3, 6] 7 1 7 1 26 2 30 1 26 1 13 1 8 5 19 2 8 3 19 2 24 1 19 1 27 1

Find min [3, 7] [7, 7] 7 1 [3, 6] 7 1 7 1

Find min [3, 7] [7, 7] 7 1 [3, 6] 7 1 7 1 26 2 30 1 26 1 13 1 8 5 19 2 8 3 19 2 24 1 19 1 27 1

Find min [3, 7] [7, 7] 7 1 [3, 6] 7 1 26 2

Find min [3, 7] [7, 7] 7 1 [3, 6] 7 1 26 2 30 1 26 1 13 1 8 5 19 2 8 3 19 2 24 1 19 1 27 1

Find min [3, 7] [7, 7] 7 1 7 1 [3, 6] = [3,

Find min [3, 7] [7, 7] 7 1 7 1 [3, 6] = [3, 3], [4, 6] 30 1 26 2 26 1 13 1 8 5 19 2 8 3 19 2 24 1 19 1 27 1

Find min [3, 7] [7, 7] 7 1 [4, 6] 7 1 30 1

Find min [3, 7] [7, 7] 7 1 [4, 6] 7 1 30 1 26 2 26 1 13 1 8 5 19 2 8 3 19 2 24 1 19 1 27 1

Find min [3, 7] [7, 7] 7 1 [4, 6] 7 1 [3, 3]

Find min [3, 7] [7, 7] 7 1 [4, 6] 7 1 [3, 3] 7 1 30 1 26 2 26 1 13 1 8 5 19 2 8 3 19 2 24 1 19 1 27 1

Find min [3, 7] [7, 7] 7 1 [4, 6] 7 1 [3, 3]

Find min [3, 7] [7, 7] 7 1 [4, 6] 7 1 [3, 3] 30 1 26 2 26 1 13 1 8 5 19 2 8 3 19 2 24 1 19 1 27 1

Find min [3, 7] [7, 7] 7 1 [4, 6] 7 1 30 1

Find min [3, 7] [7, 7] 7 1 [4, 6] 7 1 30 1 [3, 3] 26 2 26 1 13 1 8 5 19 2 8 3 19 2 24 1 19 1 27 1

Find min [3, 7] [7, 7] 7 1 7 1 [4, 6] 26 2

Find min [3, 7] [7, 7] 7 1 7 1 [4, 6] 26 2 30 1 [3, 3] 26 1 13 1 8 5 19 2 8 3 19 2 24 1 19 1 27 1

Find min [3, 7] 7 1 8 5 19 2 8 3 19 2

Find min [3, 7] 7 1 8 5 19 2 8 3 19 2 7 1 [4, 6] 26 2 30 1 [3, 3] 26 1 13 1 8 1 24 1 [7, 7] 19 1 27 1

Insert element with Value 5 at Index 5

Insert element with Value 5 at Index 5

Insert 5 at index 5 7 1 8 5 19 2 8 3 19

Insert 5 at index 5 7 1 8 5 19 2 8 3 19 2 7 1 26 2 30 1 26 1 13 1 8 1 24 1 19 1 27 1

Insert 5 at index 5 7 1 8 5 19 2 8 3 19

Insert 5 at index 5 7 1 8 5 19 2 8 3 19 2 7 1 26 2 5 1 7 1 30 1 26 1 13 1 8 1 24 1 19 1 27 1

Insert 5 at index 5 7 1 8 5 19 2 8 3 19

Insert 5 at index 5 7 1 8 5 19 2 8 3 19 2 7 1 26 2 [5, 2] [8, 2] 5 1 7 1 30 1 26 1 13 1 8 1 24 1 19 1 27 1

Insert 5 at index 5 7 1 8 5 19 2 8 2 19

Insert 5 at index 5 7 1 8 5 19 2 8 2 19 2 7 1 26 2 [5, 2] 5 2 [8, 2] 5 1 7 1 30 1 26 1 13 1 8 1 24 1 19 1 27 1

Insert 5 at index 5 7 1 8 5 19 2 8 2 19

Insert 5 at index 5 7 1 8 5 19 2 8 2 19 2 [5, 4] [8, 2] 7 1 26 2 5 1 7 1 30 1 26 1 13 1 8 1 24 1 19 1 27 1

Insert 5 at index 5 7 1 [5, 4] 7 1 26 2 5

Insert 5 at index 5 7 1 [5, 4] 7 1 26 2 5 4 [8, 2] 5 2 8 5 19 2 8 2 19 2 5 1 7 1 30 1 26 1 13 1 8 1 24 1 19 1 27 1

Insert 5 at index 5 7 1 8 5 19 2 5 4 8

Insert 5 at index 5 7 1 8 5 19 2 5 4 8 2 19 2 5 2 8 2 19 2 [5, 4] [8, 2] 7 1 26 2 5 1 7 1 30 1 26 1 13 1 8 1 24 1 19 1 27 1

Insert 5 at index 5 7 1 5 6 19 2 5 4 8

Insert 5 at index 5 7 1 5 6 19 2 5 4 8 2 19 2 5 2 8 2 19 2 [5, 4] [8, 2] 7 1 26 2 5 1 7 1 30 1 26 1 13 1 8 1 24 1 19 1 27 1

Insert 5 at index 5 7 1 7 1 26 2 5 6 19

Insert 5 at index 5 7 1 7 1 26 2 5 6 19 2 5 4 8 2 19 2 5 2 8 2 19 2 5 1 7 1 30 1 26 1 13 1 8 1 24 1 19 1 27 1

Order Maintenance using Skip Lists

Order Maintenance using Skip Lists

 • To perform Order Maintenance, we insert an element after a given element.

• To perform Order Maintenance, we insert an element after a given element. • This costs O(lg n) w. h. p • Does X precede Y? – Is the rank(X) < rank(Y)? • This costs O(lg n) w. h. p

Does 13 precede 19? 7 1 8 5 19 2 8 3 19 2

Does 13 precede 19? 7 1 8 5 19 2 8 3 19 2 7 1 26 2 30 1 26 1 13 1 8 1 24 1 19 1 27 1

Does 13 precede 19? Rank(13) = 1 + 2 + 1 = 4 7

Does 13 precede 19? Rank(13) = 1 + 2 + 1 = 4 7 1 8 5 19 2 8 3 19 2 7 1 26 2 30 1 26 1 13 1 8 1 24 1 19 1 27 1

Does 13 precede 19? Rank(19) = 1 + 5 + 1 = 7 7

Does 13 precede 19? Rank(19) = 1 + 5 + 1 = 7 7 1 8 5 19 2 8 3 19 2 7 1 26 2 30 1 26 1 13 1 8 1 24 1 19 1 27 1

The End

The End