Problem solution · Java

Minimum Cost Walk in Weighted Graph

Minimum Cost Walk in Weighted Graph: a Java solution using disjoint set union. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Disjoint set union
Source
walkccc LeetCode Solutions
Length
68 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Disjoint set union

For Minimum Cost Walk in Weighted Graph, the implementation maintains connected components and merges them as relationships are processed.

  1. Give each element a component representative.
  2. Merge representatives when a connection is accepted.
  3. Answer connectivity or component queries from the compressed representatives.

Code notes

  • 68 lines of Java from the credited upstream file 3108.java.
  • The implementation visibly relies on sequence storage.
  • 3 loop blocks detected.

Complexity

Account for every find and union operation; with path compression and ranked merging, the amortized cost is nearly constant per operation.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from walkccc LeetCode Solutions by P.-Y. Chen (walkccc) and is used under the MIT licence.

Full codeMinimum Cost Walk in Weighted Graph · JavaJava
Use this to learn the idea, then write your own version.
class UnionFind {  public UnionFind(int n) {    id = new int[n];    rank = new int[n];    weight = new int[n];    for (int i = 0; i < n; ++i)      id[i] = i;    // 2^17 - 1 is the minimum number in the form 2^x - 1 > 10^5.    Arrays.fill(weight, (1 << 17) - 1);  }   public void unionByRank(int u, int v, int w) {    final int i = find(u);    final int j = find(v);    final int newWeight = weight[i] & weight[j] & w;    weight[i] = newWeight;    weight[j] = newWeight;    if (i == j)      return;    if (rank[i] < rank[j]) {      id[i] = j;    } else if (rank[i] > rank[j]) {      id[j] = i;    } else {      id[i] = j;      ++rank[j];    }  }   public int getMinCost(int u, int v) {    if (u == v)      return 0;    final int i = find(u);    final int j = find(v);    return i == j ? weight[i] : -1;  }   private int[] id;  private int[] rank;  private int[] weight;   private int find(int u) {    return id[u] == u ? u : (id[u] = find(id[u]));  }} class Solution {  public int[] minimumCost(int n, int[][] edges, int[][] query) {    int[] ans = new int[query.length];    UnionFind uf = new UnionFind(n);     for (int[] edge : edges) {      final int u = edge[0];      final int v = edge[1];      final int w = edge[2];      uf.unionByRank(u, v, w);    }     for (int i = 0; i < query.length; ++i) {      final int u = query[i][0];      final int v = query[i][1];      ans[i] = uf.getMinCost(u, v);    }     return ans;  }} 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