CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.

40
CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University
  • date post

    22-Dec-2015
  • Category

    Documents

  • view

    218
  • download

    1

Transcript of CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.

Page 1: CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.

CS2420: Lecture 42

Vladimir Kulyukin

Computer Science Department

Utah State University

Page 2: CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.

Outline

• Graph Algorithms (Chapter 9)

Page 3: CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.

Initial Graph

B C

A DF

E

3

1

6

86 2

44

5 5

Page 4: CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.

Kruskal’s Algorithm: Basic Insight

A cycle is created if and only if the new edge connects two vertices already connected by a path, i.e., belonging to the same tree.

Page 5: CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.

Kruskal’s: Basic Ideas

• Initially, all vertices are in separate trees.

• Subsequently, when an edge (u, v) is examined, we look for the tree containing u and the tree containing v.

• If the trees are NOT the same, we unite them into a larger tree containing both u, v and (u, v).

Page 6: CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.

Kruskal’s Algorithm: UnionFind

• Kruskal’s Algorithm can be implemented with the UnionFind data structure.

• The UnionFind data structure was designed to solve the problem of partitioning a given set into a collection of disjoint subsets.

• The set partitioning problem has many applications in data mining, information retrieval, molecular biology, bioinformatics, etc.

Page 7: CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.

Disjoint Subsets of a Set

• Let S be a set of n elements. Partition S into a collection of k disjoint subsets S1, S2, S3, ..., Sk.

Page 8: CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.

Disjoint Subsets of a Set

Set S of n elements

Page 9: CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.

Disjoint Subsets of a Set

Partitioning of S into 5 disjoint subsets

Page 10: CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.

Example: Partition S into 6 Disjoint Subsets

• S = {1, 2, 3, 4, 5, 6}, k = 6– S1 = {1}

– S2 = {2}

– S3 = {3}

– S4 = {4}

– S5 = {5}

– S6 = {6}

Page 11: CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.

Example: Partition S into 5 Disjoint Subsets

• S = {1, 2, 3, 4, 5, 6}, k = 5– S1 = {1, 2}

– S2 = {3}

– S3 = {4}

– S4 = {5}

– S5 = {6}

Page 12: CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.

UnionFind Data Structure: Operations

• The UnionFind data structure has the following operations:– make a set out of a single element (singleton).– find a subset that contains some element x.– compute the union of two disjoint subsets, the

first of which contains x and the second of which contains y.

Page 13: CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.

UnionFind: Operations

• MakeSet(x) - creates a one-element set {x}.

• Find(x) - returns a subset containing x.

• Union(x, y) - constructs the union of the disjoint sets Sx and Sy such that Sx contains x and Sy contains y.

• Sx U Sy replaces both Sx and Sy in the collection of subsets.

Page 14: CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.

MakeSet: Example

• S = {1, 2, 3, 4, 5, 6}.

• MakeSet(1); MakeSet(2); MakeSet(3); MakeSet(4); MakeSet(5); MakeSet(6);

• The above sequence of MakeSet operations gives us:

• {{1}, {2}, {3}, {4}, {5}, {6}}.

Page 15: CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.

Union: Examples

• {{1}, {2}, {3}, {4}, {5}, {6}}

• Union(1, 4) {{1, 4}, {2}, {3}, {5}, {6}}

• Union(5, 2) {{1, 4}, {5, 2}, {3}, {6}}

Page 16: CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.

Union: Examples

• {{1, 4}, {5, 2}, {3}, {6}}

• Union(4, 5) {{1, 4, 5, 2}, {3}, {6}}

• Union(3, 6) {{1, 4, 5, 2}, {3, 6}}

Page 17: CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.

UnionFind: Implementation

• Use one element of each disjoint set as a representative.

• Two implementation alternatives:– QuickFind: Find = O(1); Union = O(N).– QuickUnion: Union = O(1); Find = O(N).

Page 18: CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.

QuickFind

• Two data structures:– An array of representatives; this array maps

each element into its representative.– An array of subsets; this array contains each

subset implemented as a linked list.

Page 19: CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.

{{1}, {2}, {3}, {4}, {5}, {6}}

1

2

3

4

5

6

1

2

3

4

5

6

1

2

3

4

5

6

1

2

3

4

5

6

Representatives Subsets

Page 20: CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.

Union(1, 4) => {{1, 4}, {2}, {3}, {5}, {6}}

1

2

3

1

5

6

1

2

3

4

5

6

1

2

3

4

5

6

1

2

3

4

5

6

Representatives Subsets

Null

Page 21: CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.

Union(5, 2) => {{1, 4}, {5, 2}, {3}, {6}}

1

5

3

1

5

6

1

2

3

4

5

6

1

2

3

4

5

6

1

2

3

4

5

6

Representatives Subsets

Null

Null

Page 22: CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.

Union(4, 5) => {{1, 4, 5, 2}, {3}, {6}}

1

1

3

1

1

6

1

2

3

4

5

6

1 2

3

4 5

6

1

2

3

4

5

6

Representatives Subsets

Null

Null

Null

Page 23: CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.

union(3, 6) => {{1, 4, 5, 2}, {3, 6}}

1

1

3

1

1

3

1

2

3

4

5

6

1 2

3

4 5

6

1

2

3

4

5

6

Representatives Subsets

Null

Null

Null

Null

Page 24: CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.

QuickFind: Asymptotic Analysis

• MakeSet(x) = O(1)

• Why?

• All we need to do is to create a list and add x to it.

Page 25: CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.

QuickFind: Asymptotic Analysis

• Find(x) = O(1)

• Why?

• All we need to do is look up x’s representative and then return the list to which the representative points.

Page 26: CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.

QuickFind: Asymptotic Analysis

• Union(x, y) = O(n)

• Here is what we need to do to compute Union(x, y):– Do Find(x) and Find(y); // O(1)– Append Find(y) to the end of Find(x); // O(1) – Set the y-subset in the subsets array to NULL;

// O(1)– Update the representatives for each element

in Find(y); // O(N).

Page 27: CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.

QuickFind: Asymptotic Analysis

• Consider the following sequence of unions:– union(2, 1), union(3, 2), union(4, 3), ...,

union(n, n-1).

• The first union requires 1 step, the second - 2 steps, the third - 3 steps, ..., the n-th - n-1 steps. The sequence of n unions is asymptotically quadratic:

• 1 + 2 + 3 + ... + (n - 1) = O(n2).

Page 28: CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.

QuickFind: Optimizations

• When performing the union operation, always append the shorter list to the longer one (union by size).

• The worst case run time of any legitimate sequence of unions is O(nlogn).

• The worst case run time of a sequence of n-1 unions and m finds is O(nlogn + m).

Page 29: CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.

QuickUnion

• Two data structures:– An array of nodes, each containing exactly

one element.– An array of trees, each containing one

subset.– The root of each tree is the representative for

the subset represented by that tree.– Each node has a pointer to its parent so that

we can get to the root.

Page 30: CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.

{{1}, {2}, {3}, {4}, {5}, {6}}

1

2

3

4

5

6

1

2

3

4

5

6

Nodes Trees

1

2

3

4

5

6

1

2

3

4

5

6

Page 31: CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.

Union(1, 4) => {{1, 4}, {2}, {3}, {5}, {6}}

1

2

3

4

5

6

1

2

3

4

5

6

Nodes Trees

1

2

3

4

5

6

1

2

3

4

5

6

Null

Page 32: CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.

Union(5, 2) => {{1, 4}, {5, 2}, {3}, {6}}

1

2

3

4

5

6

1

2

3

4

5

6

Nodes Trees

1

2

3

4

5

6

1

2

3

4

5

6

Null

Null

Page 33: CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.

Union(4, 5) => {{1, 4, 5, 2}, {3}, {6}}

1

2

3

4

5

6

1

2

3

4

5

6

Nodes Trees

1

2

3

4

5

6

1

2

3

4 5

6

Null

Null

Null

Page 34: CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.

Union(3, 6) => {{1, 4, 5, 2}, {3, 6}}

1

2

3

4

5

6

1

2

3

4

5

6

Nodes Trees

1

2

3

4

5

6

1

2

3

4 5

6

Null

Null

Null

Null

Page 35: CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.

Union(5, 6) => {1, 4, 5, 2, 3, 6}

1

2

3

4

5

6

1

2

3

4

5

6

Nodes Trees

1

2

3

4

5

6

1

2

34 5

6

Null

Null

Null

Null

Null

Page 36: CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.

Quick Union: Asymptotic Analysis

• MakeSet(x) = O(1)

• Why?

• All we need to do is to create a tree node and add x to it.

Page 37: CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.

QuickUnion: Asymptotic Analysis

• Find(x) = O(n)

• Why?

• We need to look up x’s node and then chase the upward pointers to the root of the tree that contains x.

Page 38: CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.

QuickUnion: Asymptotic Analysis

• Union(x, y) = O(1)

• Why?– Do Find(x) and Find(y); // O(1)– Attach the root of Find(y) to the root of

Find(x); // O(1)– Set the pointer in the tree array that used to

point to Find(y) to NULL; // O(1)

Page 39: CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.

QuickUnion: Optimization

• We can optimize the union operation by always attaching the root of a larger tree to the root of the smaller tree.

• Two alternatives:– Union by size - the size of the tree is the

number of nodes in the tree.– Union by rank - the rank of the tree is the

tree’s height.

Page 40: CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.

QuickUnion: Optimization

• If union by size or union by rank is used, the height of the tree is logn.

• The time efficiency of a sequence of n-1 unions and m finds is O(n + mlogn).