# CSE 490o â Winter 2015 CS Unplugged Activity

```CSE 490o – Winter 2015
CS Unplugged Activity
World's Worst Petting Zoo – Interval Scheduling and Greedy Algorithms
Summary
Not all algorithms need to be sophisticated. A Greedy Algorithm is one that myopically chooses whatever seems to be the ideal next step right now, with little regard for the big picture or other available data. Sometimes they turn out to be quite useful, even if they aren't necessarily correct. This activity puts students in charge of scheduling not­very­pettable animals to appear at a petting zoo over the course of a day. Required Skills
•
•
•
Time intervals
Counting
Planning
Ages
•
12 years and up
Materials
•
•
•
1 Timeline Mat (for each group)
26 Animal Tiles (for each group)
Dry erase markers (for the demonstration)
Demonstration (~10 minutes)
In this part of the presentation, you will explain the objective to the students and briefly demonstrate the tasks they will perform.
1. Describe the scenario: An eccentric billionaire has decided to open a petting zoo filled with exotic animals! His choices seem a bit odd: anacondas, komodo dragons, and warthogs hardly seem like appropriate picks for such an endeavor. Nevertheless, he has hired you to schedule the animals in such a way that as many as possible are available for the eager public to cuddle!
2. Draw the following partial time­line on the board:
9 AM
noon
3 PM
3. Explain that each of the animals is only awake for certain hours of the day. People aren't going to show up to pet sleeping animals, so we'd like to put the animals on display while they are awake. For example, Uromastyx is usually awake from 9 AM to 11 AM. So we could put him on the schedule like this:
Uromastyx (9am - 11am)
9 AM
noon
3 PM
4. Similarly Vulture is awake from Noon until 3 PM, so we could add her to the schedule like this:
Uromastyx (9am - 11am)
Vulture (12pm - 3pm)
noon
9 AM
3 PM
5. Now you'll have explain a sad reality of the animal kingdom: None of the animals really like each other, and they will get into nasty fights if they are scheduled at the same time. So, unfortunately we can't currently add Walrus (awake 10 AM ­ Noon) to the schedule, because it creates a scheduling conflict with Uromastyx from 10 to 11. Illustrate this below as follows:
Walrus (10am - 12pm)
Uromastyx (9am - 11am)
Vulture (12pm - 3pm)
noon
9 AM
3 PM
6. With this in mind, it looks like we can only schedule a single animal for any given time interval. So in this case, we could make room for Walrus if we removed Uromastyx from the schedule:
Walrus (10am - 12pm)
9 AM
Vulture (12pm - 3pm)
noon
3 PM
Activity (~15 minutes)
1. Hand out a Timeline mat and a set of Animal Tiles to each group of students (groups of 2­4 are ideal)
2. Explain to the students that their job is to schedule as many animals as possible in the course of a single day. They do not need to worry about filling as much time as possible; they want to maximize the number of animals shown, even if this means potentially leaving holes in the schedule where no animals are scheduled.
3. Before they begin, encourage them to think of some methodologies for organizing their schedules (For example: suggest that they sort the animals' awake time from shortest to longest and then try to fit as many of the smaller intervals onto the schedule). Caution them that it will be very difficult to find an optimal solution if they use a haphazard trial­and­error approach.
4. If a group of students thinks that they have found an optimal solution, they should alert the instructor who can check their work (solution is below). The instructor should ask them about how they arrived at their solution.
Solution (~15 minutes)
This unusual petting zoo is an example of what is known as The Interval Scheduling Problem, a common introduction to greedy algorithms among undergraduate computer science students.
The key to solving the problem lies in step 3 of the activity. There are many ways for the students to organize the intervals (length of time, start time, end time, number of conflicts with other intervals, etc.)
To obtain the optimal solution, the key to organize the intervals by their end times. From there, finding the solution is as simple as “greedily” choosing the animal that has the earliest end time (and has no conflicts with any animals already on the timeline) and assigning it to the timeline. This can be repeated until there are no more choices left.
If a group figures this out, they should be able to schedule Ten animals to the timeline as follows:
Alligator
Lion
Gorilla
Octopus
Rhino
Iguana
Turkey
Hippo
Mammoth
Shark
(6:30 – 8:00 AM)
(9:00 – 11:00 AM)
(11:00 AM ­ Noon)
(12:30 – 2:00 PM)
(2:30 – 3:30 PM)
(3:30 – 5:00 PM)
(5:30 – 7:00 PM)
(7:30 – 8:30 PM)
(9:00 – 11:00 PM)
(11:00 PM – Midnight)
Note also that the first letter of each of the scheduled animals spells out the word ALGORITHMS.
There is a misplaced notion that in order to solve a computer science problem, a person must be able to come up with a complex approach that most people without extensive training in the field might not be able to understand. While this is sometimes true, it is often the case that an extremely simple approach will often lead to a solution that works.
The notion of “greedy algorithms” takes this idea to the extreme. When faced with a problem that requires a discrete series of steps in order to arrive at a solution, a greedy algorithm looks at all of the currently available choices and picks the one that's best RIGHT NOW. The greedy algorithm continues to do this at every step until a solution is found.
This is not to suggest that greedy algorithms can be used to solve all problems. In fact, greedy algorithms are often incorrect and can fail in spectacular ways. Specifically, they are prone to two types of failures: •
•
Even if a better solution is obvious, a greedy algorithm may not find it, due to its short­
sightedness. Greedy algorithms can sometimes get stuck in “corners” from which they cannot escape. These corners may or may not be close to an optimal solution
Why is it important to understand what greedy algorithms are and how they work? From an educational standpoint, they are useful because they are generally very simple to explain. They don't exhibit complex blocks of logic or require the need for intricate inner details. At the same time, it is important to emphasize that the ordering step is important in order for a greedy algorithm to have any value. In our classroom example, we made it clear that part of the exercise was to order the animals' intervals by their characteristics in various ways before assigning them to the timeline. As detailed in the solution, many of the intervals don't yield an optimal schedule, but this presents another great learning opportunity. Namely, just because one particular ordering doesn't seem to be helpful, doesn't mean that there aren't other orderings that might yield valuable results.
In addition, there are several ways to demonstrate how students might use greedy algorithms in their daily lives. Perhaps the most well­known is the “Cashier's Algorithm”. Pretend that you are purchasing an item that costs 51 cents from a student, but that you only have a dollar to pay for it. If you were to ask them how they would give you your 49 cents change for your dollar, they would almost instinctively respond with “One quarter, Two dimes, and Four pennies”. They may not realize it, but they are using a greedy algorithm to perform this calculation. Here's what is likely happening in their minds:
I owe the teacher 49 cents.
What is the largest coin I can use as part of this total?
Can I use a quarter? YES!
Now how much do I owe the teacher?
49 – 25 = 24 cents
I owe the teacher 24 cents.
What is the largest coin I can use as part of this total?
Can I use a quarter? NO.
Can I use a dime? YES!
Now how much do I owe the teacher?
24 – 10 = 14 cents
I owe the teacher 14 cents.
Etc...
```