SlideShare a Scribd company logo
1 of 13
Greedy Algorithms (Chap. 16)
• Optimization problems
– Dynamic programming, but overkill sometime.
– Greedy algorithm:
• Being greedy for local optimization with the hope it
will lead to a global optimal solution, not always,
but in many situations, it works.
An Activity-Selection Problem
• Suppose A set of activities S={a1, a2,…, an}
– They use resources, such as lecture hall, one lecture at a
time
– Each ai, has a start time si, and finish time fi, with 0 si<
fi<.
– ai and aj are compatible if [si, fi) and [sj, fj) do not
overlap
• Goal: select maximum-size subset of mutually
compatible activities.
• Start from dynamic programming, then greedy
algorithm, see the relation between the two.
DP solution –step 1
• Optimal substructure of activity-selection problem.
– Furthermore, assume that f1 … fn.
– Define Sij={ak: fi sk<fksj}, i.e., all activities starting after ai
finished and ending before aj begins.
– Define two fictitious activities a0 with f0=0 and an+1 with sn+1=
• So f0 f1 … fn+1.
– Then an optimal solution including ak to Sij contains within it
the optimal solution to Sik and Skj.
DP solution –step 2
• A recursive solution
• Assume c[n+1,n+1] with c[i,j] is the number of activities in a
maximum-size subset of mutually compatible activities in Sij. So the
solution is c[0,n+1]=S0,n+1.
• C[i,j]= 0 if Sij=
max{c[i,k]+c[k,j]+1} if Sij
i<k<j and akSij
• How to implement?
– How to compute the initial cases by checking Sij=?
– How to loop to iteratively compute C[i,j]:
– For i=… for j=… for k=…? This is wrong?
– Need to be similar to MCM:
• For len=… for i=… j=i+len; for k=…
Converting DP Solution to Greedy Solution
• Theorem 16.1: consider any nonempty subproblem Sij,
and let am be the activity in Sij with earliest finish time:
fm=min{fk : ak  Sij}, then
1. Activity am is used in some maximum-size subset of
mutually compatible activities of Sij.
2. The subproblem Sim is empty, so that choosing am leaves
Smj as the only one that may be nonempty.
• Proof of the theorem:
Top-Down Rather Than Bottom-Up
• To solve Sij, choose am in Sij with the
earliest finish time, then solve Smj, (Sim is
empty)
• It is certain that optimal solution to Smj is in
optimal solution to Sij.
• No need to solve Smj ahead of Sij.
• Subproblem pattern: Si,n+1.
Optimal Solution Properties
• In DP, optimal solution depends:
– How many subproblems to divide. (2 subproblems)
– How many choices to determine which subproblem to use. (j-i-
1 choices)
• However, the above theorem (16.1) reduces both
significantly
– One subproblem (the other is sure to be empty).
– One choice, i.e., the one with earliest finish time in Sij.
– Moreover, top-down solving, rather than bottom-up in DP.
– Pattern to the subproblems that we solve, Sm,n+1 from Sij.
– Pattern to the activities that we choose. The activity with
earliest finish time.
– With this local optimal, it is in fact the global optimal.
Elements of greedy strategy
• Determine the optimal substructure
• Develop the recursive solution
• Prove one of the optimal choices is the greedy
choice yet safe
• Show that all but one of subproblems are empty
after greedy choice
• Develop a recursive algorithm that implements the
greedy strategy
• Convert the recursive algorithm to an iterative
one.
Greedy vs. DP
• Knapsack problem
– I1 (v1,w1), I2(v2,w2),…,In(vn,wn).
– Given a weight W at most he can carry,
– Find the items which maximize the values
• Fractional knapsack,
– Greed algorithm, O(nlogn)
• 0/1 knapsack.
– DP, O(nW).
– Questions: 0/1 knapsack is an NP-complete problem,
why O(nW) algorithm?
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Maximum attendance
• A little bit change to the previous activity selection. For each
activity ai such as a talk, there is an associated attendance atti.
– i.e., given
{a1,a2,…,an}={(s1,f1,att1),(s2,f2,att2),…(sn,fn,attn), compute
its maximum attendance of compatible activities.
• Use the one similar to the previous activity-selection:
• C[i,j]= 0 if Sij=
max{c[i,k]+c[k,j]+attk} if Sij
i<k<j and akSij
Maximum attendance (cont.)
• New analysis (easy to think sort activities):
– For any ai, which is the most recent previous one compatible with it?
• so, define P(i)=max{k: k<i && fk si}.
• compute P(i) is easy. P(1)=0. (for easy coding, a dummy a0=(0,0,0))
– Also, define T(i): the maximum attendance of all compatible
activities from a1 to ai. T(n) will be an answer.
– Consider activity ai, two cases:
• ai is contained within the solution, then only aP(i) can be included too.
• ai is not included, then ai-1 can be included.
– T(i)= 0 if i=0
max{T(i-1),atti+T(P(i))} if i>0
Typical tradition problem with
greedy solutions
• Coin changes
– 25, 10, 5, 1
– How about 7, 5, 1
• Minimum Spanning Tree
– Prim’s algorithm
• Begin from any node, each time add a new node which is closest to the
existing subtree.
– Kruskal’s algorithm
• Sorting the edges by their weights
• Each time, add the next edge which will not create cycle after added.
• Single source shortest pathes
– Dijkstra’s algorithm
• Huffman coding
• Optimal merge

More Related Content

Similar to Greedy Algorithms for Activity Selection and Maximum Attendance Problems

lecture 26
lecture 26lecture 26
lecture 26sajinsc
 
Lecture34
Lecture34Lecture34
Lecture34farazch
 
Skiena algorithm 2007 lecture01 introduction to algorithms
Skiena algorithm 2007 lecture01 introduction to algorithmsSkiena algorithm 2007 lecture01 introduction to algorithms
Skiena algorithm 2007 lecture01 introduction to algorithmszukun
 
Approximation algorithms
Approximation algorithmsApproximation algorithms
Approximation algorithmsGanesh Solanke
 
Greedy method1
Greedy method1Greedy method1
Greedy method1Rajendran
 
(1) collections algorithms
(1) collections algorithms(1) collections algorithms
(1) collections algorithmsNico Ludwig
 
Amortized Analysis
Amortized Analysis Amortized Analysis
Amortized Analysis sathish sak
 
Amortized analysis
Amortized analysisAmortized analysis
Amortized analysisajmalcs
 
Amortized analysis
Amortized analysisAmortized analysis
Amortized analysisajmalcs
 
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy methodhodcsencet
 
Lecture 8 dynamic programming
Lecture 8 dynamic programmingLecture 8 dynamic programming
Lecture 8 dynamic programmingOye Tu
 
4 greedy methodnew
4 greedy methodnew4 greedy methodnew
4 greedy methodnewabhinav108
 
data structures and algorithms Unit 4
data structures and algorithms Unit 4data structures and algorithms Unit 4
data structures and algorithms Unit 4infanciaj
 

Similar to Greedy Algorithms for Activity Selection and Maximum Attendance Problems (20)

lec
leclec
lec
 
lecture 26
lecture 26lecture 26
lecture 26
 
Lecture34
Lecture34Lecture34
Lecture34
 
Skiena algorithm 2007 lecture01 introduction to algorithms
Skiena algorithm 2007 lecture01 introduction to algorithmsSkiena algorithm 2007 lecture01 introduction to algorithms
Skiena algorithm 2007 lecture01 introduction to algorithms
 
Approximation algorithms
Approximation algorithmsApproximation algorithms
Approximation algorithms
 
Greedy method1
Greedy method1Greedy method1
Greedy method1
 
(1) collections algorithms
(1) collections algorithms(1) collections algorithms
(1) collections algorithms
 
Amortized Analysis
Amortized Analysis Amortized Analysis
Amortized Analysis
 
Activity selection problem
Activity selection problemActivity selection problem
Activity selection problem
 
Amortized analysis
Amortized analysisAmortized analysis
Amortized analysis
 
Amortized analysis
Amortized analysisAmortized analysis
Amortized analysis
 
Amortized analysis
Amortized analysisAmortized analysis
Amortized analysis
 
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
 
Algorithm big o
Algorithm big oAlgorithm big o
Algorithm big o
 
Lecture 8 dynamic programming
Lecture 8 dynamic programmingLecture 8 dynamic programming
Lecture 8 dynamic programming
 
4 greedy methodnew
4 greedy methodnew4 greedy methodnew
4 greedy methodnew
 
04greedy 2x2
04greedy 2x204greedy 2x2
04greedy 2x2
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
data structures and algorithms Unit 4
data structures and algorithms Unit 4data structures and algorithms Unit 4
data structures and algorithms Unit 4
 
AA_Unit 1_part-I.pptx
AA_Unit 1_part-I.pptxAA_Unit 1_part-I.pptx
AA_Unit 1_part-I.pptx
 

Recently uploaded

Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Callshivangimorya083
 
Vip Model Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...
Vip Model  Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...Vip Model  Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...
Vip Model Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...shivangimorya083
 
Data-Analysis for Chicago Crime Data 2023
Data-Analysis for Chicago Crime Data  2023Data-Analysis for Chicago Crime Data  2023
Data-Analysis for Chicago Crime Data 2023ymrp368
 
Zuja dropshipping via API with DroFx.pptx
Zuja dropshipping via API with DroFx.pptxZuja dropshipping via API with DroFx.pptx
Zuja dropshipping via API with DroFx.pptxolyaivanovalion
 
Ravak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptxRavak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptxolyaivanovalion
 
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...Pooja Nehwal
 
Call Girls 🫤 Dwarka ➡️ 9711199171 ➡️ Delhi 🫦 Two shot with one girl
Call Girls 🫤 Dwarka ➡️ 9711199171 ➡️ Delhi 🫦 Two shot with one girlCall Girls 🫤 Dwarka ➡️ 9711199171 ➡️ Delhi 🫦 Two shot with one girl
Call Girls 🫤 Dwarka ➡️ 9711199171 ➡️ Delhi 🫦 Two shot with one girlkumarajju5765
 
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 nightCheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 nightDelhi Call girls
 
Carero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptxCarero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptxolyaivanovalion
 
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAl Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAroojKhan71
 
April 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's AnalysisApril 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's Analysismanisha194592
 
Schema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfSchema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfLars Albertsson
 
BabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptxBabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptxolyaivanovalion
 
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...amitlee9823
 
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779Delhi Call girls
 
Week-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionWeek-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionfulawalesam
 
Invezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signalsInvezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signalsInvezz1
 
ALSO dropshipping via API with DroFx.pptx
ALSO dropshipping via API with DroFx.pptxALSO dropshipping via API with DroFx.pptx
ALSO dropshipping via API with DroFx.pptxolyaivanovalion
 

Recently uploaded (20)

Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
 
Vip Model Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...
Vip Model  Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...Vip Model  Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...
Vip Model Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...
 
Data-Analysis for Chicago Crime Data 2023
Data-Analysis for Chicago Crime Data  2023Data-Analysis for Chicago Crime Data  2023
Data-Analysis for Chicago Crime Data 2023
 
Zuja dropshipping via API with DroFx.pptx
Zuja dropshipping via API with DroFx.pptxZuja dropshipping via API with DroFx.pptx
Zuja dropshipping via API with DroFx.pptx
 
Ravak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptxRavak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptx
 
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
 
Call Girls 🫤 Dwarka ➡️ 9711199171 ➡️ Delhi 🫦 Two shot with one girl
Call Girls 🫤 Dwarka ➡️ 9711199171 ➡️ Delhi 🫦 Two shot with one girlCall Girls 🫤 Dwarka ➡️ 9711199171 ➡️ Delhi 🫦 Two shot with one girl
Call Girls 🫤 Dwarka ➡️ 9711199171 ➡️ Delhi 🫦 Two shot with one girl
 
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 nightCheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
 
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get CytotecAbortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
 
Carero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptxCarero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptx
 
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in Kishangarh
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in  KishangarhDelhi 99530 vip 56974 Genuine Escort Service Call Girls in  Kishangarh
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in Kishangarh
 
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAl Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
 
April 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's AnalysisApril 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's Analysis
 
Schema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfSchema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdf
 
BabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptxBabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptx
 
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
 
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
 
Week-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionWeek-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interaction
 
Invezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signalsInvezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signals
 
ALSO dropshipping via API with DroFx.pptx
ALSO dropshipping via API with DroFx.pptxALSO dropshipping via API with DroFx.pptx
ALSO dropshipping via API with DroFx.pptx
 

Greedy Algorithms for Activity Selection and Maximum Attendance Problems

  • 1. Greedy Algorithms (Chap. 16) • Optimization problems – Dynamic programming, but overkill sometime. – Greedy algorithm: • Being greedy for local optimization with the hope it will lead to a global optimal solution, not always, but in many situations, it works.
  • 2. An Activity-Selection Problem • Suppose A set of activities S={a1, a2,…, an} – They use resources, such as lecture hall, one lecture at a time – Each ai, has a start time si, and finish time fi, with 0 si< fi<. – ai and aj are compatible if [si, fi) and [sj, fj) do not overlap • Goal: select maximum-size subset of mutually compatible activities. • Start from dynamic programming, then greedy algorithm, see the relation between the two.
  • 3. DP solution –step 1 • Optimal substructure of activity-selection problem. – Furthermore, assume that f1 … fn. – Define Sij={ak: fi sk<fksj}, i.e., all activities starting after ai finished and ending before aj begins. – Define two fictitious activities a0 with f0=0 and an+1 with sn+1= • So f0 f1 … fn+1. – Then an optimal solution including ak to Sij contains within it the optimal solution to Sik and Skj.
  • 4. DP solution –step 2 • A recursive solution • Assume c[n+1,n+1] with c[i,j] is the number of activities in a maximum-size subset of mutually compatible activities in Sij. So the solution is c[0,n+1]=S0,n+1. • C[i,j]= 0 if Sij= max{c[i,k]+c[k,j]+1} if Sij i<k<j and akSij • How to implement? – How to compute the initial cases by checking Sij=? – How to loop to iteratively compute C[i,j]: – For i=… for j=… for k=…? This is wrong? – Need to be similar to MCM: • For len=… for i=… j=i+len; for k=…
  • 5. Converting DP Solution to Greedy Solution • Theorem 16.1: consider any nonempty subproblem Sij, and let am be the activity in Sij with earliest finish time: fm=min{fk : ak  Sij}, then 1. Activity am is used in some maximum-size subset of mutually compatible activities of Sij. 2. The subproblem Sim is empty, so that choosing am leaves Smj as the only one that may be nonempty. • Proof of the theorem:
  • 6. Top-Down Rather Than Bottom-Up • To solve Sij, choose am in Sij with the earliest finish time, then solve Smj, (Sim is empty) • It is certain that optimal solution to Smj is in optimal solution to Sij. • No need to solve Smj ahead of Sij. • Subproblem pattern: Si,n+1.
  • 7. Optimal Solution Properties • In DP, optimal solution depends: – How many subproblems to divide. (2 subproblems) – How many choices to determine which subproblem to use. (j-i- 1 choices) • However, the above theorem (16.1) reduces both significantly – One subproblem (the other is sure to be empty). – One choice, i.e., the one with earliest finish time in Sij. – Moreover, top-down solving, rather than bottom-up in DP. – Pattern to the subproblems that we solve, Sm,n+1 from Sij. – Pattern to the activities that we choose. The activity with earliest finish time. – With this local optimal, it is in fact the global optimal.
  • 8. Elements of greedy strategy • Determine the optimal substructure • Develop the recursive solution • Prove one of the optimal choices is the greedy choice yet safe • Show that all but one of subproblems are empty after greedy choice • Develop a recursive algorithm that implements the greedy strategy • Convert the recursive algorithm to an iterative one.
  • 9. Greedy vs. DP • Knapsack problem – I1 (v1,w1), I2(v2,w2),…,In(vn,wn). – Given a weight W at most he can carry, – Find the items which maximize the values • Fractional knapsack, – Greed algorithm, O(nlogn) • 0/1 knapsack. – DP, O(nW). – Questions: 0/1 knapsack is an NP-complete problem, why O(nW) algorithm?
  • 10. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
  • 11. Maximum attendance • A little bit change to the previous activity selection. For each activity ai such as a talk, there is an associated attendance atti. – i.e., given {a1,a2,…,an}={(s1,f1,att1),(s2,f2,att2),…(sn,fn,attn), compute its maximum attendance of compatible activities. • Use the one similar to the previous activity-selection: • C[i,j]= 0 if Sij= max{c[i,k]+c[k,j]+attk} if Sij i<k<j and akSij
  • 12. Maximum attendance (cont.) • New analysis (easy to think sort activities): – For any ai, which is the most recent previous one compatible with it? • so, define P(i)=max{k: k<i && fk si}. • compute P(i) is easy. P(1)=0. (for easy coding, a dummy a0=(0,0,0)) – Also, define T(i): the maximum attendance of all compatible activities from a1 to ai. T(n) will be an answer. – Consider activity ai, two cases: • ai is contained within the solution, then only aP(i) can be included too. • ai is not included, then ai-1 can be included. – T(i)= 0 if i=0 max{T(i-1),atti+T(P(i))} if i>0
  • 13. Typical tradition problem with greedy solutions • Coin changes – 25, 10, 5, 1 – How about 7, 5, 1 • Minimum Spanning Tree – Prim’s algorithm • Begin from any node, each time add a new node which is closest to the existing subtree. – Kruskal’s algorithm • Sorting the edges by their weights • Each time, add the next edge which will not create cycle after added. • Single source shortest pathes – Dijkstra’s algorithm • Huffman coding • Optimal merge