SlideShare une entreprise Scribd logo
1  sur  45
Welcome To Our
Presentation
Course Title: Algorithm
Course Code: CSE221
Course Teacher: Mr. Moushumi Zaman Bonny
• Group Members:
i. Md. Musfiqur Rahman Foysal ID – 163-15-8489
ii. Rasel Khandokar ID – 161-15-7040
iii. Rabia Basri Kanta ID – 142-15-3733
iv. Shahrear Alam ID – 161-15-7651
Greedy Algorithm
Fractional Knapsack
01
Greedy Algorithm
A greedy algorithm is a mathematical process that looks for simple, easy to
complex one, multiply step problem by designed which next step will be most
benefits.
Characteristics of greedy algorithm
=> Make a sequence of choices
=> Each choice is the one that seems best so far, only depends on what’s been
done so far
=> Choice produces a smaller problem to be solved
02
Fractional Knapsack
Given some items, pack the knapsack to get the maximum total value. Each
item has some weight and some value. Total weight that we can carry is no
more than some fixed number. So we must consider weights of items as well
as their values.
Item 1 2 3
Value 8 6 5
Weight 1 3 5
03
Fractional Knapsack
Pseudo code:
Input: Set S of items w, benefits bi and weight 𝒘𝒊, Weight W
Output: Amount 𝑿𝒊 of each item i to maximize benefits
𝑿𝒊 = 0, w = 0
For each item i in S
𝑽𝒊 =
𝒃 𝒊
𝒘 𝒊
While w<W
//remove item i with highest 𝑽𝒊
𝑿𝒊 = min (𝒘𝒊, 𝐖 − 𝐰)
w = w + min (𝒘𝒊, 𝐖 − 𝐰)
04
Code Implementation (Part-1) 05
Code Implementation (Part-2) 06
Code Implementation (Part-3) 07
Code Implementation (Part-4) 08
Example: Items 1 2 3 4 5
Value (𝒃𝒊) 16 32 17 32 40
Weight (𝒘𝒊) 5 6 2 6 4
𝑿𝒊 =
𝒃𝒊
𝒘 𝒊
3.2 5.3 8.5 5.3 10
Here, The size of knapsack is = 13 kg
Item 5: 4 kg = (4x10) tk = 40 tk
Remaining (13 - 4) kg = 9 kg
Item 3: 2 kg = (2x8.5) tk = 17 tk
Remaining (9 - 2) kg = 7 kg
Item 2: 6 kg = (6x5.3) tk = 32 tk
Remaining (7 - 6) kg = 1 kg
Item 4: 1 kg = (1x5.3) tk =5.3 tk
Remaining (1 - 1) kg = 0 kg
Total amount of knapsack is = (40+17+32+5.3) tk
= 94.3 tk
09
Code Implementation Output Screenshot:
10
Dynamic Programming
0/1 Knapsack
11
0 / 1 knapsack algorithm
The knapsack problem or rucksack problem is a problem in combinatorial
optimization: given a set of items, each with a weight and a value, determine
the number of each item to include in a collection so that the total weight is
less than or equal to a given limit and the total value is as large as possible. It
derives its name from the problem faced by someone who is constrained by a
fixed-size knapsack and must fill it with the most valuable items.
Dynamic programming
In computer science, mathematics, management science, economics and
bioinformatics, dynamic programming (also known as dynamic optimization) is
a method for solving a complex problem by breaking it down into a collection
of simpler sub problems, solving each of those sub problems just once, and
storing their solutions.
12
Example
Number 01 02 03 04
Weight 02 03 04 05
Value 03 04 05 06
13
14
15
16
17
18
19
20
21
22
23
24
25
Longest Common
Subsequence (LCS)
26
Definition
Longest common subsequence (LCS) of 2 sequences is a subsequence,
with maximal length, which is common to both the sequences. Given
two sequence of integers, and , find any one longest common
subsequence.
Dynamic programming
1) It is used, when the solution can be
recursively described in terms of solutions
to subproblems (optimal substructure)
2) Algorithm finds solutions to subproblems
and stores them in memory for later use
3) More efficient than “brute-force methods”,
which solve the same subproblems over
and over again
27
LCS Length Algorithm
LCS-Length(X, Y)
1. m = length(X) // get the # of symbols in X
2. n = length(Y) // get the # of symbols in Y
3. for i = 1 to m c[i,0] = 0 // special case: Y0
4. for j = 1 to n c[0,j] = 0 // special case: X0
5. for i = 1 to m // for all Xi
6. for j = 1 to n // for all Yj
7. if ( Xi == Yj )
8. c[i,j] = c[i-1,j-1] + 1
9. else c[i,j] = max( c[i-1,j], c[i,j-1] )
28
LCS Example
We’ll see how LCS algorithm works on the
following example:
X = ABCB
Y = BDCAB
29
30
31
32
33
34
35
36
37
Huffman Code
38
ALGORITHM
• n = |C|
• Q = C
• for i =1 to n-1
• do allocate a new node z
• left [z] = x = EXTRACT-MIN (Q)
• right [z] = y = EXTRACT-MIN (Q)
• f[z] = f[x] +f[y]
• INSERT(Q, Z)
• return EXTRACT-MIN (Q)
39
MAJOR STEPS
• 1. Prepare the frequency table
• 2. Construct the binary tree.
• 3. Extract the Huffman Code from the tree.
a b c d e f
45 13 12 16 9 5
Frequency Table
(b)
(a) f:5 e:9 c:12 b : 13 d : 16 a : 45
c:12 b : 13 d : 16 a : 45
f:5 e:9
T1:14
40
MAJOR STEPS
(c)
(d)
f:5 e:9
d : 16
a : 45
T1:14
c:12 b : 13
T2:25
c:12 b : 13
T2:25
a : 45
f:5 e:9
d : 16T1:14
T3:30
41
MAJOR STEPS
a : 45
c:12 b : 13
T2:25
f:5 e:9
d : 16T1:14
T3:30
(e) (f)T4:55
c:12 b : 13
T2:25
f:5 e:9
d : 16T1:14
T3:30
T4:55a : 45
T4:100
1
0
0
0
0
0
11
1
1
42
Thank you!

