Algorithm Analysis (CSC 301 2015F) : EBoards

CSC301.01 2015F, Class 28: Sets and Union-Find


Overview

Preliminaries

Admin

Extra Credit

Academic

Peer

Questions

Kruskal's Algorithm

sort all of the edges by weight
for each edge, e, in edges,
 if e connects two separate components, add it to MST

Analysis

Step back, think about what we are dealing with, abstract away details, think about implementation.

A Set ADT

Let's design the ADT: What are the primary procedures, their parameters, their product, their purpose, and potential problems?

The Union-Find algorithm

For Kruskal's algorithm, we need only two operations:

See book or board for the algorithm.

union(set1) root1 = findRoot(set1) root2 = findRoot(set2) if (root1.size > root2.size) root2.parent = root1; root1.size = root1.size + root2.size else root1.parent = root2; root2.size = root2.size + root1.size

How many steps does it take to find the set to which a value belongs?

log(size of tree)

Kruskal's algorithm, with this Union-Find structure, is O(mlogm + mlogn)

Awesome for spare graphs

Turning trees around can be really powerful.