Activity Selection
Problem
Activity:
A thing to do with a start and end.
Start time:
When I begin (first number).
Finish time:
When I’m done (second number).
Overlap:
When two activities share time (e.g., 1-4 and 3-5 clash).
Sort:
Put in order by finish time (smallest to biggest).
Greedy:
Pick what finishes earliest each time.
Maximum:
Most activities I can fit.
Goal: Do the maximum number of activities without overlapping.
Given List:
(1, 4)
(3, 5)
(5, 7)
(6, 9)
(8, 12)
(10, 11)
Sort by Finish Time:
• (1, 4) → Ends at 4
• (3, 5) → Ends at 5
• (5, 7) → Ends at 7
• (6, 9) → Ends at 9
• (10, 11) → Ends at 11
• (8, 12) → Ends at 12
Format: (StartTime, EndTime)
Start Picking
•First Activity: (1, 4)
• Ends at 4. I’ll pick it because it’s the earliest to finish.
• My list so far: [(1, 4)]
• Last finish time: 4 (when I’m free again).
•Next: (3, 5)
• Starts at 3, ends at 5.
• Can I do it? Check: Does 3 (start) come after 4 (last finish)?
• No! 3 is less than 4. They overlap (busy from 1-4, can’t start at 3).
• Skip it.
Sorted List
• (1, 4)
• (3, 5)
• (5, 7)
• (6, 9)
• (10, 11)
• (8, 12)
•Next: (5, 7)
• Starts at 5, ends at 7.
• Check: 5 ≥ 4? Yes! (5 is after 4, or equal works too.)
• No overlap—I’m free at 5. Pick it!
• My list: [(1, 4), (5, 7)]
• Last finish time: 7.
•Next: (6, 9)
• Starts at 6, ends at 9.
• Check: 6 ≥ 7? No, 6 is less than 7. Overlap!
• Skip it.
Sorted List
• (1, 4)
• (3, 5)
• (5, 7)
• (6, 9)
• (10, 11)
• (8, 12)
•Next: (10, 11)
• Starts at 10, ends at 11.
• Check: 10 ≥ 7? Yes! No overlap.
• Pick it!
• My list: [(1, 4), (5, 7), (10, 11)]
• Last finish time: 11.
•Next: (8, 12)
• Starts at 8, ends at 12.
• Check: 8 ≥ 11? No, 8 is less than 11. Overlap!
• Skip it.
•Done! No more activities.
Sorted List
• (1, 4)
• (3, 5)
• (5, 7)
• (6, 9)
• (10, 11)
• (8, 12)
Final Result:
•I picked: (1, 4), (5, 7), (10, 11).
•That’s 3 activities!
Time: 0 1 2 3 4 5 6 7 8 9 10 11 12
(1,4) |----------|
(5,7) |-----|
(10,11) |---|
•Class: Like a blueprint for something (e.g., an Activity).
•int: A number (like 1 or 4).
•Array: A list of things (like my activities).
•new: Makes a new thing (e.g., new Activity(1,4)).
•System.out.println: Prints stuff to see it.
•for loop: Repeats something (e.g., check each activity).
import java.util.Arrays;
•What’s import? It’s like borrowing a tool from Java’s toolbox.
I needArrays to sort stuff later.
•Why?Without it, I can’t use Arrays.sort. It’s not built into basic
Java.
•What’s Activity?
•It’s for one activity
with a start and finish time.
Class Definition: class Activity {
•What’s class? A blueprint to make objects
Inside Activity:
• int start, finish;
• What’s int? A whole number (like 1, 5, -3).
• What’s start, finish?Two numbers I’ll store for each activity. E.g., start at
1, finish at 4.
• Why two? Every activity needs a beginning and end.
Activity(int start, int finish) {
•What’s this? A constructor—like a recipe to make an Activity.
•What’s (int start, int finish)?When I make a new activity, I give it
two numbers.
•Why Activity again? It’s the name of my constructor, matching
the class
•this.start = start;
• What’s this? It means “the object I’m making right now.”
• What’s happening? I’m taking the start number I gave and
storing it in the object’s start.
• Example: If I say new Activity(1, 4), this.start becomes 1.
•this.finish = finish;
• Same thing, but for the finish number (e.g., 4).
•public String toString() {
• What’s this?A method (like a mini-program) that runs when I print the object.
• What’s String?Text, like "Hello".
• What’s public? Anyone can use this method.
•return "Activity [Start=" + start + ", Finish=" + finish + "]";
• What’s return? It gives back an answer.
• What’s happening? It makes text like "Activity [Start=1, Finish=4]".
• What’s +? Glues text and numbers together.
Class: public class ActivitySelection {
•What’s this?Another class—my main program lives here.
•Why public? It’s open for the world (or Java) to run.
Method: public static void selectActivities(Activity[] activities) {
• What’s static? Means I can use this without making an object first.
• What’s void? It doesn’t return anything—just does stuff.
• What’s Activity[] activities? A list (array) of Activity objects.
• Why?This method will pick activities from the list.
•Arrays.sort(activities, (a, b) -> a.finish - b.finish);
• What’s Arrays.sort? A tool to sort my array. (UsesTim Sort)
• What’s (a, b) -> a.finish - b.finish?
• A tiny rule saying “sort by finish time.”
• a and b are two activities.
• a.finish - b.finish: If negative, a goes first (smaller finish time).
• Example: (1,4) vs (3,5). 4 - 5 = -1, so (1,4) comes first.
• Why sort? I need earliest-ending activities first.
•System.out.println("Sorted Activities:");
• What’s this? Prints "Sorted Activities:" to the screen.
for (int i = 0; i < activities.length; i++) {
• What’s for?A loop—repeats stuff.
• What’s int i = 0? Starts a counter at 0.
• What’s i < activities.length? Keeps going until I reach the end of the list.
• activities.length: How many activities (e.g., 6).
• What’s i++?Adds 1 to i each time (0, 1, 2…).
•System.out.println(activities[i]);
• What’s activities[i]? The i-th activity in the list (e.g., i=0 is first).
• What happens? Prints it using toString, like "Activity [Start=1, Finish=4]".
•System.out.println("nSelected Activities:");
• What’s n? A new line—like hitting Enter.
• Why? Separates my sorted list from my picks.
•System.out.println(activities[0]);
• What’s this? Prints the first activity.
• Why? I always pick the first one (earliest finish).
•int lastFinish = activities[0].finish;
• What’s this? Saves the finish time of the first activity (e.g., 4).
• What’s .finish? Gets the finish number from the object.
• Why? I need to compare it to the next start times.
•for (int i = 1; i < activities.length; i++) {
• Why start at 1? I already picked 0,
• so check the rest.
•if (activities[i].start >= lastFinish) {
• What’s if? Only does something if true.
• What’s >=? Greater than or equal to.
• What’s activities[i].start? Start time of the current activity.
• What’s this checking? If the new start is after (or at) the last finish.
• E.g., if lastFinish = 4, and start = 5, then 5 >= 4 is true.
•System.out.println(activities[i]);
• What happens? If no overlap, print this activity.
•lastFinish = activities[i].finish;
• What’s this? Update lastFinish to the new end time.
• Why? Next activity must start after this one.
main Method: public static void main(String[] args) {
• What’s this?Where Java starts running my program.
• What’s String[] args? Stuff I could type when running it (I’m not using it now).
• Activity[] activities = { ... };
• What’s Activity[]? An array of Activity objects.
• What’s { ... }? My list of activities.
• What’s new Activity(1, 4)? Makes an activity starting at 1, ending at 4.
• Example: 6 activities total.
selectActivities(activities);
•What’s this? Runs my selectActivities method with my list.
OUTPUT:

Activity Selection Problem (Greedy Algorithm) with Step-by-Step Explanation

  • 1.
  • 2.
    Activity: A thing todo with a start and end. Start time: When I begin (first number). Finish time: When I’m done (second number). Overlap: When two activities share time (e.g., 1-4 and 3-5 clash). Sort: Put in order by finish time (smallest to biggest). Greedy: Pick what finishes earliest each time. Maximum: Most activities I can fit. Goal: Do the maximum number of activities without overlapping.
  • 3.
    Given List: (1, 4) (3,5) (5, 7) (6, 9) (8, 12) (10, 11) Sort by Finish Time: • (1, 4) → Ends at 4 • (3, 5) → Ends at 5 • (5, 7) → Ends at 7 • (6, 9) → Ends at 9 • (10, 11) → Ends at 11 • (8, 12) → Ends at 12 Format: (StartTime, EndTime)
  • 4.
    Start Picking •First Activity:(1, 4) • Ends at 4. I’ll pick it because it’s the earliest to finish. • My list so far: [(1, 4)] • Last finish time: 4 (when I’m free again). •Next: (3, 5) • Starts at 3, ends at 5. • Can I do it? Check: Does 3 (start) come after 4 (last finish)? • No! 3 is less than 4. They overlap (busy from 1-4, can’t start at 3). • Skip it. Sorted List • (1, 4) • (3, 5) • (5, 7) • (6, 9) • (10, 11) • (8, 12)
  • 5.
    •Next: (5, 7) •Starts at 5, ends at 7. • Check: 5 ≥ 4? Yes! (5 is after 4, or equal works too.) • No overlap—I’m free at 5. Pick it! • My list: [(1, 4), (5, 7)] • Last finish time: 7. •Next: (6, 9) • Starts at 6, ends at 9. • Check: 6 ≥ 7? No, 6 is less than 7. Overlap! • Skip it. Sorted List • (1, 4) • (3, 5) • (5, 7) • (6, 9) • (10, 11) • (8, 12)
  • 6.
    •Next: (10, 11) •Starts at 10, ends at 11. • Check: 10 ≥ 7? Yes! No overlap. • Pick it! • My list: [(1, 4), (5, 7), (10, 11)] • Last finish time: 11. •Next: (8, 12) • Starts at 8, ends at 12. • Check: 8 ≥ 11? No, 8 is less than 11. Overlap! • Skip it. •Done! No more activities. Sorted List • (1, 4) • (3, 5) • (5, 7) • (6, 9) • (10, 11) • (8, 12)
  • 7.
    Final Result: •I picked:(1, 4), (5, 7), (10, 11). •That’s 3 activities! Time: 0 1 2 3 4 5 6 7 8 9 10 11 12 (1,4) |----------| (5,7) |-----| (10,11) |---|
  • 8.
    •Class: Like ablueprint for something (e.g., an Activity). •int: A number (like 1 or 4). •Array: A list of things (like my activities). •new: Makes a new thing (e.g., new Activity(1,4)). •System.out.println: Prints stuff to see it. •for loop: Repeats something (e.g., check each activity).
  • 9.
    import java.util.Arrays; •What’s import?It’s like borrowing a tool from Java’s toolbox. I needArrays to sort stuff later. •Why?Without it, I can’t use Arrays.sort. It’s not built into basic Java.
  • 10.
    •What’s Activity? •It’s forone activity with a start and finish time. Class Definition: class Activity { •What’s class? A blueprint to make objects Inside Activity: • int start, finish; • What’s int? A whole number (like 1, 5, -3). • What’s start, finish?Two numbers I’ll store for each activity. E.g., start at 1, finish at 4. • Why two? Every activity needs a beginning and end.
  • 11.
    Activity(int start, intfinish) { •What’s this? A constructor—like a recipe to make an Activity. •What’s (int start, int finish)?When I make a new activity, I give it two numbers. •Why Activity again? It’s the name of my constructor, matching the class
  • 12.
    •this.start = start; •What’s this? It means “the object I’m making right now.” • What’s happening? I’m taking the start number I gave and storing it in the object’s start. • Example: If I say new Activity(1, 4), this.start becomes 1. •this.finish = finish; • Same thing, but for the finish number (e.g., 4).
  • 13.
    •public String toString(){ • What’s this?A method (like a mini-program) that runs when I print the object. • What’s String?Text, like "Hello". • What’s public? Anyone can use this method. •return "Activity [Start=" + start + ", Finish=" + finish + "]"; • What’s return? It gives back an answer. • What’s happening? It makes text like "Activity [Start=1, Finish=4]". • What’s +? Glues text and numbers together.
  • 14.
    Class: public classActivitySelection { •What’s this?Another class—my main program lives here. •Why public? It’s open for the world (or Java) to run. Method: public static void selectActivities(Activity[] activities) { • What’s static? Means I can use this without making an object first. • What’s void? It doesn’t return anything—just does stuff. • What’s Activity[] activities? A list (array) of Activity objects. • Why?This method will pick activities from the list.
  • 15.
    •Arrays.sort(activities, (a, b)-> a.finish - b.finish); • What’s Arrays.sort? A tool to sort my array. (UsesTim Sort) • What’s (a, b) -> a.finish - b.finish? • A tiny rule saying “sort by finish time.” • a and b are two activities. • a.finish - b.finish: If negative, a goes first (smaller finish time). • Example: (1,4) vs (3,5). 4 - 5 = -1, so (1,4) comes first. • Why sort? I need earliest-ending activities first.
  • 16.
    •System.out.println("Sorted Activities:"); • What’sthis? Prints "Sorted Activities:" to the screen. for (int i = 0; i < activities.length; i++) { • What’s for?A loop—repeats stuff. • What’s int i = 0? Starts a counter at 0. • What’s i < activities.length? Keeps going until I reach the end of the list. • activities.length: How many activities (e.g., 6). • What’s i++?Adds 1 to i each time (0, 1, 2…). •System.out.println(activities[i]); • What’s activities[i]? The i-th activity in the list (e.g., i=0 is first). • What happens? Prints it using toString, like "Activity [Start=1, Finish=4]".
  • 17.
    •System.out.println("nSelected Activities:"); • What’sn? A new line—like hitting Enter. • Why? Separates my sorted list from my picks. •System.out.println(activities[0]); • What’s this? Prints the first activity. • Why? I always pick the first one (earliest finish). •int lastFinish = activities[0].finish; • What’s this? Saves the finish time of the first activity (e.g., 4). • What’s .finish? Gets the finish number from the object. • Why? I need to compare it to the next start times.
  • 18.
    •for (int i= 1; i < activities.length; i++) { • Why start at 1? I already picked 0, • so check the rest. •if (activities[i].start >= lastFinish) { • What’s if? Only does something if true. • What’s >=? Greater than or equal to. • What’s activities[i].start? Start time of the current activity. • What’s this checking? If the new start is after (or at) the last finish. • E.g., if lastFinish = 4, and start = 5, then 5 >= 4 is true. •System.out.println(activities[i]); • What happens? If no overlap, print this activity. •lastFinish = activities[i].finish; • What’s this? Update lastFinish to the new end time. • Why? Next activity must start after this one.
  • 19.
    main Method: publicstatic void main(String[] args) { • What’s this?Where Java starts running my program. • What’s String[] args? Stuff I could type when running it (I’m not using it now). • Activity[] activities = { ... }; • What’s Activity[]? An array of Activity objects. • What’s { ... }? My list of activities. • What’s new Activity(1, 4)? Makes an activity starting at 1, ending at 4. • Example: 6 activities total.
  • 20.
    selectActivities(activities); •What’s this? Runsmy selectActivities method with my list.
  • 22.