-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathDynamic Programming vs Greedy vs Divide & Conquer
More file actions
42 lines (36 loc) · 2.98 KB
/
Dynamic Programming vs Greedy vs Divide & Conquer
File metadata and controls
42 lines (36 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
StackOverflow: http://stackoverflow.com/questions/16690249/what-is-the-main-difference-between-dynamic-programming-and-greedy-approach-in-t
http://stackoverflow.com/questions/13713572/how-is-dynamic-programming-different-from-greedy-technique
Divide & Conquer vs DP vs Gredy: http://stackoverflow.com/questions/6162465/divide-and-conquer-dynamic-programming-and-greedy-algorithms
Divide & Conquer vs DP: http://stackoverflow.com/questions/13538459/difference-between-divide-and-conquer-algo-and-dynamic-programming
Examples of Divide & Conquer:
SEARCHING ALGORITHM:
1.Binary Search
SORTING ALGORITHMS:
1. MergeSort
2. QuickSort
Examples of Dynamic Programming:
1. BellmanFord Algorithm for Single Source Shortest Path Calculation(It gives path from single source to all the other nodes)
[Youtube Video: https://www.youtube.com/watch?v=iTW2yFYd1Nc&list=PLATr4Ip40BjYUgHgTKe5ZJ_hl-ccCeAai]
2. Floyd Warshall Algorithm for All Pair Shorted Path Calculation(It gives path from all nodes to all other nodes)
[Youtube Video: https://www.youtube.com/watch?v=odeFemb3o-o]
Examples of Greedy:
1. Dijkstra's algorithm for Single Source Shortest Path Calculation(It gives path from single source to a single destination node)
2. Prim's Algorithm to find Minimum Spanning Tree
3. Kruskal's Algorithm to find Minimum Spanning Tree
(Difference between Prims and Kruskals Algorithm: http://www.quora.com/What-is-the-difference-in-Kruskals-and-Prims-algorithm)
Q: Given the above differences, how is Kruskal's algorithm considered as a greedy approach ?
[It is said that Kruskal's algorithm for MST construction is greedy, but the algorithm chooses global minimum and
instead of local minimum unlike Prim's algorithm. Can someone explain how Kruskal's algorithm
is considered a greedy approach ?]
Source: http://stackoverflow.com/questions/27639942/how-is-kruskals-algorithm-greedy/27640057#27640057
Ans: In Kruskal's algorithm, we sort all the edges in the graph and iteratively choose minimum edge during each iteration
to check whether it can be added to the MST (doesn't form cycle).
Thus, once an edge is selected and added in the MST, no further changes are made during the remaining other iterations.
If you compare this with Bellman-Ford algorithm for Shortest Path, during each iteration we get the shortest path, and during
other remaining iterations we check to see if there are any shortest paths that can replace the previously calculated
shortest path. Thus, here we are modifying the previous results and taking into consideration the results of all iterations,
that is the shortest paths from all the nodes (since no of iterations = (no of nodes in graph) - 1).
This, is the reason why Bellman Ford is Dynamic and Kruskal's is Greedy
[NOTE: Though Bellman Ford is for Single Source Shortest Path and Kruskal's is for MST, hence once might consider that
these two cannot be compared together, though concentrate that the solution is not compared but the approach of algorithm
is compared to understand difference between DYNAMIC vs GREEDY]