# lec12 - Introduction to Algorithms 6.046J/18.401J LECTURE...

This preview shows pages 1–11. Sign up to view the full content.

October 26, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.1 Introduction to Algorithms 6.046J/18.401J L ECTURE 12 Skip Lists Data structure Randomized insertion With-high-probability bound Analysis Coin flipping Prof. Erik D. Demaine

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
October 26, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.2 Skip lists • Simple randomized dynamic search structure – Invented by William Pugh in 1989 – Easy to implement • Maintains a dynamic set of n elements in O (lg n ) time per operation in expectation and with high probability – Strong guarantee on tail of distribution of T ( n ) O (lg n ) “almost always”
October 26, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.3 One linked list Start from simplest data structure: (sorted) linked list • Searches take Θ ( n ) time in worst case • How can we speed up searches? 14 14 23 23 34 34 42 42 50 50 59 59 66 66 72 72 79 79

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
October 26, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.4 Two linked lists Suppose we had two sorted linked lists (on subsets of the elements) • Each element can appear in one or both lists • How can we speed up searches? 14 14 23 23 34 34 42 42 50 50 59 59 66 66 72 72 79 79
October 26, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.5 Two linked lists as a subway I DEA : Express and local subway lines (à la New York City 7th Avenue Line) • Express line connects a few of the stations • Local line connects all stations • Links between lines at common stations 14 14 23 23 34 34 42 42 50 50 59 59 66 66 72 72 79 79 14 14 34 34 42 42 72 72

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
October 26, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.6 Searching in two linked lists S EARCH ( x ) : • Walk right in top linked list ( L 1 ) until going right would go too far • Walk down to bottom linked list ( L 2 ) • Walk right in L 2 until element found (or not) 14 14 23 23 34 34 42 42 50 50 59 59 66 66 72 72 79 79 14 14 34 34 42 42 72 72
October 26, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.7 Searching in two linked lists E XAMPLE : S EARCH (59) Too far: 59 < 72 14 14 23 23 34 34 42 42 50 50 59 59 66 66 72 72 79 79 14 14 34 34 42 42 72 72

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
October 26, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.8 Design of two linked lists Q UESTION : Which nodes should be in L 1 ? • In a subway, the “popular stations” • Here we care about worst-case performance Best approach: Evenly space the nodes in L 1 • But how many nodes should be in L 1 ? 14 14 23 23 34 34 42 42 50 50 59 59 66 66 72 72 79 79 14 14 34 34 42 42 72 72