week1 Union-Find

Overview

We illustrate our basic approach to developing and analyzing algorithms by considering the dynamic connectivity problem.

We will learn the union-find data type and several implementations(quick find, quick union, weighted quick union, weighted quick union with path compression).Finally, we apply the union-find data type to the percolation problem from physical chemistry.

So what is dynamic connectivity problem?

Steps to developing a usable algorithms(scientific approach)

1.Model the problem, try to understand, basically, what are the main elements of the problem that need to be solved.

2.Find an algorithm to solve it.

3.Fast enough?Fits in memory?

4.If not, figure out why.

5.Find a way to address the problem

6.Iterate until satisfied.

dynamic connectivity

Given a set of N objects.
Union command:connect two objects.
Find/connected query:is there a path connecting two object?


week1 Union-Find_第1张图片
image.png

week1 Union-Find_第2张图片
image.png

week1 Union-Find_第3张图片
image.png

In part two of the course, we'll consider algorithms that explicitly find paths(more work to do).

Modeling the objects

Applications involve manipulating objects of all types.

  1. Pixels in a digital video.
  2. Computers in a network.
  3. Friends in a social network.
  4. Transistors in a computer chip.
  5. Elements in a mathematical set.
  6. Variable names in Fortran Programs.
  7. Metallic sites in a computer system.

When programming, convenient to name objects 0 to N-1

  1. Use integers as array index.
  2. Suppress details not relevant to union-find.
    image.png

Modeling the connections

We assume "is connected to" is an equivalence relation:
Reflexive:p is connected to p.
Symmetirc:if p is connected to q, then q is connected to p.
Transitive:if p is connected to q and q is connected to r, then p is connected to r.

Connected components. Maximal set of objects that are mutually connected

week1 Union-Find_第4张图片
image.png

Implementing the operations

Find query.Check if two objects are in the same component.
Union command.Replace components containing two objects with their union.

week1 Union-Find_第5张图片
image.png

你可能感兴趣的:(week1 Union-Find)