Activity Selection | DAA - IndianTechnoEra
Latest update Android YouTube

Activity Selection | DAA

Admin

 

What is Activity selection?

The Activity Selection Problem is an optimization problem which deals with the selection of non-conflicting activities that needs to be executed by a single person or machine in a given time frame.


Code:

#include <stdio.h>

void printMaxActivities(int s[], int f[], int n)
{
    int i, j;

    printf("Following activities are selected \n");

    i = 0;
    printf("%d ", i);

    // Consider rest of the activities
    for (j = 1; j < n; j++)
    {
        // If this activity has start time greater than or
        // equal to the finish time of previously selected
        // activity, then select it
        if (s[j] >= f[i])
        {
            printf("%d ", j);
            i = j;
        }
    }
}

// driver program to test above function
int main()
{
    int i, size;

    printf("\nAim: Activity Selection in C");
    printf("\nUser Input: By user");
    printf("\n==============================================\n");

    printf("Enter the no of activity: ");
    scanf("%d", &size);

    int s[size];
    int f[size];

    printf("Enter the %d activity :\n", size);
    printf("Activity No \t Start Time \t Finish Time\n");
    for (i = 0; i < size; i++)
    {
        printf("Activity[%d]: \t", i + 1);
        scanf("%d\t%d", &s[i], &f[i]);
    }

    int n = sizeof(s) / sizeof(s[0]);
    printMaxActivities(s, f, n);
    return 0;
}


Output:

إرسال تعليق

Feel free to ask your query...
Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.