0% found this document useful (0 votes)
19 views27 pages

Sets & Disjoint Set Union

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views27 pages

Sets & Disjoint Set Union

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Sets & Disjoint Set Union

Bheekya D
Definition and properties of sets and disjoint sets
● A set is a collection of distinct elements.
○ Examples: when n = 10, the elements can be partitioned into three
sets as following;
■ S1 = {1,7,8,9}
■ S2 = {2,5,10} and
■ S3 = {3,4,6}
● Sets Si and Sj are said to be Disjoint Sets iff Si ∩ Sj = Ø.
○ Examples:
■ S1∩ S2 = Ø
■ S1∩ S3 = Ø
■ S2∩ S3 = Ø
■ S1∩ S2∩ S3 = Ø
Definition and properties of sets and disjoint sets
● Sets and Disjoint-sets are special type of data structures, are used in
implementing some algorithms(Kruskal's algorithm).
● In Practice, sets are represented by numbers and they might denotes
the indices of the array.
● Sets and disjoint sets are are represented by forest which is called
collection of trees.
Definition and properties of sets and disjoint sets

One possible representation of S1 = {1,7,8,9}, S2 = {2,5,10} and S3 =


{3,4,6}.

● In this representation, each set is represented as a tree.


● In each tree, nodes are linked from the children to the parent, rather
than our usual method of linking from the parent to the children.
Operations on sets and disjoint sets
● Disjoint set union: If Si and Sj are two disjoint sets,then their union
Si U Sj = all elements x such that x is in Si or Sj.Thus, Si U S2= {1,7,
8, 9,2,5,10}. Since we have assumed that all sets are disjoint, we
can assume that following the union of Si and Sj, the sets Si and Sj
do not exist independently; that is, they are replaced by Si U Sj in
the collection of sets.
● Find(i): Given the element i, find the set containing i. Thus, 4 is in
set S3,and 9 is in set S1.
Union Operations
Suppose that we wish to obtain the union of, S1 and S2. one of the tree is
a subtree of the other. To obtain the union of two sets, all that has to be
done is to set the parent field of one of the roots to the other root.
Simple FIND and UNION Algorithms
Union operation

Initially, we have n elements are in forest or having n independent sets

Their array representation is


i [1] [2] [3] [4] .... [n]
p -1 -1 -1 -1 .... -1

Let us process the following sequence of union operations using a simple union algorithm
3 n
2

2 N-1
1

1
Finally, we will get a degenerated tree
Suppose, if we want to perform the following sequence Find operations

Find(1), Find(2),. . . . . .., Find(n).


The simple Union and Find operations time complexities are O(n) and O(n2) respectively.

Although these two algorithms are very easy to state, but their performance characteristics
are not very good, we can improve the performance of our union and find algorithms by
avoiding the creation of degenerated trees. To do this, we make use of a weighting rule for
Union(i, j).
Definition: Weighted rule Union(i, j):- If the number of nodes in the tree
with root i is less than the number in the tree with root j, then make j the
parent of of i, otherwise make i is the parent of j.

Note: to implement weighted rule, we need to know how many nodes there are in every tree, maintain a
count field in the root of every tree, then count[i] equals the number of nodes in that tree.
Here, avoided the creation of degenerated tree and time required to process
all the n finds is only O(n).
Still we can the improve the performance of FIND operation, using collapsing rule.

Definition: Collapsing rule:- If j is a node on the path from i to its


root and p[i] ≠ root[i], then set p[j] to root[i].
Theorem: Assume that we start with a forest of trees, each having one node.
Let T be a tree with n nodes created as a result of algorithm WeightedUNION.
The height of T is no greater than log2 n + 1.

According to this theorem the maximum time to process a find is at most O(log n) if there
are n elements in a tree.
Note: Tree generate
using the weighted
union algorithms.
Suppose if we want Find(8) 8-times(sequence of finds), we will see how the collapsing
find algorithm will give the better result than simple FIND.
If Simple FIND is used, find(8) 8-times requires 24 moves.

If we used collapsing find it will required 13 moves only.(3 moves for finding root, 3
moves for collapsing and 7moves requires for remaining 7 finds).

Collapsing Find algorithm doubles the time for individual find. However, it reduces the
worst-case time over a sequence of finds.
AND/OR Graphs,

An AND/OR graph is a graph which represents a problem-solving process.


The solution involves decomposing the problem into smaller problems.
We then solve these smaller problems.
AND/OR graph corresponding to the laundry problem
Game trees

Complete game tree for Nim Game with n=6


It is estimated that the game tree for chess has >10100 nodes. Even using a computer which is
capable of generating 1011 nodes a second, the complete generation of the game tree for chess
would require more than 1080 years.
Alpha-beta pruning(cutoffs) or min-max process

The alpha value of a max position is defined to be the minimum possible value for that
position. If the value of a min position is determined to be less than or equal to the alpha
value of its parent, then we may stop generation of the remaining children of this min position.

The beta value of a min position is the maximum possible value for that position. If the value of
a max position is determined to be greater than or equal to the beta value of its parent node,
then we may stop generation of the remaining children of this max position.
Postorder evaluation of a game tree using alpha-beta pruning

Portion of Game Tree for a Hypothetical Game.


The value of terminal nodes is obtained from the evaluation function(E(x)) for player A.
Rough notes

You might also like