Problem solution · Python

Minimum Threshold Path with Limited Heavy Edges

Minimum Threshold Path with Limited Heavy Edges: a Python solution using breadth-first search. Learn the idea, check the complexity, and read the full code, with credit to Kamyu LeetCode Solutions.

Technique
Breadth-first search
Source
Kamyu LeetCode Solutions
Length
52 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

Breadth-first search

For Minimum Threshold Path with Limited Heavy Edges, the implementation explores reachable states in layers, which is the standard shape for unweighted shortest paths and minimum-step transitions.

  1. Model each valid configuration as a state and each legal move as an edge.
  2. Seed the queue with the starting state and mark it immediately.
  3. Expand each state once, recording distance or reachability for unseen neighbours.

Code notes

  • 52 lines of Python from the credited upstream file minimum-threshold-path-with-limited-heavy-edges.py.
  • The implementation visibly relies on sequence storage, ordered lookup, work queue.
  • No explicit loop blocks detected.

Complexity

Verify that each state and transition is processed only a bounded number of times; that determines the traversal cost.

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

Source

Code and credit

This code comes from Kamyu LeetCode Solutions by kamyu104 and is used under the MIT licence.

Full codeMinimum Threshold Path with Limited Heavy Edges · PythonPython
Use this to learn the idea, then write your own version.
# Time:  O((n + e) * loge)# Space: O(n + e) import collections  # binary search, 0-1 bfs, dequeclass Solution(object):    def minimumThreshold(self, n, edges, source, target, k):        """        :type n: int        :type edges: List[List[int]]        :type source: int        :type target: int        :type k: int        :rtype: int        """        def binary_search(left, right, check):            while left <= right:                mid = left+(right-left)//2                if check(mid):                    right = mid-1                else:                    left = mid+1            return left         def check(i):            t = weights[i]            lookup = [False]*n            dq = collections.deque([(source, 0)])            while dq:                u, d = dq.popleft()                if lookup[u]:                    continue                lookup[u] = True                if u == target:                    return d <= k                for v, w in adj[u]:                    if w <= t:                        dq.appendleft((v, d))                    else:                        dq.append((v, d+1))            return False         adj = [[] for _ in xrange(n)]        for u, v, w in edges:            adj[u].append((v, w))            adj[v].append((u, w))        weights = sorted(set([0]+[w[2] for w in edges]))        i = binary_search(0, len(weights)-1, check)        return weights[i] if i < len(weights) else -1 

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 ↗