Skip to content

Commit

Permalink
Format source files.
Browse files Browse the repository at this point in the history
  • Loading branch information
mondrasovic committed Sep 23, 2018
1 parent 4d58d4a commit cb25079
Show file tree
Hide file tree
Showing 3 changed files with 212 additions and 215 deletions.
7 changes: 2 additions & 5 deletions Application.cs
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!");
}
}
}
}
161 changes: 80 additions & 81 deletions DisjointSet.cs
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;
}
}
}
Loading

0 comments on commit cb25079

Please sign in to comment.