1514. Path with Maximum Probability
Problem
In an undirected graph, traversing edge edges[i] succeeds with probability succProb[i]. Return the highest probability of successfully going from start_node to end_node, or 0 if there's no path.
Example: n = 3, edges = [[0,1],[1,2],[0,2]], succProb = [0.5,0.5,0.2], start = 0, end = 2 → 0.25
Constraints: n ≤ 10⁴; up to 2·10⁴ edges.
Dijkstra with a max-heap on probability: probabilities only shrink along a path (multiplying by ≤ 1), so the greedy "best first" argument still holds.
Complexity: O(E log V).
class Solution:
def maxProbability(self, n: int, edges: List[List[int]], succProb: List[float], start_node: int, end_node: int) -> float:
g = defaultdict(list)
for (a, b), p in zip(edges, succProb):
g[a].append((b, p))
g[b].append((a, p))
best = [0.0] * n
best[start_node] = 1.0
pq = [(-1.0, start_node)]
while pq:
p, u = heappop(pq)
p = -p
if u == end_node:
return p
if p < best[u]:
continue
for v, w in g[u]:
if p * w > best[v]:
best[v] = p * w
heappush(pq, (-best[v], v))
return 0.0
assert abs(Solution().maxProbability(3, [[0, 1], [1, 2], [0, 2]], [0.5, 0.5, 0.2], 0, 2) - 0.25) < 1e-9
assert Solution().maxProbability(3, [[0, 1]], [0.5], 0, 2) == 0.0
class Solution {
public double maxProbability(int n, int[][] edges, double[] succProb, int start, int end) {
List<List<double[]>> g = new ArrayList<>();
for (int i = 0; i < n; i++) g.add(new ArrayList<>());
for (int i = 0; i < edges.length; i++) {
g.get(edges[i][0]).add(new double[]{edges[i][1], succProb[i]});
g.get(edges[i][1]).add(new double[]{edges[i][0], succProb[i]});
}
double[] best = new double[n];
best[start] = 1;
PriorityQueue<double[]> pq = new PriorityQueue<>((a, b) -> Double.compare(b[0], a[0]));
pq.add(new double[]{1, start});
while (!pq.isEmpty()) {
double[] cur = pq.poll();
int u = (int) cur[1];
if (u == end) return cur[0];
if (cur[0] < best[u]) continue;
for (double[] e : g.get(u)) {
int v = (int) e[0];
if (cur[0] * e[1] > best[v]) { best[v] = cur[0] * e[1]; pq.add(new double[]{best[v], v}); }
}
}
return 0;
}
}
type probItem struct {
p float64
u int
}
type probHeap []probItem // largest probability first
func (h probHeap) Len() int { return len(h) }
func (h probHeap) Less(i, j int) bool { return h[i].p > h[j].p }
func (h probHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *probHeap) Push(x any) { *h = append(*h, x.(probItem)) }
func (h *probHeap) Pop() any {
old := *h
x := old[len(old)-1]
*h = old[:len(old)-1]
return x
}
func maxProbability(n int, edges [][]int, succProb []float64, start_node int, end_node int) float64 {
type edge struct {
to int
p float64
}
g := make([][]edge, n)
for i, e := range edges {
g[e[0]] = append(g[e[0]], edge{e[1], succProb[i]})
g[e[1]] = append(g[e[1]], edge{e[0], succProb[i]})
}
best := make([]float64, n)
best[start_node] = 1
pq := &probHeap{{1, start_node}}
for pq.Len() > 0 { // Dijkstra on max-product: multiplying by p <= 1 never helps later
cur := heap.Pop(pq).(probItem)
if cur.u == end_node {
return cur.p
}
if cur.p < best[cur.u] {
continue
}
for _, e := range g[cur.u] {
if np := cur.p * e.p; np > best[e.to] {
best[e.to] = np
heap.Push(pq, probItem{np, e.to})
}
}
}
return 0
}