Set of n activities E = {1, 2 ,..., N}. Each activity requires the use of the same resource, such as the speech venue. Only one activity can use this resource within the same time period. Each activity I has a start time si that requires the use of the resource and an end time fi, and si <fi. If activity I is selected, it occupies resources within the half-open time range [si, fi. If the interval [si, fi) and interval [sj, fj) do not overlap, it is said that activity I is compatible with activity j. That is, when si> = fj or sj> = fi, activity I is compatible with activity j.
 
The greedy algorithm provides a simple and beautiful way to make as many activities as possible compatible with public resources.
 
For example, the following table lists the start time and end time of the 11 activities to be scheduled, which are arranged in non-descending order.
 
 
Algorithm analysis:
Sort the input activities in non-descending order according to their completion time, and always add the compatible activities with the earliest completion time to the schedule. The greedy choice of this algorithm makes the remaining configurable time periods extremely large, so as to arrange as many activities as possible.
 
The following is the c language implementation (DEV c ++ 4.9.2 runs through)
 
[Cpp]
# Include <stdio. h>
 
Void greedy (int s [], int f [], int a [], int k );
 
Int main ()
{
Int s [] = {, 12 };
Int f [] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 };
Int k;
K = sizeof (f)/sizeof (f [0]);
Int *;
A = (int *) malloc (sizeof (int) * k );
Greedy (s, f, a, k );
System ("PAUSE ");
}
 
/*
* S []: the start time of the activity.
* F []: End Time of the activity (non-descending order)
* A []: 0 or 1. 0 indicates that the activity is not scheduled. 1 indicates that the activity is scheduled.
* K: number of activities
*/
 
Void greedy (int s [], int f [], int a [], int k)
{
Int I;
Int j = 0;
For (I = 0; I <k; I ++)
{
A [I] = 0; // all initial activities are not scheduled
}
A [0] = 1;
Printf ("1st activities scheduled \ n ");
Int count = 1;
For (I = 1; I <k; I ++)
{
If (s [I]> f [j])
{
A [I] = 1;
Printf ("START % d, end % d.", s [I], f [I]);
J = I;
Count ++;
Printf ("% d activities scheduled \ n", I + 1 );
}
}
Printf ("Total % d activities scheduled \ n", count );
}
 
# Include <stdio. h>
 
Void greedy (int s [], int f [], int a [], int k );
 
Int main ()
{
 
Int s [] = {, 12 };
Int f [] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 };
Int k;
K = sizeof (f)/sizeof (f [0]);
Int *;
A = (int *) malloc (sizeof (int) * k );
 
Greedy (s, f, a, k );
System ("PAUSE ");
 
}
 
/*
* S []: the start time of the activity.
* F []: End Time of the activity (non-descending order)
* A []: 0 or 1. 0 indicates that the activity is not scheduled. 1 indicates that the activity is scheduled.
* K: number of activities
*/
 
Void greedy (int s [], int f [], int a [], int k)
{
Int I;
Int j = 0;
For (I = 0; I <k; I ++)
{
A [I] = 0; // all initial activities are not scheduled
}
A [0] = 1;
Printf ("1st activities scheduled \ n ");
Int count = 1;
For (I = 1; I <k; I ++)
{
If (s [I]> f [j])
{
A [I] = 1;
Printf ("START % d, end % d.", s [I], f [I]);
J = I;
Count ++;
Printf ("% d activities scheduled \ n", I + 1 );
}
}
Printf ("Total % d activities scheduled \ n", count );
}
 
 
 
Greedy algorithms can always find the optimal solution to the problem of activity arrangement.
If you are interested, you can use mathematical induction to prove it.