CS 1501 www.cs.pitt.edu/~nlf4/cs1501/ Union Find Dynamic - - PowerPoint PPT Presentation

cs 1501
SMART_READER_LITE
LIVE PREVIEW

CS 1501 www.cs.pitt.edu/~nlf4/cs1501/ Union Find Dynamic - - PowerPoint PPT Presentation

CS 1501 www.cs.pitt.edu/~nlf4/cs1501/ Union Find Dynamic connectivity problem For a given graph G, can we determine whether or not two vertices are connected in G? Can also be viewed as checking subset membership Important for


slide-1
SLIDE 1

CS 1501

www.cs.pitt.edu/~nlf4/cs1501/

Union Find

slide-2
SLIDE 2
  • For a given graph G, can we determine whether or not two

vertices are connected in G?

  • Can also be viewed as checking subset membership
  • Important for many practical applications
  • We will solve this problem using a union/find data structure

Dynamic connectivity problem

2

slide-3
SLIDE 3
  • Have an id array simply store the component id for each

item in the union/find structure

○ How do we determine if two vertices are connected? ○ How do we establish the connected components? ■ Add graph edges one at a time to UF data structure using union operations

A simple approach

3

slide-4
SLIDE 4

Example

5 3 4 2 1 6 7 U(2, 0) U(4, 7) U(1, 2) U(3, 2) U(4, 5) U(5, 7) U(6, 3) 1 2 3 4 5 6 1 2 3 4 5 6 ID: 7 7 4 1 1 3 3 3 4 6 6 6 6

4

slide-5
SLIDE 5
  • Runtime?

○ To find if two vertices are connected? ○ For a union operation?

Analysis of our simple approach

5

slide-6
SLIDE 6

UF (int n) void union(int p, int q) int find (int p) boolean connected (int p, int q) int count()

Union Find API

Initialize with n items numbered 0 to n-1 Connect p with q Return id of the connected component that p is in True if p and q are connected Number of connected components

6

slide-7
SLIDE 7

public int count() { return count; } public boolean connected(int p, int q) { return find(p) == find(q); }

Covering the basics

7

slide-8
SLIDE 8

public UF(int n) { count = n; id = new int[n]; for (int i = 0; i < n; i++) { id[i] = i; } } public int find(int p) { return id[p]; } public void union(int p, int q) { int pID = find(p), qID = find(q); if (pID == qID) return; for(int i = 0; i < id.length; i++) if (id[i] == pID) id[i] = qID; count--; }

Implementing the Fast-Find approach

8

slide-9
SLIDE 9
  • With this knowledge of union/find, how, exactly can it be

used as a part of Kruskal’s algorithm?

○ What is the runtime of Kruskal’s algorithm?

Kruskal’s algorithm

9

slide-10
SLIDE 10

Kruskal's example revisited

1 3 2 4 5 6 5 1 5 5 3 2 4 6 6 PQ: 5: (0, 3) 6: (0, 1) 1: (0, 2) 3: (1, 4) 5: (1, 2) 5: (2, 3) 2: (3, 5) 4: (2, 5) 6: (2, 4) 6: (4, 5)

10

slide-11
SLIDE 11
  • What if we store our connected components as a forest of

trees?

○ Each tree representing a different connected component ○ Every time a new connection is made, we simply make one tree the child of another

Can we improve on union()’s runtime?

11

slide-12
SLIDE 12

Tree example

5 3 4 2 1 6 7 1 2 3 4 5 6 7 2 7 1 3

12

slide-13
SLIDE 13

public int find(int p) { while (p != id[p]) p = id[p]; return p; } public void union(int p, int q) { int i = find(p); int j = find(q); if (i == j) return; id[i] = j; count--; }

Implementation using the same id array

13

slide-14
SLIDE 14
  • Runtime?

○ find(): ■ Bound by the height of the tree ○ union(): ■ Bound by the height of the tree

  • What is the max height of the tree?

○ Can we modify our approach to cap its max height?

Forest of trees implementation analysis

14

slide-15
SLIDE 15

Weighted tree example

5 3 4 2 1 6 7

15

1 2 3 4 5 6 7 2 7

slide-16
SLIDE 16

public UF(int n) { count = n; id = new int[n]; sz = new int[n]; for (int i = 0; i < n; i++) { id[i] = i; sz[i] = 1; } } public void union(int p, int q) { int i = find(p), j = find(q); if (i == j) return; if (sz[i] < sz[j]) { id[i] = j; sz[j] += sz[i]; } else { id[j] = i; sz[i] += sz[j]; } count--; }

Weighted trees

16

slide-17
SLIDE 17
  • Runtime?

○ find()? ○ union()?

  • Can we do any better?

Weighted tree approach analysis

17

slide-18
SLIDE 18
  • What is the runtime of Kruskal’s algorithm??

Kruskal’s algorithm, once again

18

slide-19
SLIDE 19

Path Compression

19

1 2 3 5 6 7 4 find(4) 4 5 4 find(0) 2