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?


image.png

image.png

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

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.

image.png

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容