Union-find Data Structure Last time Today Next Trees within - - PowerPoint PPT Presentation

union find data structure last time today next
SMART_READER_LITE
LIVE PREVIEW

Union-find Data Structure Last time Today Next Trees within - - PowerPoint PPT Presentation

Union-find Data Structure Last time Today Next Trees within Graphs Trees within Graphs Final Spanning Trees More efficient END OF 15-122! Minimum Spanning Trees Reminders Final exam Review session Practice


slide-1
SLIDE 1

Union-find Data Structure

slide-2
SLIDE 2

Last time

Trees within Graphs

  • Spanning Trees
  • Minimum Spanning

Trees Trees within Graphs

  • More efficient

Today Next

Final END OF 15-122!

slide-3
SLIDE 3

Reminders

  • Final exam
  • Review session
  • Practice exams
  • Practice written homework
slide-4
SLIDE 4

Spanning Tree

3 4 1 2 5 8 6 7 A spanning tree is a subgraph of original graph that is a tree containing all vertices but only a minimal subset of the original edges

slide-5
SLIDE 5

Finding a minimum spanning tree

Edge-centric algorithm: Kruskal’s algorithm Sort edges T = graph_new For each edge (i,j) of G: Are i and j already connected in T? Yes: Discard edge No: Add edge to T Greedy algorithm

  • Choice at each step will be part of the solution
  • No need for backtracking
slide-6
SLIDE 6

Juarez Fort Worth Columbus Erie Boston Indianapolis Detroit Atlanta Houston Galveston

6 7 3 9 11 1 8 5 6 11 2 2 2 3 7 5 2

A-B (11) A-C (6) A-H (8) A-I (5) B-C (7) B-E (5) C-D (2) C-E (2) C-H(11) C-I (2) D-E (2) D-I (3) F-H (3) F-I (9) F-J (6) G-H (1) H-J (7)

slide-7
SLIDE 7

Juarez Fort Worth Columbus Erie Boston Indianapolis Detroit Atlanta Houston Galveston

6 7 3 9 11 1 8 5 6 11 2 2 2 3 7 5 2

G-H (1) C-E (2) C-I (2) D-E (2) C-D (2) D-I (3) F-H (3) B-E (5) A-I (5) F-J (6) A-C (6) B-C (7) H-J (7) A-H (8) F-I (9) C-H (11) A-B (11)

slide-8
SLIDE 8

Juarez Fort Worth Columbus Erie Boston Indianapolis Detroit Atlanta Houston Galveston

6 7 9 11 8 5 6 11 2 3 7 5

G-H (1) C-E (2) C-I (2) D-E (2) C-D (2) D-I (3) F-H (3) B-E (5) A-I (5) F-J (6) A-C (6) B-C (7) H-J (7) A-H (8) F-I (9) C-H (11) A-B (11)

3 1 2 2 2

slide-9
SLIDE 9

Juarez Fort Worth Columbus Erie Boston Indianapolis Detroit Atlanta Houston Galveston

6 7 9 11 8 5 6 11 3 7 5

G-H (1) C-E (2) C-I (2) D-E (2) C-D (2) D-I (3) F-H (3) B-E (5) A-I (5) F-J (6) A-C (6) B-C (7) H-J (7) A-H (8) F-I (9) C-H (11) A-B (11)

2 3 1 2 2 2

slide-10
SLIDE 10

Juarez Fort Worth Columbus Erie Boston Indianapolis Detroit Atlanta Houston Galveston

6 7 9 11 8 5 6 11 3 7 5

G-H (1) C-E (2) C-I (2) D-E (2) C-D (2) D-I (3) F-H (3) B-E (5) A-I (5) F-J (6) A-C (6) B-C (7) H-J (7) A-H (8) F-I (9) C-H (11) A-B (11)

2 3 1 2 2 2

slide-11
SLIDE 11

Juarez Fort Worth Columbus Erie Boston Indianapolis Detroit Atlanta Houston Galveston

6 7 9 11 8 5 6 11 3 7 5

G-H (1) C-E (2) C-I (2) D-E (2) C-D (2) D-I (3) F-H (3) B-E (5) A-I (5) F-J (6) A-C (6) B-C (7) H-J (7) A-H (8) F-I (9) C-H (11) A-B (11)

2 3 1 2 2 2

slide-12
SLIDE 12

Juarez Fort Worth Columbus Erie Boston Indianapolis Detroit Atlanta Houston Galveston

6 7 9 11 8 5 6 11 3 7 5

G-H (1) C-E (2) C-I (2) D-E (2) C-D (2) D-I (3) F-H (3) B-E (5) A-I (5) F-J (6) A-C (6) B-C (7) H-J (7) A-H (8) F-I (9) C-H (11) A-B (11)

2 3 1 2 2 2

slide-13
SLIDE 13

Juarez Fort Worth Columbus Erie Boston Indianapolis Detroit Atlanta Houston Galveston

6 7 9 11 8 5 6 11 3 7 5

G-H (1) C-E (2) C-I (2) D-E (2) C-D (2) D-I (3) F-H (3) B-E (5) A-I (5) F-J (6) A-C (6) B-C (7) H-J (7) A-H (8) F-I (9) C-H (11) A-B (11)

2 3 1 2 2 2

slide-14
SLIDE 14

Juarez Fort Worth Columbus Erie Boston Indianapolis Detroit Atlanta Houston Galveston

6 7 9 11 8 5 6 11 3 7 5

G-H (1) C-E (2) C-I (2) D-E (2) C-D (2) D-I (3) F-H (3) B-E (5) A-I (5) F-J (6) A-C (6) B-C (7) H-J (7) A-H (8) F-I (9) C-H (11) A-B (11)

2 3 2 2 1 2

slide-15
SLIDE 15

Juarez Fort Worth Columbus Erie Boston Indianapolis Detroit Atlanta Houston Galveston

6 7 9 11 8 5 6 11 3 7 5

G-H (1) C-E (2) C-I (2) D-E (2) C-D (2) D-I (3) F-H (3) B-E (5) A-I (5) F-J (6) A-C (6) B-C (7) H-J (7) A-H (8) F-I (9) C-H (11) A-B (11)

2 3 2 2 1 2

slide-16
SLIDE 16

Juarez Fort Worth Columbus Erie Boston Indianapolis Detroit Atlanta Houston Galveston

6 7 9 11 8 5 6 11 3 7 5

G-H (1) C-E (2) C-I (2) D-E (2) C-D (2) D-I (3) F-H (3) B-E (5) A-I (5) F-J (6) A-C (6) B-C (7) H-J (7) A-H (8) F-I (9) C-H (11) A-B (11)

2 3 2 2 1 2

slide-17
SLIDE 17

Juarez Fort Worth Columbus Erie Boston Indianapolis Detroit Atlanta Houston Galveston

6 7 9 11 8 5 6 11 3 7 5

G-H (1) C-E (2) C-I (2) D-E (2) C-D (2) D-I (3) F-H (3) B-E (5) A-I (5) F-J (6) A-C (6) B-C (7) H-J (7) A-H (8) F-I (9) C-H (11) A-B (11)

2 3 1 2 2 2

slide-18
SLIDE 18

Juarez Fort Worth Columbus Erie Boston Indianapolis Detroit Atlanta Houston Galveston

6 7 9 11 8 5 6 11 3 7 5

G-H (1) C-E (2) C-I (2) D-E (2) C-D (2) D-I (3) F-H (3) B-E (5) A-I (5) F-J (6) A-C (6) B-C (7) H-J (7) A-H (8) F-I (9) C-H (11) A-B (11)

2 3 2 2 1 2

slide-19
SLIDE 19

Juarez Fort Worth Columbus Erie Boston Indianapolis Detroit Atlanta Houston Galveston

6 7 9 11 8 5 6 11 3 7 5

G-H (1) C-E (2) C-I (2) D-E (2) C-D (2) D-I (3) F-H (3) B-E (5) A-I (5) F-J (6) A-C (6) B-C (7) H-J (7) A-H (8) F-I (9) C-H (11) A-B (11)

2 3 2 2 1 2

slide-20
SLIDE 20

Juarez Fort Worth Columbus Erie Boston Indianapolis Detroit Atlanta Houston Galveston

6 7 9 11 8 5 6 11 3 7 5

G-H (1) C-E (2) C-I (2) D-E (2) C-D (2) D-I (3) F-H (3) B-E (5) A-I (5) F-J (6) A-C (6) B-C (7) H-J (7) A-H (8) F-I (9) C-H (11) A-B (11)

2 3 2 2 1 2

slide-21
SLIDE 21

Juarez Fort Worth Columbus Erie Boston Indianapolis Detroit Atlanta Houston Galveston

6 7 9 11 8 5 6 11 3 7 5

G-H (1) C-E (2) C-I (2) D-E (2) C-D (2) D-I (3) F-H (3) B-E (5) A-I (5) F-J (6) A-C (6) B-C (7) H-J (7) A-H (8) F-I (9) C-H (11) A-B (11)

2 3 2 2 1 2

slide-22
SLIDE 22

Spanning Tree

3 4 1 2 5 8 6 7 Complexity

  • General graph DFS/BFS

𝑃(𝑓) with adjacency lists

  • Running DFS/BFS on spanning tree

𝑃(𝑓) but 𝑓 = 𝑤 − 1  𝑃(𝑤)

slide-23
SLIDE 23

Finding a minimum spanning tree

Edge-centric algorithm: Kruskal’s algorithm Sort edges T = graph_new For each edge (i,j) of G: Are i and j already connected in T? Yes: Discard edge No: Add edge to T O(e log e) O(1) e O(v) O(1) O(1) = O(ev + e log e)  O(ev) Where can we make it faster?

slide-24
SLIDE 24

Canonical Representatives

Trying to speed up connectivity check 3 4 1 2 5 8 6 7 9

Relation of being connected is

  • reflexive
  • symmetric
  • transitive
slide-25
SLIDE 25

Canonical Representatives

Trying to speed up connectivity check 3 4 1 2 5 8 6 7 9

Every connected component is an equivalence class

slide-26
SLIDE 26

Union-find

  • 1. Check connectivity, finding canonical representative
  • 2. Create union

3 4 1 2 5 8 6 7 9

slide-27
SLIDE 27

Basic Union-find

4 5 3 2 1 4 5 3 2 1

1 2 3 4 5

Edge order (4,5) (3,5) (1,2) (3,4) (2,3) (0,2) (0,1) Union-find data structure

1 2 3 4 4 1 2 3 3 4 1 1 3 3 4 1 1 1 3 4

Init

1 1 1 1 3 4

After (4,5) After (3,5) After (1,2) After (2,3) After (0,2) How fast is the connectivity check?

slide-28
SLIDE 28

Union-find (UF) Tree

4 5 3 2 1

slide-29
SLIDE 29

Finding a minimum spanning tree

Edge-centric algorithm: Kruskal’s algorithm Sort edges T = graph_new For each edge (i,j) of G: Are i and j already connected in T? Yes: Discard edge No: Add edge to T O(e log e) O(1) e * O(v) O(1) O(1) = O(ev + e log e)  O(ev) With our basic union-find, still O(v) because UF tree may not be balanced

slide-30
SLIDE 30

Basic Union-find

4 5 3 2 1 4 5 3 2 1

1 2 3 4 5

Edge order (4,5) (3,5) (1,2) (3,4) (2,3) (0,2) (0,1) Union-find data structure

1 2 3 4 4 1 2 3 3 4 1 1 3 3 4 1 1 1 3 4

Init

1 1 1 1 3 4

After (4,5) After (3,5) After (1,2) After (2,3) After (0,2)

slide-31
SLIDE 31

Height Tracking

How should we combine trees to control height?

Merge shorter tree into taller tree by appointing the root of the taller tree as the canonical representative of the combined tree  height becomes the height of the bigger tree If trees have the same height, it does not matter which tree is merged into which  height increases by 1

slide-32
SLIDE 32

1 2 3 4 5

Basic Union-find

4 5 3 2 1 4 5 3 2 1

  • 1
  • 1
  • 1
  • 1
  • 1
  • 1

Edge order (4,5) (3,5) (1,2) (3,4) (2,3) (0,2) (0,1)

Union-find data structure

  • 1
  • 1
  • 1
  • 1
  • 2

4

  • 1
  • 1
  • 1
  • 3

3 4

  • 1
  • 2

1

  • 3

3 4

  • 1
  • 4

1 1 3 4

Init

1

  • 4

1 1 3 4

After (4,5) After (3,5) After (1,2) After (2,3) After (0,2) Negative numbers: height (used for canonical rep. only)

slide-33
SLIDE 33

1 2 3 4 5

Height-tracking Union-find

4 5 3 2 1 4 5 3 2 1

  • 1
  • 1
  • 1
  • 1
  • 1
  • 1

Edge order (4,5) (3,5) (1,2) (3,4) (2,3) (0,2) (0,1)

Union-find data structure

  • 1
  • 1
  • 1
  • 1
  • 2

4

  • 1
  • 1
  • 1

4

  • 2

4

  • 1
  • 2

1 4

  • 2

4

  • 1
  • 3

1 4 1 4

Init

1

  • 3

1 1 1 4

After (4,5) After (3,5) After (1,2) After (2,3) After (0,2) Negative numbers: height (used for canonical rep. only)

slide-34
SLIDE 34

Finding a minimum spanning tree

Edge-centric algorithm: Kruskal’s algorithm Sort edges T = graph_new For each edge (i,j) of G: Are i and j already connected in T? Yes: Discard edge No: Add edge to T O(e log e) O(1) e * O(log v) O(1) O(1)

slide-35
SLIDE 35

Union-find with Height Tracking

J F C E B I D A H G 5 3 8 4 2 1 6 9 7

G-H (1) C-E (2) C-I (2) D-E (2) C-D (2) D-I (3) F-H (3) B-E (5) A-I (5) F-J (6) A-C (6) B-C (7) H-J (7) A-H (8) F-I (9) C-H (11) A-B (11)

A B C D E F G H I J 1 2 3 4 5 6 7 8 9

slide-36
SLIDE 36

Union-find with Height Tracking

J F C E B I D A H G 5 3 8 4 2 1 6 9 7

G-H (1) C-E (2) C-I (2) D-E (2) C-D (2) D-I (3) F-H (3) B-E (5) A-I (5) F-J (6) A-C (6) B-C (7) H-J (7) A-H (8) F-I (9) C-H (11) A-B (11)

A B C D E F G H I J 1 2 3 4 5 6 7 8 9

  • 1
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1
slide-37
SLIDE 37

Union-find with Height Tracking

J F C E B I D A H G 5 3 8 4 2 1 6 9 7

G-H (1) C-E (2) C-I (2) D-E (2) C-D (2) D-I (3) F-H (3) B-E (5) A-I (5) F-J (6) A-C (6) B-C (7) H-J (7) A-H (8) F-I (9) C-H (11) A-B (11)

A B C D E F G H I J 1 2 3 4 5 6 7 8 9

  • 1
  • 1
  • 1
  • 1
  • 1
  • 1
  • 2

6

  • 1
  • 1
slide-38
SLIDE 38

Union-find with Height Tracking

J F C E B I D A H G 5 3 8 4 2 1 6 9 7

G-H (1) C-E (2) C-I (2) D-E (2) C-D (2) D-I (3) F-H (3) B-E (5) A-I (5) F-J (6) A-C (6) B-C (7) H-J (7) A-H (8) F-I (9) C-H (11) A-B (11)

A B C D E F G H I J 1 2 3 4 5 6 7 8 9

  • 1
  • 1

4

  • 1
  • 2
  • 1
  • 2

6

  • 1
  • 1
slide-39
SLIDE 39

Union-find with Height Tracking

J F C E B I D A H G 5 3 8 4 2 1 6 9 7

G-H (1) C-E (2) C-I (2) D-E (2) C-D (2) D-I (3) F-H (3) B-E (5) A-I (5) F-J (6) A-C (6) B-C (7) H-J (7) A-H (8) F-I (9) C-H (11) A-B (11)

A B C D E F G H I J 1 2 3 4 5 6 7 8 9

  • 1
  • 1

4

  • 1
  • 2
  • 1
  • 2

6 4

  • 1
slide-40
SLIDE 40

Union-find with Height Tracking

J F C E B I D A H G 5 3 8 4 2 1 6 9 7

G-H (1) C-E (2) C-I (2) D-E (2) C-D (2) D-I (3) F-H (3) B-E (5) A-I (5) F-J (6) A-C (6) B-C (7) H-J (7) A-H (8) F-I (9) C-H (11) A-B (11)

A B C D E F G H I J 1 2 3 4 5 6 7 8 9

  • 1
  • 1

4 4

  • 2
  • 1
  • 2

6 4

  • 1
slide-41
SLIDE 41

Union-find with Height Tracking

J F C E B I D A H G 5 3 8 4 2 1 6 9 7

G-H (1) C-E (2) C-I (2) D-E (2) C-D (2) D-I (3) F-H (3) B-E (5) A-I (5) F-J (6) A-C (6) B-C (7) H-J (7) A-H (8) F-I (9) C-H (11) A-B (11)

A B C D E F G H I J 1 2 3 4 5 6 7 8 9

  • 1
  • 1

4 4

  • 2
  • 1
  • 2

6 4

  • 1
slide-42
SLIDE 42

Union-find with Height Tracking

J F C E B I D A H G 5 3 8 4 2 1 6 9 7

G-H (1) C-E (2) C-I (2) D-E (2) C-D (2) D-I (3) F-H (3) B-E (5) A-I (5) F-J (6) A-C (6) B-C (7) H-J (7) A-H (8) F-I (9) C-H (11) A-B (11)

A B C D E F G H I J 1 2 3 4 5 6 7 8 9

  • 1
  • 1

4 4

  • 2

6

  • 2

6 4

  • 1
slide-43
SLIDE 43

Union-find with Height Tracking

J F C E B I D A H G 5 3 8 4 2 1 6 9 7

G-H (1) C-E (2) C-I (2) D-E (2) C-D (2) D-I (3) F-H (3) B-E (5) A-I (5) F-J (6) A-C (6) B-C (7) H-J (7) A-H (8) F-I (9) C-H (11) A-B (11)

A B C D E F G H I J 1 2 3 4 5 6 7 8 9

  • 1

4 4 4

  • 2

6

  • 2

6 4

  • 1
slide-44
SLIDE 44

Union-find with Height Tracking

J F C E B I D A H G 5 3 8 4 2 1 6 9 7

G-H (1) C-E (2) C-I (2) D-E (2) C-D (2) D-I (3) F-H (3) B-E (5) A-I (5) F-J (6) A-C (6) B-C (7) H-J (7) A-H (8) F-I (9) C-H (11) A-B (11)

A B C D E F G H I J 1 2 3 4 5 6 7 8 9 4 4 4 4

  • 2

6

  • 2

6 4

  • 1
slide-45
SLIDE 45

Union-find with Height Tracking

J F C E B I D A H G 5 3 8 4 2 1 6 9 7

G-H (1) C-E (2) C-I (2) D-E (2) C-D (2) D-I (3) F-H (3) B-E (5) A-I (5) F-J (6) A-C (6) B-C (7) H-J (7) A-H (8) F-I (9) C-H (11) A-B (11)

A B C D E F G H I J 1 2 3 4 5 6 7 8 9 4 4 4 4

  • 2

6

  • 2

6 4 6

slide-46
SLIDE 46

Union-find with Height Tracking

J F C E B I D A H G 5 3 8 4 2 1 6 9 7

G-H (1) C-E (2) C-I (2) D-E (2) C-D (2) D-I (3) F-H (3) B-E (5) A-I (5) F-J (6) A-C (6) B-C (7) H-J (7) A-H (8) F-I (9) C-H (11) A-B (11)

A B C D E F G H I J 1 2 3 4 5 6 7 8 9 4 4 4 4

  • 2

6

  • 2

6 4 6

slide-47
SLIDE 47

Union-find with Height Tracking

J F C E B I D A H G 5 3 8 4 2 1 6 9 7

G-H (1) C-E (2) C-I (2) D-E (2) C-D (2) D-I (3) F-H (3) B-E (5) A-I (5) F-J (6) A-C (6) B-C (7) H-J (7) A-H (8) F-I (9) C-H (11) A-B (11)

A B C D E F G H I J 1 2 3 4 5 6 7 8 9 4 4 4 4

  • 2

6

  • 2

6 4 6

slide-48
SLIDE 48

Union-find with Height Tracking

J F C E B I D A H G 5 3 8 4 2 1 6 9 7

G-H (1) C-E (2) C-I (2) D-E (2) C-D (2) D-I (3) F-H (3) B-E (5) A-I (5) F-J (6) A-C (6) B-C (7) H-J (7) A-H (8) F-I (9) C-H (11) A-B (11)

A B C D E F G H I J 1 2 3 4 5 6 7 8 9 4 4 4 4

  • 2

6

  • 2

6 4 6

slide-49
SLIDE 49

Union-find with Height Tracking

J F C E B I D A H G 5 3 8 4 2 1 6 9 7

G-H (1) C-E (2) C-I (2) D-E (2) C-D (2) D-I (3) F-H (3) B-E (5) A-I (5) F-J (6) A-C (6) B-C (7) H-J (7) A-H (8) F-I (9) C-H (11) A-B (11)

A B C D E F G H I J 1 2 3 4 5 6 7 8 9 4 4 4 4 6 6

  • 3

6 4 6

slide-50
SLIDE 50

Finding a minimum spanning tree

Edge-centric algorithm: Kruskal’s algorithm with height tracking Sort edges T = graph_new For each edge (i,j) of G: Are i and j already connected in T? Yes: Discard edge No: Add edge to T O(e log e) O(1) e O(log v) O(1) O(1) = O(e log v + e log e)  O(e log e)

slide-51
SLIDE 51

Height invariant for UF graphs

A tree with height h has at least 2h-1 nodes A tree with v vertices has height at most log v + 1 Equivalently,

slide-52
SLIDE 52

Height invariant for UF graphs

A tree with height h has at least 2h-1 nodes

Base case: h = 1 Inductive case: h > 1 (height of tree after merging T1 with height h1 and T2 with height h2) case: h1 > h2 case: h2 > h1 case: h1 = h2

slide-53
SLIDE 53

Height invariant for UF graphs

A tree with height h has at least 2h-1 nodes

Inductive case: h > 1 (height of tree after merging T1 with height h1 and T2 with height h2) case: h1 > h2 We merge the shorter tree into the taller one, so h = h1. By inductive hypothesis, T1 has at least 2h-1 nodes. T is the combined tree. So, it has at least 2h-1 nodes.

slide-54
SLIDE 54

Height invariant for UF graphs

A tree with height h has at least 2h-1 nodes

Inductive case: h > 1 (height of tree after merging T1 with height h1 and T2 with height h2) case: h1 < h2 …

slide-55
SLIDE 55

Height invariant for UF graphs

A tree with height h has at least 2h-1 nodes

Inductive case: h > 1 (height of tree after merging T1 with height h1 and T2 with height h2) case: h1 = h2 h = h1+ 1 = h2+ 1. By inductive hypothesis, T1 has at least 2 h1 -1 nodes and T1 has at least 2 h2 -1 nodes . T is the combined tree. So it has at least 2h-2 + 2h-2 = 2h-1 nodes.

slide-56
SLIDE 56

Complexity of Union-find

Finding a canonical representative costs O(log v) Can we do better?

  • As we follow a path to the root,

point all the intermediate nodes to the root

  • This is called path compression and has almost constant amortized complexity.

2 3 4 1

After looking for the canonical representative of 3

2 3 4 1

slide-57
SLIDE 57

Congratulations and Thanks!