-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4d58d4a
commit cb25079
Showing
3 changed files
with
212 additions
and
215 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,9 @@ | ||
using System; | ||
|
||
namespace UnionFind | ||
namespace UnionFind | ||
{ | ||
class Application | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
Console.WriteLine("Hello World!"); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,109 +1,108 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
|
||
namespace UnionFind | ||
{ | ||
internal class Node<T> | ||
where T : System.IComparable<T> | ||
{ | ||
public T Data { get; set; } | ||
public Node<T> Parent { get; set; } | ||
public int Rank { get; set; } | ||
|
||
public Node(T data) | ||
{ | ||
Data = data; | ||
Parent = this; | ||
Rank = 0; | ||
} | ||
} | ||
|
||
public class DisjointSet<T> : IEnumerable<T> | ||
where T: System.IComparable<T> | ||
class Node<T> | ||
where T : System.IComparable<T> | ||
{ | ||
private Dictionary<T, Node<T>> nodes; | ||
public T Data { get; set; } | ||
public Node<T> Parent { get; set; } | ||
public int Rank { get; set; } | ||
|
||
public int Count { get { return nodes.Count; } } | ||
public Node(T data) | ||
{ | ||
Data = data; | ||
Parent = this; | ||
Rank = 0; | ||
} | ||
} | ||
|
||
public class DisjointSet<T> : IEnumerable<T> | ||
where T : System.IComparable<T> | ||
{ | ||
Dictionary<T, Node<T>> nodes; | ||
|
||
public int Count { get { return nodes.Count; } } | ||
|
||
public DisjointSet() | ||
{ | ||
nodes = new Dictionary<T, Node<T>>(); | ||
nodes = new Dictionary<T, Node<T>>(); | ||
} | ||
|
||
public IEnumerator<T> GetEnumerator() | ||
public IEnumerator<T> GetEnumerator() | ||
{ | ||
return nodes.Keys.GetEnumerator(); | ||
return nodes.Keys.GetEnumerator(); | ||
} | ||
|
||
IEnumerator IEnumerable.GetEnumerator() | ||
{ | ||
return nodes.Keys.GetEnumerator(); | ||
return nodes.Keys.GetEnumerator(); | ||
} | ||
|
||
public bool ContainsData(T data) | ||
{ | ||
return nodes.ContainsKey(data); | ||
} | ||
|
||
public bool MakeSet(T data) | ||
{ | ||
if (ContainsData(data)) | ||
return false; | ||
nodes.Add(data, new Node<T>(data)); | ||
return true; | ||
} | ||
|
||
public bool Union(T dataA, T dataB) | ||
{ | ||
var nodeA = nodes[dataA]; | ||
var nodeB = nodes[dataB]; | ||
|
||
var parentA = nodeA.Parent; | ||
var parentB = nodeB.Parent; | ||
|
||
if (parentA == parentB) | ||
return false; | ||
|
||
if (parentA.Rank >= parentB.Rank) | ||
{ | ||
if (parentA.Rank == parentB.Rank) | ||
++parentA.Rank; | ||
|
||
parentB.Parent = parentA; | ||
} | ||
else | ||
{ | ||
parentA.Parent = parentB; | ||
} | ||
|
||
return true; | ||
} | ||
{ | ||
return nodes.ContainsKey(data); | ||
} | ||
|
||
public bool MakeSet(T data) | ||
{ | ||
if (ContainsData(data)) | ||
return false; | ||
|
||
nodes.Add(data, new Node<T>(data)); | ||
return true; | ||
} | ||
|
||
public bool Union(T dataA, T dataB) | ||
{ | ||
var nodeA = nodes[dataA]; | ||
var nodeB = nodes[dataB]; | ||
|
||
var parentA = nodeA.Parent; | ||
var parentB = nodeB.Parent; | ||
|
||
if (parentA == parentB) | ||
return false; | ||
|
||
if (parentA.Rank >= parentB.Rank) | ||
{ | ||
if (parentA.Rank == parentB.Rank) | ||
++parentA.Rank; | ||
|
||
parentB.Parent = parentA; | ||
} | ||
else | ||
{ | ||
parentA.Parent = parentB; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public T FindSet(T data) | ||
{ | ||
return FindSet(nodes[data]).Data; | ||
} | ||
{ | ||
return FindSet(nodes[data]).Data; | ||
} | ||
|
||
public bool IsEmpty() | ||
{ | ||
return Count == 0; | ||
} | ||
{ | ||
return Count == 0; | ||
} | ||
|
||
public void Clear() | ||
{ | ||
nodes.Clear(); | ||
} | ||
{ | ||
nodes.Clear(); | ||
} | ||
|
||
Node<T> FindSet(Node<T> node) | ||
{ | ||
var parent = node.Parent; | ||
if (parent == node) | ||
return node; | ||
|
||
node.Parent = FindSet(node.Parent); | ||
return node.Parent; | ||
} | ||
} | ||
{ | ||
var parent = node.Parent; | ||
if (parent == node) | ||
return node; | ||
|
||
node.Parent = FindSet(node.Parent); | ||
return node.Parent; | ||
} | ||
} | ||
} |
Oops, something went wrong.