The River Problem:
You are a person trying to cross a river and reach the land on the other side, however you cannot swim.
There are rocks in the water which you can step on to allow you to cross the river, however you start at speed 0 and can only increase your speed by 1, decrease it by 1, or maintain the same speed for each step that you take. You cannot have a negative speed (go backwards).
Given a string representing rocks (“*”) and water (“~”) write a method to return True or False depending on whether any path exists across the river.
Examples River: *****~~*~~~*~~~~*~*~~*** -> true Steps: 011_2__3___4____5_____6 Success River: ****~~~~~~~~~~~~** -> false Steps: 01_2__3 Failure
This problem, courtesy of Sitong, was an interesting programming challenge we tried to tackle yesterday. The problem itself is a bit unique as it represents a sort of tree traversal problem where we have a series of possibilities (accelerate, decelerate, or maintain constant speed) at every step and search the tree for a valid solution. Given that it is a tree traversal problem, the most basic approach would be recursive.
I have always favored iteration over recursion when I program just because of the way my mind things, but recursive solutions tend to be easy to read and relatively short. However the catch with recursion I always am wary of is issues with run-time, but we’ll get to that problem later!
The solution that we both came up with (myself with a little prodding from Sitong) was one of tree traversal. My solution in ruby follows:
def can_cross_river?(river, loc, speed)
# base-case success if we reach landreturn true if loc + speed + 1 > river.length # If we landed on another rock, try 3 steps recursively # Accelerate first every time so we get the fastest path across if river[loc + speed + 1] == "*" return true if can_cross_river?(river, loc + speed + 1, speed + 1) end # If accelerating fails try maintaining speed for this step if speed > 0 && river[loc + speed] == "*" return true if can_cross_river?(river, loc + speed, speed) end # If maintaining speed fails try slowing down if speed > 1 && river[loc + speed - 1] == "*" return true if can_cross_river?(river, loc + speed - 1, speed - 1) end return false end
The code checks every path possible starting with the fastest and looks for the first successful run. However, when calculating the run time of this path we ran into some interesting caveats. The initial bound of run time when I first looked at the problem was O(3^n) because the problem recursively results in 3 possible choices for each path. For n choices this results in 3^n paths. However that is merely an upper-bound, as the tree depth shortens as we accelerate in speed and reach the goal faster. Therefore we will have significantly less than 3^n possible paths. I don’t think I can mathematically calculate this bound of worst-case but my intuition can be captured in the figure below:
The triangle represents a normal 3^n tree of possibilities but the grey shaded area represents the area of this problem as we accelerate when we take a right branch and reach a true or false conclusion faster. We cannot go slower than 1 speed so we don’t have any branches that go left from the origin.
There are other solutions for this problem and other optimizations including memoization or caching, however those also have issues. This problem is unique as for each sub-problem while we may face the same remainder of the river, we might approach this at a different speed. Thus caching will involve a huge problem space and questions of efficiency arise. I think this problem was great fodder for the mind however and provided a lot of interesting food for thought. I also asked David the same problem and saw a completely different approach to solving it, however we haven’t figured out the details on how to efficiently tackle the problem from his angle yet. Perhaps if we do I will write another post comparing his solution and this solution.