Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find path with maximum probability using Python
Suppose we have an undirected weighted graph with n nodes (nodes are numbered from 0 onwards). This graph is given as input using an edge list, and for each edge e, it has a probability of success of traversing that edge probability[e]. We also have start and end nodes, and we need to find the path with the maximum probability of success to go from start to end and return its success probability. If we cannot find any path, then return 0.
Problem Example
Consider the following graph:
There are two paths from node 0 to node 2:
Direct path: 0 ? 2 with probability 0.2
Indirect path: 0 ? 1 ? 2 with probability 0.4 × 0.6 = 0.24
The maximum probability is 0.24.
Algorithm Approach
We use a modified Dijkstra's algorithm with the following steps ?
Build a graph from the edge list using probability values as weights
Use a queue to store (node, probability) pairs
Start with (start_node, 1.0) since we begin with 100% probability
Track visited nodes with their maximum probability
For each neighbor, update if we find a better probability path
Implementation
from collections import defaultdict, deque
def solve(edges, probability, start, end):
# Build adjacency list graph
graph = defaultdict(list)
for i in range(len(edges)):
src, dst = edges[i][0], edges[i][1]
prob = probability[i]
graph[src].append((dst, prob))
graph[dst].append((src, prob))
# Initialize queue and visited tracking
queue = deque()
queue.append((start, 1.0)) # Start with 100% probability
visited = defaultdict(float)
while queue:
node, prob = queue.popleft()
# Skip if we've found a better path to this node
if visited[node] > prob:
continue
else:
visited[node] = prob
# Check all neighbors
for neighbor, edge_prob in graph[node]:
new_prob = prob * edge_prob
if visited[neighbor] < new_prob:
queue.append((neighbor, new_prob))
return visited[end]
# Test the algorithm
edges = [[0,1],[1,2],[0,2]]
probability = [0.4, 0.6, 0.2]
start = 0
end = 2
result = solve(edges, probability, start, end)
print(f"Maximum probability from {start} to {end}: {result}")
Maximum probability from 0 to 2: 0.24
How It Works
The algorithm works by:
Graph Construction: Build an undirected graph where each edge has a probability weight
BFS Traversal: Use breadth-first search to explore all possible paths
Probability Tracking: Keep track of the maximum probability to reach each node
Path Optimization: Only explore paths that improve the current best probability
Time Complexity
The time complexity is O(E + V) where E is the number of edges and V is the number of vertices, similar to BFS traversal.
Conclusion
This solution uses a modified Dijkstra's algorithm to find the path with maximum probability. The key insight is treating probabilities as weights and using BFS to explore all possible paths while tracking the best probability to reach each node.
