Professional Documents
Culture Documents
Unit3 - DC - DP - Greedy
Unit3 - DC - DP - Greedy
Greedy Techniques
Dynamic Programming
Dr R Manimegalai
Professor and Head / CSE
Divide and Conquer Strategy
https://www.geeksforgeeks.org/quick-sort/
Stable Sorting Algorithms
• A Stable Sort is one which preserves the original order of input set, where the
comparison algorithm does not distinguish between two or more items. A Stable
Sort will guarantee that the original order of data having the same rank is preserved
in the output
• A sorting algorithm is said to be stable if two objects with equal keys appear in the
same order in sorted output as they appear in the input unsorted array.
– Stable Sorting Algorithms : Insertion Sort, Merge Sort and Bubble Sort .
– Not stable : Quick Sort, Heap Sort.
Inplace Sorting Algorithms
• An In-Place Sorting Algorithm directly modifies the list that is
received as input instead of creating a new list that is then modified;
small amount of extra space it uses to manipulate the input set.
– In-Place, Sorting Algorithm updates input only through replacement or
swapping of elements.
• Bubble sort, insertion sort, and selection sort are in-place sorting
algorithms. Because only swapping of the element in the input array is
required.
• Bubble sort and insertion sort are stable algorithms but selection sort
is not
In-place vs. Stable Sorting Algorithms
Two main properties of a problem that suggests that the given problem can
be solved using Dynamic programming:
1) Overlapping Sub-problems
2) Optimal Substructure
• Overlapping Sub-problems
– Like Divide and Conquer, Dynamic Programming combines solutions to
sub-problems
– Dynamic Programming is mainly used when solutions of the same
sub-problems are needed again and again
– In dynamic programming, computed solutions to sub-problems are stored in a
table so that these don’t have to be recomputed
– Dynamic Programming is not useful when there are no common (overlapping)
sub-problems because there is no point storing the solutions if they are not
* needed again.
Dynamic Programming / Dr R Manimegalai, Professor and Head / CSE 43
Multi-stage Graph Problem
Multi-stage Graph - Example
• Cost of matrix multiplication: Two matrices are called compatible only if the
number of columns in the first matrix and the number of rows in the second matrix
are the same. Matrix multiplication is possible only if they are compatible.
• This algorithm works in steps. In each step it selects the best available
options until all options are finished.
• If the inclusion of the next input into the partially constructed optimal
solution results suboptimal / infeasible solution, then that input is not
added to the partial solution. Otherwise, it is added.
Select selects an input from a[ ] and removes it. The selected input’s value is
assigned to x. Feasible is a Boolean-valued function that determines whether x
can be included into the solution vector or not. Union combines x with the
solution and updates the objective function.
What are the components of a Greedy algorithm?
Components of Greedy Algorithm
• Candidate set: A solution that is created from the set is known as a
candidate set.
• Selection function: This function is used to choose the candidate or
subset which can be added in the solution.
• Feasibility function: A function that is used to determine whether the
candidate or subset can be used to contribute to the solution or not.
• Objective function: A function is used to assign the value to the
solution or the partial solution.
• Solution function: This function is used to intimate whether the
complete function has been reached or not.
• What is greedy choice property?
– A global optimum can be arrived at by selecting a local optimum. An optimal
solution to the problem contains an optimal solution to sub-problems.
100
Greedy Properties
• We can sort the activities according to their finishing time so that we always
consider the next activity as the minimum finishing time activity
int s[] = { 1, 3, 0, 5, 8, 5 };
int f[] = { 2, 4, 6, 7, 9, 9 };
Time Complexity: O(N)
Auxiliary Space: O(1) Following activities are selected
107
0134
Activity Selection Problem (source: javapoint)
* 112
Optimal Merge Patterns
125
Minimum Weighted Path Tree using Huffman Codes