Skip to content

Commit 0f1c554

Browse files
authored
Merge pull request #220 from Jatin86400/patch-2
updated the readme.md file
2 parents 043135f + 8116cd9 commit 0f1c554

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

Competitive Coding/Graphs/Detecting cycles/detecting-cycles_readme.md

+13-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
Representation
1+
# Disjoint-set
22

33
A disjoint-set forest consists of a number of elements each of which stores an id, a parent pointer, and, in efficient algorithms, a value called the "rank".
44
The parent pointers of elements are arranged to form one or more trees, each representing a set. If an element's parent pointer points to no other element, then the element is the root of a tree and is the representative member of its set. A set may consist of only a single element. However, if the element has a parent, the element is part of whatever set is identified by following the chain of parents upwards until a representative element (one without a parent) is reached at the root of the tree.
55
Forests can be represented compactly in memory as arrays in which parents are indicated by their array index.
66

7-
MAKE SET
8-
MakeSet[edit]
7+
# MAKE SET
8+
99
The MakeSet operation makes a new set by creating a new element with a unique id, a rank of 0, and a parent pointer to itself. The parent pointer to itself indicates that the element is the representative member of its own set.
1010
The MakeSet operation has O(1) time complexity.
1111
Pseudocode:
@@ -16,17 +16,22 @@ function MakeSet(x)
1616
x.parent := x
1717
x.rank := 0
1818

19-
FIND
19+
![alt text](https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRkmVdlJLBBEPsQUbG3igzyitb6PDbqCuVbVtQzc009uyLX65U-)
20+
21+
# FIND
2022
Find(x) follows the chain of parent pointers from x upwards through the tree until an element is reached whose parent is itself. This element is the root of the tree and is the representative member of the set to which x belongs, and may be x itself.
2123
Path compression, is a way of flattening the structure of the tree whenever Find is used on it. Since each element visited on the way to a root is part of the same set, all of these visited elements can be reattached directly to the root. The resulting tree is much flatter, speeding up future operations not only on these elements, but also on those referencing them.
24+
2225
Pseudocode:
2326

2427
function Find(x)
2528
if x.parent != x
2629
x.parent := Find(x.parent)
2730
return x.parent
31+
32+
![alt text](https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRptXJI8DhupTU5GNaF5qKZX94VPYB7GTiXkYMTadEGRuFMS5PKtg)
2833

29-
UNION
34+
# UNION
3035

3136
Union(x,y) uses Find to determine the roots of the trees x and y belong to. If the roots are distinct, the trees are combined by attaching the root of one to the root of the other. If this is done naively, such as by always making x a child of y, the height of the trees can grow as {\displaystyle O(n)} O(n). To prevent this union by rank is used.
3237
Union by rank always attaches the shorter tree to the root of the taller tree. Thus, the resulting tree is no taller than the originals unless they were of equal height, in which case the resulting tree is taller by one node.
@@ -49,4 +54,6 @@ function Union(x, y)
4954
else
5055
//Arbitrarily make one root the new parent
5156
yRoot.parent := xRoot
52-
xRoot.rank := xRoot.rank + 1
57+
xRoot.rank := xRoot.rank + 1
58+
59+
![alt text](https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSZkZmlExRXCYS-k3TO9dSM0dH_Ql2WuiB-VTIGj6090KEI0Jmjjw)

0 commit comments

Comments
 (0)