lecture20 - Introduction to Algorithms...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 20 Prof. Erik Demaine
Background image of page 1

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

View Full DocumentRight Arrow Icon
Introduction to Algorithms Day 33 L20.2 © 2001 by Erik D. Demaine Disjoint-set data structure (Union-Find) Problem: Maintain a dynamic collection of pairwise-disjoint sets S = { S 1 , S 2 , …, S r } . Each set S i has one element distinguished as the representative element, rep [ S i ] . Must support 3 operations: M AKE -S ET ( x ) : adds new set { x } to S with rep [{ x }] = x (for any x S i for all i ). U NION ( x , y ) : replaces sets S x , S y with S x S y in S for any x , y in distinct sets S x , S y . F IND -S ET ( x ) : returns representative rep [ S x ] of set S x containing element x .
Background image of page 2
Introduction to Algorithms Day 33 L20.3 © 2001 by Erik D. Demaine Simple linked-list solution Store each set S i = { x 1 , x 2 , …, x k } as an (unordered) doubly linked list. Define representative element rep [ S i ] to be the front of the list, x 1 . S i : x 1 x 2 x k rep [ S i ] M AKE -S ET ( x ) initializes x as a lone node. F IND -S ET ( x ) walks left in the list containing x until it reaches the front of the list. U NION ( x , y ) concatenates the lists containing x and y , leaving rep. as F IND -S ET [ x ] . Θ (1) Θ ( n ) Θ ( n )
Background image of page 3

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

View Full DocumentRight Arrow Icon
Introduction to Algorithms Day 33 L20.4 © 2001 by Erik D. Demaine Simple balanced-tree solution Store each set S i = { x 1 , x 2 , …, x k } as a balanced tree (ignoring keys). Define representative element rep [ S i ] to be the root of the tree. x 1 x 4 x 3 x 2 x 5 M AKE -S ET ( x ) initializes x as a lone node. F IND -S ET ( x ) walks up the tree containing x until it reaches the root. U NION ( x , y ) concatenates the trees containing x and y , changing rep. S i ={ x 1 , x 2 , x 3 , x 4 , x 5 } rep [ S i ] Θ (1) Θ (lg n ) Θ (lg n )
Background image of page 4
Introduction to Algorithms Day 33 L20.5 © 2001 by Erik D. Demaine Plan of attack We will build a simple disjoint-union data structure that, in an amortized sense, performs significantly better than Θ (lg n ) per op., even better than Θ (lg lg n ) , Θ (lg lg lg n ) , etc., but not quite Θ (1). To reach this goal, we will introduce two key tricks . Each trick converts a trivial Θ ( n ) solution into a simple Θ (lg n ) amortized solution. Together, the two tricks yield a much better solution. First trick arises in an augmented linked list. Second trick arises in a tree structure.
Background image of page 5

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

View Full DocumentRight Arrow Icon
Introduction to Algorithms Day 33 L20.6 © 2001 by Erik D. Demaine Augmented linked-list solution S i : x 1 x 2 x k rep [ S i ] rep Store set S i = { x 1 , x 2 , …, x k } as unordered doubly linked list. Define rep [ S i ] to be front of list, x 1
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 07/09/2009 for the course CSE 6.046J/18. taught by Professor Piotrindykandcharlese.leiserson during the Fall '04 term at MIT.

Page1 / 25

lecture20 - Introduction to Algorithms...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online