SlideShare a Scribd company logo
1 of 10
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

More Related Content

What's hot

Divide and conquer 1
Divide and conquer 1Divide and conquer 1
Divide and conquer 1
Kumar
ย 
Bellman ford Algorithm
Bellman ford AlgorithmBellman ford Algorithm
Bellman ford Algorithm
taimurkhan803
ย 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithm
Mohd Arif
ย 
01 knapsack using backtracking
01 knapsack using backtracking01 knapsack using backtracking
01 knapsack using backtracking
mandlapure
ย 

What's hot (20)

0/1 knapsack
0/1 knapsack0/1 knapsack
0/1 knapsack
ย 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithms
ย 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithms
ย 
Greedy algorithm
Greedy algorithmGreedy algorithm
Greedy algorithm
ย 
Knapsack problem
Knapsack problemKnapsack problem
Knapsack problem
ย 
Prims and kruskal algorithms
Prims and kruskal algorithmsPrims and kruskal algorithms
Prims and kruskal algorithms
ย 
Divide and conquer 1
Divide and conquer 1Divide and conquer 1
Divide and conquer 1
ย 
Bellman Ford's Algorithm
Bellman Ford's AlgorithmBellman Ford's Algorithm
Bellman Ford's Algorithm
ย 
knapsack problem
knapsack problemknapsack problem
knapsack problem
ย 
Dynamic Programming-Knapsack Problem
Dynamic Programming-Knapsack ProblemDynamic Programming-Knapsack Problem
Dynamic Programming-Knapsack Problem
ย 
Quick sort-Data Structure
Quick sort-Data StructureQuick sort-Data Structure
Quick sort-Data Structure
ย 
Merge sort algorithm
Merge sort algorithmMerge sort algorithm
Merge sort algorithm
ย 
Bellman ford Algorithm
Bellman ford AlgorithmBellman ford Algorithm
Bellman ford Algorithm
ย 
Quick sort
Quick sortQuick sort
Quick sort
ย 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithm
ย 
Greedy Algorithms
Greedy AlgorithmsGreedy Algorithms
Greedy Algorithms
ย 
01 knapsack using backtracking
01 knapsack using backtracking01 knapsack using backtracking
01 knapsack using backtracking
ย 
Kruskal Algorithm
Kruskal AlgorithmKruskal Algorithm
Kruskal Algorithm
ย 
Shortest Path in Graph
Shortest Path in GraphShortest Path in Graph
Shortest Path in Graph
ย 
Algorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IAlgorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms I
ย 

Similar to Fractional Knapsack Problem

Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.pptParallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
dakccse
ย 
Design and analysis of Algorithms - Lecture 15.ppt
Design and analysis of Algorithms - Lecture 15.pptDesign and analysis of Algorithms - Lecture 15.ppt
Design and analysis of Algorithms - Lecture 15.ppt
QurbanAli72
ย 
knapsackusingbranchandbound
knapsackusingbranchandboundknapsackusingbranchandbound
knapsackusingbranchandbound
hodcsencet
ย 
Lecture34
Lecture34Lecture34
Lecture34
guestc24b39
ย 
lecture 26
lecture 26lecture 26
lecture 26
sajinsc
ย 
Lecture34
Lecture34Lecture34
Lecture34
farazch
ย 
Greedy
GreedyGreedy
Greedy
koralverma
ย 

Similar to Fractional Knapsack Problem (20)

12 Greeddy Method
12 Greeddy Method12 Greeddy Method
12 Greeddy Method
ย 
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal's
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
ย 
Knapsack problem
Knapsack problemKnapsack problem
Knapsack problem
ย 
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.pptParallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
ย 
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
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
Design and analysis of Algorithms - Lecture 15.pptDesign and analysis of Algorithms - Lecture 15.ppt
Design and analysis of Algorithms - Lecture 15.ppt
ย 
Optimization problems
Optimization problemsOptimization problems
Optimization problems
ย 
knapsackusingbranchandbound
knapsackusingbranchandboundknapsackusingbranchandbound
knapsackusingbranchandbound
ย 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
ย 
Lec30
Lec30Lec30
Lec30
ย 
Greedy+Day-3.pptx
Greedy+Day-3.pptxGreedy+Day-3.pptx
Greedy+Day-3.pptx
ย 
Knapsack Dynamic
Knapsack DynamicKnapsack Dynamic
Knapsack Dynamic
ย 
Ms nikita greedy agorithm
Ms nikita greedy agorithmMs nikita greedy agorithm
Ms nikita greedy agorithm
ย 
lect
lectlect
lect
ย 
lect
lectlect
lect
ย 
Lecture34
Lecture34Lecture34
Lecture34
ย 
lec
leclec
lec
ย 
lecture 26
lecture 26lecture 26
lecture 26
ย 
Lecture34
Lecture34Lecture34
Lecture34
ย 
Greedy
GreedyGreedy
Greedy
ย 

More from harsh kothari (11)

Instructuion set of 8085
Instructuion set of 8085Instructuion set of 8085
Instructuion set of 8085
ย 
Java(Polymorphism)
Java(Polymorphism)Java(Polymorphism)
Java(Polymorphism)
ย 
stirling method maths
stirling method mathsstirling method maths
stirling method maths
ย 
170120107066 dbms
170120107066 dbms170120107066 dbms
170120107066 dbms
ย 
170120107066 dielectric.ppt
170120107066 dielectric.ppt170120107066 dielectric.ppt
170120107066 dielectric.ppt
ย 
Hk
HkHk
Hk
ย 
170120107074 looping statements and nesting of loop statements
170120107074 looping statements and nesting of loop statements170120107074 looping statements and nesting of loop statements
170120107074 looping statements and nesting of loop statements
ย 
170120107066 chronemics.ppt
170120107066 chronemics.ppt170120107066 chronemics.ppt
170120107066 chronemics.ppt
ย 
170120107066 power series.ppt
170120107066 power series.ppt170120107066 power series.ppt
170120107066 power series.ppt
ย 
170120107066 flowchart.ppt
170120107066 flowchart.ppt170120107066 flowchart.ppt
170120107066 flowchart.ppt
ย 
control circuit
 control circuit control circuit
control circuit
ย 

Recently uploaded

Call Girls in Ramesh Nagar Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort Service
Call Girls in Ramesh Nagar Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort ServiceCall Girls in Ramesh Nagar Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort Service
Call Girls in Ramesh Nagar Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
ย 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Christo Ananth
ย 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Christo Ananth
ย 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
ankushspencer015
ย 

Recently uploaded (20)

Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...
ย 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
ย 
Call Girls in Ramesh Nagar Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort Service
Call Girls in Ramesh Nagar Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort ServiceCall Girls in Ramesh Nagar Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort Service
Call Girls in Ramesh Nagar Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort Service
ย 
Vivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design SpainVivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design Spain
ย 
Top Rated Pune Call Girls Budhwar Peth โŸŸ 6297143586 โŸŸ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth โŸŸ 6297143586 โŸŸ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth โŸŸ 6297143586 โŸŸ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth โŸŸ 6297143586 โŸŸ Call Me For Genuine Se...
ย 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
ย 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
ย 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
ย 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
ย 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
ย 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
ย 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
ย 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
ย 
Glass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesGlass Ceramics: Processing and Properties
Glass Ceramics: Processing and Properties
ย 
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLPVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
ย 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
ย 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
ย 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
ย 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
ย 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
ย 

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