1514. Path with Maximum Probability - Explanation
Description
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.25000Explanation: 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.30000Explanation: There is an edge which connects the nodes 0 and 2 with probability = 0.3.
Example 3:
Input: n = 3, edges = [[0,1]], succProb = [0.5], start = 0, end = 2
Output: 0.00000Explanation: There is no path between 0 and 2.
Constraints:
2 <= n <= 10,0000 <= start, end < nstart != end0 <= a, b < na != b0 <= succProb.length == edges.length <= 20,0000 <= succProb[i] <= 1- There is at most one edge between every two nodes.
Topics
Prerequisites
Before attempting this problem, you should be comfortable with:
- Graph Representation - Building adjacency lists to represent weighted undirected graphs
- Dijkstra's Algorithm - Finding optimal paths using a priority queue (max-heap for this problem)
- Priority Queue / Heap - Efficiently extracting the maximum or minimum element
- Bellman-Ford Algorithm - Relaxing edges iteratively to find optimal paths
1. Dijkstra's Algorithm - I
Intuition
This problem asks for the path with maximum probability, which is similar to finding the shortest path but with multiplication instead of addition. Since probabilities are between 0 and 1, multiplying them gives smaller values, so we want to maximize the product. Dijkstra's algorithm works here because we can negate probabilities (or use a max-heap) to always process the most promising path first. Once we reach the destination, we have found the optimal path.
Algorithm
- Build an adjacency list where each node maps to its neighbors and the corresponding edge probabilities.
- Use a max-heap (priority queue) to always process the node with the highest probability first. Start with probability
1.0at the source node. - Mark nodes as visited once processed to avoid redundant work.
- For each node popped from the heap, if it is the destination, return the current probability.
- Otherwise, for each unvisited neighbor, compute the new probability by multiplying the current probability with the edge probability, and push it to the heap.
- If the destination is never reached, return
0.
class Solution:
def maxProbability(self, n: int, edges: List[List[int]], succProb: List[float], start_node: int, end_node: int) -> float:
adj = collections.defaultdict(list)
for i in range(len(edges)):
src, dst = edges[i]
adj[src].append((dst, succProb[i]))
adj[dst].append((src, succProb[i]))
pq = [(-1, start_node)]
visit = set()
while pq:
prob, cur = heapq.heappop(pq)
visit.add(cur)
if cur == end_node:
return -prob
for nei, edgeProb in adj[cur]:
if nei not in visit:
heapq.heappush(pq, (prob * edgeProb, nei))
return 0.0Time & Space Complexity
- Time complexity:
- Space complexity:
Where is the number nodes and is the number of edges.
2. Dijkstra's Algorithm - II
Intuition
This is a refined version of Dijkstra's algorithm that tracks the maximum probability to reach each node. Instead of just using a visited set, we maintain an array storing the best probability found so far for each node. This allows us to skip processing a node if we have already found a better path to it, reducing unnecessary work.
Algorithm
- Build an adjacency list mapping each node to its neighbors and edge probabilities.
- Initialize a
maxProbarray wheremaxProb[i]stores the highest probability to reach nodei. SetmaxProb[start] = 1.0. - Use a max-heap starting with
(1.0, start_node). - For each node popped from the heap, if it is the destination, return the probability. If the current probability is worse than the recorded best, skip it.
- For each neighbor, compute the new probability. If it improves the best known probability for that neighbor, update the array and push the neighbor to the heap.
- Return
0if the destination is unreachable.
class Solution:
def maxProbability(self, n: int, edges: List[List[int]], succProb: List[float], start_node: int, end_node: int) -> float:
adj = [[] for _ in range(n)]
for i in range(len(edges)):
src, dst = edges[i]
adj[src].append((dst, succProb[i]))
adj[dst].append((src, succProb[i]))
maxProb = [0] * n
maxProb[start_node] = 1.0
pq = [(-1.0, start_node)]
while pq:
curr_prob, node = heapq.heappop(pq)
curr_prob *= -1
if node == end_node:
return curr_prob
if curr_prob > maxProb[node]:
continue
for nei, edge_prob in adj[node]:
new_prob = curr_prob * edge_prob
if new_prob > maxProb[nei]:
maxProb[nei] = new_prob
heapq.heappush(pq, (-new_prob, nei))
return 0.0Time & Space Complexity
- Time complexity:
- Space complexity:
Where is the number nodes and is the number of edges.
3. Bellman Ford Algorithm
Intuition
The Bellman-Ford algorithm can find the best path by relaxing all edges repeatedly. For this problem, we relax edges to maximize probability instead of minimizing distance. Since the graph is undirected, we check both directions for each edge. The algorithm runs for at most n iterations, but we can stop early if no updates occur in a round, meaning we have found the optimal solution.
Algorithm
- Initialize a
maxProbarray with all zeros exceptmaxProb[start] = 1.0. - For up to
niterations, iterate through all edges. - For each edge
(src, dst)with probabilityp, try to relax in both directions: ifmaxProb[src] * p > maxProb[dst], updatemaxProb[dst], and vice versa. - Track whether any update occurred. If no updates happen in an iteration, break early.
- Return
maxProb[end_node].
class Solution:
def maxProbability(self, n: int, edges: List[List[int]], succProb: List[float], start_node: int, end_node: int) -> float:
maxProb = [0.0] * n
maxProb[start_node] = 1.0
for i in range(n):
updated = False
for j in range(len(edges)):
src, dst = edges[j]
if maxProb[src] * succProb[j] > maxProb[dst]:
maxProb[dst] = maxProb[src] * succProb[j]
updated = True
if maxProb[dst] * succProb[j] > maxProb[src]:
maxProb[src] = maxProb[dst] * succProb[j]
updated = True
if not updated:
break
return maxProb[end_node]Time & Space Complexity
- Time complexity:
- Space complexity:
Where is the number nodes and is the number of edges.
4. Shortest Path Faster Algorithm
Intuition
SPFA is an optimization of Bellman-Ford that uses a queue to process only nodes whose distances (or probabilities) have changed. Instead of iterating through all edges in every round, we only process edges from nodes that might lead to improvements. This can be significantly faster in practice, especially for sparse graphs.
Algorithm
- Build an adjacency list and initialize
maxProbarray withmaxProb[start] = 1.0. - Use a queue and add the start node. Maintain a boolean array to track which nodes are currently in the queue.
- While the queue is not empty, dequeue a node and mark it as no longer in the queue.
- For each neighbor, compute the new probability. If it improves the neighbor's best probability, update it.
- If the neighbor is not already in the queue, add it and mark it as in the queue.
- Return
maxProb[end_node].
class Solution:
def maxProbability(self, n: int, edges: List[List[int]], succProb: List[float], start_node: int, end_node: int) -> float:
adj = [[] for _ in range(n)]
for i in range(len(edges)):
src, dst = edges[i]
adj[src].append((dst, succProb[i]))
adj[dst].append((src, succProb[i]))
maxProb = [0.0] * n
maxProb[start_node] = 1.0
q = deque([start_node])
while q:
node = q.popleft()
for nei, edge_prob in adj[node]:
new_prob = maxProb[node] * edge_prob
if new_prob > maxProb[nei]:
maxProb[nei] = new_prob
q.append(nei)
return maxProb[end_node]Time & Space Complexity
- Time complexity:
- Space complexity:
Where is the number nodes and is the number of edges.
Common Pitfalls
Using Min-Heap Instead of Max-Heap
Unlike shortest path problems where we minimize distance, this problem requires maximizing probability. Using a min-heap (the default in most languages) will process low-probability paths first, leading to incorrect results or inefficiency. Always use a max-heap or negate the probabilities when using a min-heap.
Initializing Start Probability to Zero
The starting node should have a probability of 1.0 (certainty), not 0.0. Multiplying any edge probability by zero will always yield zero, preventing the algorithm from finding any valid path. Initialize maxProb[start_node] = 1.0 before beginning the search.
Forgetting the Graph is Undirected
Each edge connects two nodes bidirectionally, so you must add both directions to the adjacency list. Forgetting to add the reverse edge means some paths will be unreachable, potentially missing the optimal solution or returning zero when a valid path exists.
Sign in to join the discussion