Introduction To Algorithms.
                     §16. Greedy Algorithm.
                                      2010 / 06




2010   6   22
What is Greedy Algorithm?

            DP

                  Optimal Substructure & Overlapping Subproblem

            Greedy Algorithm :

           




2010   6   22
What is Greedy Algorithm?

           




                            (    )




2010   6   22
Step of Greedy Algorithm.

            Optimal Substructure

           

           

                                   Iterative




2010   6   22
2010   6   22
§16.1 Activity-selection Problem

           

            Activity Set S = { a1 , a2 , ... , an }

            Activity             s            f

            0 <= s < f < ∞




2010   6   22
§16.1 Activity-selection Problem

            Activity    f
                i    1   2    3    4    5    6      7    8    9    10   11

                si   1   3    0    5    3    5       6    8    8    2   12

                fi   4   5    6    7    9    9      10   11   12   14   16


                                      : { a3 , a9 , a11}

                        : { a1 , a4 , a8 , a11 }



2010   6   22
§16.1 Activity-selection Problem

           S

                  (DP)

                  Optimal Substructure




2010   6   22
Optimal Substructure of §16.1

            Sij : ai                   aj

                                            Aij

            Sij        Activity ak                  ?

                                            : Sik       Skj

            Aij = Aik ∪ { ak } ∪ Akj



2010   6   22
Like Dynamic Programming

            Aij = Aik ∪ { ak } ∪ Akj

           

                             c[i][j]




2010   6   22
Like Dynamic Programming

           

            Recursive       Memoize

           

            Greedy Choice




2010   6   22
Greedy Choice #01

                            Activity

                   Greedy Choice

                 Activity

                              ?(       )




2010   6   22
Greedy Choice #02

                 ?→

                 ? → Activity

                                (   )

           




2010   6   22
Greedy Choice #03

           

           
                i    1   2   3   4   5   6   7    8    9    10   11

                si   1   3   0   5   3   5    6    8    8    2   12

                fi   4   5   6   7   9   9   10   11   12   14   16




2010   6   22
Greedy Choice #04

            Sk = { ai ∈ S | si >= fk }

                   S1   a1               Activity

                   a1

            S1




2010   6   22
Imprement

                   DP

            P419 : RecursiveActivitySelector( s , f , k , n )

           k:           0,n:    Activity

           

                     a0                    Activity(f0=0)



2010   6   22
Code
                  void RAS( int s[], int f[], int k, int n){
                     int m = k + 1;
                     while( m <= n && s[m] < f[k])
                         m++;
                     if( m <= n ){
                         printf("%d ", m);
                         RAS( s , f, m , n );
                     }
                  }




2010   6   22
Iterative
                  int N = 11;
                  void GAS( int s[], int f[]){
                      printf("%d ", 1);
                      int k = 1, m;
                      for( m = 2 ; m <= N ; m++ ){
                          if( s[m] >= f[k] ){
                              printf("%d ", m);
                              k = m;
                          }
                      }
                  }




2010   6   22
2010   6   22
§16.2 Elements of Greedy Strategy

            Optimal Substructure   DP     (      )

            Make choice that seems best at moment.(   )




2010   6   22
Steps.

            Optimal Substructure
           
            Greedy Choice                      1   ?
            Greedy Choice
           
                                   Iterative




2010   6   22
Design Greedy Algorithms

           

            Greedy Choice


            Optimal Substructure
                  DP
           




2010   6   22
Key point 1.

            Greedy-choice property

            DP                             → Bottom up

            Greedy Algorithm

                                     → Top down




2010   6   22
Key point 2.

            Optimal Substructure

            DP




2010   6   22
Greedy vs DP

                Optimal Substructure

                            …

                                       !




2010   6   22
2010   6   22
0-1 Knapsack Problem

                           n

           i                     vi     wi

                                W

           

            0-1 : take it or leave it



2010   6   22
Fractional Knapsack Problem

           

           

            0-1   Fractional

                  Fractional Greedy

                    0-1



2010   6   22
How to Solve

                         ( vi / wi )




2010   6   22
 §16.3 Huffman Codes

            §16.4 Matroids and greed methods

            §16.5 A task-scheduling problem as a matroid




2010   6   22
Problem

            16-1 Coin changing

            16-2 Scheduling to minimize average completion time

            16-3 Acyclic Subgraphs

            16-4 Scheduling variations

            16-5 Off-line caching



2010   6    16-1 Coin changing
           22
 Huffman Code        …

            Matroid

                      Graph

                       §22-26




2010   6   22
 Greedy Algorithm : 1971

           

                Wikipedia




2010   6   22

Sec16 greedy algorithm no1

  • 1.
    Introduction To Algorithms. §16. Greedy Algorithm. 2010 / 06 2010 6 22
  • 2.
    What is GreedyAlgorithm?  DP Optimal Substructure & Overlapping Subproblem  Greedy Algorithm :  2010 6 22
  • 3.
    What is GreedyAlgorithm?   ( ) 2010 6 22
  • 4.
    Step of GreedyAlgorithm.  Optimal Substructure    Iterative 2010 6 22
  • 5.
    2010 6 22
  • 6.
    §16.1 Activity-selection Problem   Activity Set S = { a1 , a2 , ... , an }  Activity s f  0 <= s < f < ∞ 2010 6 22
  • 7.
    §16.1 Activity-selection Problem  Activity f i 1 2 3 4 5 6 7 8 9 10 11 si 1 3 0 5 3 5 6 8 8 2 12 fi 4 5 6 7 9 9 10 11 12 14 16  : { a3 , a9 , a11}  : { a1 , a4 , a8 , a11 } 2010 6 22
  • 8.
    §16.1 Activity-selection Problem S  (DP)  Optimal Substructure 2010 6 22
  • 9.
    Optimal Substructure of§16.1  Sij : ai aj  Aij  Sij Activity ak ?  : Sik Skj  Aij = Aik ∪ { ak } ∪ Akj 2010 6 22
  • 10.
    Like Dynamic Programming  Aij = Aik ∪ { ak } ∪ Akj   c[i][j] 2010 6 22
  • 11.
    Like Dynamic Programming   Recursive Memoize   Greedy Choice 2010 6 22
  • 12.
    Greedy Choice #01  Activity  Greedy Choice  Activity  ?( ) 2010 6 22
  • 13.
    Greedy Choice #02  ?→  ? → Activity  ( )  2010 6 22
  • 14.
    Greedy Choice #03   i 1 2 3 4 5 6 7 8 9 10 11 si 1 3 0 5 3 5 6 8 8 2 12 fi 4 5 6 7 9 9 10 11 12 14 16 2010 6 22
  • 15.
    Greedy Choice #04  Sk = { ai ∈ S | si >= fk }  S1 a1 Activity  a1  S1 2010 6 22
  • 16.
    Imprement  DP  P419 : RecursiveActivitySelector( s , f , k , n ) k: 0,n: Activity   a0 Activity(f0=0) 2010 6 22
  • 17.
    Code void RAS( int s[], int f[], int k, int n){ int m = k + 1; while( m <= n && s[m] < f[k]) m++; if( m <= n ){ printf("%d ", m); RAS( s , f, m , n ); } } 2010 6 22
  • 18.
    Iterative int N = 11; void GAS( int s[], int f[]){ printf("%d ", 1); int k = 1, m; for( m = 2 ; m <= N ; m++ ){ if( s[m] >= f[k] ){ printf("%d ", m); k = m; } } } 2010 6 22
  • 19.
    2010 6 22
  • 20.
    §16.2 Elements ofGreedy Strategy  Optimal Substructure DP ( )  Make choice that seems best at moment.( ) 2010 6 22
  • 21.
    Steps.  Optimal Substructure   Greedy Choice 1 ?  Greedy Choice   Iterative 2010 6 22
  • 22.
    Design Greedy Algorithms   Greedy Choice  Optimal Substructure  DP  2010 6 22
  • 23.
    Key point 1.  Greedy-choice property  DP → Bottom up  Greedy Algorithm  → Top down 2010 6 22
  • 24.
    Key point 2.  Optimal Substructure  DP 2010 6 22
  • 25.
    Greedy vs DP  Optimal Substructure  …  ! 2010 6 22
  • 26.
    2010 6 22
  • 27.
    0-1 Knapsack Problem  n i vi wi  W   0-1 : take it or leave it 2010 6 22
  • 28.
    Fractional Knapsack Problem    0-1 Fractional  Fractional Greedy  0-1 2010 6 22
  • 29.
    How to Solve  ( vi / wi ) 2010 6 22
  • 30.
     §16.3 HuffmanCodes  §16.4 Matroids and greed methods  §16.5 A task-scheduling problem as a matroid 2010 6 22
  • 31.
    Problem  16-1 Coin changing  16-2 Scheduling to minimize average completion time  16-3 Acyclic Subgraphs  16-4 Scheduling variations  16-5 Off-line caching 2010 6  16-1 Coin changing 22
  • 32.
     Huffman Code …  Matroid  Graph  §22-26 2010 6 22
  • 33.
     Greedy Algorithm: 1971   Wikipedia 2010 6 22