Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 25fd4fb

Browse files
committed
commit
1 parent a5be2f4 commit 25fd4fb

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from typing import List
2+
3+
class Solution:
4+
def maxProbability(self, n: int, edges: List[List[int]], succProb: List[float], start: int, end: int) -> float:
5+
max_prob = [0] * n
6+
max_prob[start] = 1
7+
8+
for i in range(n - 1):
9+
# If there is no larger probability found during an entire round of updates,
10+
# stop the update process.
11+
has_update = False
12+
for j in range(len(edges)):
13+
u, v = edges[j]
14+
path_prob = succProb[j]
15+
if max_prob[u] * path_prob > max_prob[v]:
16+
max_prob[v] = max_prob[u] * path_prob
17+
has_update = True
18+
if max_prob[v] * path_prob > max_prob[u]:
19+
max_prob[u] = max_prob[v] * path_prob
20+
has_update = True
21+
if not has_update:
22+
break
23+
24+
return max_prob[end]
25+
26+
27+
# Take input from the user
28+
n = int(input("Enter the number of vertices (n): "))
29+
m = int(input("Enter the number of edges (m): "))
30+
edges = []
31+
succProb = []
32+
33+
for i in range(m):
34+
print(f"Enter the source and destination vertices of edge {i+1}:")
35+
u = int(input())
36+
v = int(input())
37+
edges.append([u, v])
38+
39+
for i in range(m):
40+
print(f"Enter the success probability of edge {i+1}: ")
41+
p = float(input())
42+
succProb.append(p)
43+
44+
start = int(input("Enter the start vertex: "))
45+
end = int(input("Enter the end vertex: "))
46+
47+
# Create an instance of Solution
48+
solution = Solution()
49+
50+
# Calculate maximum probability
51+
maxProbability = solution.maxProbability(n, edges, succProb, start, end)
52+
53+
# Output the result
54+
print(f"The maximum probability from vertex {start} to vertex {end} is: {maxProbability}")
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
from collections import defaultdict, deque
2+
from typing import List
3+
4+
5+
class Solution:
6+
def maxProbability(self, n: int, edges: List[List[int]], succProb: List[float], start: int, end: int) -> float:
7+
graph = defaultdict(list)
8+
for i, (a, b) in enumerate(edges):
9+
graph[a].append([b, succProb[i]])
10+
graph[b].append([a, succProb[i]])
11+
12+
max_prob = [0.0] * n
13+
max_prob[start] = 1.0
14+
15+
queue = deque([start])
16+
while queue:
17+
cur_node = queue.popleft()
18+
for nxt_node, path_prob in graph[cur_node]:
19+
20+
# Only update max_prob[nxt_node] if the current path increases
21+
# the probability of reaching nxt_node.
22+
if max_prob[cur_node] * path_prob > max_prob[nxt_node]:
23+
max_prob[nxt_node] = max_prob[cur_node] * path_prob
24+
queue.append(nxt_node)
25+
26+
return max_prob[end]
27+
28+
29+
# Take input from the user
30+
n = int(input("Enter the number of vertices (n): "))
31+
m = int(input("Enter the number of edges (m): "))
32+
edges = []
33+
succProb = []
34+
35+
for i in range(m):
36+
print(f"Enter the source and destination vertices of edge {i+1}:")
37+
u = int(input())
38+
v = int(input())
39+
edges.append([u, v])
40+
41+
for i in range(m):
42+
print(f"Enter the success probability of edge {i+1}: ")
43+
p = float(input())
44+
succProb.append(p)
45+
46+
start = int(input("Enter the start vertex: "))
47+
end = int(input("Enter the end vertex: "))
48+
49+
# Create an instance of Solution
50+
solution = Solution()
51+
52+
# Calculate maximum probability
53+
maxProbability = solution.maxProbability(n, edges, succProb, start, end)
54+
55+
# Output the result
56+
print(f"The maximum probability from vertex {start} to vertex {end} is: {maxProbability}")

0 commit comments

Comments
 (0)