-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWeightedQuickUnionPathCompressionUF.cs
41 lines (36 loc) · 1.25 KB
/
WeightedQuickUnionPathCompressionUF.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
namespace SedgewickWayne.Algorithms.DynamicConnectivity
{
/// <summary>
/// WeightedQuickUnionPathCompressionUF.java.
/// The amortized cost per operation for this algorithm is known to be bounded
/// by a function known as the inverse Ackermann function and is less than 5
/// for any conceivable value of N that arises in practice.
/// Quick-union with path compression (but no weighting by size or rank)
/// has the same <see cref="Find(int)"/> implementation.
/// </summary>
/// <see href="https://algs4.cs.princeton.edu/15uf/WeightedQuickUnionPathCompressionUF.java.html"/>
/// <see href="https://algs4.cs.princeton.edu/15uf/QuickUnionPathCompressionUF.java.html"/>
public sealed class WeightedQuickUnionPathCompressionUF : WeightedQuickUnionUF
{
public WeightedQuickUnionPathCompressionUF(int N) : base(N)
{
}
/// <summary>
/// FIND with path compression (the while is the only difference)
/// </summary>
/// <param name="p">integer representing one site</param>
public override int Find(int p)
{
int root = p;
while (root != id[root]) root = id[root];
while (p != root)
{
int newp = id[p];
id[p] = root;
p = newp;
}
return root;
}
}
}