简体   繁体   中英

Divide and conquer, dynamic programming and greedy algorithms!

When I have a problem with optimal substructur and no subproblem shares subsubproblems then I can use a divide and conquer algorithm to solve it?

But when the subproblem shares subsubproblems (overlapping subproblems) then I can use dynamic programming to solve the problem?

Is this correct?

And how is greedy algorithms similar to dynamic programming?

When I have a problem with optimal substructur and no subproblem shares subsubproblems then I can use a divide and conquer algorithm to solve it?

Yes, as long as you can find an optimal algorithm for each kind of subproblem.

But when the subproblem shares subsubproblems (overlapping subproblems) then I can use dynamic programming to solve the problem?

Is this correct?

Yes. Dynamic programming is basically a special case of the family of Divide & Conquer algorithms, where all subproblems are the same.

And how is greedy algorithms similar to dynamic programming?

They're different.
Dynamic programming gives you the optimal solution.
A Greedy algorithm usually give a good/fair solution in a small amount of time but it doesn't assure to reach the optimum.

It is, let's say , similar because it usually divides the solution construction in several stages in which it takes choices that are locally optimal. But if stages are not optimal substructures of the original problem, then normally it doesn't lead to the best solution.

EDIT:

As pointed out by @rrenaud, there are some greedy algorithms that have been proven to be optimal (eg Dijkstra, Kruskal, Prim etc.).
So, to be more correct, the main difference between greedy and dynamic programming is that the former is not exhaustive on the space of solutions while the latter is.
In fact greedy algorithms are short-sighted on that space, and each choice made during solution construction is never reconsidered.

Dynamic program uses bottom-up approach, saves the previous solution and refer it, this will allow us to make optimal solution among all available solutions, whereas greedy approach uses the top-down approach, so it takes an optimal solution from the locally available solution, will not take the previous level solutions which leads to the less optimized solution. Dynamic= bottom up, optimal solution Greedy= top down, less optimal, less time consuming

Greedy approach works in top-down fashion but dynamic works in bottom-up fashion. So greedy approach may give a solution that is not optimal, it can be sub optimal(close to optimal) if we use dynamic approach we have to keep all previous solutions as well but in greedy we take a best choice at each moment without concerning about previous one. Here's the difference, that's why we can get a solution that is not optimal.

See the link http://en.wikipedia.org/wiki/File:Greedy-search-path-example.gif

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM