Advanced Graphs Weighted shortest paths
481. Path with Maximum Probability
You are given an undirected graph with n nodes, labelled from 0 to n - 1. Each edges[i] = [a, b] joins
nodes a and b, and you can cross it in either direction. Crossing it succeeds with probability
succProb[i].
A path from start_node to end_node succeeds only if every edge on it succeeds, so its probability is the
product of the probabilities of its edges. Return the largest probability of any path from start_node to
end_node. If no path joins them, return 0. If start_node and end_node are the same node, return 1.
An answer within 10-5 of the expected one is accepted.
Example 1
Input: n = 5
edges = [[0, 1],
[1, 4],
[0, 2],
[2, 3],
[3, 4],
[0, 4]]
succProb = [0.5, 0.8, 0.9, 0.9, 0.5, 0.3]
start_node = 0
end_node = 4
0.405Explanation: The path 0 → 2 → 3 → 4 succeeds with probability 0.9 × 0.9 × 0.5 = 0.405. That beats 0 → 1 → 4, with 0.5 × 0.8 = 0.4, and the direct edge, with 0.3.
Example 2
Input: n = 4
edges = [[0, 1], [2, 3]]
succProb = [0.7, 0.9]
start_node = 0
end_node = 3
0Explanation: No path joins node 0 to node 3, so the answer is 0.
Example 3
Input: n = 3
edges = [[1, 0], [1, 2]]
succProb = [0.25, 0.5]
start_node = 2
end_node = 2
1Explanation: We start at the end node, so we cross no edge, and the answer is 1.
Check your understanding
Work out the answer for this input before you write any code.
Constraints
1 ≤ n ≤ 1050 ≤ edges.length ≤ 105edges[i] = [a, b], with0 ≤ a, b < nsuccProb.length == edges.length0 ≤ succProb[i] ≤ 10 ≤ start_node, end_node < n- Two edges can join the same two nodes, and an edge can join a node to itself.
Clarifying questions
What you would ask an interviewer before writing any code.