Solution: Path with Maximum Probability
Let’s solve the Path with Maximum Probability problem using the Graphs pattern.
We'll cover the following
Statement
You are given an undirected weighted graph of n
nodes, represented by a 0-indexed list, edges
, where edges[i] = [a, b]
is an undirected edge connecting the nodes a
and b
. There is another list succProb
, where succProb[i]
is the probability of success of traversing the edge edges[i]
.
Additionally, you are given two nodes, start
and end
. Your task is to 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.
Constraints:
n
start
,end
n
start
end
a
,b
n
a
b
succProb.length
edges.length
succProb[i]
There is, at most, one edge between every two nodes.
Solution
In this solution, we use a graph-based approach leveraging Dijkstra's algorithm, but instead of minimizing distances, we maximize probabilities. The given graph consists of n
nodes connected by edges, each associated with a probability of success. We aim to find the path from the start
node to the end
node with the highest probability.
First, we construct an adjacency list to represent the graph, where each node maps to a list of neighboring nodes along with the probability of traversing the edge connecting them. Once the graph is built, we use a max heap (priority queue) to process nodes in decreasing order of probability. We initialize the heap with the start
node having a probability of end
node (in which case we return the computed probability) or exhaust all possibilities (returning
Let's look at the algorithm steps of this solution:
Create a dictionary to represent the graph as an adjacency list. For each edge
(u, v)
inedges
, store the probabilitysuccProb[i]
in both directions as the graph is undirected.Create a list
max_prob
of sizen
, initialized with(default probability). Set max_prob[start]
tobecause we are 100% certain we are at the start node. Create a priority queue,
pq
(max heap), to always process the most probable path first. Push the start node into the heap with probability. Python's default heap is a min heap, so we store the negative probabilities such as to simulate a max heap. Process the nodes in the priority queue, so while the queue is not empty:
Pop the node
cur_node
with the highest probability from the heap. Convert the probability back to a positive value (-cur_prob
). If the popped node is theend
node, return-cur_prob
as the result.For each neighbor of
cur_node
and the edge probability in thegraph
, calculate the new probability:new_prob=(−cur_prob) × edge_prob
.If
new_prob
is greater thanmax_prob[neighbor]
, updatemax_prob[neighbor]
and push(new_prob, neighbor)
into the heap.
After exploring all the neighbors of
cur_node
, cleargraph[cur_node]
to prevent revisiting already processed nodes.
If we have exhausted the priority queue without reaching the
end
, return, meaning no path exists.
Let’s look at the following illustration to get a better understanding of the solution:
Level up your interview prep. Join Educative to access 70+ hands-on prep courses.