Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created July 19, 2017 00:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/3866d576756aa7fb07856e42c0b8c02d to your computer and use it in GitHub Desktop.
Save jianminchen/3866d576756aa7fb07856e42c0b8c02d to your computer and use it in GitHub Desktop.
minimum spanning tree - running code and execution error - will figure out the issue later
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MinimumSpanningTree_StudyCode
{
/// <summary>
/// minimum spanning tree
/// code review on July 18, 2017
/// Review IComparer and also ArrayList
/// Study example code on this webpage:
/// https://msdn.microsoft.com/en-us/library/system.collections.icomparer(v=vs.110).aspx
///
/// </summary>
class Connection
{
public String node1 {get; set;}
public String node2 { get; set; }
public int cost { get; set; }
public Connection(String a, String b, int c)
{
node1 = a;
node2 = b;
cost = c;
}
}
class DisjointSet
{
public HashSet<String> set;
public Dictionary<String, String> map;
public int count;
public DisjointSet()
{
count = 0;
set = new HashSet<string>();
map = new Dictionary<String, String>();
}
public void MakeSet(String s)
{
if (!set.Contains(s))
{
count++;
set.Add(s);
map.Add(s, s);
}
}
public String Find(String s)
{
if (!set.Contains(s))
{
return null;
}
if (s.CompareTo(map[s]) == 0)
{
return s;
}
String root = this.Find(map[s]);
map.Add(s, root);
return root;
}
public void Union(String s, String t)
{
if (!set.Contains(s) || !set.Contains(t))
{
return;
}
if (s.CompareTo(t) == 0)
{
return;
}
count--;
map.Add(s, t);
}
}
class ConnectionComparator1 : IComparer
{
int IComparer.Compare(Object a, Object b)
{
var itemA = (Connection) a;
var itemB = (Connection) b;
return itemA.cost - itemB.cost;
}
}
class ConnectionComparator2 : IComparer
{
int IComparer.Compare(Object a, Object b)
{
var itemA = (Connection)a;
var itemB = (Connection)b;
if (itemA.node1.CompareTo(itemB.node1) == 0)
{
return itemA.node2.CompareTo(itemB.node2);
}
else
{
return itemA.node1.CompareTo(itemB.node1);
}
}
}
class Program
{
public static ArrayList getMST(ArrayList connections)
{
var comparator1 = new ConnectionComparator1();
var comparator2 = new ConnectionComparator2();
connections.Sort(comparator1);
var set = new DisjointSet();
var result = new ArrayList();
foreach(var itr in connections)
{
var connect = (Connection)itr;
set.MakeSet(connect.node1);
set.MakeSet(connect.node2);
}
foreach(var itr in connections)
{
var connect = (Connection)itr;
String s = set.Find(connect.node1);
String t = set.Find(connect.node2);
if(s.CompareTo(t) != 0)
{
set.Union(s, t);
result.Add(itr);
if(set.count == 1) break;
}
}
if (set.count == 1)
{
result.Sort(comparator2);
return result;
}
else
{
return new ArrayList();
}
}
public static void Main(String[] args) {
var connections = new ArrayList();
connections.Add(new Connection("A","B",6));
connections.Add(new Connection("B","C",4));
connections.Add(new Connection("C","D",5));
connections.Add(new Connection("D","E",8));
connections.Add(new Connection("E","F",1));
connections.Add(new Connection("B","F",10));
connections.Add(new Connection("E","C",9));
connections.Add(new Connection("F","C",7));
connections.Add(new Connection("B","E",3));
connections.Add(new Connection("A","F",1));
var result = getMST(connections);
foreach (var item in result){
var connection = (Connection)item;
Console.WriteLine(connection.node1 + " -> " + connection.node2 + " cost : " + connection.cost);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment