Fractional Knapsack Problem

H
GANDHINAGAR INSTITUTE OF TECHNOLOGY
Computer Engineering Department
ADA (2150703)
Fractional knapsack problem
Prepared By:
Harsh Kothari
(170120107066)
Guided By:
Prof. Kiran Shah
Contents:
What is greedy approach?
Greedy Algorithms and techniques.
Knapsack problem.
Knapsack Algorithm.
Example of Knapsack problem.
Greedy Algorithms:
• Many real-world problems are optimization problems in that they
attempt to find an optimal solution among many possible
candidate solutions.
• An optimization problem is one in which you want to find, not just a
solution, but the best
solution
• A “greedy algorithm” sometimes works well for optimization problems
• A greedy algorithm works in phases. At each phase: You take the best
you can get right now, without regard for future consequences.You hope
that by choosing a local optimum at each step, you will end up at a
global optimum.
• Constructs a solution to an optimization problem piece by
• piece through a sequence of choices that are:
1.feasible, i.e. satisfying the constraints.
2.locally optimal (with respect to some neighborhood definition).
3.greedy (in terms of some measure).
Greedy Technique:
Knapsack Problem:
• Given n objects each have a weight wi and a value vi , and given
a knapsack of total capacity W. The problem is to pack the
knapsack with these objects in order to maximize the total value
of those objects packed without exceeding the knapsack’s
capacity.
• More formally, let xi denote the fraction of the object i to be
included in the knapsack, 0 
xi  1, for 1  i  n. The problem is to find values for the xi
such that
n n
 xivi
i1
 xi wi
i1
is maximized. W and
The optimal Knapsack Algorithm:
This algorithm is for time complexity O(n lgn))
(1) Sort the n objects from large to small based on the ratios vi/wi . We
assume the
arrays w[1..n] and v[1..n] store the respective weights and
values after sorting.
(2) initialize array x[1..n] to zeros.
(3) weight = 0; i = 1
(4) while (i  n and weight < W) do
(I) if weight + w[i]  W then x[i] = 1
(II) else x[i] = (W – weight) / w[i]
(III) weight = weight + x[i] * w[i]
(IV) i++
Example of Knapsack problem:
 Let us consider that the capacity of the knapsack W = 60 and the list
of provided items are shown in the following table −
Item A B C D
Profit 280 100 120 120
Weight 40 10 20 24
Ratio 7 10 6 5
 As the provided items are not sorted. After sorting, the items are as shown in the
following table.
Item B A C D
Profit 100 280 120 120
Weight 10 40 20 24
Ratio 10 7 6 5
 After sorting all the items, First all of B is chosen as weight
of B is less than the capacity of the knapsack.
 Next, item A is chosen, as the available capacity of the knapsack
is greater than the weight of A.
 Now, C is chosen as the next item. However, the whole item
cannot be chosen as the remaining capacity of the knapsack is
less than the weight of C.
 Hence, fraction of C (i.e. (60 − 50)/20) is chosen.
Solution:
 Now, the capacity of the Knapsack is equal to the selected items.
Hence, no more item can be selected.
 The total weight of the selected items is
10 + 40 + 20 * (10/20) = 60
And the total profit is
100 + 280 + 120 * (10/20) = 380 + 60 = 440
 This is the optimal solution. We cannot gain more profit
selecting any different combination of items.
Cont.
Item A B C D
Selected 1 1 0.5 0
Fractional Knapsack Problem
1 de 10

Recomendados

Knapsack problem algorithm, greedy algorithm por
Knapsack problem algorithm, greedy algorithmKnapsack problem algorithm, greedy algorithm
Knapsack problem algorithm, greedy algorithmHoneyChintal
984 vistas9 diapositivas
Knapsack problem using greedy approach por
Knapsack problem using greedy approachKnapsack problem using greedy approach
Knapsack problem using greedy approachpadmeshagrekar
609 vistas14 diapositivas
greedy algorithm Fractional Knapsack por
greedy algorithmFractional Knapsack greedy algorithmFractional Knapsack
greedy algorithm Fractional Knapsack Md. Musfiqur Rahman Foysal
227 vistas45 diapositivas
Matrix chain multiplication por
Matrix chain multiplicationMatrix chain multiplication
Matrix chain multiplicationRespa Peter
33.8K vistas27 diapositivas
Greedy Algorithm - Knapsack Problem por
Greedy Algorithm - Knapsack ProblemGreedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemMadhu Bala
24.5K vistas15 diapositivas
Knapsack Problem (DP & GREEDY) por
Knapsack Problem (DP & GREEDY)Knapsack Problem (DP & GREEDY)
Knapsack Problem (DP & GREEDY)Ridhima Chowdhury
4.2K vistas14 diapositivas

Más contenido relacionado

La actualidad más candente

Dynamic Programming-Knapsack Problem por
Dynamic Programming-Knapsack ProblemDynamic Programming-Knapsack Problem
Dynamic Programming-Knapsack ProblemAmrita Yadav
731 vistas19 diapositivas
Greedy algorithms por
Greedy algorithmsGreedy algorithms
Greedy algorithmsRajendran
2.1K vistas20 diapositivas
0/1 knapsack por
0/1 knapsack0/1 knapsack
0/1 knapsackAmin Omi
5.3K vistas39 diapositivas
N queen problem por
N queen problemN queen problem
N queen problemRidhima Chowdhury
6.8K vistas12 diapositivas
Greedy algorithm por
Greedy algorithmGreedy algorithm
Greedy algorithmInternational Islamic University
6.4K vistas48 diapositivas
The Maximum Subarray Problem por
The Maximum Subarray ProblemThe Maximum Subarray Problem
The Maximum Subarray ProblemKamran Ashraf
3.2K vistas8 diapositivas

La actualidad más candente(20)

Dynamic Programming-Knapsack Problem por Amrita Yadav
Dynamic Programming-Knapsack ProblemDynamic Programming-Knapsack Problem
Dynamic Programming-Knapsack Problem
Amrita Yadav731 vistas
Greedy algorithms por Rajendran
Greedy algorithmsGreedy algorithms
Greedy algorithms
Rajendran 2.1K vistas
0/1 knapsack por Amin Omi
0/1 knapsack0/1 knapsack
0/1 knapsack
Amin Omi5.3K vistas
The Maximum Subarray Problem por Kamran Ashraf
The Maximum Subarray ProblemThe Maximum Subarray Problem
The Maximum Subarray Problem
Kamran Ashraf3.2K vistas
Shortest path algorithms por Amit Rathi
Shortest path algorithmsShortest path algorithms
Shortest path algorithms
Amit Rathi1.8K vistas
0 1 knapsack using branch and bound por Abhishek Singh
0 1 knapsack using branch and bound0 1 knapsack using branch and bound
0 1 knapsack using branch and bound
Abhishek Singh24.5K vistas
daa-unit-3-greedy method por hodcsencet
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
hodcsencet2.1K vistas
Knapsack problem por Vikas Sharma
Knapsack problemKnapsack problem
Knapsack problem
Vikas Sharma9.9K vistas
Assignment problem branch and bound.pptx por KrishnaVardhan50
Assignment problem branch and bound.pptxAssignment problem branch and bound.pptx
Assignment problem branch and bound.pptx
KrishnaVardhan501.5K vistas
Job sequencing with Deadlines por YashiUpadhyay3
Job sequencing with DeadlinesJob sequencing with Deadlines
Job sequencing with Deadlines
YashiUpadhyay31.6K vistas
Knapsack Problem por Jenny Galino
Knapsack ProblemKnapsack Problem
Knapsack Problem
Jenny Galino104.5K vistas
Knapsack problem using dynamic programming por khush_boo31
Knapsack problem using dynamic programmingKnapsack problem using dynamic programming
Knapsack problem using dynamic programming
khush_boo315.3K vistas
Job sequencing with deadline por Arafat Hossan
Job sequencing with deadlineJob sequencing with deadline
Job sequencing with deadline
Arafat Hossan3.6K vistas

Similar a Fractional Knapsack Problem

12 Greeddy Method por
12 Greeddy Method12 Greeddy Method
12 Greeddy MethodAndres Mendez-Vazquez
634 vistas110 diapositivas
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal's por
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal'sGreedy algorithms -Making change-Knapsack-Prim's-Kruskal's
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal'sJay Patel
3.1K vistas21 diapositivas
Knapsack problem por
Knapsack problemKnapsack problem
Knapsack problemgarishma bhatia
134 vistas13 diapositivas
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt por
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.pptParallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.pptBinayakMukherjee4
4 vistas45 diapositivas
Design and analysis of Algorithms - Lecture 15.ppt por
Design and analysis of Algorithms - Lecture 15.pptDesign and analysis of Algorithms - Lecture 15.ppt
Design and analysis of Algorithms - Lecture 15.pptQurbanAli72
7 vistas19 diapositivas
Optimization problems por
Optimization problemsOptimization problems
Optimization problemsRuchika Sinha
84 vistas45 diapositivas

Similar a Fractional Knapsack Problem(20)

Greedy algorithms -Making change-Knapsack-Prim's-Kruskal's por Jay Patel
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal'sGreedy algorithms -Making change-Knapsack-Prim's-Kruskal's
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal's
Jay Patel3.1K vistas
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt por BinayakMukherjee4
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.pptParallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
Design and analysis of Algorithms - Lecture 15.ppt por QurbanAli72
Design and analysis of Algorithms - Lecture 15.pptDesign and analysis of Algorithms - Lecture 15.ppt
Design and analysis of Algorithms - Lecture 15.ppt
QurbanAli727 vistas
knapsackusingbranchandbound por hodcsencet
knapsackusingbranchandboundknapsackusingbranchandbound
knapsackusingbranchandbound
hodcsencet139 vistas
Greedy+Day-3.pptx por Akswant
Greedy+Day-3.pptxGreedy+Day-3.pptx
Greedy+Day-3.pptx
Akswant1 vista
Knapsack Dynamic por Paras Patel
Knapsack DynamicKnapsack Dynamic
Knapsack Dynamic
Paras Patel382 vistas
lect por farazch
lectlect
lect
farazch268 vistas
lec por farazch
leclec
lec
farazch474 vistas
lecture 26 por sajinsc
lecture 26lecture 26
lecture 26
sajinsc1.7K vistas
Lecture34 por farazch
Lecture34Lecture34
Lecture34
farazch356 vistas
lect por farazch
lectlect
lect
farazch478 vistas
0 1 knapsack using naive recursive approach and top-down dynamic programming ... por Abhishek Singh
0 1 knapsack using naive recursive approach and top-down dynamic programming ...0 1 knapsack using naive recursive approach and top-down dynamic programming ...
0 1 knapsack using naive recursive approach and top-down dynamic programming ...
Abhishek Singh5.1K vistas

Más de harsh kothari

Instructuion set of 8085 por
Instructuion set of 8085Instructuion set of 8085
Instructuion set of 8085harsh kothari
406 vistas15 diapositivas
Java(Polymorphism) por
Java(Polymorphism)Java(Polymorphism)
Java(Polymorphism)harsh kothari
7.4K vistas13 diapositivas
stirling method maths por
stirling method mathsstirling method maths
stirling method mathsharsh kothari
1.5K vistas10 diapositivas
170120107066 dbms por
170120107066 dbms170120107066 dbms
170120107066 dbmsharsh kothari
92 vistas8 diapositivas
170120107066 dielectric.ppt por
170120107066 dielectric.ppt170120107066 dielectric.ppt
170120107066 dielectric.pptharsh kothari
1.7K vistas12 diapositivas
Hk por
HkHk
Hkharsh kothari
108 vistas7 diapositivas

Más de harsh kothari(11)

Instructuion set of 8085 por harsh kothari
Instructuion set of 8085Instructuion set of 8085
Instructuion set of 8085
harsh kothari406 vistas
stirling method maths por harsh kothari
stirling method mathsstirling method maths
stirling method maths
harsh kothari1.5K vistas
170120107066 dielectric.ppt por harsh kothari
170120107066 dielectric.ppt170120107066 dielectric.ppt
170120107066 dielectric.ppt
harsh kothari1.7K vistas
170120107074 looping statements and nesting of loop statements por harsh kothari
170120107074 looping statements and nesting of loop statements170120107074 looping statements and nesting of loop statements
170120107074 looping statements and nesting of loop statements
harsh kothari1.2K vistas
170120107066 chronemics.ppt por harsh kothari
170120107066 chronemics.ppt170120107066 chronemics.ppt
170120107066 chronemics.ppt
harsh kothari5.9K vistas
170120107066 power series.ppt por harsh kothari
170120107066 power series.ppt170120107066 power series.ppt
170120107066 power series.ppt
harsh kothari1.8K vistas
170120107066 flowchart.ppt por harsh kothari
170120107066 flowchart.ppt170120107066 flowchart.ppt
170120107066 flowchart.ppt
harsh kothari129 vistas

Último

How I learned to stop worrying and love the dark silicon apocalypse.pdf por
How I learned to stop worrying and love the dark silicon apocalypse.pdfHow I learned to stop worrying and love the dark silicon apocalypse.pdf
How I learned to stop worrying and love the dark silicon apocalypse.pdfTomasz Kowalczewski
24 vistas66 diapositivas
Codes and Conventions.pptx por
Codes and Conventions.pptxCodes and Conventions.pptx
Codes and Conventions.pptxIsabellaGraceAnkers
7 vistas5 diapositivas
Wire Rope por
Wire RopeWire Rope
Wire RopeIwiss Tools Co.,Ltd
9 vistas5 diapositivas
SEMI CONDUCTORS por
SEMI CONDUCTORSSEMI CONDUCTORS
SEMI CONDUCTORSpavaniaalla2005
20 vistas8 diapositivas
SNMPx por
SNMPxSNMPx
SNMPxAmatullahbutt
14 vistas12 diapositivas
SWM L15-L28_drhasan (Part 2).pdf por
SWM L15-L28_drhasan (Part 2).pdfSWM L15-L28_drhasan (Part 2).pdf
SWM L15-L28_drhasan (Part 2).pdfMahmudHasan747870
28 vistas93 diapositivas

Último(20)

How I learned to stop worrying and love the dark silicon apocalypse.pdf por Tomasz Kowalczewski
How I learned to stop worrying and love the dark silicon apocalypse.pdfHow I learned to stop worrying and love the dark silicon apocalypse.pdf
How I learned to stop worrying and love the dark silicon apocalypse.pdf
DevOps to DevSecOps: Enhancing Software Security Throughout The Development L... por Anowar Hossain
DevOps to DevSecOps: Enhancing Software Security Throughout The Development L...DevOps to DevSecOps: Enhancing Software Security Throughout The Development L...
DevOps to DevSecOps: Enhancing Software Security Throughout The Development L...
Anowar Hossain12 vistas
NEW SUPPLIERS SUPPLIES (copie).pdf por georgesradjou
NEW SUPPLIERS SUPPLIES (copie).pdfNEW SUPPLIERS SUPPLIES (copie).pdf
NEW SUPPLIERS SUPPLIES (copie).pdf
georgesradjou14 vistas
STUDY OF SMART MATERIALS USED IN CONSTRUCTION-1.pptx por AnnieRachelJohn
STUDY OF SMART MATERIALS USED IN CONSTRUCTION-1.pptxSTUDY OF SMART MATERIALS USED IN CONSTRUCTION-1.pptx
STUDY OF SMART MATERIALS USED IN CONSTRUCTION-1.pptx
AnnieRachelJohn31 vistas
What is Whirling Hygrometer.pdf por IIT KHARAGPUR
What is Whirling Hygrometer.pdfWhat is Whirling Hygrometer.pdf
What is Whirling Hygrometer.pdf
IIT KHARAGPUR 11 vistas
CHI-SQUARE ( χ2) TESTS.pptx por ssusera597c5
CHI-SQUARE ( χ2) TESTS.pptxCHI-SQUARE ( χ2) TESTS.pptx
CHI-SQUARE ( χ2) TESTS.pptx
ssusera597c529 vistas
Update 42 models(Diode/General ) in SPICE PARK(DEC2023) por Tsuyoshi Horigome
Update 42 models(Diode/General ) in SPICE PARK(DEC2023)Update 42 models(Diode/General ) in SPICE PARK(DEC2023)
Update 42 models(Diode/General ) in SPICE PARK(DEC2023)
Tsuyoshi Horigome19 vistas
Informed search algorithms.pptx por Dr.Shweta
Informed search algorithms.pptxInformed search algorithms.pptx
Informed search algorithms.pptx
Dr.Shweta13 vistas

Fractional Knapsack Problem

  • 1. GANDHINAGAR INSTITUTE OF TECHNOLOGY Computer Engineering Department ADA (2150703) Fractional knapsack problem Prepared By: Harsh Kothari (170120107066) Guided By: Prof. Kiran Shah
  • 2. Contents: What is greedy approach? Greedy Algorithms and techniques. Knapsack problem. Knapsack Algorithm. Example of Knapsack problem.
  • 3. Greedy Algorithms: • Many real-world problems are optimization problems in that they attempt to find an optimal solution among many possible candidate solutions. • An optimization problem is one in which you want to find, not just a solution, but the best solution • A “greedy algorithm” sometimes works well for optimization problems • A greedy algorithm works in phases. At each phase: You take the best you can get right now, without regard for future consequences.You hope that by choosing a local optimum at each step, you will end up at a global optimum.
  • 4. • Constructs a solution to an optimization problem piece by • piece through a sequence of choices that are: 1.feasible, i.e. satisfying the constraints. 2.locally optimal (with respect to some neighborhood definition). 3.greedy (in terms of some measure). Greedy Technique:
  • 5. Knapsack Problem: • Given n objects each have a weight wi and a value vi , and given a knapsack of total capacity W. The problem is to pack the knapsack with these objects in order to maximize the total value of those objects packed without exceeding the knapsack’s capacity. • More formally, let xi denote the fraction of the object i to be included in the knapsack, 0  xi  1, for 1  i  n. The problem is to find values for the xi such that n n  xivi i1  xi wi i1 is maximized. W and
  • 6. The optimal Knapsack Algorithm: This algorithm is for time complexity O(n lgn)) (1) Sort the n objects from large to small based on the ratios vi/wi . We assume the arrays w[1..n] and v[1..n] store the respective weights and values after sorting. (2) initialize array x[1..n] to zeros. (3) weight = 0; i = 1 (4) while (i  n and weight < W) do (I) if weight + w[i]  W then x[i] = 1 (II) else x[i] = (W – weight) / w[i] (III) weight = weight + x[i] * w[i] (IV) i++
  • 7. Example of Knapsack problem:  Let us consider that the capacity of the knapsack W = 60 and the list of provided items are shown in the following table − Item A B C D Profit 280 100 120 120 Weight 40 10 20 24 Ratio 7 10 6 5  As the provided items are not sorted. After sorting, the items are as shown in the following table. Item B A C D Profit 100 280 120 120 Weight 10 40 20 24 Ratio 10 7 6 5
  • 8.  After sorting all the items, First all of B is chosen as weight of B is less than the capacity of the knapsack.  Next, item A is chosen, as the available capacity of the knapsack is greater than the weight of A.  Now, C is chosen as the next item. However, the whole item cannot be chosen as the remaining capacity of the knapsack is less than the weight of C.  Hence, fraction of C (i.e. (60 − 50)/20) is chosen. Solution:
  • 9.  Now, the capacity of the Knapsack is equal to the selected items. Hence, no more item can be selected.  The total weight of the selected items is 10 + 40 + 20 * (10/20) = 60 And the total profit is 100 + 280 + 120 * (10/20) = 380 + 60 = 440  This is the optimal solution. We cannot gain more profit selecting any different combination of items. Cont. Item A B C D Selected 1 1 0.5 0