Skip to content
This repository has been archived by the owner on May 29, 2024. It is now read-only.

Add UnionFind in C# #1331

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions algorithms/CSharp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,7 @@ To run the `.cs` file, kindly use [.Net Finddle](https://dotnetfiddle.net/)
- [Kruskals Algorithm to Find Minimum Spanning Tree](src/Graph/kruskals-algorithm.cs)
- [Dijkstras Algorithm to Find Shortest Path](src/Graph/dijkstra.cs)
- [Floyd Warshalls Algorithm to Find All Pair Shortest Path](src/Graph/floyd-warshall-algorithm.cs)


## UnionFind
- [Union Find](src/Union-Find/union-find.cs)
66 changes: 66 additions & 0 deletions algorithms/CSharp/src/Union-Find/union-find.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Algorithms.UnionFind
{
public class UnionFind
{
public static int MAXN = 1000001;
private static int[] father = new int[MAXN];
private static int[] size = new int[MAXN];
// the stack is used to store all the node along the backtracking path
private static int[] stack = new int[MAXN];

public static int n;

public static void Build()
{
for (int i = 0; i <= n; i++)
{
father[i] = i;
size[i] = 1;
}
}

public static int Find(int x)
{
int top = 0;
while (father[x] != x)
{
stack[top++] = x;
x = father[x];
}
for (int i = 0; i < top; i++)
{
father[stack[i]] = x;
}
return x;
}

public static bool IsSameSet(int u, int v)
{
return Find(u) == Find(v);
}

public static void Union(int u, int v)
{
int x = Find(u);
int y = Find(v);
if (x != y)
{
if (size[x] >= size[y])
{
father[y] = x;
size[x] += size[y];
}
else
{
father[x] = y;
size[y] += size[x];
}
}
}
}
}
43 changes: 43 additions & 0 deletions algorithms/CSharp/test/Union-Find/union-find.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using NUnit.Framework;

namespace Algorithms.Tests.UnionFind
{
[TestFixture]
public class UnionFind
{
[SetUp]
public void Setup()
{
Algorithms.UnionFind.UnionFind.n = 5;
Algorithms.UnionFind.UnionFind.Build();
}

[Test]
public void Find_ShouldGetExpectedResult()
{
Assert.That(Algorithms.UnionFind.UnionFind.Find(1), Is.EqualTo(1));
}

[Test]
public void IsSameSet_ShouldGetExpectedResult()
{
Assert.IsTrue(Algorithms.UnionFind.UnionFind.IsSameSet(1, 1));
Assert.IsFalse(Algorithms.UnionFind.UnionFind.IsSameSet(1, 2));
}

[Test]
public void Union_ShouldGetExpectedResult()
{
Algorithms.UnionFind.UnionFind.Union(1, 2);
Assert.IsTrue(Algorithms.UnionFind.UnionFind.IsSameSet(1, 2));
}

[Test]
public void UnionAndFind_ShouldGetExpectedResult()
{
Algorithms.UnionFind.UnionFind.Union(1, 2);
Algorithms.UnionFind.UnionFind.Union(2, 3);
Assert.That(Algorithms.UnionFind.UnionFind.Find(3), Is.EqualTo(Algorithms.UnionFind.UnionFind.Find(1)));
}
}
}