Minimum Spanning Tree
Example
Note
Code
/**
* Definition for a Connection.
* public class Connection {
* public String city1, city2;
* public int cost;
* public Connection(String city1, String city2, int cost) {
* this.city1 = city1;
* this.city2 = city2;
* this.cost = cost;
* }
* }
*/
public class Solution {
/**
* @param connections given a list of connections include two cities and cost
* @return a list of connections from results
*/
private String find(String str, Map<String, String> father) {
if (!father.containsKey(str)) {
father.put(str, str);
} else if (!father.get(str).equals(str)) {
father.put(str, find(father.get(str), father));
}
return father.get(str);
}
public List<Connection> lowestCost(List<Connection> connections) {
// Write your code here
List<Connection> res = new ArrayList<Connection>();
Collections.sort(connections, new Comparator<Connection>() {
public int compare(Connection a, Connection b) {
if (a.cost != b.cost) {
return a.cost - b.cost;
}
if (!a.city1.equals(b.city1)) {
return a.city1.compareTo(b.city1);
}
return a.city2.compareTo(b.city2);
}
});
Map<String, String> father = new HashMap<>();
for (Connection con : connections) {
String root1 = find(con.city1, father);
String root2 = find(con.city2, father);
/*
Kruskal's algorithm:
将边排序后选择边,在选择边的过程中使用并查集维护保证无环。
*/
if (!root1.equals(root2)) {
father.put(root1, root2);
res.add(con);
}
}
if (res.size() != father.size() - 1) {
return new ArrayList<Connection>();
}
return res;
}
}Last updated