Title: CS2420: Lecture 42
1CS2420 Lecture 42
- Vladimir Kulyukin
- Computer Science Department
- Utah State University
2Outline
- Graph Algorithms (Chapter 9)
3Initial Graph
1
B
C
6
3
4
4
5
5
D
A
F
2
6
8
E
4Kruskals 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.
5Kruskals 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).
6Kruskals Algorithm UnionFind
- Kruskals 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.
7Disjoint 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.
8Disjoint Subsets of a Set
Set S of n elements
9Disjoint Subsets of a Set
Partitioning of S into 5 disjoint subsets
10Example 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
11Example 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
12UnionFind 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.
13UnionFind 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.
14MakeSet 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.
15Union 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
16Union 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
17UnionFind 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).
18QuickFind
- 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.
191, 2, 3, 4, 5, 6
1
1
1
1
2
2
2
2
3
3
3
3
4
4
4
4
5
5
5
5
6
6
6
6
Subsets
Representatives
20Union(1, 4) gt 1, 4, 2, 3, 5, 6
1
4
1
1
1
2
2
2
2
3
3
3
3
1
4
4
Null
5
5
5
5
6
6
6
6
Subsets
Representatives
21Union(5, 2) gt 1, 4, 5, 2, 3, 6
1
4
1
1
1
5
2
Null
2
3
3
3
3
1
4
4
Null
2
5
5
5
5
6
6
6
6
Subsets
Representatives
22Union(4, 5) gt 1, 4, 5, 2, 3, 6
1
2
4
5
1
1
1
1
2
2
Null
3
3
3
3
1
4
4
Null
1
5
5
Null
6
6
6
6
Subsets
Representatives
23union(3, 6) gt 1, 4, 5, 2, 3, 6
1
2
4
5
1
1
1
1
2
Null
2
3
6
3
3
3
1
4
4
Null
1
5
5
Null
Null
3
6
6
Subsets
Representatives
24QuickFind Asymptotic Analysis
- MakeSet(x) O(1)
- Why?
- All we need to do is to create a list and add x
to it.
25QuickFind Asymptotic Analysis
- Find(x) O(1)
- Why?
- All we need to do is look up xs representative
and then return the list to which the
representative points.
26QuickFind 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).
27QuickFind 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).
28QuickFind 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).
29QuickUnion
- 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.
301, 2, 3, 4, 5, 6
1
1
1
1
2
2
2
2
3
3
3
3
4
4
4
4
5
5
5
5
6
6
6
6
Trees
Nodes
31Union(1, 4) gt 1, 4, 2, 3, 5, 6
1
4
1
1
1
2
2
2
2
3
3
3
3
4
4
4
Null
5
5
5
5
6
6
6
6
Trees
Nodes
32Union(5, 2) gt 1, 4, 5, 2, 3, 6
1
4
1
1
1
2
2
Null
2
3
3
3
3
4
4
4
Null
2
5
5
5
5
6
6
6
6
Trees
Nodes
33Union(4, 5) gt 1, 4, 5, 2, 3, 6
1
4
5
1
1
1
2
2
2
Null
2
3
3
3
3
4
4
4
Null
5
5
5
Null
6
6
6
6
Trees
Nodes
34Union(3, 6) gt 1, 4, 5, 2, 3, 6
1
4
5
1
1
1
2
2
2
Null
2
3
3
3
6
3
4
4
4
Null
5
5
5
Null
Null
6
6
6
Trees
Nodes
35Union(5, 6) gt 1, 4, 5, 2, 3, 6
1
3
4
5
1
1
1
2
6
2
2
Null
2
3
3
Null
3
4
4
4
Null
5
5
5
Null
Null
6
6
6
Trees
Nodes
36Quick Union Asymptotic Analysis
- MakeSet(x) O(1)
- Why?
- All we need to do is to create a tree node and
add x to it.
37QuickUnion Asymptotic Analysis
- Find(x) O(n)
- Why?
- We need to look up xs node and then chase the
upward pointers to the root of the tree that
contains x.
38QuickUnion 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)
39QuickUnion 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
trees height.
40QuickUnion 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).