0% found this document useful (0 votes)
4 views15 pages

L9 Greedy, Knapsack

The document discusses the Greedy Algorithm, particularly in the context of the Knapsack Problem, which is an optimization problem aimed at maximizing profit within given constraints. It outlines the principles of greedy algorithms, their applications, and provides examples of fractional knapsack problems along with their algorithms. The time complexity for solving the knapsack problem using a greedy approach is noted as O(n log n).

Uploaded by

aryanjawla01
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)
4 views15 pages

L9 Greedy, Knapsack

The document discusses the Greedy Algorithm, particularly in the context of the Knapsack Problem, which is an optimization problem aimed at maximizing profit within given constraints. It outlines the principles of greedy algorithms, their applications, and provides examples of fractional knapsack problems along with their algorithms. The time complexity for solving the knapsack problem using a greedy approach is noted as O(n log n).

Uploaded by

aryanjawla01
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/ 15

Greedy Algorithm

Knapsack Problem
Lecture 12
Optimization Problems
➢ Finding out the optimal solution from all feasible solutions.
➢ Solution Space: Set of all solution over the given n input.
➢ Feasible Solution: Solutions from the solution space which will satisfy the constraints (conditions).
➢ Optimal Solution: One of the feasible solution which optimizes our goal.
➢ Optimization problem can be either maximization problem or minimization problem.
➢ Techniques used to solve Optimization Problems:
▪ Greedy Algorithms
▪ Dynamic Algorithms
▪ Branch and Bound
Greedy Algorithm
▪ The idea of a greedy algorithm is to make the choice that looks best at that moment.
▪ Most of the problems in greedy technique contain ‘n’ inputs (solution space) and our
objective is finding a subset which will produce optimal solution.
▪ Does not cover all combinations (takes less time).
▪ Does not always yield optimal solution.
Greedy Algorithm: Example
Problem: Want to travel from Delhi to Mumbai in 18 hours with minimum cost.
Solution Space: on foot, Bike, Bus, Car, Train, Flight.
Feasible Solution: Train, Flight (18 hours)
Optimal Solution: Train (minimum cost)
Greedy Algorithm: Applications
1. Knapsack Problem (fractional)
2. Huffman Code
3. Activity Selection Problem
4. Minimum Spanning Trees :
▪ Prim’s Algorithm
▪ Kruskal’s Algorithm
5. Single Source Shortest Paths:
▪ Dijkstra's Algorithm
▪ Bellman Ford
Knapsack Problem (Fractional)
Problem: We are given ‘n’ objects and a knapsack or bag. Object i has a weight wi and the knapsack has a capacity
W. If fraction xi, 0 ≤ xi≤1, is placed into the knapsack, then a profit of xipi is earned. The objective is to obtain a filling
of the knapsack that maximizes the total profit earned. Since the knapsack capacity is W, we require the total weight
of all chosen objects to be at most W.

Type of Problem: Optimization problem → maximization problem


Formal Representation:
Problem: σ𝑛𝑖=1 𝑤𝑖 > 𝑊
Feasible Solution: σ𝑛𝑖=1 𝑥𝑖 𝑤𝑖 ≤ 𝑊
Optimal Solution: σ𝑛𝑖=1 𝑥𝑖 𝑝𝑖 (𝑠ℎ𝑜𝑢𝑙𝑑 𝑏𝑒 𝑚𝑎𝑥𝑖𝑚𝑢𝑚)
Fractional Knapsack – Example 1

Rs 120
Item 3 30 +

Item 2 50
20 Rs 100
Item 1 30
20
10 Rs 220
Rs60 Rs100 Rs120
Fractional Knapsack – Example 2

• E.g.: 20
Rs 80
Item 3 +

Item 2 50 50
20 Rs 100
Item 1 30
20 +
10 10 Rs 60

Rs60 Rs100 Rs120 Rs 240


Knapsack Problem: Algorithm
𝑝
Step 1: Find 𝑤𝑖 for all the objects. →O(n)
𝑖

𝑝
Step 2: Sort the array based on 𝑤𝑖 in decreasing
𝑖

order. →O(nlogn)
Step 3: Take one by one object until knapsack
size becomes zero. →O(n)

Time complexity: O (nlogn)


n=3 M=20

Object Object1 Object2 Object3


Profit 25 24 15
Weight 18 15 10
Problem 1
Number of objects n=3,
knapsack size W=20

Objects i Object 1 Object 2 Object 3


Profits p[i] 25 24 15
Weights w[i] 18 15 10
25/18=1.39 24/15=1.6 15/10=1.5
Rs 7.5
5
+

50
Rs 24
15

Rs 31.5
Problem 2
Objects i Object 1 Object 2 Object 3 Object 4 Object 5
Profits p[i] 25 55 45 20 35
Weights w[i] 5 15 12 7 6
Number of objects n=5,
Knapsack size W=3.5
Problem 3
Objects i Object 1 Object 2 Object 3 Object 4 Object 5 Object 6 Object 7
Profits p[i] 10 5 15 7 6 18 3
Weights w[i] 2 3 5 7 1 4 1
Number of objects n=7,
Knapsack size W=15.
n=5 M=15

Object Object1 Object2 Object3 Object4 Object5

Profit 2 28 25 18 9
Weight 1 4 5 3 3
Thank You

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy