Path With Maximum Probability

Moderate
0/80
Average time to solve is 25m
profile
Contributed by
2 upvotes
Asked in companies
MAQ SoftwareTechwave Consulting

Problem statement

You are given an undirected unweighted graph and an array 'sProability' which denotes the probability of traversing edges such that 'sProability[i]' denotes the probability of traversing ith edge. You are given the start and end vertex, You need to determine the maximum path probability on going from start to end vertex if there is no path from start to end return 0.

Detailed explanation ( Input/output format, Notes, Images )
Input Format
The first line of input contains an integer 'T', the number 
of test cases.

The first line of the test case contains four integers ‘N’, 
‘M’, 'START', 'END' denoting the number of vertices, edges, starting vertex, and ending vertex.

The next ‘M’ lines describe the edge. Each edge is 
characterized by two integers ‘A’ and ‘B’ where ‘A’ and ‘B’ denotes the endpoints of the edge.

The last line of each test case contains ‘M’ space-separated floating-point number denoting the probability of traversing ith edge.

The edges[i][0], edges[i][1] contains the vertex that is 
connected to the edge.
Output Format
Return the maximum probability of path from start to end vertex up to 6 decimal places. If there is no path, return 0.
Note:
Graph does not contain self-loops.
Constraints
1 <= T <= 10
1 <= N <= 5 * 10 ^ 4
1 <= M <= min((N * (N - 1) / 2),10^5)
0 <= VERTEX VALUE, START, END <= N - 1
0 <= sProability[i] <= 1

Time Limit: 1 second
Sample Input 1
1
3 3 0 2
0 1
1 2
0 2
0.9 0.9 0.75
Sample Output 1
0.810000
Explanation for Sample Output 1:
For the test case 1:

The graph is as follows:

Path with maximum probability from 0 to 1 is 0->2->1. Hence the maximum probability is 0.90 * 0.90 = 0.81.
Sample Input 2
1
4 2 0 2
0 1
2 3
0.8 0.8
Sample Output 2
0.000000
Hint

Find all the paths from start to end.

Approaches (2)
DFS Approach

Traverse all the paths from start to end and then one with maximum probability is the answer.

 

Algorithm :

 

  • Create a boolean visited array. Start the depth-first search from the ‘start’ vertex such that it will give maximum probability from start to end.
  • If current vertex equal to end return 1.
  • Mark the current vertex true in the ‘visited’ array. Now iterate all the children of the current node such that it will give maximum probability from the current child’s vertex to end. Then multiply this with a probability of edge from the current node to children. If it is greater than the answer, replace the answer with the value.
  • After iterating all children mark the current node false in the visited array.
  • Return the answer.
Time Complexity

O(M ^ 2), Where 'M' is the number of edges.

 

In the worse case, we need to traverse every edge ‘M’ times. Hence time complexity will be O(M ^ 2).

Space Complexity

O(N), Where 'N' is the number of vertices.

 

It is because the boolean array, ‘visited’ takes O(N) space.

Code Solution
(100% EXP penalty)
Path With Maximum Probability
Full screen
Console