Are p and q connected? Network connectivity Yes, they are - - PowerPoint PPT Presentation

are p and q connected network connectivity
SMART_READER_LITE
LIVE PREVIEW

Are p and q connected? Network connectivity Yes, they are - - PowerPoint PPT Presentation

Are p and q connected? Network connectivity Yes, they are connected! Network connectivity Problem: Given a set of nodes N and a set of links between pairs of nodes L. Find connectivity for node p and node q.(p N,q N) Real World


slide-1
SLIDE 1

Are p and q connected?

slide-2
SLIDE 2

Network connectivity

Yes, they are connected!

slide-3
SLIDE 3

Network connectivity

◮ Problem:

Given a set of nodes N and a set of links between pairs of nodes L. Find connectivity for node p and node q.(p∈N,q∈N)

slide-4
SLIDE 4

Real World Application

slide-5
SLIDE 5

Kruskal

slide-6
SLIDE 6

The union-find data structure

Zhengtian Xu Xiaoqing Geng Lihua Qian Ruxuan Zhang Chen Feng

Department of Computer Science and Engineering Shanghai Jiao Tong University

8th December 2016

slide-7
SLIDE 7

Outline

Brief Introduction for Union-Find Data Structure Improvement Time Complexity Analysis

slide-8
SLIDE 8

Union-Find data structure type

Goal.

Support three operations on a set of elements:

◮ MAKE-SET(x).Create a new set containing only element x. ◮ FIND(x). Return a canonical element in the set containing x. ◮ UNION(x, y). Merge the sets containing x and y.

slide-9
SLIDE 9

Union-Find example

6 a 2,7,9,3 b 4,5,8 c

slide-10
SLIDE 10

Union-Find example

6 a 2,7,9,3 b 4,5,8 c

FIND(9) = 2

slide-11
SLIDE 11

Union-Find example

6 a 2,7,9,3 b 4,5,8 c

FIND(9) = 2 MAKE-SET(1)

slide-12
SLIDE 12

Union-Find example

6 a 2,7,9,3 b 4,5,8 c 1 d

FIND(9) = 2 MAKE-SET(1)

slide-13
SLIDE 13

Union-Find example

6 a 2,7,9,3 b 4,5,8 c 1 d

FIND(9) = 2 MAKE-SET(1) UNION(2,4)

slide-14
SLIDE 14

Union-Find example

6 a 2,7,9,3,4,5,8 e 1 d

FIND(9) = 2 MAKE-SET(1) UNION(2,4)

slide-15
SLIDE 15

Union-Find data structure

Representation

Represent each set as a tree of elements.

◮ Each element has a parent pointer in the tree. ◮ The root serves as the canonical element. ◮ FIND(x). Find the root of the tree containing x. ◮ UNION(x, y). Make the root of one tree point to root of

  • ther tree.

d a c e b f root parent of e is c

slide-16
SLIDE 16

Find operation

Representation

FIND(x). Find the root of the tree containing x. d a c e b g f

slide-17
SLIDE 17

Find operation

Representation

FIND(x). Find the root of the tree containing x. d a c e b g f

FIND(g)

slide-18
SLIDE 18

Find operation

Representation

FIND(x). Find the root of the tree containing x. d a c e b g f

FIND(g)

slide-19
SLIDE 19

Find operation

Representation

FIND(x). Find the root of the tree containing x. d a c e b g f

FIND(g)

slide-20
SLIDE 20

Find operation

Representation

FIND(x). Find the root of the tree containing x. d a c e b g f

FIND(g)

slide-21
SLIDE 21

Find operation

Representation

FIND(x). Find the root of the tree containing x. d a c e b g f

FIND(g)

slide-22
SLIDE 22

Find operation

Representation

FIND(x). Find the root of the tree containing x. d a c e b g f

FIND(g) FIND(d)

slide-23
SLIDE 23

Find operation

Representation

FIND(x). Find the root of the tree containing x. d a c e b g f

FIND(g) FIND(d)

slide-24
SLIDE 24

