Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic programming in the functional paradigm

Tags:

I'm looking at Problem thirty one on Project Euler, which asks, how many different ways are there of making £2 using any number of coins of 1p, 2p, 5p, 10p, 20p, 50p, £1 (100p) and £2 (200p).

There are recursive solutions, such as this one in Scala (credit to Pavel Fatin)

def f(ms: List[Int], n: Int): Int = ms match {   case h :: t =>     if (h > n) 0 else if (n == h) 1 else f(ms, n - h) + f(t, n)   case _ => 0 }  val r = f(List(1, 2, 5, 10, 20, 50, 100, 200), 200) 

and although it runs fast enough, it's relatively inefficient, calling the f function around 5.6 million times.

I saw someone else's solution in Java which was programmed dynamically (credit to wizeman from Portugal)

final static int TOTAL = 200;  public static void main(String[] args) {     int[] coins = {1, 2, 5, 10, 20, 50, 100, 200};     int[] ways = new int[TOTAL + 1];     ways[0] = 1;      for (int coin : coins) {         for (int j = coin; j <= TOTAL; j++) {             ways[j] += ways[j - coin];         }     }      System.out.println("Result: " + ways[TOTAL]); } 

This is much more efficient and passes the inner loop only 1220 times.

While I could obviously translate this more or less verbatim into Scala using Array objects, is there an idiomatic functional way to do this using immutable data structures, preferably with similar conciseness and performance?

I have tried and become stuck trying to recursively update a List before deciding I'm probably just approaching it the wrong way.

like image 885
Luigi Plinge Avatar asked Jul 10 '11 03:07

Luigi Plinge


People also ask

Is functional programming dynamic programming?

Dynamic typing, a type system, is orthogonal to 'functional', a programming paradigm. Dynamic 'languages' are actually dynamically typed. This means that you don't have compile-time checking of your variable types. Functional languages offer loads of support for e.g. lambda calculus - anonymous functions.

Is dynamic programming a paradigm?

Dynamic Programming is an algorithmic paradigm that solves a given complex problem by breaking it into sub-problems and stores the results of sub-problems to avoid computing the same results again.

What is the functional programming paradigm?

Functional programming is a programming paradigm in which we try to bind everything in pure mathematical functions style. It is a declarative type of programming style. Its main focus is on “what to solve” in contrast to an imperative style where the main focus is “how to solve”.

What are the four main components of the functional paradigm?

An illustration of Parson's four-function paradigm: (a) latent pattern-maintenance, (b) integration, (c) adaption, and (d) goal attainment. The four functions of actions form a general paradigm for all system.


2 Answers

Whenever some part of a list of data is computed based on a previous element, I think of Stream recursion. Unfortunately, such recursion cannot happen inside method definitions or functions, so I had to turn a function into a class to make it work.

class IterationForCoin(stream: Stream[Int], coin: Int) {   val (lower, higher) = stream splitAt coin   val next: Stream[Int] = lower #::: (higher zip next map { case (a, b) => a + b }) } val coins = List(1, 2, 5, 10, 20, 50, 100, 200) val result = coins.foldLeft(1 #:: Stream.fill(200)(0)) { (stream, coin) =>   new IterationForCoin(stream, coin).next } last 

The definitions of lower and higher are not necessary -- I could easily replace them with stream take coin and stream drop coin, but I think it's a little clearer (and more efficient) this way.

like image 132
Daniel C. Sobral Avatar answered Dec 06 '22 17:12

Daniel C. Sobral


I don't know enough about Scala to comment specifically on that, but the typical way to translation a DP solution in to a recursive one is to memoization (use http://en.wikipedia.org/wiki/Memoization). This is basically caching the result of your function for all values of the domain

I found this as well http://michid.wordpress.com/2009/02/23/function_mem/. HTH

like image 34
dfb Avatar answered Dec 06 '22 16:12

dfb