DSS Lecture 4.: Greedy
DSS Lecture 4.: Greedy
DSS Lecture 4.
Quick recap on lecture 3 pre-processing part (group discussion):
Implement data pre-processing on the below table.
Note that this table will be used in analyzing the growth of each one in the table.
ID Name Gender Height Weight (lbs) Blood
(inches) Pressure
1 Ahmed M 70 120 120/80
2 Mohamed M 75 115 125/75
1 Ahmed M 70 120 120/80
3 Mona F 60 100 120/70
4 Rahma F 55 115/75
5 Soha F 60 400 115/75
> Greedy.
Example:
Imagine you are going for hiking and your goal is to reach the highest peak possible.
You already have the map before you start, but there are thousands of possible paths
shown on the map. You are too lazy and simply don’t have the time to evaluate each
of them. Screw the map! You started hiking with a simple strategy – be greedy and
short-sighted.
Just take paths that slope upwards the most. This seems like a good strategy for
hiking. But is it always the best ?
DSS | Lecture 4
DSS | El-Zayat | El-Safwa center | 01007211327
After the trip ended and your whole body is sore and tired, you look at the hiking
map for the first time. Oh my god! There’s a muddy river that I should’ve crossed,
instead of keep walking upwards. This means that a greedy algorithm picks the best
immediate choice and never reconsiders its choices. In terms of optimizing a
solution, this simply means that the greedy solution will try and find local optimum
solutions - which can be many - and might miss out on a global optimum solution.
• Formal Definition
Assume that you have an objective function that needs to be optimized (either
MAXIMIZED or MINIMIZED) at a given point. A Greedy algorithm makes greedy
choices at each step to ensure that the objective function is optimized. The Greedy
algorithm has only one shot to compute the optimal solution so that it never goes
back and reverses the decision.
• Lectures’ example:
A bag with capacity of 860 grams, we want to fill it using greedy technique.
Consider to not to exceed the bag capacity.
Items
500 grams with profit = 6
450 grams with profit = 4
410 grams with profit = 3
300 grams with profit = 0.5
Start by selecting the biggest values from top to down.
500 + 450 = 950 (exceeds the bag limit, so we can’t select).
500 + 410 = 910 (exceeds the bag limit, so we can’t select).
500 + 300 = 910 (NOT exceeding the bag limit).
So, we combine their profits to be 6 + 0.5, so the greedy technique gives us a profit of
6.5, While it’s a solution but it’s not the best one as he could choose 450 + 410 to
match 860 exactly with a value of 7.
DSS | Lecture 4
DSS | El-Zayat | El-Safwa center | 01007211327
And we can consider it as an approximate solution as the optimal one is there, but it
couldn't give us it.
NOTE: Change the value only if the new value is less than it.
Using greedy, Suppose that the numbers is the distance to reach each point.
DSS | Lecture 4
DSS | El-Zayat | El-Safwa center | 01007211327
1. Let's start with the root node 20. The weight of the right child is 3 and the weight
of the left child is 2.
2. Our problem is to find the shortest path. And, the optimal solution at the moment
is 2. So, the greedy algorithm will choose 2.
3. Now and at the moment we have two choices, 7 and 10, so the greedy will choose
7.
3. This gives us our final result 20 + 2 + 7 = 29.
However, it is not the optimal solution. There is another path that carries less weight
(20 + 3 + 1 = 24).
Using Dynamic programming (group discussion).
6
2 E
9
A 5 3 1 C
8 D F 3
2
DSS | Lecture 4