import java.util.*;
class Connection{
String node1;
String node2;
int cost;
public Connection(String a, String b, int c){
node1 = a;
node2 = b;
cost = c;
}
}
public class Solution {
static class DisjointSet
{
Set<String> set;
Map<String, String> map;
int count;
public DisjointSet()
{
count = 0;
set = new HashSet<>();
map = new HashMap<>();
}
public void MakeSet(String s)
{
if(!set.contains(s))
{
count++;
set.add(s);
map.put(s, s);
}
}
public String Find(String s)
{
if(!set.contains(s)) return null;
if(s.equals(map.get(s))) return s;
String root = this.Find(map.get(s));
map.put(s, root);
return root;
}
public void Union(String s, String t)
{
if(!set.contains(s) || !set.contains(t)) return;
if(s.equals(t)) return;
count--;
map.put(s, t);
}
}
static class ConnectionComparator1 implements Comparator<Connection>
{
@Override
public int compare(Connection a, Connection b)
{
return a.cost - b.cost;
}
}
static class ConnectionComparator2 implements Comparator<Connection>
{
@Override
public int compare(Connection a, Connection b)
{
if(a.node1.equals(b.node1)) return a.node2.compareTo(b.node2);
else return a.node1.compareTo(b.node1);
}
}
public static List<Connection> getMST(List<Connection> connections)
{
Comparator<Connection> comparator1 = new ConnectionComparator1();
Comparator<Connection> comparator2 = new ConnectionComparator2();
Collections.sort(connections, comparator1);
DisjointSet set = new DisjointSet();
List<Connection> res = new ArrayList<>();
for(Connection itr: connections)
{
set.MakeSet(itr.node1);
set.MakeSet(itr.node2);
}
for(Connection itr: connections)
{
String s = set.Find(itr.node1);
String t = set.Find(itr.node2);
if(!s.equals(t))
{
set.Union(s, t);
res.add(itr);
if(set.count == 1) break;
}
}
if(set.count == 1)
{
Collections.sort(res, comparator2);
return res;
}
else return new ArrayList<Connection>();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<Connection> connections = new ArrayList<>();
// connections.add(new Connection("Acity","Bcity",1));
// connections.add(new Connection("Acity","Ccity",2));
// connections.add(new Connection("Bcity","Ccity",3));
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));
List<Connection> res = getMST(connections);
for (Connection c : res){
System.out.println(c.node1 + " -> " + c.node2 + " cost : " + c.cost);
}
}
}
Minimum Spanning Tree
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- Given a binary treestruct TreeLinkNode {TreeLinkNode *lef...
- 层次遍历法 用层次遍历方法很简单,但是用深度遍历法则需要好好思考思考,递归就是整体考虑,就把run当做了求最小值的方法
- 原题是: 代码是: 要注意的是: 1.python 里没有&&, 而是and; 没有Null,而是None.2.调...
- Easy 给定二叉树,返回最浅深度。 简单的递归问题,与二叉树最大深度类似。注意:如果一棵树只有左或右节点,这个树...
- Aug 3 2017 Update 递归方法1: 树只有一个孩子的情况要去递归执行它的孩子,depth要+1。最后...