-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPath with Maximum Probability 27th Aug 2024.java
44 lines (42 loc) · 1.35 KB
/
Path with Maximum Probability 27th Aug 2024.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import java.util.*;
class Solution {
public double maxProbability(int n, int[][] edges, double[] succProb, int start_node, int end_node) {
if (start_node == end_node) return 1.0;
List<Pair>[] graph = new List[n];
for (int i = 0; i < n; i++) {
graph[i] = new ArrayList<>();
}
for (int i = 0; i < edges.length; i++) {
int u = edges[i][0];
int v = edges[i][1];
double prob = succProb[i];
graph[u].add(new Pair(v, prob));
graph[v].add(new Pair(u, prob));
}
double[] probs = new double[n];
Arrays.fill(probs, 0);
probs[start_node] = 1.0;
Queue<Integer> q = new LinkedList<>();
q.add(start_node);
while (!q.isEmpty()) {
int node = q.poll();
for (Pair neighbor : graph[node]) {
int nextNode = neighbor.node;
double edgeProb = neighbor.prob;
if (probs[node] * edgeProb > probs[nextNode]) {
probs[nextNode] = probs[node] * edgeProb;
q.add(nextNode);
}
}
}
return probs[end_node];
}
class Pair {
int node;
double prob;
Pair(int node, double prob) {
this.node = node;
this.prob = prob;
}
}
}