2

I'm taking a Java 2 course and trying to study but I don't understand this concept:

  • What is the difference between recursive programming calls and dynamic programming calls?

  • What would be an example of each?

JavaJew22
  • 83
  • 1
  • 5

4 Answers4

3

The two concept are rather different:

  • A function is called recursive if it calls itself.

  • Dynamic programming is a technique for solving problems. It involves first solving a smaller sub-problem and then extending the solutions to a solution for the overall problem.

It is often the case the dynamic programming algorithm can be expressed recursively.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
2

Recursive solutions solve the problems in a Top-Down way, while Dynamic solutions solve them in a Bottom-Up way, taking advantage that optimal solutions to sub-problems are previously pre-calculated. Not always you can perform Dynamic Programming, you need to ensure that optimal solutions to sub-problems are also optimal solution the global problem.

A classic example is the calculation of the n-th Fibonacci number:

-Recursive

Fib(n)
  if (n <= 2) return 1
  return Fib(n-1) + Fib(n-2)

-Dynamic

Fib(n)
 previous = 1
 next = 1
 temp = 0
 for i = 2 to n do
   temp = previous
   previous = next
   next = temp + next
 return next

This is a pseudo-code, The first one repeats calls and the second one is based on previous cases

Robin Curbelo
  • 1,323
  • 1
  • 11
  • 21
  • Thank you everyone! This thread really helped me ace the concept! :D I didn't know there was a big difference between the two without your explanations – JavaJew22 Mar 19 '13 at 18:30
1

"Devide and Conquer" concept algorithms are consider dynamic and recursive both e.g sorting in lists/arrays where you split the item in 2 parts and call sort function in each part again... and so on

apomene
  • 14,282
  • 9
  • 46
  • 72
1

Recursive code, is code that calls itself, dynamic code is code that programs and then calls itself.

I'd need some context to try and figure out what whoever wrote the course is trying to say. Dynamic code can be recursive and recursive code can be dynamic, so I find the intent of your question confusing.

Tony Hopkinson
  • 20,172
  • 3
  • 31
  • 39