-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUnionFindBase.cs
More file actions
41 lines (35 loc) · 1.12 KB
/
Copy pathUnionFindBase.cs
File metadata and controls
41 lines (35 loc) · 1.12 KB
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
using System;
using System.Linq;
namespace DataStructure.Set
{
public abstract class UnionFindBase : IUnionFind
{
protected readonly int[] Connections;
public int Count { get; protected set; }
/// <summary>
/// Initializes an empty union-find data structure with length items
/// Each item is initially in its own set.
/// O(n)
/// </summary>
/// <param name="length"></param>
protected UnionFindBase(int length)
{
Connections = Enumerable.Range(0, length).ToArray();
Count = length;
}
/// <summary>
/// Check item range value
/// </summary>
/// <param name="p"></param>
protected void Validate(int p)
{
if (p < 0 || p >= Connections.Length)
{
throw new IndexOutOfRangeException("index " + p + " is not between 0 and " + (Connections.Length - 1));
}
}
public abstract bool IsConnected(int p, int q);
public abstract void Connect(int p, int q);
public abstract int Find(int p);
}
}