SlideShare ist ein Scribd-Unternehmen logo
1 von 29
Greedy Algorithms ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Overview ,[object Object],[object Object],[object Object],[object Object],[object Object]
Greedy Strategy ,[object Object],[object Object],[object Object]
Activity-selection Problem ,[object Object],[object Object],[object Object],[object Object],[object Object],Example: Activities in each line are compatible.
Example: i  1 2 3 4 5 6 7 8 9 10 11 s i 1 3 0 5 3 5 6 8 8 2 12 f i 4 5 6 7 8 9 10 11 12 13 14 { a 3 ,  a 9 ,  a 11 }  consists of mutually compatible activities. But it is not a maximal set. { a 1 ,  a 4 ,  a 8 ,  a 11 }  is a largest subset of mutually compatible activities. Another largest subset is  { a 2 ,  a 4 ,  a 9 ,  a 11 } .
Optimal Substructure ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Recursive Solution ,[object Object],[object Object],[object Object],Recursive  Solution:               ij j k i ij S j k c k i c S j i c if } 1 ] , [ ] , [ max{ if 0 ] , [
Greedy-choice Property ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Recursive Algorithm ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Initial Call:  Recursive-Activity-Selector (s, f, 0, n+1) Complexity:    (n) Straightforward to convert the algorithm to an iterative one. See the text.
Typical Steps ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Elements of Greedy Algorithms ,[object Object],[object Object],[object Object]
Minimum Spanning Trees ,[object Object],[object Object],[object Object],b c a d e f 5 11 0 3 1 7 -3 2 a b c f e d 5 3 -3 1 0 Acyclic  subset of edges( E ) that connects all vertices of  G . w ( T ) =  weight of  T :
Generic Algorithm ,[object Object],[object Object],[object Object],[object Object],A :=   ; while  A not complete tree  do find a safe edge (u, v); A := A    {(u, v)} od
Definitions ,[object Object],[object Object],b c a d e f 5 11 0 3 1 7 -3 2 a  light  edge crossing cut (could be more than one) cut that  respects  an edge set  A =  {(a, b), (b, c)} ,[object Object],[object Object],[object Object],[object Object],[object Object]
Theorem 23.1 Proof: Let  T  be a MST that includes  A . Case:  ( u ,  v ) in  T . We’re done. Case:  ( u ,  v ) not in  T .  We have the following:  u y x v edge in  A cut shows edges in T Theorem 23.1:  Let ( S ,  V  -  S ) be any cut that respects  A , and let ( u ,  v )  be a light edge crossing ( S ,  V  -  S ). Then, ( u , v) is safe for  A . ( x ,  y )  crosses cut. Let  T ´  = { T  - {( x ,  y )}}    {( u ,  v )} . Because ( u ,  v ) is light for cut, w ( u ,  v )     w ( x ,  y ). Thus,  w ( T ´) =  w ( T ) -  w ( x ,  y ) +  w ( u ,  v )     w ( T ). Hence,  T ´ is also a MST.  So,  ( u ,  v ) is safe for  A .
Corollary In general, A will consist of several connected components (CC). Corollary:  If ( u ,  v ) is a light edge connecting one CC in  G A   = ( V ,  A ) to another CC in  G A , then ( u ,  v ) is safe for  A .
Kruskal’s Algorithm ,[object Object],[object Object],[object Object],[object Object]
Prim’s Algorithm ,[object Object],[object Object],[object Object],[object Object]
Prim’s Algorithm ,[object Object],[object Object],implemented as a min-heap Max-heap as a binary tree. 11 14 13 18 17 19 20 18 24 26 1 2 3 4 5 6 7 8 9 10
[object Object],[object Object],[object Object],[object Object],Prim’s Algorithm
Prim’s Algorithm Q := V[G]; for  each u    Q  do key[u] :=   od ; key[r] := 0;  [r] := NIL; while  Q        do u := Extract - Min(Q); for  each v    Adj[u]  do if  v    Q    w(u, v) < key[v]  then  [v] := u; key[v] := w(u, v) fi od od Complexity: Using binary heaps: O(E lg V). Initialization – O(V). Building initial queue – O(V). V Extract-Min’s – O(V lgV). E Decrease-Key’s – O(E lg V). Using Fibonacci heaps: O(E + V lg V). (see book) Note:   A = {(v,   [v]) : v    v - {r} - Q}. decrease-key operation
Example of Prim’s Algorithm b/  c/  a/0 d/  e/  f/  5 11 0 3 1 7 -3 2 Q = a  b  c  d  e  f 0      Not in tree
Example of Prim’s Algorithm b/5 c/  a/0 d/11 e/  f/  5 11 0 3 1 7 -3 2 Q = b  d  c  e  f 5 11     
Example of Prim’s Algorithm Q = e  c  d  f 3  7 11   b/5 c/7 a/0 d/11 e/3 f/  5 11 0 3 1 7 -3 2
Example of Prim’s Algorithm Q = d  c  f 0  1  2 b/5 c/1 a/0 d/0 e/3 f/2 5 11 0 3 1 7 -3 2
Example of Prim’s Algorithm Q = c  f 1  2 b/5 c/1 a/0 d/11 e/3 f/2 5 11 0 3 1 7 -3 2
Example of Prim’s Algorithm Q = f -3 b/5 c/1 a/0 d/11 e/3 f/-3 5 11 0 3 1 7 -3 2
Example of Prim’s Algorithm Q =   b/5 c/1 a/0 d/11 e/3 f/-3 5 11 0 3 1 7 -3 2
Example of Prim’s Algorithm 0 b/5 c/1 a/0 d/0 e/3 f/-3 5 3 1 -3

Weitere ähnliche Inhalte

Was ist angesagt?

Comparitive Analysis of Algorithm strategies
Comparitive Analysis of Algorithm strategiesComparitive Analysis of Algorithm strategies
Comparitive Analysis of Algorithm strategiesTalha Shaikh
 
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal's
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal'sGreedy algorithms -Making change-Knapsack-Prim's-Kruskal's
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal'sJay Patel
 
unit-4-dynamic programming
unit-4-dynamic programmingunit-4-dynamic programming
unit-4-dynamic programminghodcsencet
 
Greedy Algorithm
Greedy AlgorithmGreedy Algorithm
Greedy AlgorithmWaqar Akram
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programmingJay Nagar
 
Algorithm chapter 9
Algorithm chapter 9Algorithm chapter 9
Algorithm chapter 9chidabdu
 
5.3 dynamic programming 03
5.3 dynamic programming 035.3 dynamic programming 03
5.3 dynamic programming 03Krish_ver2
 
Dynamic programming in Algorithm Analysis
Dynamic programming in Algorithm AnalysisDynamic programming in Algorithm Analysis
Dynamic programming in Algorithm AnalysisRajendran
 
Daa:Dynamic Programing
Daa:Dynamic ProgramingDaa:Dynamic Programing
Daa:Dynamic Programingrupali_2bonde
 
Dynamic programming - fundamentals review
Dynamic programming - fundamentals reviewDynamic programming - fundamentals review
Dynamic programming - fundamentals reviewElifTech
 
Lecture 8 dynamic programming
Lecture 8 dynamic programmingLecture 8 dynamic programming
Lecture 8 dynamic programmingOye Tu
 

Was ist angesagt? (20)

Greedymethod
GreedymethodGreedymethod
Greedymethod
 
Comparitive Analysis of Algorithm strategies
Comparitive Analysis of Algorithm strategiesComparitive Analysis of Algorithm strategies
Comparitive Analysis of Algorithm strategies
 
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal's
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal'sGreedy algorithms -Making change-Knapsack-Prim's-Kruskal's
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal's
 
Greedy Algorithms
Greedy AlgorithmsGreedy Algorithms
Greedy Algorithms
 
unit-4-dynamic programming
unit-4-dynamic programmingunit-4-dynamic programming
unit-4-dynamic programming
 
Greedy algorithm
Greedy algorithmGreedy algorithm
Greedy algorithm
 
36 greedy
36 greedy36 greedy
36 greedy
 
Greedy Algorithm
Greedy AlgorithmGreedy Algorithm
Greedy Algorithm
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
Algorithm chapter 9
Algorithm chapter 9Algorithm chapter 9
Algorithm chapter 9
 
5.3 dynamic programming 03
5.3 dynamic programming 035.3 dynamic programming 03
5.3 dynamic programming 03
 
Greedy method
Greedy method Greedy method
Greedy method
 
Dynamic programming in Algorithm Analysis
Dynamic programming in Algorithm AnalysisDynamic programming in Algorithm Analysis
Dynamic programming in Algorithm Analysis
 
Greedy Algorithms with examples' b-18298
Greedy Algorithms with examples'  b-18298Greedy Algorithms with examples'  b-18298
Greedy Algorithms with examples' b-18298
 
Lec30
Lec30Lec30
Lec30
 
DP
DPDP
DP
 
Daa:Dynamic Programing
Daa:Dynamic ProgramingDaa:Dynamic Programing
Daa:Dynamic Programing
 
Dynamic programming - fundamentals review
Dynamic programming - fundamentals reviewDynamic programming - fundamentals review
Dynamic programming - fundamentals review
 
Lecture 8 dynamic programming
Lecture 8 dynamic programmingLecture 8 dynamic programming
Lecture 8 dynamic programming
 
A greedy algorithms
A greedy algorithmsA greedy algorithms
A greedy algorithms
 

Ähnlich wie test pre

Unit 3- Greedy Method.pptx
Unit 3- Greedy Method.pptxUnit 3- Greedy Method.pptx
Unit 3- Greedy Method.pptxMaryJacob24
 
Ch3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhh
Ch3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhhCh3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhh
Ch3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhhdanielgetachew0922
 
Unit 3 greedy method
Unit 3  greedy methodUnit 3  greedy method
Unit 3 greedy methodMaryJacob24
 
Unit 3 - Greedy Method
Unit 3  - Greedy MethodUnit 3  - Greedy Method
Unit 3 - Greedy MethodMaryJacob24
 
final-ppts-daa-unit-iii-greedy-method.pdf
final-ppts-daa-unit-iii-greedy-method.pdffinal-ppts-daa-unit-iii-greedy-method.pdf
final-ppts-daa-unit-iii-greedy-method.pdfJasmineSayyed3
 
Algorithm Design and Complexity - Course 9
Algorithm Design and Complexity - Course 9Algorithm Design and Complexity - Course 9
Algorithm Design and Complexity - Course 9Traian Rebedea
 
Secure Domination in graphs
Secure Domination in graphsSecure Domination in graphs
Secure Domination in graphsMahesh Gadhwal
 
module4_dynamic programming_2022.pdf
module4_dynamic programming_2022.pdfmodule4_dynamic programming_2022.pdf
module4_dynamic programming_2022.pdfShiwani Gupta
 
Dynamic1
Dynamic1Dynamic1
Dynamic1MyAlome
 
Chapter 5.pptx
Chapter 5.pptxChapter 5.pptx
Chapter 5.pptxTekle12
 
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra Sahil Kumar
 
Analysis and Design of Algorithms notes
Analysis and Design of Algorithms  notesAnalysis and Design of Algorithms  notes
Analysis and Design of Algorithms notesProf. Dr. K. Adisesha
 
Cs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer keyCs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer keyappasami
 

Ähnlich wie test pre (20)

Unit 3- Greedy Method.pptx
Unit 3- Greedy Method.pptxUnit 3- Greedy Method.pptx
Unit 3- Greedy Method.pptx
 
Daa chapter11
Daa chapter11Daa chapter11
Daa chapter11
 
Ch3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhh
Ch3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhhCh3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhh
Ch3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhh
 
Weighted graphs
Weighted graphsWeighted graphs
Weighted graphs
 
Chapter 16
Chapter 16Chapter 16
Chapter 16
 
Unit 3 greedy method
Unit 3  greedy methodUnit 3  greedy method
Unit 3 greedy method
 
Unit 3 - Greedy Method
Unit 3  - Greedy MethodUnit 3  - Greedy Method
Unit 3 - Greedy Method
 
Approx
ApproxApprox
Approx
 
final-ppts-daa-unit-iii-greedy-method.pdf
final-ppts-daa-unit-iii-greedy-method.pdffinal-ppts-daa-unit-iii-greedy-method.pdf
final-ppts-daa-unit-iii-greedy-method.pdf
 
Algorithm Design and Complexity - Course 9
Algorithm Design and Complexity - Course 9Algorithm Design and Complexity - Course 9
Algorithm Design and Complexity - Course 9
 
Lecture26
Lecture26Lecture26
Lecture26
 
Secure Domination in graphs
Secure Domination in graphsSecure Domination in graphs
Secure Domination in graphs
 
Chapter 24 aoa
Chapter 24 aoaChapter 24 aoa
Chapter 24 aoa
 
module4_dynamic programming_2022.pdf
module4_dynamic programming_2022.pdfmodule4_dynamic programming_2022.pdf
module4_dynamic programming_2022.pdf
 
Network Design Assignment Help
Network Design Assignment HelpNetwork Design Assignment Help
Network Design Assignment Help
 
Dynamic1
Dynamic1Dynamic1
Dynamic1
 
Chapter 5.pptx
Chapter 5.pptxChapter 5.pptx
Chapter 5.pptx
 
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra
 
Analysis and Design of Algorithms notes
Analysis and Design of Algorithms  notesAnalysis and Design of Algorithms  notes
Analysis and Design of Algorithms notes
 
Cs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer keyCs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer key
 

test pre

  • 1.
  • 2.
  • 3.
  • 4.
  • 5. Example: i 1 2 3 4 5 6 7 8 9 10 11 s i 1 3 0 5 3 5 6 8 8 2 12 f i 4 5 6 7 8 9 10 11 12 13 14 { a 3 , a 9 , a 11 } consists of mutually compatible activities. But it is not a maximal set. { a 1 , a 4 , a 8 , a 11 } is a largest subset of mutually compatible activities. Another largest subset is { a 2 , a 4 , a 9 , a 11 } .
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15. Theorem 23.1 Proof: Let T be a MST that includes A . Case: ( u , v ) in T . We’re done. Case: ( u , v ) not in T . We have the following: u y x v edge in A cut shows edges in T Theorem 23.1: Let ( S , V - S ) be any cut that respects A , and let ( u , v ) be a light edge crossing ( S , V - S ). Then, ( u , v) is safe for A . ( x , y ) crosses cut. Let T ´ = { T - {( x , y )}}  {( u , v )} . Because ( u , v ) is light for cut, w ( u , v )  w ( x , y ). Thus, w ( T ´) = w ( T ) - w ( x , y ) + w ( u , v )  w ( T ). Hence, T ´ is also a MST. So, ( u , v ) is safe for A .
  • 16. Corollary In general, A will consist of several connected components (CC). Corollary: If ( u , v ) is a light edge connecting one CC in G A = ( V , A ) to another CC in G A , then ( u , v ) is safe for A .
  • 17.
  • 18.
  • 19.
  • 20.
  • 21. Prim’s Algorithm Q := V[G]; for each u  Q do key[u] :=  od ; key[r] := 0;  [r] := NIL; while Q   do u := Extract - Min(Q); for each v  Adj[u] do if v  Q  w(u, v) < key[v] then  [v] := u; key[v] := w(u, v) fi od od Complexity: Using binary heaps: O(E lg V). Initialization – O(V). Building initial queue – O(V). V Extract-Min’s – O(V lgV). E Decrease-Key’s – O(E lg V). Using Fibonacci heaps: O(E + V lg V). (see book) Note: A = {(v,  [v]) : v  v - {r} - Q}. decrease-key operation
  • 22. Example of Prim’s Algorithm b/  c/  a/0 d/  e/  f/  5 11 0 3 1 7 -3 2 Q = a b c d e f 0   Not in tree
  • 23. Example of Prim’s Algorithm b/5 c/  a/0 d/11 e/  f/  5 11 0 3 1 7 -3 2 Q = b d c e f 5 11  
  • 24. Example of Prim’s Algorithm Q = e c d f 3 7 11  b/5 c/7 a/0 d/11 e/3 f/  5 11 0 3 1 7 -3 2
  • 25. Example of Prim’s Algorithm Q = d c f 0 1 2 b/5 c/1 a/0 d/0 e/3 f/2 5 11 0 3 1 7 -3 2
  • 26. Example of Prim’s Algorithm Q = c f 1 2 b/5 c/1 a/0 d/11 e/3 f/2 5 11 0 3 1 7 -3 2
  • 27. Example of Prim’s Algorithm Q = f -3 b/5 c/1 a/0 d/11 e/3 f/-3 5 11 0 3 1 7 -3 2
  • 28. Example of Prim’s Algorithm Q =  b/5 c/1 a/0 d/11 e/3 f/-3 5 11 0 3 1 7 -3 2
  • 29. Example of Prim’s Algorithm 0 b/5 c/1 a/0 d/0 e/3 f/-3 5 3 1 -3