Minimum Spanning Tree

Posted flagyuri

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Minimum Spanning Tree相关的知识,希望对你有一定的参考价值。

Description

Given a list of Connections, which is the Connection class (the city name at both ends of the edge and a cost between them), find edges that can connect all the cities and spend the least amount.
Return the connects if can connect all the cities, otherwise return empty list.

Return the connections sorted by the cost, or sorted city1 name if their cost is same, or sorted city2 if their city1 name is also same.

 

Example

Example 1:

Input:
["Acity","Bcity",1]
["Acity","Ccity",2]
["Bcity","Ccity",3]
Output:
["Acity","Bcity",1]
["Acity","Ccity",2]

Example 2:

Input:
["Acity","Bcity",2]
["Bcity","Dcity",5]
["Acity","Dcity",4]
["Ccity","Ecity",1]
Output:
[]

Explanation:
No way
给出一些Connections,即Connections类,找到一些能够将所有城市都连接起来并且花费最小的边。
如果说可以将所有城市都连接起来,则返回这个连接方法;不然的话返回一个空列表。
思路:考察并查集。如果满足条件的连接边数不等于n - 1(n为节点数)则表示没有可行方法。使用HashMap(将每个节点对应一个id), 对id使用并查集。用map存储自动排序,再利用并查集,连通的点根应该相同。
/**
 * 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 int[] father;
    private int find(int a) {
        int root = a;
        while (root != father[root]) {
            root = father[root];
        }
        
        while(a != root) {
            int tmp = father[a];
            father[a] = root;
            a = tmp;
        }
        return root;
    }
    
    static Comparator<Connection> comp = 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.city2.compareTo(b.city2);
                }
                return a.city1.compareTo(b.city1);
            }
        };
        
    public List<Connection> lowestCost(List<Connection> connections) {
        Collections.sort(connections, comp);
        Map<String, Integer> hash = new HashMap<>();
        ArrayList<Connection> result = new ArrayList<>();
        int id = 0;
        for (Connection conn : connections) {
            if (!hash.containsKey(conn.city1)) {
                hash.put(conn.city1, ++id);
            }
            
            if (!hash.containsKey(conn.city2)) {
                hash.put(conn.city2, ++id);
            }
        }
        
        father = new int[id + 1];
        for (int i = 1; i < id + 1; i++) {
            father[i] = i;
        }
        
        List<Connection> results = new ArrayList<>();
        for (Connection conn: connections) {
            int id1 = hash.get(conn.city1);
            int id2 = hash.get(conn.city2);
            
            int root1 = find(id1);
            int root2 = find(id2);
            if (root1 != root2) {
                father[root1] = father[root2];
                result.add(conn);
            }
        }
        
        if (result.size() != id - 1) {
            return new ArrayList<>();
        }
        return result;
        
    }
}

  



 

 

以上是关于Minimum Spanning Tree的主要内容,如果未能解决你的问题,请参考以下文章

Minimum Spanning Tree

[思维]Minimum Spanning Tree

[LeetCode] Minimum Spanning Tree

Minimum spanning tree for each edge

百度之星2018资格赛t6三原色图(MST minimum spanning tree)

cf 609E.Minimum spanning tree for each edge