Activity arrangement problem (implemented in C Language) -- greedy algorithm application (2)

Source: Internet
Author: User

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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.