Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why mergesort is not Dynamic programming

I have read these words:

There are two key attributes that a problem must have in order for dynamic programming to be applicable: optimal substructure and overlapping subproblems. If a problem can be solved by combining optimal solutions to non-overlapping subproblems, the strategy is called "divide and conquer". This is why mergesort and quicksort are not classified as dynamic programming problems.

I have the 3 questions:

  1. Why mergesort and quicksort is not Dynamic programming? I think mergesort also can be divided small problems and small problems then do the same thing and so on.
  2. Is Dijkstra Algorithm using dynamic algorithm?
  3. Are there applied examples of using Dynamic programming?
like image 763
Zhong Yang Avatar asked Mar 24 '13 08:03

Zhong Yang


People also ask

Is Mergesort dynamic programming?

If a problem can be solved by combining optimal solutions to non-overlapping subproblems, the strategy is called "divide and conquer". This is why mergesort and quicksort are not classified as dynamic programming problems.

Why is Mergesort not in-place?

Merge sort is not in place because it requires additional memory space to store the auxiliary arrays. The quick sort is in place as it doesn't require any additional storage.

Can Mergesort be stable?

Unlike some (efficient) implementations of quicksort, merge sort is a stable sort. Merge sort's most common implementation does not sort in place; therefore, the memory size of the input must be allocated for the sorted output to be stored in (see below for variations that need only n/2 extra spaces).

Is divide and conquer dynamic programming?

The difference between divide and conquer and dynamic programming is that the former is a method of dividing a problem into smaller parts and then solving each one separately, while the latter is a method of solving larger problems by breaking them down into smaller pieces.


1 Answers

  1. The key words here are "overlapping subproblems" and "optimal substructure". When you execute quicksort or mergesort, you are recursively breaking down your array into smaller pieces that do not overlap. You never operate over the same elements of the original array twice during any given level of the recursion. This means there is no opportunity to re-use previous calculations. On the other hand, many problems DO involve performing the same calculations over overlapping subsets, and have the useful characteristic that an optimal solution to a subproblem can be re-used when computing the optimal solution to a larger problem.

  2. Dijkstra's algorithm is a classic example of dynamic programming, as it re-uses prior computations to discover the shortest path between two nodes A and Z. Say that A's immediate neighbors are B and C. We can find the shortest path from A to Z by summing the distance between A and B with our computed shortest path from B to Z; and do similarly for finding the shortest path from C to Z. Then the shortest path from A to Z will be the shorter of these two paths. The key insight here is that we can re-use the shortest path computations for paths of length 2 when computing the shortest paths of length 3, and so on. Doing so results in a much more efficient algorithm.

  3. Dynamic programming can be used to solve many types of problems -- see http://en.wikipedia.org/wiki/Dynamic_programming#Examples:_Computer_algorithms for some examples.

like image 102
erlandsson Avatar answered Oct 14 '22 06:10

erlandsson