|
| 1 | +package problem1514 |
| 2 | + |
| 3 | +/* |
| 4 | +You are given an undirected weighted graph of n nodes (0-indexed), |
| 5 | +represented by an edge list where edges[i] = [a, b] is an undirected edge connecting the nodes a and b with a |
| 6 | +probability of success of traversing that edge succProb[i]. |
| 7 | +Given two nodes start and end, find the path with the maximum probability of |
| 8 | +success to go from start to end and return its success probability. |
| 9 | +If there is no path from start to end, return 0. |
| 10 | +Your answer will be accepted if it differs from the correct answer by at most 1e-5. |
| 11 | +*/ |
| 12 | + |
| 13 | +type Pair struct { |
| 14 | + Prob float64 |
| 15 | + Pos int |
| 16 | +} |
| 17 | + |
| 18 | +func maxProbability(size int, edges [][]int, succProb []float64, start int, end int) float64 { |
| 19 | + var res float64 |
| 20 | + // Building graph |
| 21 | + var graph = make([][]Pair, size) |
| 22 | + for i := range edges { |
| 23 | + graph[edges[i][0]] = append(graph[edges[i][0]], Pair{succProb[i], edges[i][1]}) |
| 24 | + graph[edges[i][1]] = append(graph[edges[i][1]], Pair{succProb[i], edges[i][0]}) |
| 25 | + } |
| 26 | + // Cache for probability |
| 27 | + var seen = make([]float64, size) |
| 28 | + // BFS buckets |
| 29 | + var cur, nxt []Pair |
| 30 | + cur = []Pair{{1, start}} |
| 31 | + // Run BFS until we run out of paths |
| 32 | + for len(cur) > 0 { |
| 33 | + nxt = nxt[:0] |
| 34 | + // For each node in our list |
| 35 | + for _, c := range cur { |
| 36 | + // Check if we already visited with higher probability |
| 37 | + if seen[c.Pos] > c.Prob { |
| 38 | + continue |
| 39 | + } |
| 40 | + // For each potential path from there |
| 41 | + for _, n := range graph[c.Pos] { |
| 42 | + temp := Pair{c.Prob * n.Prob, n.Pos} |
| 43 | + if n.Pos == end { |
| 44 | + // Check we reached the end |
| 45 | + res = max(res, temp.Prob) |
| 46 | + } else { |
| 47 | + // Check if we're going to a visited node with higher |
| 48 | + // probability |
| 49 | + if seen[temp.Pos] >= temp.Prob { |
| 50 | + continue |
| 51 | + } |
| 52 | + // Record the current probability |
| 53 | + seen[temp.Pos] = temp.Prob |
| 54 | + // Add next node to bucket |
| 55 | + nxt = append(nxt, temp) |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + // Switch buckets |
| 60 | + cur, nxt = nxt, cur |
| 61 | + } |
| 62 | + return res |
| 63 | +} |
| 64 | + |
| 65 | +func max(a, b float64) float64 { |
| 66 | + if a > b { |
| 67 | + return a |
| 68 | + } |
| 69 | + return b |
| 70 | +} |
0 commit comments