Lec22_23_DisjointSets_Kruskal

Lec22_23_DisjointSets_Kruskal - Disjoint Data Sets This...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
Disjoint Data Sets
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
This class The methods of disjoint set data structure An application Implementations
Background image of page 2
Data Structures for Disjoint Sets A disjoint-set data structure is a collection of sets S = { S 1 S k }, such that S i S j = for i j , The methods are: find ( x ) : returns a reference to S i S such that x S i merge (x ,y ) : results in S S - { S i , S j } { S i S j } where x S i and y S j A merge consists of 2 finds, and a union of two sets S = { { a }, { b }, { c }, { d }, { e } } Union ( {a}, {d}), and update collection { a } find ( a ) { d } find ( d )
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Application of disjoint-set data structure Problem: Find the connected components of a graph. 1. Make a set of each vertex. 2. For each edge do: if the two end points are not in the same set, merge the two sets. At end each set contains the vertices of a connected component. We can now answer the question: are vertices x and y in the same component?
Background image of page 4
Example: Find Connected Vertices 1 5 4 2 3 G = Set of sets of vertices V = { {1}, {2}, {3}, {4}, {5} } E = { (1,2), (1,5), (2,5), (3,4)} merge(1,2) V = { {1, 2}, {3}, {4}, {5} } merge (2,5) V = { {1, 2, 5}, {3}, {4} } merge(3,4) V = { {1, 2, 5}, {3,4} } merge (1,5) V = { {1, 2, 5}, {3}, {4} } 1. Make a set of each vertex 2. For each edge in E do:
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Disjoint Set Implementation in an array We can use an array, or a linked list to implement the collection. In this lecture we examine only an array implementation. The size of the array is N for a total of N elements One element is the representative of the set. In the array Set , each element i for i = 1,…,N has the value rep of the representative of its set. ( Set [i] = rep ) We use the smallest “value” of the elements in a set as the representative.
Background image of page 6
Using an Array to implement DS 1 2 3 4 5 6 7 8 Set = { {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8} } 1 2 3 4 5 6 4 8 merge ( "4", "7") Set = { {1}, {2}, {3}, {4,7}, {5}, {6}, {8} } 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
DS implemented as an array find 1( x ) return Set[x]; // θ (1). union
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 30

Lec22_23_DisjointSets_Kruskal - Disjoint Data Sets This...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online