DZone
Big Data Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Big Data Zone > Activity Selection Problem: Algorithms [Code Snippet]

Activity Selection Problem: Algorithms [Code Snippet]

Activity selection problem is a greedy algorithm, i.e always select the next optimal solution.

Piyush Arora user avatar by
Piyush Arora
·
Oct. 28, 16 · Big Data Zone · Code Snippet
Like (3)
Save
Tweet
5.95K Views

Join the DZone community and get the full member experience.

Join For Free

Activity selection problem is a greedy algorithm, i.e always select the next optimal solution.

The greedy choice is to always pick the next activity whose finish time is least among the remaining activities and the start time is more than or equal to the finish time of previously selected activity. We can sort the activities according to their finishing time so that we always consider the next activity as minimum finishing time activity.

  1. Sort the activities according to their finishing time

  2. Select the first activity from the sorted array and print it.

  3. Do following for remaining activities in the sorted array.

If the start time of this activity is greater than the finish time of previously selected activity then select this activity and print it.

C Implementation:

#include<stdio.h>

voidprintMaxActivities(ints[], intf[], intn)
{
    inti, j;

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

//first activity always gets selected


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

    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
intmain()
{
    ints[] =  {1, 3, 0, 5, 8, 5};
    intf[] =  {2, 4, 6, 7, 9, 9};
    intn = sizeof(s)/sizeof(s[0]);
    printMaxActivities(s, f, n);
    getchar();
    return0;
}


Algorithm Snippet (programming)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How to Modify Java Command-Line Arguments
  • Revoking Access to JWTs With a Blacklist/Deny List
  • Which JVM Version Is the Fastest?
  • How to Make Git Forget a Tracked File Now in .gitignore

Comments

Big Data Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo