0% found this document useful (0 votes)
33 views4 pages

DSS Lecture 4.: Greedy

The document discusses greedy algorithms and shortest path problems. It provides examples of applying greedy algorithms to filling a bag and finding a hiking path. Greedy algorithms make locally optimal choices at each step without reconsidering previous decisions. This can find a local optimum but miss the global optimum. Dynamic programming is also discussed as an alternative approach to find the shortest path between cities that considers all options.

Uploaded by

Abdullah Elzayat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views4 pages

DSS Lecture 4.: Greedy

The document discusses greedy algorithms and shortest path problems. It provides examples of applying greedy algorithms to filling a bag and finding a hiking path. Greedy algorithms make locally optimal choices at each step without reconsidering previous decisions. This can find a local optimum but miss the global optimum. Dynamic programming is also discussed as an alternative approach to find the shortest path between cities that considers all options.

Uploaded by

Abdullah Elzayat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

DSS | El-Zayat | El-Safwa center | 01007211327

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

• Approximation: A technique used to estimate a value or result that is not


exact but is close enough to be useful in decision making.
• Optimization: The process of finding the best solution or outcome given a
set of constraints or objectives.

> 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.

• Advantages of the Greedy Approach:


1. The greedy approach is easy to implement.
2. Typically have less time complexity.
3. Greedy algorithms can be used for optimization purposes.

• Disadvantages of the Greedy Approach:


1. The local optimal solution may not always be globally optimal.

> Shortest path

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

The graph is representing cities and the routes between them.


Calculate shortest path from fixed node to any other node.
Step1: Mark all nodes as unvisited.
Step2: Assign all nodes a temporary distance value.
Step3: For the current node, calculate the distance to all unvisited neighbors.
Step3.1: Update shortest distance if the distance is shorter than old distance.
Step4: Mark the current node as visited node and replace neighbors’ previous node
value.
Step5: Choose new current node from unvisited nodes with the current minimal
distance.
 Go to step 3 if not finished.
4

DSS | Lecture 4

You might also like