Path with Maximum Probability - Problem

Imagine you're a network engineer tasked with finding the most reliable communication path between two servers in an unreliable network. You have an undirected weighted graph of n nodes (0-indexed), where each edge represents a network connection with a specific probability of successful data transmission.

Given:

  • edges[i] = [a, b] - an undirected edge connecting nodes a and b
  • succProb[i] - probability of success for traversing edge i (between 0.0 and 1.0)
  • start and end nodes

Goal: Find the path from start to end with the maximum probability of success and return that probability.

If no path exists, return 0. Your answer will be accepted if it differs from the correct answer by at most 1e-5.

Example: If you have edges [[0,1],[1,2],[0,2]] with probabilities [0.5, 0.5, 0.2], the path 0→1→2 has probability 0.5 × 0.5 = 0.25, while direct path 0→2 has probability 0.2. Choose the higher probability path!

Input & Output

example_1.py — Basic Path Finding
$ Input: n = 3, edges = [[0,1],[1,2],[0,2]], succProb = [0.5,0.5,0.2], start = 0, end = 2
Output: 0.25000
💡 Note: There are two paths from start to end. Path 0→1→2 has probability 0.5 × 0.5 = 0.25. Direct path 0→2 has probability 0.2. We choose the maximum: 0.25.
example_2.py — No Path Available
$ Input: n = 3, edges = [[0,1],[1,2]], succProb = [0.5,0.5], start = 0, end = 2
Output: 0.25000
💡 Note: Only one path exists from 0 to 2: 0→1→2 with probability 0.5 × 0.5 = 0.25.
example_3.py — Disconnected Graph
$ Input: n = 3, edges = [[0,1]], succProb = [0.5], start = 0, end = 2
Output: 0.00000
💡 Note: There is no path from node 0 to node 2, so return 0.

Visualization

Tap to expand
Path with Maximum Probability - Network RoutingStart1End50% reliable50% reliableDirect: 20% reliableBest Path AnalysisRoute 1: Start→1→EndProbability: 50% × 50% = 25%Route 2: Start→End (Direct)Probability: 20%Algorithm Choice✓ Choose Route 1 (25%)✗ Reject Route 2 (20%)Maximum: 25%High Priority (Dijkstra's Queue)Medium PriorityLow Priority (Skip)
Understanding the Visualization
1
Network Graph
Each node represents a server, edges represent network connections with reliability percentages
2
Priority Queue
Always explore the most promising (highest probability) path first, like choosing the most reliable route
3
Path Calculation
Multiply probabilities along the path (like compound reliability in network engineering)
4
Optimal Solution
Return the maximum probability found when reaching the destination
Key Takeaway
🎯 Key Insight: Transform the maximum probability problem into a shortest path problem using Dijkstra's algorithm with a max-heap, always exploring the most promising paths first for optimal efficiency.

Time & Space Complexity

Time Complexity
⏱️
O(E log V)

Each edge is processed once, and priority queue operations take O(log V) time

n
2n
Linearithmic
Space Complexity
O(V + E)

Space for adjacency list, priority queue, and probability tracking array

n
2n
Linear Space

Constraints

  • 2 ≤ n ≤ 104
  • 0 ≤ edges.length ≤ 2 × 104
  • edges[i].length == 2
  • 0 ≤ ai, bi ≤ n - 1
  • ai ≠ bi
  • 0 ≤ start, end ≤ n - 1
  • start ≠ end
  • 0 < succProb[i] ≤ 1
  • At most one edge between any two nodes
Asked in
Google 42 Amazon 38 Meta 31 Microsoft 28
73.4K Views
High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen