0% found this document useful (0 votes)
23 views23 pages

Union-Find Structures

Uploaded by

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

Union-Find Structures

Uploaded by

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

Union-Find Structures

Union-Find and Its Applications


Social Networking
• Social networking - relationships between various people can
influence behavior
• Given a set, S, of people, define a social network for S by creating an
element, x, for each person and then create a set, E, of edges or ties
between pairs of people that have a certain kind of relationship.
• For example, in a friendship network, ties would be defined by pairs
of friends
• connected through friendship can influence behavior, how people
vote or shop or whether more likely to gain weight or lose weight
Connected Components
• Social networking - > identifying connected components in a
friendship network
• A connected component is a subset, T, of people from S that satisfies
the following properties:
• Every person in T is related through friendship, that is, for any x and y
in T, either x and y are friends or there is a chain of friendship, such as
through a friend of a friend of a friend, that connects x and y.
• No one in T is friends with anyone outside of T.
Union-Find structure
• An efficient data structure that can be used to identify the connected
components in a social network
• A union-find structure gives a way of maintaining a collection of
disjoint sets of people,
• Support three operations:
• Make a set, which initially contains just a single person, x, and has that
person’s name, “X,” as the name of the set
• Union two sets, A and B, together, naming the result as being one of “A” or
“B,” so that everyone that was in A or B is now identified as belonging to this
union
• Find the name of the set containing a particular person, x.
Union-Find Structure
• Partition or union-find structure is a data structure supporting a
collection of disjoint sets
• Methods: assume to have a constant time to access a node
• For instance, items/ nodes or some kind of lookup table or map for
finding the node associated with an item
• Methods include the following:
• makeSet(e): Create a singleton set containing the element e and name this set
“e”.
• union(A,B): Update A and B to create A ∪ B, naming the result as “A” or “B”.
• find(e): Return the name of the set containing the element e.
Connected Components
• Suppose in given social network, N,
defined by a set, S, of people, and a set, E,
of edges defining relationships between
pairs of people, in no particular order, and
asked to find all the connected components
for N.
• Output would identify Lafayette,
Rochambeau, George Washington, John
Adams, and Abigail Adams as belonging to
the “Washington” connected component,
• Charles Cornwallis and Benedict Arnold as
belonging to the “Cornwallis” connected
component.
• Algorithm UFConnectedComponents(S,E):
• Input: A set, S, of n people and a set, E, of m pairs of people from S defining
pairwise relationships
• Output: An identification, for each x in S, of the connected component
containing x
for each x in S do
makeSet(x)
for each (x, y) in E do
if find(x) = find(y) then
union(find(x), find(y))
for each x in S do
Output “Person x belongs to connected component” find(x)
Algorithm’s Efficiency
• If performing a sequence of m union and find operations, starting
with n singleton sets created with the makeSet method, takes
O(t(n,m)) time,
• then the running time of the UFConnectedComponents algorithm is
O(t(n, n + m)), since two find operations for each edge in E and then
one find operation for each of the n members of S.
• Running time that is either O((n+m) log n), using a list-based
implementation, or
• “almost” O(n+m), using a tree-based implementation
Maze Construction
• Goal: To find a path from a start location to a finish location that traverses
connected cells in the maze without crossing any walls
• An automated way to construct mazes that have exactly one solution, finding
that solution is nontrivial
Algorithm for Maze Generator
• Algorithm MazeGenerator(G,E)
• Input: A grid, G, consisting of n cells and a set, E, of m “walls,”
each of which divides two cells, x and y, such that the walls in E
initially separate and isolate all the cells in G
• Output: A subset, R of E, such that removing the edges in R from
E creates a maze defined on G by the remaining walls
while R has fewer than n − 1 edges do
Choose an edge, (x, y), in E uniformly at random from among
those previously unchosen
if find(x) != find(y) then
union(find(x), find(y))
Add the edge (x, y) to R
• use a collection of linked
lists
• one for each set, where
the list for a set A
contains a head node,
which stores
• size of A
• name of A
• a pointer to the first and
last nodes of a linked list
containing pointers to all
the elements of A.
A Tree-Based Implementation

• A tree-based implementation of a union-find structure for three disjoint sets:


• A = {1, 4, 7}, B = {2, 3, 6, 9}, and C = {5, 8, 10, 11, 12}.
• Tree-based implementation of a partition: (a) operation union(A,B);
• (b) operation find(), where denotes the node for element 12.
• Union-by-Size: Store with each node v the size of the subtree rooted
at v, denoted by n(v).
• In a union, make the tree of the smaller set a subtree of the other
tree, and update the size field of the root of the resulting tree
• Path Compression: In a find operation, for each node v that the find
visits, reset the parent pointer from v to point to the root.
• Path-compression heuristic:
• (a) path traversed by operation find on element 12;
• (b) restructured tree.

You might also like