Contenu connexe

Tendances

daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
hodcsencet
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexity
Ankit Katiyar
 
Knapsack problem dynamicprogramming
Knapsack problem dynamicprogrammingKnapsack problem dynamicprogramming
Knapsack problem dynamicprogramming
rowntu
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
Nikhil Sharma
 

Tendances (20)

Knapsack problem using dynamic programming
Knapsack problem using dynamic programmingKnapsack problem using dynamic programming
Knapsack problem using dynamic programming
 
Knapsack problem algorithm, greedy algorithm
Knapsack problem algorithm, greedy algorithmKnapsack problem algorithm, greedy algorithm
Knapsack problem algorithm, greedy algorithm
 
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
 
Dijkstra's Algorithm
Dijkstra's Algorithm Dijkstra's Algorithm
Dijkstra's Algorithm
 
Knapsack Problem
Knapsack ProblemKnapsack Problem
Knapsack Problem
 
Dbms lab questions
Dbms lab questionsDbms lab questions
Dbms lab questions
 
01 Knapsack using Dynamic Programming
01 Knapsack using Dynamic Programming01 Knapsack using Dynamic Programming
01 Knapsack using Dynamic Programming
 
Dynamic Programming-Knapsack Problem
Dynamic Programming-Knapsack ProblemDynamic Programming-Knapsack Problem
Dynamic Programming-Knapsack Problem
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexity
 
Knapsack problem dynamicprogramming
Knapsack problem dynamicprogrammingKnapsack problem dynamicprogramming
Knapsack problem dynamicprogramming
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithms
 
Shortest path algorithm
Shortest path algorithmShortest path algorithm
Shortest path algorithm
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithms
 
Spanning trees
Spanning treesSpanning trees
Spanning trees
 
Branch and bound
Branch and boundBranch and bound
Branch and bound
 
Intro automata theory
Intro automata theory Intro automata theory
Intro automata theory
 
Dijkstra's algorithm presentation
Dijkstra's algorithm presentationDijkstra's algorithm presentation
Dijkstra's algorithm presentation
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
 
Knapsack problem
Knapsack problemKnapsack problem
Knapsack problem
 
Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)
Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)
Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)
 

Similaire à greedy algorithm Fractional Knapsack

Dynamic1
Dynamic1Dynamic1
Dynamic1
MyAlome
 
Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"
Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"
Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"
22bcs058
 
376951072-3-Greedy-Method-new-ppt.ppt
376951072-3-Greedy-Method-new-ppt.ppt376951072-3-Greedy-Method-new-ppt.ppt
376951072-3-Greedy-Method-new-ppt.ppt
RohitPaul71
 

Similaire à greedy algorithm Fractional Knapsack (20)

Chapter 5.pptx
Chapter 5.pptxChapter 5.pptx
Chapter 5.pptx
 
Unit-3 greedy method, Prim's algorithm, Kruskal's algorithm.pdf
Unit-3 greedy method, Prim's algorithm, Kruskal's algorithm.pdfUnit-3 greedy method, Prim's algorithm, Kruskal's algorithm.pdf
Unit-3 greedy method, Prim's algorithm, Kruskal's algorithm.pdf
 
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
 
Dynamic1
Dynamic1Dynamic1
Dynamic1
 
Computer algorithm(Dynamic Programming).pdf
Computer algorithm(Dynamic Programming).pdfComputer algorithm(Dynamic Programming).pdf
Computer algorithm(Dynamic Programming).pdf
 
Daa chapter 3
Daa chapter 3Daa chapter 3
Daa chapter 3
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
Dynamic programming - fundamentals review
Dynamic programming - fundamentals reviewDynamic programming - fundamentals review
Dynamic programming - fundamentals review
 
Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"
Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"
Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"
 
376951072-3-Greedy-Method-new-ppt.ppt
376951072-3-Greedy-Method-new-ppt.ppt376951072-3-Greedy-Method-new-ppt.ppt
376951072-3-Greedy-Method-new-ppt.ppt
 
Greedy with Task Scheduling Algorithm.ppt
Greedy with Task Scheduling Algorithm.pptGreedy with Task Scheduling Algorithm.ppt
Greedy with Task Scheduling Algorithm.ppt
 
Greedy with Task Scheduling Algorithm.ppt
Greedy with Task Scheduling Algorithm.pptGreedy with Task Scheduling Algorithm.ppt
Greedy with Task Scheduling Algorithm.ppt
 
Support Vector Machines Simply
Support Vector Machines SimplySupport Vector Machines Simply
Support Vector Machines Simply
 
Greedy Algorithms with examples' b-18298
Greedy Algorithms with examples'  b-18298Greedy Algorithms with examples'  b-18298
Greedy Algorithms with examples' b-18298
 
AAC ch 3 Advance strategies (Dynamic Programming).pptx
AAC ch 3 Advance strategies (Dynamic Programming).pptxAAC ch 3 Advance strategies (Dynamic Programming).pptx
AAC ch 3 Advance strategies (Dynamic Programming).pptx
 
Ms nikita greedy agorithm
Ms nikita greedy agorithmMs nikita greedy agorithm
Ms nikita greedy agorithm
 
data structures and algorithms Unit 4
data structures and algorithms Unit 4data structures and algorithms Unit 4
data structures and algorithms Unit 4
 
Chapter 16
Chapter 16Chapter 16
Chapter 16
 
Lecture01a correctness
Lecture01a correctnessLecture01a correctness
Lecture01a correctness
 
Greedy Algorithms
Greedy AlgorithmsGreedy Algorithms
Greedy Algorithms
 

Plus de Md. Musfiqur Rahman Foysal

Plus de Md. Musfiqur Rahman Foysal (20)

Education system in Bangladesh statistics presentation
Education system in Bangladesh statistics presentationEducation system in Bangladesh statistics presentation
Education system in Bangladesh statistics presentation
 
Electrical Device and Circuit
Electrical Device and CircuitElectrical Device and Circuit
Electrical Device and Circuit
 
2^nd Translation Property of Laplace Transform
2^nd Translation Property of Laplace Transform2^nd Translation Property of Laplace Transform
2^nd Translation Property of Laplace Transform
 
Food Order in a Restaurant - Data Structure Project
Food Order in a Restaurant - Data Structure ProjectFood Order in a Restaurant - Data Structure Project
Food Order in a Restaurant - Data Structure Project
 
