Azzera filtri
Azzera filtri

find all pathbetween 2 node under constraint

2 visualizzazioni (ultimi 30 giorni)
shalom kobi
shalom kobi il 30 Mar 2021
Risposto: Pratik il 11 Giu 2024
hey
i want to find all path between A to B ( start from left buttom)
i have tryed to use meshgrid and allpath command without succses
the constraint are the i can go right or up only
also, i need to write code that calculates the probability on each node to go up or right if given that all route is equal probability
i will appreciate hint how to write my code and which commands to use
here is the mesh

Risposte (1)

Pratik
Pratik il 11 Giu 2024
Hi Shalom,
From what I can understand, we need to find all possible upward or rightward paths from the bottom-left corner to the top-right corner of a grid, and to calculate the probability of choosing to move right or up at any node, assuming each path is equally likely.
The problem can be approached using dynamic programming,
  • We’ll create a matrix to represent the grid, where each cell represents a node.
  • Initialize the first row and first column with 1 (since there’s only one way to reach any cell in the first row or column).
  • For each cell (except the first row and first column), the number of ways to reach it is the sum of the ways to reach the cell above it and the cell to its left.
  • Finally, the value in the last cell (B) will give you the total number of paths from A to B.
  • We can then backtrack to find all the paths.
I hope this helps!

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by