21
UNION - FIND ALGORITHM

Union find

Embed Size (px)

Citation preview

Page 1: Union find

UNION - FIND

ALGORITHM

Page 2: Union find

Union Find problem and a set of algorithms for solving it

Dynamic connectivityQuick find algorithmQuick union algorithmImprovementsApplications

Page 3: Union find
Page 4: Union find

There are basically two major parts in each algorithm:

-> Union Command: Connect the two objects-> Find / Connected query: Is there a path

connecting the two objects !

Page 5: Union find
Page 6: Union find

QUICK FIND (Eager Approach)

Data StructureInteger array id[] of size NInterpretation : p and q are connected iff they

have the same id.FindCheck if p and q have the same id.UnionTo merge components containing p and q,

change all entries whose id equals id[p] to id[q].

Page 7: Union find
Page 8: Union find
Page 9: Union find

QUICK UNION (Lazy Approach)

Data StructureInteger array id[] of size NInterpretation: id[i] is parent of IRoot of i is id[id[id[…id[i]…]]].FindCheck if p and q have the same root.UnionTo merge components between p and q, set the

id ofp’s root to the id of q’s root.

Page 10: Union find
Page 11: Union find
Page 12: Union find
Page 13: Union find
Page 14: Union find
Page 15: Union find
Page 16: Union find
Page 17: Union find
Page 18: Union find
Page 19: Union find
Page 20: Union find
Page 21: Union find

THANK YOU