Skip to content

1514. Path with Maximum Probability #415

Answered by topugit
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

We can use a modified version of Dijkstra's algorithm. Instead of finding the shortest path, you'll be maximizing the probability of success.

Let's implement this solution in PHP: 1514. Path with Maximum Probability

<?php
function maxProbability($n, $edges, $succProb, $start_node, $end_node) {
    // Create an adjacency list to represent the graph
    $graph = array_fill(0, $n, []);
    foreach ($edges as $i => $edge) {
        list($a, $b) = $edge;
        $graph[$a][] = [$b, $succProb[$i]];
        $graph[$b][] = [$a, $succProb[$i]];
    }

    // Initialize the maximum probabilities array
    $maxProb = array_fill(0, $n, 0.0);
    $maxProb[$start_node] = 1.0;

    // Use a max heap (pr…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@mah-shamim
Comment options

mah-shamim Aug 27, 2024
Maintainer Author

@topugit
Comment options

topugit Aug 27, 2024
Collaborator

Answer selected by mah-shamim
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
2 participants