Electrical Circuit - CSE132
Electrical Circuit - CSE132Electrical Circuit - CSE132
Electrical Circuit - CSE132
 
Application of integration
Application of integrationApplication of integration
Application of integration
 
Hacking - A Technical Threat
Hacking - A Technical ThreatHacking - A Technical Threat
Hacking - A Technical Threat
 
Cloud computing
Cloud computingCloud computing
Cloud computing
 
Thermal Engine & Carnot's Cycle and their Working Principle
Thermal Engine & Carnot's Cycle and their Working PrincipleThermal Engine & Carnot's Cycle and their Working Principle
Thermal Engine & Carnot's Cycle and their Working Principle
 
Projectile & projectile motion
Projectile & projectile motionProjectile & projectile motion
Projectile & projectile motion
 
Basic Mechanics & some application on Engineering
Basic Mechanics & some application on EngineeringBasic Mechanics & some application on Engineering
Basic Mechanics & some application on Engineering
 
Heat and thermodynamics
Heat and thermodynamicsHeat and thermodynamics
Heat and thermodynamics
 
Differentiation
DifferentiationDifferentiation
Differentiation
 
Differentiation of application
Differentiation of applicationDifferentiation of application
Differentiation of application
 
Applications of derivative
Applications of derivativeApplications of derivative
Applications of derivative
 
Home Town - Pabna
Home Town - PabnaHome Town - Pabna
Home Town - Pabna
 
Home Town - Noakhali
Home Town - NoakhaliHome Town - Noakhali
Home Town - Noakhali
 
Home Town - Gopalganj
Home Town - GopalganjHome Town - Gopalganj
Home Town - Gopalganj
 
Home Town - Chandpur
Home Town - ChandpurHome Town - Chandpur
Home Town - Chandpur
 
Home Town - Bogra
Home Town - BograHome Town - Bogra
Home Town - Bogra
 

Dernier

Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
jaanualu31
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
HenryBriggs2
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
mphochane1998
 

Dernier (20)

Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
Engineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planesEngineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planes
 
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxA CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdf
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
 
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdf
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
 
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
 
Rums floating Omkareshwar FSPV IM_16112021.pdf
Rums floating Omkareshwar FSPV IM_16112021.pdfRums floating Omkareshwar FSPV IM_16112021.pdf
Rums floating Omkareshwar FSPV IM_16112021.pdf
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
 
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARHAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 

greedy algorithm Fractional Knapsack

  • 2. Course Title: Algorithm Course Code: CSE221 Course Teacher: Mr. Moushumi Zaman Bonny • Group Members: i. Md. Musfiqur Rahman Foysal ID – 163-15-8489 ii. Rasel Khandokar ID – 161-15-7040 iii. Rabia Basri Kanta ID – 142-15-3733 iv. Shahrear Alam ID – 161-15-7651
  • 4. Greedy Algorithm A greedy algorithm is a mathematical process that looks for simple, easy to complex one, multiply step problem by designed which next step will be most benefits. Characteristics of greedy algorithm => Make a sequence of choices => Each choice is the one that seems best so far, only depends on what’s been done so far => Choice produces a smaller problem to be solved 02
  • 5. Fractional Knapsack Given some items, pack the knapsack to get the maximum total value. Each item has some weight and some value. Total weight that we can carry is no more than some fixed number. So we must consider weights of items as well as their values. Item 1 2 3 Value 8 6 5 Weight 1 3 5 03
  • 6. Fractional Knapsack Pseudo code: Input: Set S of items w, benefits bi and weight 𝒘𝒊, Weight W Output: Amount 𝑿𝒊 of each item i to maximize benefits 𝑿𝒊 = 0, w = 0 For each item i in S 𝑽𝒊 = 𝒃 𝒊 𝒘 𝒊 While w<W //remove item i with highest 𝑽𝒊 𝑿𝒊 = min (𝒘𝒊, 𝐖 − 𝐰) w = w + min (𝒘𝒊, 𝐖 − 𝐰) 04
  • 11. Example: Items 1 2 3 4 5 Value (𝒃𝒊) 16 32 17 32 40 Weight (𝒘𝒊) 5 6 2 6 4 𝑿𝒊 = 𝒃𝒊 𝒘 𝒊 3.2 5.3 8.5 5.3 10 Here, The size of knapsack is = 13 kg Item 5: 4 kg = (4x10) tk = 40 tk Remaining (13 - 4) kg = 9 kg Item 3: 2 kg = (2x8.5) tk = 17 tk Remaining (9 - 2) kg = 7 kg Item 2: 6 kg = (6x5.3) tk = 32 tk Remaining (7 - 6) kg = 1 kg Item 4: 1 kg = (1x5.3) tk =5.3 tk Remaining (1 - 1) kg = 0 kg Total amount of knapsack is = (40+17+32+5.3) tk = 94.3 tk 09
  • 12. Code Implementation Output Screenshot: 10
  • 14. 0 / 1 knapsack algorithm The knapsack problem or rucksack problem is a problem in combinatorial optimization: given a set of items, each with a weight and a value, determine the number of each item to include in a collection so that the total weight is less than or equal to a given limit and the total value is as large as possible. It derives its name from the problem faced by someone who is constrained by a fixed-size knapsack and must fill it with the most valuable items. Dynamic programming In computer science, mathematics, management science, economics and bioinformatics, dynamic programming (also known as dynamic optimization) is a method for solving a complex problem by breaking it down into a collection of simpler sub problems, solving each of those sub problems just once, and storing their solutions. 12
  • 15. Example Number 01 02 03 04 Weight 02 03 04 05 Value 03 04 05 06 13
  • 16. 14
  • 17. 15
  • 18. 16
  • 19. 17
  • 20. 18
  • 21. 19
  • 22. 20
  • 23. 21
  • 24. 22
  • 25. 23
  • 26. 24
  • 27. 25
  • 29. Definition Longest common subsequence (LCS) of 2 sequences is a subsequence, with maximal length, which is common to both the sequences. Given two sequence of integers, and , find any one longest common subsequence. Dynamic programming 1) It is used, when the solution can be recursively described in terms of solutions to subproblems (optimal substructure) 2) Algorithm finds solutions to subproblems and stores them in memory for later use 3) More efficient than “brute-force methods”, which solve the same subproblems over and over again 27
  • 30. LCS Length Algorithm LCS-Length(X, Y) 1. m = length(X) // get the # of symbols in X 2. n = length(Y) // get the # of symbols in Y 3. for i = 1 to m c[i,0] = 0 // special case: Y0 4. for j = 1 to n c[0,j] = 0 // special case: X0 5. for i = 1 to m // for all Xi 6. for j = 1 to n // for all Yj 7. if ( Xi == Yj ) 8. c[i,j] = c[i-1,j-1] + 1 9. else c[i,j] = max( c[i-1,j], c[i,j-1] ) 28
  • 31. LCS Example We’ll see how LCS algorithm works on the following example: X = ABCB Y = BDCAB 29
  • 32. 30
  • 33. 31
  • 34. 32
  • 35. 33
  • 36. 34
  • 37. 35
  • 38. 36
  • 39. 37
  • 41. ALGORITHM • n = |C| • Q = C • for i =1 to n-1 • do allocate a new node z • left [z] = x = EXTRACT-MIN (Q) • right [z] = y = EXTRACT-MIN (Q) • f[z] = f[x] +f[y] • INSERT(Q, Z) • return EXTRACT-MIN (Q) 39
  • 42. MAJOR STEPS • 1. Prepare the frequency table • 2. Construct the binary tree. • 3. Extract the Huffman Code from the tree. a b c d e f 45 13 12 16 9 5 Frequency Table (b) (a) f:5 e:9 c:12 b : 13 d : 16 a : 45 c:12 b : 13 d : 16 a : 45 f:5 e:9 T1:14 40
  • 43. MAJOR STEPS (c) (d) f:5 e:9 d : 16 a : 45 T1:14 c:12 b : 13 T2:25 c:12 b : 13 T2:25 a : 45 f:5 e:9 d : 16T1:14 T3:30 41
  • 44. MAJOR STEPS a : 45 c:12 b : 13 T2:25 f:5 e:9 d : 16T1:14 T3:30 (e) (f)T4:55 c:12 b : 13 T2:25 f:5 e:9 d : 16T1:14 T3:30 T4:55a : 45 T4:100 1 0 0 0 0 0 11 1 1 42