The Indian Engineer

Problem 1514 Path With Maximum Probability

Posted on Last edited 3 mins

Dijkstra Graph

Problem Statement

Link - Problem 1514

Question

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

img1

Input: n = 3, edges = [[0,1],[1,2],[0,2]], succProb = [0.5,0.5,0.2], start = 0, end = 2
Output: 0.25000
Explanation: 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

img2

Input: n = 3, edges = [[0,1],[1,2],[0,2]], succProb = [0.5,0.5,0.3], start = 0, end = 2
Output: 0.30000

Example 3

img3

Input: n = 3, edges = [[0,1]], succProb = [0.5], start = 0, end = 2
Output: 0.00000
Explanation: There is no path between 0 and 2.

Constraints

Solution

class Solution {
public:
    double maxProbability(int n, vector<vector<int>>& edges, vector<double>& succProb, int start, int end) {
        vector<double>prob(n,0.0);
        prob[start]= 1.0;
        vector<vector<pair<double,int>>>adj(n);
        for(int i = 0; i<edges.size(); i++){
            adj[edges[i][0]].push_back({succProb[i],edges[i][1]});
            adj[edges[i][1]].push_back({succProb[i],edges[i][0]});
        }

        priority_queue<pair<double,int>,vector<pair<double,int>>>pq;

        pq.push({1.0,start});

        while(!pq.empty()){
            auto [curr,i] = pq.top();
            pq.pop();

            if(i == end)
                return curr;

            for(auto [nextprob,j]:adj[i]){
                double newprob = curr*nextprob;
                if(newprob>prob[j]){
                    prob[j] = newprob;
                    pq.push({newprob,j});
                }
            }
        }

        return 0.0;
    }
};

Complexity Analysis

Algorithm Time Complexity Space Complexity
Dijkstra O(ElogV) O(V+E)

Explanation

1. Intuition

It’s known that

\[P(A \cap B) = P(A \mid B) \times P(B)\]

Let \(B\) denote the set for a path to the current vertex.

Let \(A\) denote the set for paths to the next vertex.

So \(A \cap B\) is the set for a path passing the current vertex and the next vertex.

Let \(P(B)\) denote the probability to the current vertex.

Let \(P(A \mid B)\) denote the probability from the current vertex to the next vertex.

Then \(P(A \cap B)\) is the probability of the path passing the current vertex and then the next vertex.

2. Implementation