Union operation

◮ Maintain an integer rank for each node, initially 0. ◮ Link root of smaller rank to root of larger rank; if tie, increase

rank of new root by 1.

  • Note. For now, rank = height.
slide-25
SLIDE 25

Union operation

◮ Maintain an integer rank for each node, initially 0. ◮ Link root of smaller rank to root of larger rank; if tie, increase

rank of new root by 1.

union(d, g)

d c i j g k b a h e rank = 2 rank = 1

  • Note. For now, rank = height.
slide-26
SLIDE 26

Union by rank

◮ Maintain an integer rank for each node, initially 0. ◮ Link root of smaller rank to root of larger rank; if tie, increase

rank of new root by 1.

union(d, g)

d c i j g k b a h e rank = 2

slide-27
SLIDE 27

Union by rank

◮ Maintain an integer rank for each node, initially 0. ◮ Link root of smaller rank to root of larger rank; if tie, increase

rank of new root by 1.

union(d, g)

d c l i j g k b a h e rank = 2 rank = 2

slide-28
SLIDE 28

Union by rank

◮ Maintain an integer rank for each node, initially 0. ◮ Link root of smaller rank to root of larger rank; if tie, increase

rank of new root by 1.

union(d, g)

d c l i j g k b a h e rank = 3

slide-29
SLIDE 29

Union by rank: analysis

Lemma 1. Using union by rank, for every root node r size(r) ≥ 2rank(r) Proof.[ by induction on number of links ]

◮ Base case: singleton tree has size 1 and rank 0. ◮ Inductive hypothesis: assume true after first i links.

slide-30
SLIDE 30

Union by rank: analysis

Proof.

◮ Case 1. [ rank(r) > rank(s) ] or [ rank(r) < rank(s) ]

size′(r) ≥ size(r) ≥ 2rank(r) = 2rank′(r) s r size = 8 size = 3 (rank = 2) (rank = 1)

slide-31
SLIDE 31

Union by rank: analysis

Proof.

◮ Case 2. [ rank(r) = rank(s) ]

size′(r) = size(r) + size(s) ≥ 2 × size(r) ≥ 2 × 2rank(r) = 2rank(r)+1 = 2rank′(r) s r size = 6 size = 3 (rank = 2) (rank = 1)

slide-32
SLIDE 32

Union by rank:analysis

Lemma 2. There are at most

n 2k elements of rank k.

slide-33
SLIDE 33

Union by rank:analysis

Lemma 2. There are at most

n 2k elements of rank k.

  • Proof. According to Lemma 1, for node has rank k, its sizes are

at least 2k. If the size of all elements is n. Obviously we can get Lemma 2.

slide-34
SLIDE 34

Union by rank:analysis

  • Theorem. Using Union by rank, any FIND operations takes

O(log2n) time in the worst case, where n is the number of elements; any UNION operations take constant time.

slide-35
SLIDE 35

Union by rank:analysis

  • Theorem. Using Union by rank, any FIND operations takes

O(log2n) time in the worst case, where n is the number of elements; any UNION operations take constant time. Proof.

◮ The running time of each operation is bounded by the tree

height.

◮ We can know that the height ≤ ⌊log2n⌋

slide-36
SLIDE 36

Outline

Brief Introduction for Union-Find Data Structure Improvement Time Complexity Analysis

slide-37
SLIDE 37

Improvement

Observation

◮ It is the height of the tree that affects the running time. ◮ When we’re trying to find the root of the tree containing a

given node, we’re touching all the nodes on the path from that node to the root.

So...

◮ Why not make each of those just point to the root? ◮ That’s the idea of path compression!

slide-38
SLIDE 38

Path compression

◮ Just after computing the root of the target node, set the

parent of each examined node to point to that root. a b d g i j h e f c height=4 find(j)

slide-39
SLIDE 39

Path compression

◮ Just after computing the root of the target node, set the

parent of each examined node to point to that root. a b d g i h e f c j

slide-40
SLIDE 40

Path compression

◮ Just after computing the root of the target node, set the

parent of each examined node to point to that root. a b d h e f c j g i

slide-41
SLIDE 41

Path compression

◮ Just after computing the root of the target node, set the

parent of each examined node to point to that root. a b e f c j g i d h height=2

slide-42
SLIDE 42

Path compression: benefits

◮ The resulting tree is much flatter.

◮ If the target node is very deep, path compression may

dramatically decrease the height of the tree.

◮ Speed up future operations on all the nodes on the path and

  • n those referencing them, directly or indirectly.
slide-43
SLIDE 43

Path compression: rank vs. height

◮ The rank of a tree does not change during path compression. ◮ ...but the height of a tree may decrease. ◮ Now it is possible that rank = height!

slide-44
SLIDE 44

Path compression: rank vs. height

◮ Example: Apply the following operations on the forest below.

◮ Union(a,g) ◮ Find(f) ◮ Find(j)

a b d f e c g h j i 3 2 1 2 1

slide-45
SLIDE 45

Path compression: rank vs. height

◮ Union(a,g) ◮ Find(f) ◮ Find(j)

a b d f e c g h j i 3 2 1 2 1

slide-46
SLIDE 46

Path compression: rank vs. height

◮ Union(a,g) ◮ Find(f) ◮ Find(j)

a b e d f c g h j i 3 2 1 2 1

slide-47
SLIDE 47

Path compression: rank vs. height

◮ Union(a,g) ◮ Find(f) ◮ Find(j)

a b e d f c g i h j 3 2 1 2 1

slide-48
SLIDE 48

Path compression: rank vs. height

◮ Union(a,g) ◮ Find(f) ◮ Find(j)

a b e d f c g i h j 3 2 1 2 1 height(a)=2=rank(a)

slide-49
SLIDE 49

Outline

Brief Introduction for Union-Find Data Structure Improvement Time Complexity Analysis

slide-50
SLIDE 50

Time Complexity Analysis

◮ Without path compression : O(log n) per find instruction. ◮ With path compression : O(log log n) per find instruction.

slide-51
SLIDE 51

Time Complexity Analysis

Lemma.

◮ There are at most n 2k nodes with rank k. ◮ rank(parent(x)) > rank(x). ◮ rank(root) ≤ log2 n.

slide-52
SLIDE 52

Time Complexity Analysis

Definition.

◮ f (t) = 2 × t

a b rank=2 rank=6 rank(a) > f (rank(b)) happy node, long edge a b rank=2 rank=4 rank(a) ≤ f (rank(b)) sad node, short edge

slide-53
SLIDE 53

Time Complexity Analysis

  • Observation. In one find operation, at most log log n long edges

are traversed. Proof. r b · · · c d rank ≤ log n rank ≥ 1

  • Observation. x is sad for at most rank(x) find operations.

Proof.

◮ rank(parent(x)) > rank(x) ◮ rank(parent(x)) increases per find operation. ◮ After rank(x) find ops,

rank(parent(x)) > rank(x) + rank(x) = f (rank(x))

slide-54
SLIDE 54

Time Complexity Analysis

time of all find ops

  • find ops #long edges

log log n × m

  • find ops #short edges
  • e(#find operations when e is short)
  • x rank(x)

2n

slide-55
SLIDE 55

Time Complexity Analysis

cost of all find operations =

  • find ops

(#long edges + #short edges)

  • find ops

#long edges ≤ log log n × #find ops

  • find ops

#short edges =

  • e

(#find operations when e is short) ≤

  • x

rank(x) ≤

log n

  • k=0

(k × #rank − k − nodes) ≤

  • k=0

k × n 2k ≤ 2n

slide-56
SLIDE 56

Time Complexity Analysis

  • Conclusion. With path compression, time for one find
  • p(#find operation ≥ n)is:

O(log log n + 2) = O(log log n) Improvement.

◮ f (t) = 2

t 2 → log ∗n

◮ Ackerman Function → α(n)

slide-57
SLIDE 57

Thanks!