advanced algorithms
DESCRIPTION
Greedy algorithm Job-select problem Greedy vs DP. Advanced Algorithms. Learning outcomes. Able to write greedy algorithms for the problems discussed in the class able to analyse the complexity of the algorithms. Greedy Approach. Also for optimization problem - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Advanced Algorithms](https://reader030.vdocuments.site/reader030/viewer/2022032805/56813348550346895d9a4596/html5/thumbnails/1.jpg)
CSE 780 Algorithms
Advanced Algorithms
Greedy algorithm Job-select problem
Greedy vs DP
![Page 2: Advanced Algorithms](https://reader030.vdocuments.site/reader030/viewer/2022032805/56813348550346895d9a4596/html5/thumbnails/2.jpg)
CSE 780 Algorithms
Learning outcomes
Able to write greedy algorithms for the problems discussed in the class
able to analyse the complexity of the algorithms
![Page 3: Advanced Algorithms](https://reader030.vdocuments.site/reader030/viewer/2022032805/56813348550346895d9a4596/html5/thumbnails/3.jpg)
CSE 780 Algorithms
Greedy Approach
Also for optimization problem Best choice at any moment
It makes a locally optimal choice in hopes of achieving a globally optimal solution.
Do not always yield optimal solutions, but for many problems they do
Examples: Activity selection problem, Fractional knapsack problem, Huffman coding, mst, SS shortest path.
![Page 4: Advanced Algorithms](https://reader030.vdocuments.site/reader030/viewer/2022032805/56813348550346895d9a4596/html5/thumbnails/4.jpg)
CSE 780 Algorithms
Change-Making Problem
Given an amount A and set of denominations d1, d2,
…, dn
find a minimum number of coins. E.g A=18, d1=1, d2=5 d3=10
require 1 coin of 10, 1 coin of 5, and 3 coins of 1.
This is optimal. Now let A=12 and d1=1, d2=6, d3=10
Using the same approach, it requires
1 coin of 10 and 2 coins of 1. This is not optimal.
![Page 5: Advanced Algorithms](https://reader030.vdocuments.site/reader030/viewer/2022032805/56813348550346895d9a4596/html5/thumbnails/5.jpg)
CSE 780 Algorithms
Activity-selection Problem
![Page 6: Advanced Algorithms](https://reader030.vdocuments.site/reader030/viewer/2022032805/56813348550346895d9a4596/html5/thumbnails/6.jpg)
CSE 780 Algorithms
Example:
A :
![Page 7: Advanced Algorithms](https://reader030.vdocuments.site/reader030/viewer/2022032805/56813348550346895d9a4596/html5/thumbnails/7.jpg)
CSE 780 Algorithms
Define the Problem
![Page 8: Advanced Algorithms](https://reader030.vdocuments.site/reader030/viewer/2022032805/56813348550346895d9a4596/html5/thumbnails/8.jpg)
CSE 780 Algorithms
Optimal Substructure Property
![Page 9: Advanced Algorithms](https://reader030.vdocuments.site/reader030/viewer/2022032805/56813348550346895d9a4596/html5/thumbnails/9.jpg)
CSE 780 Algorithms
DP Approach
Can continue with DP algorithm
Time complexity: Build 2D DP table O (n^3)
But can do better!
![Page 10: Advanced Algorithms](https://reader030.vdocuments.site/reader030/viewer/2022032805/56813348550346895d9a4596/html5/thumbnails/10.jpg)
CSE 780 Algorithms
Theorem
Proof for (1):
![Page 11: Advanced Algorithms](https://reader030.vdocuments.site/reader030/viewer/2022032805/56813348550346895d9a4596/html5/thumbnails/11.jpg)
CSE 780 Algorithms
Use of Theorem
DP: Greedy:
12
#subproblem
for each choice
1j - i -1
#choices
to make
After
theorem
Before theorem
![Page 12: Advanced Algorithms](https://reader030.vdocuments.site/reader030/viewer/2022032805/56813348550346895d9a4596/html5/thumbnails/12.jpg)
CSE 780 Algorithms
Top-down Greedy Algorithm
Goal: to compute max activity Pseudo-code
Rec-Job-Select( s,f, i, n )
m = i + 1
while (m ≤ n) and ( < )
do m = m + 1
if m ≤ n
return { } Rec-Job-Select(s, f, m, n)
else return
sm f i
am
![Page 13: Advanced Algorithms](https://reader030.vdocuments.site/reader030/viewer/2022032805/56813348550346895d9a4596/html5/thumbnails/13.jpg)
CSE 780 Algorithms
Example
![Page 14: Advanced Algorithms](https://reader030.vdocuments.site/reader030/viewer/2022032805/56813348550346895d9a4596/html5/thumbnails/14.jpg)
CSE 780 Algorithms
Remarks
Top-down rather than bottom-up Inductively
Time complexity Sorting time + O (n)
Opt-solution not unique But one can be found using greedy approach
![Page 15: Advanced Algorithms](https://reader030.vdocuments.site/reader030/viewer/2022032805/56813348550346895d9a4596/html5/thumbnails/15.jpg)
CSE 780 Algorithms
Elements of Greedy Strategy
For previous example We first follow DP Then show that instead we can make a greedy choice
In general Cast opt-problem as one in which we make one choice
and are left with one subproblem to solve Prove greedy step + opt-subproblem will lead to one
opt-solution
Greedy-choice propertyOptimal substructure property
![Page 16: Advanced Algorithms](https://reader030.vdocuments.site/reader030/viewer/2022032805/56813348550346895d9a4596/html5/thumbnails/16.jpg)
CSE 780 Algorithms
Remarks
Greedy => DP Top-down rather than bottom-up Greedy algorithm developed does not necessarily
lead to opt-solution
![Page 17: Advanced Algorithms](https://reader030.vdocuments.site/reader030/viewer/2022032805/56813348550346895d9a4596/html5/thumbnails/17.jpg)
CSE 780 Algorithms
Knapsack Problem
0-1 knapsack problem: How to fill the knapsack with best total value w/o breaking each
item Fractional knapsack problem:
How to fill the knapsack with best total value
![Page 18: Advanced Algorithms](https://reader030.vdocuments.site/reader030/viewer/2022032805/56813348550346895d9a4596/html5/thumbnails/18.jpg)
CSE 780 Algorithms
Solution
DP for 0-1 knapsack Greedy for fractional knapsack
Compute value per pound (VPP) for each item Take as much as possible of the item with largest VPP Continue till reach weight limit
![Page 19: Advanced Algorithms](https://reader030.vdocuments.site/reader030/viewer/2022032805/56813348550346895d9a4596/html5/thumbnails/19.jpg)
CSE 780 Algorithms
Example
10
20
30
$60 $100 $120
VPP: 6 5 4
Knapsack: hole weight 50
10
20
20--30
![Page 20: Advanced Algorithms](https://reader030.vdocuments.site/reader030/viewer/2022032805/56813348550346895d9a4596/html5/thumbnails/20.jpg)
CSE 780 Algorithms
Greedy Fails for 0-1 Knapsack
10
20
30
$60 $100 $120
VPP: 6 5 410
20
$160
10
30
$180
20
30
$220
![Page 21: Advanced Algorithms](https://reader030.vdocuments.site/reader030/viewer/2022032805/56813348550346895d9a4596/html5/thumbnails/21.jpg)
CSE 780 Algorithms
Summary
Greedy algorithm: Job-select problem Follow the thinking of DP Or directly construct
More examples in the course later