Path with Maximum Probability Problem
Description
LeetCode Problem 1514.
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.
If there is no path from start to end, return 0. Your answer will be accepted if it differs from the correct answer by at most 1e-5.
Example 1:

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.
Example 2:

Input: n = 3, edges = [[0,1],[1,2],[0,2]], succProb = [0.5,0.5,0.3], start = 0, end = 2
Output: 0.30000
Example 3:

Input: n = 3, edges = [[0,1]], succProb = [0.5], start = 0, end = 2
Output: 0.00000
Explanation: There is no path between 0 and 2.
Constraints:
2 <= n <= 10^40 <= start, end < nstart != end0 <= a, b < na != b0 <= succProb.length == edges.length <= 2*10^40 <= succProb[i] <= 1- There is at most one edge between every two nodes.
Solution
Approach: Dijkstra's Algorithm with a Max-Heap
The probability of a path is the product of its edge probabilities. Every factor is at most 1, so extending a path can never make it more likely. That is the same property that makes Dijkstra's algorithm work for shortest paths (adding an edge never makes a path shorter), with "multiply and take the maximum" in place of "add and take the minimum".
Keep best[v], the highest probability found so far for reaching v (1 for the start, 0 for everyone else), and a max-heap of (probability, node) pairs, starting with (1, start):
- Pop the most probable entry
(w, a). Ifais the end node,wis the answer: no path still waiting in the heap can beat it, because those paths can only lose probability as they grow. - If
w < best[a], the entry is stale (we already found a better path toa), so skip it. - Otherwise, for each edge
(a, b)with probabilityp, ifw * p > best[b], updatebest[b]and push(w * p, b).
If the heap runs empty before we reach the end node, the end node cannot be reached, and the answer is 0.
Complexity: O((n + m) log m) time and O(n + m) space for the graph and the heap, where m is the number of edges.
import heapq
class Solution:
def maxProbability(self, n: int, edges: List[List[int]], succProb: List[float], start_node: int, end_node: int) -> float:
graph = [[] for _ in range(n)]
for (a, b), p in zip(edges, succProb):
graph[a].append((b, p))
graph[b].append((a, p))
best = [0.0] * n # best[v]: highest probability found so far to reach v
best[start_node] = 1.0
heap = [(-1.0, start_node)] # max-heap through negated probabilities
while heap:
w, a = heapq.heappop(heap)
w = -w
if a == end_node:
return w
if w < best[a]:
continue # a stale entry: a better path to a was already handled
for b, p in graph[a]:
if w * p > best[b]:
best[b] = w * p
heapq.heappush(heap, (-best[b], b))
return 0.0