Week10_Optimization
Week10_Optimization
2. Greedy Algorithms
Definition
A Greedy algorithm makes a sequence of choices, each of which is the best at
that moment (locally optimal), in the hope that the final result is globally
optimal.
When to Use Greedy Approach
• The problem exhibits the Greedy Choice Property (a global optimum can
be obtained by choosing local optimums).
• The problem has Optimal Substructure (an optimal solution to the
problem contains optimal solutions to its subproblems).
Characteristics
• Simple and fast
• No backtracking
• No need to solve overlapping subproblems
Examples
Problem Description
Activity Selection Select the maximum number of non-overlapping
activities
Huffman Coding Build a binary tree with minimum weighted path
length
Dijkstra’s Algorithm Find shortest path from a source (non-negative
weights)
Fractional Knapsack Choose items with maximum value/weight ratio
until full
Prim's & Kruskal's Construct Minimum Spanning Tree (MST) by
Algorithms greedy edge selection
The knapsack problem states that − given a set of items, holding weights and
profit values, one must determine the subset of the items to be added in a
knapsack such that, the total weight of the items must not exceed the limit of
the knapsack and its total profit value is maximum.
It is one of the most popular problems that take greedy approach to be solved.
It is called as the Fractional Knapsack Problem.
Knapsack Algorithm
The weights (Wi) and profit values (Pi) of the items to be added in the knapsack
are taken as an input for the fractional knapsack algorithm and the subset of
the items added in the knapsack without exceeding the limit and with
maximum profit is achieved as the output.
Algorithm steps:
1. Consider all the items with their weights and profits mentioned
respectively.
2. Calculate Pi/Wi of all the items and sort the items in descending order
based on their Pi/Wi values.
3. Without exceeding the limit, add the items into the knapsack.
4. If the knapsack can still store some weight, but the weights of other
items exceed the limit, the fractional part of the next time can be added.
5. Hence, giving it the name fractional knapsack problem.
Examples
For the given set of items and the knapsack capacity of 10 kg, find the subset
of the items to be added in the knapsack such that the profit is maximum.
Items 1 2 3 4 5
Weights (in kg) 3 3 2 5 1
Profits 10 15 10 12 8
Solution
If we prioritize items with the smallest weight first (greedy approach), we choose
items in ascending order of weight:
Step 1
Given, n = 5
Wi = {3, 3, 2, 5, 1}
Pi = {10, 15, 10, 12, 8}
Calculate Pi/Wi for all the items
Items 1 2 3 4 5
Weights (in kg) 3 3 2 5 1
Profits 10 15 10 20 8
Pi/Wi 3.3 5 5 4 8
Step 2
Arrange all the items in descending order based on Pi/Wi
Items 5 2 3 4 1
Weights (in kg) 1 3 2 5 3
Profits 8 15 10 20 10
Pi/Wi 8 5 5 4 3.3
Step 3
Without exceeding the knapsack capacity, insert the items in the knapsack
with maximum profit.
Knapsack = {5, 2, 3}
However, the knapsack can still hold 4 kg weight, but the next item having 5
kg weight will exceed the capacity. Therefore, only 4 kg weight of the 5 kg will
be added in the knapsack.
Items 5 2 3 4 1
Weights (in kg) 1 3 2 5 3
Profits 8 15 10 20 10
Knapsack 1 1 1 4/5 0
Problem 2:
Solution: