0
$\begingroup$

I'm trying to devise an algorithm for the following prompt from LeetCode's daily challenge:

You are given an undirected weighted graph of n nodes (0-indexed), represented by an edge list where edges[i] = [a, b] is an undirected edge connecting the nodes a and b with a probability of success of traversing that edge succProb[i].

Given two nodes start and end, find the path with the maximum probability of success to go from start to end and return its success probability.

If there is no path from start to end, return 0. Your answer will be accepted if it differs from the correct answer by at most 1e-5.

Example: n = 3, edges = [[0,1],[1,2],[0,2]], succProb = [0.5,0.5,0.2], start = 0, end = 2 Output: 0.25

Here is my algorithm:

def maxProbability(self, n: int, edges: List[List[int]], succProb: List[float], start_node: int, end_node: int) -> float:
        graph = collections.defaultdict(list)
        for i in range(len(edges)):
            graph[edges[i][0]].append((edges[i][1], succProb[i]))
            graph[edges[i][1]].append((edges[i][0], succProb[i]))

        def dfs(node, seen, curr_prob):
            if node == end_node:
                ans.append(curr_prob)

            for neighbour, weight in graph[node]:
                if neighbour not in seen:
                    seen.add(neighbour)
                    dfs(neighbour, seen, curr_prob * weight)
                    seen.remove(neighbour)
        
        ans = []
        dfs(start_node, {start_node}, 1)
        return max(ans) if ans else 0

This algorithm is returning the correct answer for the smaller test cases, but is timing out on the larger ones. Could you please help me

(i) Analyze the time complexity of this

(ii) Improve it

For (ii), I feel like a lot of unnecessary work is being repeated - if I reach a node $a$, I should probably memoize the probability of reaching the end_node from $a$. I will try this.

For part (i), we are doing $O(e)$ work to create graph. Then, my confusion is, the DFS here isn't exactly running once per node in the graph. In some cases it is running multiple times per node. So is it right to say that the DFS part runs in $O(n)$.

$\endgroup$
4
  • $\begingroup$ Edit: I tried using DP. I think this will work, but the only thing I need to do is memoize a set. Anyone know how that works? $\endgroup$ Commented Aug 28, 2024 at 1:09
  • $\begingroup$ Thank you! We're not a coding site, so we generally don't analyze your code for you. Not everyone here understands Python or the running time of basic Python operations (e.g., append, etc.), so we encourage you to use concise pseudocode rather than an actual implementation. $\endgroup$ Commented Aug 28, 2024 at 17:41
  • $\begingroup$ cs.stackexchange.com/q/19207/755, cs.stackexchange.com/a/152204/755 $\endgroup$ Commented Aug 28, 2024 at 17:45
  • $\begingroup$ Thank you very much! $\endgroup$ Commented Aug 30, 2024 at 11:26

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.