C
C#2y ago
Theos

❔ Maximum path sum in matrix

Given a M x N grid filled with some values I need to find a path from a target node which maximizes the sum of all values along its path. I can move in any direction. How can I do this? I've found some solutions online but they all go only from the top row to the bottom row when I need to go in all directions from any node.
50 Replies
Theos
TheosOP2y ago
also there is a max length of the path
HimmDawg
HimmDawg2y ago
What do you have so far?
Theos
TheosOP2y ago
not much, can't figure out how to do this
HimmDawg
HimmDawg2y ago
There might exists some clever algorithm or heuristic to find the 100% correct path. But if you have no clue on how to go about it, you should start with the easiest solution Which would be: Just try out every path and track the one with the highest sum 😄
Theos
TheosOP2y ago
that wont work my grid is huge and max lenght of the path ranges from 50 to 100 so bruteforcing it wont work
HimmDawg
HimmDawg2y ago
Hmm, then that seems like something that dynamic programming could solve fluffyFoxThink lemme think So here's what I think: We start with finding the max sum path for length 1 Which is just the maximum value of the grid Ah, question: Does any direction include diagonally?
Theos
TheosOP2y ago
yep
HimmDawg
HimmDawg2y ago
Then you check all 8 neighbours of that cell. You take the neighbour with the highest value to get the max sum path of length 2. This is the first thing that came into my mind. Pretty sure this approach wouldn't work if e.g. the max value of the grid is 10, this is surrounded by 1's and everything else is a 9. And we try to get a path length of 2 But maybe we can work from there
Theos
TheosOP2y ago
i was thinking about using breadth first search algorithm
Theos
TheosOP2y ago
Theos
TheosOP2y ago
what u described would fail in this scenario if path length = 2 then it will go from yellow to 1 and then to the next 1
HimmDawg
HimmDawg2y ago
But the max is 9, so my approach would start there
Theos
TheosOP2y ago
nah, we dont start from the highest value we start from a predefined cell so in this example grid[2,2]
HimmDawg
HimmDawg2y ago
Oh, i thought we just need to find the max sum path
Theos
TheosOP2y ago
nope yeah, i didnt say it earlier, mb
HimmDawg
HimmDawg2y ago
You should add this to the description then 😄
Theos
TheosOP2y ago
one sec
HimmDawg
HimmDawg2y ago
I suppose the conditions are set in a way that you can find the max sum path guaranteed?
Theos
TheosOP2y ago
wdym?
HimmDawg
HimmDawg2y ago
Because if we take your example, then what would happen if the path length doesn't reach the outer ring of the matrix.
Theos
TheosOP2y ago
yeah, dont worry about that
HimmDawg
HimmDawg2y ago
Aight
Moods
Moods2y ago
Okay so What’s the goal When does the path start and stop Is there a predetermined length or you’re going from the top left to the bottom right
Theos
TheosOP2y ago
when path reaches n length
Moods
Moods2y ago
So you have a given starting point right? And you want to look for the biggest number possible in any path starting from it with length of n Sounds like dynamic programming
Theos
TheosOP2y ago
exactly
Moods
Moods2y ago
Because you’d have to explore all possible paths and then return the biggest of them all Can you provide an illustration of the problem?
Theos
TheosOP2y ago
let me draw one ;p one sec
Theos
TheosOP2y ago
Theos
TheosOP2y ago
so max length of the path = 6 so this path's value = 18
Theos
TheosOP2y ago
Theos
TheosOP2y ago
and this path's value = 21 so thats the best path for this given grid and path length
Moods
Moods2y ago
So the initial cell doesn’t contribute to the count? And you said maximum path, that makes me curious, can negative numbers be included here?
Theos
TheosOP2y ago
nope, cell's value is a random int from 0 to 100 nope but i guess if it would make the problem easier then we can give the initial cell a value of 1 and make it contribute to the count
Moods
Moods2y ago
I don’t think it’d change anything, I just asked because if it counted then the length would be 7 for each path instead of 6
Theos
TheosOP2y ago
👍
Moods
Moods2y ago
Okay so what solution/approach do you have in mind already
Theos
TheosOP2y ago
none right now, I had a few ideas but i dropped them cuz they would be just too slow
Moods
Moods2y ago
I think with how non-restrictive this problem is that may not be unavoidable The issue is that your movement is not restricted, you could move in any direction whatsoever, that also means considering a single path you have to be aware of the cells you already hit Usually you’d start from the top left and they’d say some shit like you can only move right or down. I’m not saying this isn’t unsolvable but you may have to sacrifice speed here
Theos
TheosOP2y ago
okay so just a random idea but what if we would divide the grid into 4 parts
Theos
TheosOP2y ago
Theos
TheosOP2y ago
something like this and for each part lets say, top right one we find a max value in it which is 9 and then
Theos
TheosOP2y ago
Theos
TheosOP2y ago
from the max value to the origin point we create this "sub grid" and then we try to find the best path from bottom left corner to the top left corner with only possible moves being up, right, up-right well, we wont be able to find 100% best path but i think that will be enough for my project
Theos
TheosOP2y ago
GeeksforGeeks
Maximum sum path in a matrix from top-left to bottom-right - Geeksf...
A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
Theos
TheosOP2y ago
and we would do this for each of these 4 parts @moooodie
Moods
Moods2y ago
Uh The link you posted is a different thing no? That’s from top left to bottom right How would doing it for each grid work
Theos
TheosOP2y ago
yep i would use it for the sub grid this thingy
Moods
Moods2y ago
Yeah but how would that get you to your answer
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?