Advertisement
xiutianxiudi

Lintcode 629. Minimum Spanning Tree

Nov 13th, 2019
439
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.69 KB | None | 0 0
  1. /**
  2.  * Definition for a Connection.
  3.  * public class Connection {
  4.  *   public String city1, city2;
  5.  *   public int cost;
  6.  *   public Connection(String city1, String city2, int cost) {
  7.  *       this.city1 = city1;
  8.  *       this.city2 = city2;
  9.  *       this.cost = cost;
  10.  *   }
  11.  * }
  12.  */
  13. public class Solution {
  14.  
  15.     private class DisjointSet<T> {
  16.         private Map<T, T> parents = new HashMap<T, T>();
  17.         private Map<T, Integer> sizes = new HashMap<T, Integer>();
  18.  
  19.         public DisjointSet() {}
  20.  
  21.         public void add(T e) {
  22.             if(parents.containsKey(e)) {
  23.                 return;
  24.             }
  25.  
  26.             parents.put(e, e);
  27.             sizes.put(e, 1);
  28.         }
  29.  
  30.         public T find(T e) {
  31.             if(!e.equals(parents.get(e))) {
  32.                 parents.put(e, find(parents.get(e)));
  33.             }
  34.             return parents.get(e);
  35.         }
  36.  
  37.         public boolean union(T e1, T e2) {
  38.             T r1 = find(e1);
  39.             T r2 = find(e2);
  40.  
  41.             if(r1.equals(r2)) {
  42.                 return false;
  43.             }
  44.  
  45.             int size1 = sizes.get(r1);
  46.             int size2 = sizes.get(r2);
  47.             if(size1 > size2) {
  48.                 parents.put(r2, r1);
  49.                 sizes.put(r1, size1 + size2);
  50.             } else {
  51.                 parents.put(r1, r2);
  52.                 sizes.put(r2, size1 + size2);
  53.             }
  54.             return true;
  55.         }
  56.  
  57.         public int size() {
  58.             return parents.size();
  59.         }
  60.     }
  61.  
  62.     /**
  63.      * @param connections given a list of connections include two cities and cost
  64.      * @return a list of connections from results
  65.      */
  66.     public List<Connection> lowestCost(List<Connection> connections) {
  67.         Collections.sort(connections, (Connection c1, Connection c2) -> {
  68.             if(c1.cost != c2.cost) {
  69.                 return c1.cost - c2.cost;
  70.             } else if(!c1.city1.equals(c2.city1)) {
  71.                 return c1.city1.compareTo(c2.city1);
  72.             } else {
  73.                 return c1.city2.compareTo(c2.city2);
  74.             }
  75.         });
  76.  
  77.         List<Connection> mst = new ArrayList<Connection>();
  78.  
  79.         DisjointSet<String> ds = new DisjointSet<String>();
  80.         for(Connection connection : connections) {
  81.             String city1 = connection.city1;
  82.             String city2 = connection.city2;
  83.  
  84.             ds.add(city1);
  85.             ds.add(city2);
  86.  
  87.             if(ds.union(city1, city2)) {
  88.                 mst.add(connection);
  89.             }
  90.         }
  91.  
  92.         // if mst doesn't form a spanning tree, return an empty list
  93.         if(mst.size() != ds.size() - 1) {
  94.             return new ArrayList(0);
  95.         }
  96.         return mst;
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement