# Huffman Coding – Activity Selection Problem

Huffman Coding -: Greedy Algorithms 👇 👇 🙂
HUFFMAN CODING

The Problem
You have a set of things to do (activities). Each activity has a start time and a end time. You aren’t allowed to perform more than one activity at a time. Your task is to ﬁnd a way to perform the maximum number of activities.
For example, suppose you have a selection of classes to choose from.

Remember, you can’t take two classes at the same time. That means you can’t take class 1 and 2 because they share a common time 10.30 A.M to 11.00 A.M. However, you can take class 1 and 3 because they don’t share a common time.

So your task is to take maximum number of classes as possible without any overlap. How can you do that?

Analysis

Lets think for the solution by greedy approach.First of all we randomly chose some approach and check that will work or not.

• sort the activity by start time that means which activity start ﬁrst we will take them ﬁrst. then take ﬁrst to last from sorted list and check it will intersect from previous taken activity or not. If the current activity is not intersect with the previously taken activity, we will perform the activity otherwise we will not perform. this approach will work for some cases like

the sorting order will be 4–>1–>2–>3 .The activity 4–> 1–> 3 will be performed and the activity 2 will be skipped. the maximum 3 activity will be performed. It works for this type of cases. but it will fail for some cases. Lets apply this approach for the case

The sort order will be 4–>1–>2–>3 and only activity 4 will be performed but the answer can be activity 1–>3 or 2–>3 will be performed. So our approach will not work for the above case. Let’s try another approach

• Sort the activity by time duration that means perform the shortest activity ﬁrst. that can solve the previous problem . Although the problem is not completely solved. There still some cases that can fail the solution. Apply this approach on the case bellow.

if we sort the activity by time duration the sort order will be 2–> 3 —>1 . and if we perform activity No. 2 ﬁrst then no other activity can be performed. But the answer will be perform activity 1 then perform 3.
So we can perform maximum 2 activity.So this can not be a solution of this problem. We should try a diﬀerent approach.

The solution

• Sort the Activity by ending time that means the activity ﬁnishes ﬁrst that come ﬁrst. the algorithm is given below

1. Sort the activities by its ending times.
2. If the activity to be performed do not share a common time with the activities that previously performed, perform the activity.

Lets analyse the ﬁrst example

sort the activity by its ending times , So sort order will be 1–>5–>2–>4–>3.. the answer is 1–>3 these two activities will be performed. ans that’s the answer. here is the sudo code.

1. sort: activities
2. perform ﬁrst activity from the sorted list of activities.
3. Set : Current_activity := ﬁrst activity
4. set: end_time := end_time of Current activity
5. go to next activity if exist, if not exist terminate .
6. if start_time of current activity <= end_time : perform the activity and go to 4
7. else: got to 5.

Huffman Coding -: Change-making problem 👇 👇 🙂
👉 👉