algorithmhaskelldynamic-programminglazy-evaluationtying-the-knot

Lazily Tying the Knot for 1 Dimensional Dynamic Programming


Several years ago I took an algorithms course where we were giving the following problem (or one like it):

There is a building of n floors with an elevator that can only go up 2 floors at a time and down 3 floors at a time. Using dynamic programming write a function that will compute the number of steps it takes the elevator to get from floor i to floor j.

This is obviously easy using a stateful approach, you create an array n elements long and fill it up with the values. You could even use a technically non-stateful approach that involves accumulating a result as recursively passing it around. My question is how to do this in a non-stateful manner by using lazy evaluation and tying the knot.


I think I've devised the correct mathematical formula:

f(i,j) = 0 when i is equal to j and f(i,j) = 1 + min of f(i+2,j) and f(i-3,j)

where i+2 and i-3 are within the allowed values.

Unfortunately I can't get it to terminate. If I put the i+2 case first and then choose an even floor I can get it to evaluate the even floors below the target level but that's it. I suspect that it shoots straight to the highest even floor for everything else, drops 3 levels, then repeats, forever oscillating between the top few floors.

So it's probably exploring the infinite space (or finite but with loops) in a depth first manner. I can't think of how to explore the space in a breadth first fashion without using a whole lot of data structures in between that effectively mimic a stateful approach.


Although this simple problem is disappointingly difficult I suspect that having seen a solution in 1 dimension I might be able to make it work for a 2 dimensional variation of the problem.


EDIT: A lot of the answers tried to solve the problem in a different way. The problem itself isn't interesting to me, the question is about the method used. Chaosmatter's approach of creating a minimal function which can compare potentially infinite numbers is possibly a step in the right direction. Unfortunately if I try to create a list representing a building with 100 floors the result takes too long to compute, since the solutions to sub problems are not reused.

I made an attempt to use a self-referencing data structure but it doesn't terminate, there is some kind of infinite loop going on. I'll post my code so you can understand what it is I'm going for. I'll change the accepted answer if someone can actually solve the problem using dynamic programming on a self-referential data structure using laziness to avoid computing things more than once.

levels = go [0..10]
  where
    go [] = []
    go (x:xs) = minimum
      [ if i == 7
          then 0
          else 1 + levels !! i
        | i <- filter (\n -> n >= 0 && n <= 10) [x+2,x-3] ]
      : go xs

You can see how 1 + levels !! i tries to reference the previously calculated result and how filter (\n -> n >= 0 && n <= 10) [x+2,x-3] tries to limit the values of i to valid ones. As I said, this doesn't actually work, it simply demonstrates the method by which I want to see this problem solved. Other ways of solving it are not interesting to me.


Solution

  • Since you're trying to solve this in two dimensions, and for other problems than the one described, let's explore some more general solutions. We are trying to solve the shortest path problem on directed graphs.

    Our representation of a graph is currently something like a -> [a], where the function returns the vertices reachable from the input. Any implementation will additionally require that we can compare to see if two vertices are the same, so we'll need Eq a.

    The following graph is problematic, and introduces almost all of the difficulty in solving the problem in general:

    problematic 1 = [2]
    problematic 2 = [3]
    problematic 3 = [2]
    problematic 4 = []
    

    When trying to reach 4 from 1, there are is a cycle involving 2 and 3 that must be detected to determine that there is no path from 1 to 4.

    Breadth-first search

    The algorithm Will presented has, if applied to the general problem for finite graphs, worst case performance that is unbounded in both time and space. We can modify his solution to attack the general problem for graphs containing only finite paths and finite cycles by adding cycle detection. Both his original solution and this modification will find finite paths even in infinite graphs, but neither is able to reliably determine that there is no path between two vertices in an infinite graph.

    acyclicPaths :: (Eq a) => (a->[a]) -> a -> a -> [[a]]
    acyclicPaths steps i j = map (tail . reverse) . filter ((== j).head) $ queue
      where
        queue = [[i]] ++ gen 1 queue
        gen d _ | d <= 0 = []
        gen d (visited:t) = let r = filter ((flip notElem) visited) . steps . head $ visited 
                            in map (:visited) r ++ gen (d+length r-1) t
    
    shortestPath :: (Eq a) => (a->[a]) -> a -> a -> Maybe [a]
    shortestPath succs i j = listToMaybe (acyclicPaths succs i j)
    

    Reusing the step function from Will's answer as the definition of your example problem, we could get the length of the shortest path from floor 4 to 5 of an 11 story building by fmap length $ shortestPath (step 11) 4 5. This returns Just 3.

    Let's consider a finite graph with v vertices and e edges. A graph with v vertices and e edges can be described by an input of size n ~ O(v+e). The worst case graph for this algorithm is to have one unreachable vertex, j, and the remaining vertexes and edges devoted to creating the largest number of acyclic paths starting at i. This is probably something like a clique containing all the vertices that aren't i or j, with edges from i to every other vertex that isn't j. The number of vertices in a clique with e edges is O(e^(1/2)), so this graph has e ~ O(n), v ~ O(n^(1/2)). This graph would have O((n^(1/2))!) paths to explore before determining that j is unreachable.

    The memory required by this function for this case is O((n^(1/2))!), since it only requires a constant increase in the queue for each path.

    The time required by this function for this case is O((n^(1/2))! * n^(1/2)). Each time it expands a path, it must check that the new node isn't already in the path, which takes O(v) ~ O(n^(1/2)) time. This could be improved to O(log (n^(1/2))) if we had Ord a and used a Set a or similar structure to store the visited vertices.

    For non-finite graphs, this function should only fail to terminate exactly when there doesn't exists a finite path from i to j but there does exist a non-finite path from i to j.

    Dynamic Programming

    A dynamic programming solution doesn't generalize in the same way; let's explore why.

    To start with, we'll adapt chaosmasttter's solution to have the same interface as our breadth-first search solution:

    instance Show Natural where
        show = show . toNum 
    
    infinity = Next infinity
    
    shortestPath' :: (Eq a) => (a->[a]) -> a -> a -> Natural
    shortestPath' steps i j = go i
        where
            go i | i == j = Zero
                 | otherwise = Next . foldr minimal infinity . map go . steps $ i
    

    This works nicely for the elevator problem, shortestPath' (step 11) 4 5 is 3. Unfortunately, for our problematic problem, shortestPath' problematic 1 4 overflows the stack. If we add a bit more code for Natural numbers:

    fromInt :: Int -> Natural
    fromInt x = (iterate Next Zero) !! x    
    
    instance Eq Natural where
        Zero == Zero         = True
        (Next a) == (Next b) = a == b
        _ == _ = False
    
    instance Ord Natural where
        compare Zero Zero         = EQ
        compare Zero _            = LT
        compare _ Zero            = GT
        compare (Next a) (Next b) = compare a b
    

    we can ask if the shortest path is shorter than some upper bound. In my opinion, this really shows off what's happening with lazy evaluation. problematic 1 4 < fromInt 100 is False and problematic 1 4 > fromInt 100 is True.

    Next, to explore dynamic programming, we'll need to introduce some dynamic programming. Since we will build a table of the solutions to all of the sub-problems, we will need to know the possible values that the vertices can take. This gives us a slightly different interface:

    shortestPath'' :: (Ix a) => (a->[a]) -> (a, a) -> a -> a -> Natural
    shortestPath'' steps bounds i j = go i
        where
            go i = lookupTable ! i
            lookupTable = buildTable bounds go2
            go2 i | i == j = Zero
                  | otherwise = Next . foldr minimal infinity . map go . steps $ i
    
    -- A utility function that makes memoizing things easier
    buildTable :: (Ix i) => (i, i) -> (i -> e) -> Array i e
    buildTable bounds f = array bounds . map (\x -> (x, f x)) $ range bounds
    

    We can use this like shortestPath'' (step 11) (1,11) 4 5 or shortestPath'' problematic (1,4) 1 4 < fromInt 100. This still can't detect cycles...

    Dynamic programming and cycle detection

    The cycle detection is problematic for dynamic programming, because the sub-problems aren't the same when they are approached from different paths. Consider a variant of our problematic problem.

    problematic' 1 = [2, 3]
    problematic' 2 = [3]
    problematic' 3 = [2]
    problematic' 4 = []
    

    If we are trying to get from 1 to 4, we have two options:

    If we choose to explore 2, we will be faced with the following option:

    We want to combine the two explorations of the shortest path from 3 to 4 into the same entry in the table. If we want to avoid cycles, this is really something slightly more subtle. The problems we faced were really:

    After choosing 2

    These two questions about how to get from 3 to 4 have two slightly different answers. They are two different sub-problems which can't fit in the same spot in a table. Answering the first question eventually requires determining that you can't get to 4 from 2. Answering the second question is straightforward.

    We could make a bunch of tables for each possible set of previously visited vertices, but that doesn't sound very efficient. I've almost convinced myself that we can't do reach-ability as a dynamic programming problem using only laziness.

    Breadth-first search redux

    While working on a dynamic programming solution with reach-ability or cycle detection, I realized that once we have seen a node in the options, no later path visiting that node can ever be optimal, whether or not we follow that node. If we reconsider problematic':

    If we are trying to get from 1 to 4, we have two options:

    This gives us an algorithm to find the length of the shortest path quite easily:

    -- Vertices first reachable in each generation
    generations :: (Ord a) => (a->[a]) -> a -> [Set.Set a]
    generations steps i = takeWhile (not . Set.null) $ Set.singleton i: go (Set.singleton i) (Set.singleton i)
        where go seen previouslyNovel = let reachable = Set.fromList (Set.toList previouslyNovel >>= steps)
                                            novel = reachable `Set.difference` seen
                                            nowSeen = reachable `Set.union` seen
                                        in novel:go nowSeen novel
    
    lengthShortestPath :: (Ord a) => (a->[a]) -> a -> a -> Maybe Int
    lengthShortestPath steps i j = findIndex (Set.member j) $ generations steps i
    

    As expected, lengthShortestPath (step 11) 4 5 is Just 3 and lengthShortestPath problematic 1 4 is Nothing.

    In the worst case, generations requires space that is O(v*log v), and time that is O(v*e*log v).