Minimum Cost Walk in Weighted Graph - Complete Solution Guide
Minimum Cost Walk in Weighted Graph is LeetCode problem 3108, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
There is an undirected weighted graph with n vertices labeled from 0 to n - 1 . You are given the integer n and an array edges , where edges[i] = [u i , v i , w i ] indicates that there is an edge between vertices u i and v i with a weight of w i . A walk on a graph is a sequence of vertices and edges. The walk starts and ends with a vertex, and each edge connects the vertex that comes before it and the vertex that comes after it. It's important to note that a walk may visit the same edge or ver
Detailed Explanation
The problem asks us to find the minimum cost of a walk between two given vertices in a weighted undirected graph for multiple queries. The cost of a walk is defined as the bitwise AND of the weights of the edges traversed during the walk. If no walk exists between the given vertices, the cost is -1. The graph has 'n' vertices labeled from 0 to n-1, and we are provided with edges in the form of [u, v, w], where 'u' and 'v' are the vertices connected by the edge, and 'w' is the weight of the edge. We are also given multiple queries in the form of [s, t], where 's' is the starting vertex and 't' is the ending vertex.
Solution Approach
The solution utilizes an iterative approach with Union-Find to determine the minimum cost of a walk between two vertices for each query. It starts by checking if a path exists between the source and target vertices using a DSU. If a path exists, the minimum cost is initialized to 0. The solution then iteratively refines the minimum cost by considering different bitmasks. In each iteration, edges that are 'supermasks' of the current cost are used to build connected components using DSU. The minimum weight within each component is calculated, and the minimum cost for each query is updated. This process continues until no further updates are possible, or, effectively, the minimum possible cost is found.
Step-by-Step Algorithm
- Step 1: Initialize the answer array with -1 for all queries.
- Step 2: Use a DSU (Union-Find) to check if a path exists between the source and target vertices for each query. If a path exists, initialize the corresponding answer to 0, otherwise keep it as -1.
- Step 3: Create a list of 'active queries' for the queries which has a path (initially identified as '0' min cost).
- Step 4: Iteratively improve the answer for the active queries. This involves grouping active queries by their current minimum costs.
- Step 5: For each group, build a DSU and include only the edges whose weights are 'supermasks' of the group's 'cost_mask' .
- Step 6: Calculate the minimum weight within each connected component (using the supermask edges)
- Step 7: Update the answers for queries in this group with the component cost. If any answer changes, then continue with active queries which got updated.
- Step 8: Repeat from step 4 until there are no changes to update the active queries or until all queries are correctly processed.
- Step 9: Return the answer array.
Key Insights
- Insight 1: If two vertices are not connected, the minimum cost is -1. We can efficiently determine connectivity using the Union-Find (DSU) data structure.
- Insight 2: The bitwise AND operation implies that the cost can only decrease (or stay the same) as we add more edges to the walk. Therefore, we can iterate on potential costs which are bitmasks present in the edge weights.
- Insight 3: The key idea behind the iterative improvement is that we can consider edges whose weight 'w' is a supermask of the current cost bitmask 'cost_mask'. We can then determine connected components using only those edges, and find the minimum weight within those components.
- Insight 4: The number of iterations required is bounded by the number of bits in the largest possible weight (10^5). Since the maximum weight is 10^5, we will have at most ~17 iterations.
Complexity Analysis
Time Complexity: O(Q * E * A * log*N)
Space Complexity: O(N + E + Q)
Topics
This problem involves: Array, Bit Manipulation, Union Find, Graph.
Companies
Asked at: DE Shaw.