Path with Maximum Probability — LeetCode 1514 Python Solution

MediumGraphArrayShortest PathHeap (Priority Queue)
Problem
#1514
Reading time
5 min

The problem

You are given an undirected weighted graph of n nodes (0-indexed), represented by an edge list where edges[i] = [a, b] is an undirected edge connecting the nodes a and b with a probability of success of traversing that edge succProb[i]. Given two nodes start and end, find the path with the maximum probability of success to go from start to end and return its success probability.

Example

Input
n = 3, edges = [[0,1],[1,2],[0,2]], succProb = [0.5,0.5,0.2], start = 0, end = 2
Output
0.25000
Explanation
There are two paths from start to end, one having a probability of success = 0.2 and the other has 0.5 * 0.5 = 0.25.

Python solution

Python
class Solution:
    def maxProbability(
        self,
        n: int,
        edges: List[List[int]],
        succProb: List[float],
        start_node: int,
        end_node: int,
    ) -> float:
        g: List[List[Tuple[int, float]]] = [[] for _ in range(n)]
        for (a, b), p in zip(edges, succProb):
            g[a].append((b, p))
            g[b].append((a, p))
        pq = [(-1, start_node)]
        dist = [0] * n
        dist[start_node] = 1
        while pq:
            w, a = heappop(pq)
            w = -w
            if dist[a] > w:
                continue
            for b, p in g[a]:
                if (t := w * p) > dist[b]:
                    dist[b] = t
                    heappush(pq, (-t, b))
        return dist[end_node]

Complexity

MeasureComplexity
TimeO(m \times \log m)
SpaceO(m) auxiliary

Pattern: Heap / Priority Queue

Keep only the best k elements, or always pull the smallest, in log time. LeetCode 1514. Path with Maximum Probability is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Heap (Priority Queue).

The heap / priority queue guide has the Python template for the pattern and the 163 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 1514. Path with Maximum Probability?
LeetCode 1514. Path with Maximum Probability is rated Medium on LeetCode.
What is the time complexity of LeetCode 1514. Path with Maximum Probability?
The Python solution on this page runs in O(m \times \log m).
What is the space complexity of LeetCode 1514. Path with Maximum Probability?
The Python solution on this page uses O(m) auxiliary space.
What topics does LeetCode 1514. Path with Maximum Probability cover?
LeetCode 1514. Path with Maximum Probability is tagged Graph, Array, Shortest Path and Heap (Priority Queue) on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview