MCPcopy Create free account
hub / github.com/Tiwarishashwat/InterviewCodes / maxProbability

Method maxProbability

PathWithMaximumProbability.java:2–28  ·  view source on GitHub ↗
(int n, int[][] edges, double[] succProb, int start_node, int end_node)

Source from the content-addressed store, hash-verified

1class Solution {
2 public double maxProbability(int n, int[][] edges, double[] succProb, int start_node, int end_node) {
3 double prob[] = new double[n];
4 prob[start_node] = 1;
5 //bellmanford
6 for(int i=0;i<n-1;i++){
7 int j=0;
8 boolean isChanged=false;
9 for(int edge[] : edges){ // j
10 int u = edge[0];
11 int v = edge[1];
12 double sp = succProb[j];
13 if(prob[u] * sp > prob[v]){
14 prob[v] = prob[u] * sp;
15 isChanged=true;
16 }
17 if(prob[v] * sp > prob[u]){
18 prob[u] = prob[v] * sp;
19 isChanged=true;
20 }
21 j++;
22 }
23 if(!isChanged){
24 break;
25 }
26 }
27 return prob[end_node];
28 }
29}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected