Movatterモバイル変換


[0]ホーム

URL:


Uploaded bychidabdu
PDF, PPTX1,854 views

Algorithm chapter 8

Dynamic programming is an algorithm design technique that solves problems by breaking them down into smaller overlapping subproblems and storing the solutions to subproblems to avoid recomputing them. It involves building up a solution using previously found subsolutions and working in a bottom-up fashion to solve larger subproblems from solutions to smaller subproblems. Examples where dynamic programming has been applied include computing Fibonacci numbers, finding the shortest paths in a graph, and solving optimization problems like the knapsack problem.

Embed presentation

Download as PDF, PPTX
Dynamic Programming
Dynamic Programming  Dynamic Programming is a general algorithm design technique.“Programming” here means “planning”.  Invented by American mathematician Richard Bellman in the 1950s tosolve optimization problems  Main idea:      solve several smaller (overlapping) subproblems      record solutions in a table so that each subproblem is only solved    once      final state of the table will be (or contain) solution  Dynamic programming vs. divide-and-conquer      partition a problem into overlapping subproblems and    independent ones      store and not store solutions to subproblems
Example: Fibonacci numbers• Recall definition of Fibonacci numbers:    f(0) = 0    f(1) = 1    f(n) = f(n-1) + f(n-2)• Computing the nth Fibonacci number recursively (top-down):                           f(n)         f(n-1)            +               f(n-2)f(n-2)    +       f(n-3)          f(n-3)     +      f(n-4)                    ...
Example: Fibonacci numbers    Computing the nth fibonacci number    using bottom-up iteration:• f(0) = 0• f(1) = 1• f(2) = 0+1 = 1• f(3) = 1+1 = 2• f(4) = 1+2 = 3           ALGORITHM Fib(n)• f(5) = 2+3 = 5           F[0]    0, F[1]    1•                          for i 2 to n do•                                  F[i]    F[i-1] + F[i-2]•                          return F[n]• f(n-2) =• f(n-1) =                 extra space• f(n) = f(n-1) + f(n-2)
Examples of Dynamic Programming             Algorithms  Computing binomial coefficients  Warshall’s algorithm for transitive closure  Floyd’s algorithms for all-pairs shortest paths  Constructing an optimal binary search tree  Some instances of difficult discrete optimizationproblems:     knapsack
Computing Binomial Coefficients  A binomial coefficient, denoted C(n, k), is the number of  combinations of k elements from an n-element set (0 ≤ k ≤ n).  Recurrence relation (a problem 2 overlapping problems)     C(n, k) = C(n-1, k-1) + C(n-1, k), for n > k > 0, and     C(n, 0) = C(n, n) = 1  Dynamic programming solution:     Record the values of the binomial coefficients in a table of n+1     rows and k+1 columns, numbered from 0 to n and 0 to k      respectively.  1  1    1  1    2     1  1    3     3        1  1    4     6    4       1  1    5    10    10      5   1  …
Transitive ClosureThe transitive closure of a directed graph with n vertices can be   defined as the nxn matrix T = {tij}, in which the element in the   ith row (1 ≤ i ≤ n) and the jth column (1 ≤ j ≤ n) is 1 if there   exists a nontrivial directed path (i.e., a directed path of a positive   length) from the ith vertex to the jth vertex; otherwise, tij is 0.Graph traversal-based algorithm and Warshall’s algorithm               3                                      3   1                                      1                     4                                      4          0   0   1   0       2                  0   0   1   0       2                          1   0   0   1                                1   1   1   1                          0   0   0   0                                0   0   0   0       Adjacency matrix                                                1   1   1   1                          0   1   0   0           Transitive closure
Warshall’s Algorithm• Main idea: Use a bottom-up method to construct the transitive closure of a givendigraph with n vertices through a series of nxn boolean matrices:                     R(0),…, R(k-1), R(k) , …, R(n)The question is: how to obtain R(k) from R(k-1) ?                                                  R(k) : rij(k) = 1 in R(k) , iff                                                         there   is an edge from i to j; or                                                         there   is a path from i to j going through vertex 1; or                                                         there   is a path from i to j going through vertex 1 and/or 2; or                                                        ...                                                         there   is a path from i to j going through 1, 2, … and/or k                 3                            3                           3                           3                              3 1                            1                            1                           1                              1                         4                         4                            4                             4           2                  4     2                            2                            2                           2          R(0)                     R(1)                        R(2)                            R(3)                           R(4) 0       0 1     0                 0 0    1   0                0 0    1   0                    0 0    1   0                   0 0    1   0 1       0 0     1                 1 0    1   1                1 0    1   1                    1 0    1   1                   1 1    1   1 0       0 0     0                 0 0    0   0                0 0    0   0                    0 0    0   0                   0 0    0   0 0       1 0     0                 0 1    0   0                1 1    1   1                    1 1    1   1                   1 1    1   1Does not allow an              Allow 1 to be an  Allow 1,2 to be an                 Allow 1,2,3 to be an          Allow 1,2,3,4 to be anintermediate node              intermediate node intermediate node                  intermediate node             intermediate node
Warshall’s AlgorithmIn the kth stage: to determine R(k) is to determine if a path existsbetween two vertices i, j using just vertices among 1,…,k          {rij(k) = 1:                rij(k-1) = 1                   or                                             (path using just 1 ,…,k-1)              (rik(k-1) = 1 and rkj(k-1)) = 1 (path from i to k                                                       and from k to i                                                       using just 1 ,…,k-1)                     k                          kth stagei                          Rule to determine whether rij (k) should be 1 in R(k) :                          • If an element rij is 1 in R(k-1) , it remains 1 in R(k) .                j         • If an element rij is 0 in R(k-1) ,it has to be changed                          to 1 in R(k) iff the element in its row i and column k                          and the element in its column j and row k are both                          1’s in R(k-1).
Floyd’s Algorithm: All pairs shortest paths All pairs shortest paths problem: In a weighted graph,                                                                  dij(k) = length of thefind shortest paths between every pair of vertices.               shortest path from i to                                                                  j with each vertexApplicable to: undirected and directed weighted graphs;           numbered no higherno negative weight.                                               than k.Same idea as the Warshall’s algorithm : constructsolution through series of matrices D(0) , D(1), …, D(n)Example:           4       3             0 ∞   4   ∞                0 ∞   4   ∞   1                             1 0   6   3                1 0   5   3                       1         ∞ ∞   0   ∞           6                                                ∞ ∞   0   ∞  1            5                 ∞ 5   1   0                6 5   1   0                           4       2       3               weight matrix               distance matrix
Floyd’s AlgorithmD(k) : allow 1, 2, …, k to be intermediate vertices.In the kth stage, determine whether the introduction of k as a new eligibleintermediate vertex will bring about a shorter path from i to j.dij(k) = min{dij(k-1) , dik(k-1) + dkj(k-1} for k ≥ 1, dij(0) = wij                         dik(k-1)                                               k                  i                                    dkj(k-1)       kth stage                      dij(k-1)                                        j
General CommentsThe crucial step in designing a dynamicprogramming algorithm:  Deriving a recurrence relating a solution to  the problem’s current instance with  solutions of its smaller ( and overlapping)  subinstances.
The Knapsack Problem and Memory             Functions (1)The problem  Find the most valuable subset of the given n  items that fit into a knapsack of capacity W.Consider the following subproblem P(i, j)  Find the most valuable subset of the first i items  that fit into a knapsack of capacity j, where 1 ≤ i ≤  n, and 1≤ j ≤ W  Let V[i, j] be the value of an optimal solution to  the above subproblem P(i, j). Goal: V[n, W]  The question: What is the recurrence relation that  expresses a solution to this instance in terms of  solutions to smaller subinstances?
The Knapsack Problem and Memory                 Functions (2)The Recurrence  Two possibilities for the most valuable subset  for the subproblem P(i, j)     It does not include the ith item: V[i, j] = V[i-1, j]     It includes the ith item: V[i, j] = vi+ V[i-1, j – wi]V[i, j] =   max{V[i-1, j], vi+ V[i-1, j – wi] }, if j – wi ≥ 0            {            V[i-1, j]              if j – wi < 0V[0, j] = 0 for j ≥ 0 and V[i, 0] = 0 for i ≥ 0
Memory Functions   Memory functions: a combination of the top-down and bottom-   up method. The idea is to solve the subproblems that are   necessary and do it only once.       Top-down: solve common subproblems more than once.       Bottom-up: Solve subproblems whose solution are not necessary       for the solving the original problem.ALGORITHM MFKnapsack(i, j)if V[i, j] < 0 //if subproblem P(i, j) hasn’t been solved yet.    if j < Weights[i]            value     MFKnapsack(i – 1, j)    else            value max(MFKnapsack(i – 1, j),                              values[I] + MFKnapsck( i – 1, j – Weights[i]))    V[i, j]     valuereturn V[i, j]

Recommended

PDF
Automata theory
DOCX
Counters In Digital Logic Design
PPT
Algorithmic Notations
PPTX
A Case Study on Java. Java Presentation
PPTX
LINKED LIST.pptx
PDF
Language
PPT
Sequential Logic Circuit
PPTX
NP completeness
PPTX
TOC Introduction
PPTX
Np Completeness
PDF
Projeto ULA
PDF
Hazards in pipeline
PDF
Signed Binary Numbers
PPT
Dynamic programming
PDF
Object-oriented analysis and design - questions and answers
PDF
Functions in discrete mathematics
PPT
Counters
PDF
Introduction to Binary Tree and Conersion of General tree to Binary Tree
PPTX
Theory of automata and formal language
PPTX
Hashing .pptx
PPTX
K - Map
PPT
Finite automata
PPTX
7-Operator Precedence Parser-23-05-2023.pptx
PPTX
Push Down Automata (PDA) | TOC (Theory of Computation) | NPDA | DPDA
PDF
Chapter 1 digital systems and binary numbers
PPTX
FUNDAMENTALS OF DIGITAL CIRCUITS by anand kumar - PHI Learning
PPTX
Hash table
PDF
Sienna 10 dynamic
PPTX

More Related Content

PDF
Automata theory
DOCX
Counters In Digital Logic Design
PPT
Algorithmic Notations
PPTX
A Case Study on Java. Java Presentation
PPTX
LINKED LIST.pptx
PDF
Language
PPT
Sequential Logic Circuit
PPTX
NP completeness
Automata theory
Counters In Digital Logic Design
Algorithmic Notations
A Case Study on Java. Java Presentation
LINKED LIST.pptx
Language
Sequential Logic Circuit
NP completeness

What's hot

PPTX
TOC Introduction
PPTX
Np Completeness
PDF
Projeto ULA
PDF
Hazards in pipeline
PDF
Signed Binary Numbers
PPT
Dynamic programming
PDF
Object-oriented analysis and design - questions and answers
PDF
Functions in discrete mathematics
PPT
Counters
PDF
Introduction to Binary Tree and Conersion of General tree to Binary Tree
PPTX
Theory of automata and formal language
PPTX
Hashing .pptx
PPTX
K - Map
PPT
Finite automata
PPTX
7-Operator Precedence Parser-23-05-2023.pptx
PPTX
Push Down Automata (PDA) | TOC (Theory of Computation) | NPDA | DPDA
PDF
Chapter 1 digital systems and binary numbers
PPTX
FUNDAMENTALS OF DIGITAL CIRCUITS by anand kumar - PHI Learning
PPTX
Hash table
TOC Introduction
Np Completeness
Projeto ULA
Hazards in pipeline
Signed Binary Numbers
Dynamic programming
Object-oriented analysis and design - questions and answers
Functions in discrete mathematics
Counters
Introduction to Binary Tree and Conersion of General tree to Binary Tree
Theory of automata and formal language
Hashing .pptx
K - Map
Finite automata
7-Operator Precedence Parser-23-05-2023.pptx
Push Down Automata (PDA) | TOC (Theory of Computation) | NPDA | DPDA
Chapter 1 digital systems and binary numbers
FUNDAMENTALS OF DIGITAL CIRCUITS by anand kumar - PHI Learning
Hash table

Similar to Algorithm chapter 8

PDF
Sienna 10 dynamic
PPTX
PPTX
PPTX
UNIT 4 Chapter 1 DYNAMIC PROGRAMMING.pptx
PPT
d0a2de03-27d3-4ca2-9ac6-d83440657a6c.ppt
PPTX
LU_30_Dynamic_Programming_Warshal_Floyd_1712140744434.pptx
PPT
Dynamic Programming for 4th sem cse students
PDF
Maritaferreira Artwork2007
PDF
Sienna 3 bruteforce
PPT
05 adversarial
PDF
Skiena algorithm 2007 lecture16 introduction to dynamic programming
 
PDF
Part4 graph algorithms
PPT
Chap08alg
PPT
Chap08alg
PDF
Algorithm chapter 5
PPTX
unit-4-dynamic programming
PPTX
Dynamic Programming in design and analysis .pptx
PPTX
Applied Algorithms and Structures week999
PDF
My presentation all shortestpath
PPTX
Computational Social Science, Lecture 05: Networks, Part I
Sienna 10 dynamic
UNIT 4 Chapter 1 DYNAMIC PROGRAMMING.pptx
d0a2de03-27d3-4ca2-9ac6-d83440657a6c.ppt
LU_30_Dynamic_Programming_Warshal_Floyd_1712140744434.pptx
Dynamic Programming for 4th sem cse students
Maritaferreira Artwork2007
Sienna 3 bruteforce
05 adversarial
Skiena algorithm 2007 lecture16 introduction to dynamic programming
 
Part4 graph algorithms
Chap08alg
Chap08alg
Algorithm chapter 5
unit-4-dynamic programming
Dynamic Programming in design and analysis .pptx
Applied Algorithms and Structures week999
My presentation all shortestpath
Computational Social Science, Lecture 05: Networks, Part I

More from chidabdu

PDF
Sienna 12 huffman
PDF
Sienna 11 graphs
PDF
Sienna 9 hashing
PDF
Sienna 8 countingsorts
PDF
Sienna 7 heaps
PDF
Sienna 6 bst
PDF
Sienna 5 decreaseandconquer
PDF
Sienna 4 divideandconquer
PDF
Sienna 2 analysis
PDF
Sienna 1 intro
PDF
Sienna 13 limitations
PPT
Unit 3 basic processing unit
PPT
Unit 5 I/O organization
PDF
Algorithm chapter 1
PDF
Algorithm chapter 11
PDF
Algorithm chapter 10
PDF
Algorithm chapter 9
PDF
Algorithm chapter 7
PDF
Algorithm chapter 6
PDF
Algorithm chapter 2
Sienna 12 huffman
Sienna 11 graphs
Sienna 9 hashing
Sienna 8 countingsorts
Sienna 7 heaps
Sienna 6 bst
Sienna 5 decreaseandconquer
Sienna 4 divideandconquer
Sienna 2 analysis
Sienna 1 intro
Sienna 13 limitations
Unit 3 basic processing unit
Unit 5 I/O organization
Algorithm chapter 1
Algorithm chapter 11
Algorithm chapter 10
Algorithm chapter 9
Algorithm chapter 7
Algorithm chapter 6
Algorithm chapter 2

Recently uploaded

PPTX
How to Choose the Right Vendor for ADA PDF Accessibility and Compliance in 2026
PDF
Open Source Post-Quantum Cryptography - Matt Caswell
PDF
KMWorld - KM & AI Bring Collectivity, Nostalgia, & Selectivity
PDF
Top Crypto Supers 15th Report November 2025
PDF
Lets Build a Serverless Function with Kiro
PDF
[BDD 2025 - Full-Stack Development] Agentic AI Architecture: Redefining Syste...
PPTX
Connecting the unconnectable: Exploring LoRaWAN for IoT
PDF
Oracle MySQL HeatWave - Complete - Version 3
PDF
Cybersecurity Prevention and Detection: Unit 2
PDF
Transforming Supply Chains with Amazon Bedrock AgentCore (AWS Swiss User Grou...
PDF
Mastering UiPath Maestro – Session 2 – Building a Live Use Case - Session 2
PDF
[BDD 2025 - Artificial Intelligence] Building AI Systems That Users (and Comp...
PDF
Rolling out Enterprise AI: Tools, Insights, and Team Empowerment
PDF
Beyond Basics: How to Build Scalable, Intelligent Imagery Pipelines
PDF
The Evolving Role of the CEO in the Age of AI
PDF
Crane Accident Prevention Guide: Key OSHA Regulations for Safer Operations
PDF
The Necessity of Digital Forensics, the Digital Forensics Process & Laborator...
PDF
Top 10 AI Development Companies in UK 2025
PDF
Supervised Machine Learning Approaches for Log-Based Anomaly Detection: A Cas...
PDF
[BDD 2025 - Mobile Development] Crafting Immersive UI with E2E and AGSL Shade...
How to Choose the Right Vendor for ADA PDF Accessibility and Compliance in 2026
Open Source Post-Quantum Cryptography - Matt Caswell
KMWorld - KM & AI Bring Collectivity, Nostalgia, & Selectivity
Top Crypto Supers 15th Report November 2025
Lets Build a Serverless Function with Kiro
[BDD 2025 - Full-Stack Development] Agentic AI Architecture: Redefining Syste...
Connecting the unconnectable: Exploring LoRaWAN for IoT
Oracle MySQL HeatWave - Complete - Version 3
Cybersecurity Prevention and Detection: Unit 2
Transforming Supply Chains with Amazon Bedrock AgentCore (AWS Swiss User Grou...
Mastering UiPath Maestro – Session 2 – Building a Live Use Case - Session 2
[BDD 2025 - Artificial Intelligence] Building AI Systems That Users (and Comp...
Rolling out Enterprise AI: Tools, Insights, and Team Empowerment
Beyond Basics: How to Build Scalable, Intelligent Imagery Pipelines
The Evolving Role of the CEO in the Age of AI
Crane Accident Prevention Guide: Key OSHA Regulations for Safer Operations
The Necessity of Digital Forensics, the Digital Forensics Process & Laborator...
Top 10 AI Development Companies in UK 2025
Supervised Machine Learning Approaches for Log-Based Anomaly Detection: A Cas...
[BDD 2025 - Mobile Development] Crafting Immersive UI with E2E and AGSL Shade...

Algorithm chapter 8

  • 1.
  • 2.
    Dynamic ProgrammingDynamic Programming is a general algorithm design technique.“Programming” here means “planning”. Invented by American mathematician Richard Bellman in the 1950s tosolve optimization problems Main idea: solve several smaller (overlapping) subproblems record solutions in a table so that each subproblem is only solved once final state of the table will be (or contain) solution Dynamic programming vs. divide-and-conquer partition a problem into overlapping subproblems and independent ones store and not store solutions to subproblems
  • 3.
    Example: Fibonacci numbers•Recall definition of Fibonacci numbers: f(0) = 0 f(1) = 1 f(n) = f(n-1) + f(n-2)• Computing the nth Fibonacci number recursively (top-down): f(n) f(n-1) + f(n-2)f(n-2) + f(n-3) f(n-3) + f(n-4) ...
  • 4.
    Example: Fibonacci numbers Computing the nth fibonacci number using bottom-up iteration:• f(0) = 0• f(1) = 1• f(2) = 0+1 = 1• f(3) = 1+1 = 2• f(4) = 1+2 = 3 ALGORITHM Fib(n)• f(5) = 2+3 = 5 F[0] 0, F[1] 1• for i 2 to n do• F[i] F[i-1] + F[i-2]• return F[n]• f(n-2) =• f(n-1) = extra space• f(n) = f(n-1) + f(n-2)
  • 5.
    Examples of DynamicProgramming Algorithms Computing binomial coefficients Warshall’s algorithm for transitive closure Floyd’s algorithms for all-pairs shortest paths Constructing an optimal binary search tree Some instances of difficult discrete optimizationproblems: knapsack
  • 6.
    Computing Binomial Coefficients A binomial coefficient, denoted C(n, k), is the number of combinations of k elements from an n-element set (0 ≤ k ≤ n). Recurrence relation (a problem 2 overlapping problems) C(n, k) = C(n-1, k-1) + C(n-1, k), for n > k > 0, and C(n, 0) = C(n, n) = 1 Dynamic programming solution: Record the values of the binomial coefficients in a table of n+1 rows and k+1 columns, numbered from 0 to n and 0 to k respectively. 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 …
  • 7.
    Transitive ClosureThe transitiveclosure of a directed graph with n vertices can be defined as the nxn matrix T = {tij}, in which the element in the ith row (1 ≤ i ≤ n) and the jth column (1 ≤ j ≤ n) is 1 if there exists a nontrivial directed path (i.e., a directed path of a positive length) from the ith vertex to the jth vertex; otherwise, tij is 0.Graph traversal-based algorithm and Warshall’s algorithm 3 3 1 1 4 4 0 0 1 0 2 0 0 1 0 2 1 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 Adjacency matrix 1 1 1 1 0 1 0 0 Transitive closure
  • 8.
    Warshall’s Algorithm• Mainidea: Use a bottom-up method to construct the transitive closure of a givendigraph with n vertices through a series of nxn boolean matrices: R(0),…, R(k-1), R(k) , …, R(n)The question is: how to obtain R(k) from R(k-1) ? R(k) : rij(k) = 1 in R(k) , iff there is an edge from i to j; or there is a path from i to j going through vertex 1; or there is a path from i to j going through vertex 1 and/or 2; or ... there is a path from i to j going through 1, 2, … and/or k 3 3 3 3 3 1 1 1 1 1 4 4 4 4 2 4 2 2 2 2 R(0) R(1) R(2) R(3) R(4) 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 1 0 0 1 1 0 1 1 1 0 1 1 1 0 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1Does not allow an Allow 1 to be an Allow 1,2 to be an Allow 1,2,3 to be an Allow 1,2,3,4 to be anintermediate node intermediate node intermediate node intermediate node intermediate node
  • 9.
    Warshall’s AlgorithmIn thekth stage: to determine R(k) is to determine if a path existsbetween two vertices i, j using just vertices among 1,…,k {rij(k) = 1: rij(k-1) = 1 or (path using just 1 ,…,k-1) (rik(k-1) = 1 and rkj(k-1)) = 1 (path from i to k and from k to i using just 1 ,…,k-1) k kth stagei Rule to determine whether rij (k) should be 1 in R(k) : • If an element rij is 1 in R(k-1) , it remains 1 in R(k) . j • If an element rij is 0 in R(k-1) ,it has to be changed to 1 in R(k) iff the element in its row i and column k and the element in its column j and row k are both 1’s in R(k-1).
  • 10.
    Floyd’s Algorithm: Allpairs shortest paths All pairs shortest paths problem: In a weighted graph, dij(k) = length of thefind shortest paths between every pair of vertices. shortest path from i to j with each vertexApplicable to: undirected and directed weighted graphs; numbered no higherno negative weight. than k.Same idea as the Warshall’s algorithm : constructsolution through series of matrices D(0) , D(1), …, D(n)Example: 4 3 0 ∞ 4 ∞ 0 ∞ 4 ∞ 1 1 0 6 3 1 0 5 3 1 ∞ ∞ 0 ∞ 6 ∞ ∞ 0 ∞ 1 5 ∞ 5 1 0 6 5 1 0 4 2 3 weight matrix distance matrix
  • 11.
    Floyd’s AlgorithmD(k) :allow 1, 2, …, k to be intermediate vertices.In the kth stage, determine whether the introduction of k as a new eligibleintermediate vertex will bring about a shorter path from i to j.dij(k) = min{dij(k-1) , dik(k-1) + dkj(k-1} for k ≥ 1, dij(0) = wij dik(k-1) k i dkj(k-1) kth stage dij(k-1) j
  • 12.
    General CommentsThe crucialstep in designing a dynamicprogramming algorithm: Deriving a recurrence relating a solution to the problem’s current instance with solutions of its smaller ( and overlapping) subinstances.
  • 13.
    The Knapsack Problemand Memory Functions (1)The problem Find the most valuable subset of the given n items that fit into a knapsack of capacity W.Consider the following subproblem P(i, j) Find the most valuable subset of the first i items that fit into a knapsack of capacity j, where 1 ≤ i ≤ n, and 1≤ j ≤ W Let V[i, j] be the value of an optimal solution to the above subproblem P(i, j). Goal: V[n, W] The question: What is the recurrence relation that expresses a solution to this instance in terms of solutions to smaller subinstances?
  • 14.
    The Knapsack Problemand Memory Functions (2)The Recurrence Two possibilities for the most valuable subset for the subproblem P(i, j) It does not include the ith item: V[i, j] = V[i-1, j] It includes the ith item: V[i, j] = vi+ V[i-1, j – wi]V[i, j] = max{V[i-1, j], vi+ V[i-1, j – wi] }, if j – wi ≥ 0 { V[i-1, j] if j – wi < 0V[0, j] = 0 for j ≥ 0 and V[i, 0] = 0 for i ≥ 0
  • 15.
    Memory Functions Memory functions: a combination of the top-down and bottom- up method. The idea is to solve the subproblems that are necessary and do it only once. Top-down: solve common subproblems more than once. Bottom-up: Solve subproblems whose solution are not necessary for the solving the original problem.ALGORITHM MFKnapsack(i, j)if V[i, j] < 0 //if subproblem P(i, j) hasn’t been solved yet. if j < Weights[i] value MFKnapsack(i – 1, j) else value max(MFKnapsack(i – 1, j), values[I] + MFKnapsck( i – 1, j – Weights[i])) V[i, j] valuereturn V[i, j]

[8]ページ先頭

©2009-2025 Movatter.jp