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

More Related Content

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

What's hot

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

Similar to Algorithm chapter 8

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

More from chidabdu

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

Recently uploaded

PDF
10 Best Automation QA Testing Software Tools in 2025.pdf
PDF
The Necessity of Digital Forensics, the Digital Forensics Process & Laborator...
PDF
The Evolving Role of the CEO in the Age of AI
PDF
How Much Does It Cost to Build an eCommerce Website in 2025.pdf
PDF
MuleSoft Meetup: Dreamforce'25 Tour- Vibing With AI & Agents.pdf
PDF
Transforming Supply Chains with Amazon Bedrock AgentCore (AWS Swiss User Grou...
PDF
Mulesoft Meetup Online Portuguese: MCP e IA
PPTX
UFCD 0797 - SISTEMAS OPERATIVOS_Unidade Completa.pptx
PPTX
How to Choose the Right Vendor for ADA PDF Accessibility and Compliance in 2026
PDF
KMWorld - KM & AI Bring Collectivity, Nostalgia, & Selectivity
PDF
Lets Build a Serverless Function with Kiro
PDF
Mastering Agentic Orchestration with UiPath Maestro | Hands on Workshop
PDF
Dev Dives: Build smarter agents with UiPath Agent Builder
PDF
Top 10 AI Development Companies in UK 2025
PDF
"DISC as GPS for team leaders: how to lead a team from storming to performing...
 
PDF
[BDD 2025 - Mobile Development] Exploring Apple’s On-Device FoundationModels
PDF
Cybersecurity Prevention and Detection: Unit 2
PDF
Running Non-Cloud-Native Databases in Cloud-Native Environments_ Challenges a...
PPTX
Guardrails in Action - Ensuring Safe AI with Azure AI Content Safety.pptx
PDF
Mastering UiPath Maestro – Session 2 – Building a Live Use Case - Session 2
10 Best Automation QA Testing Software Tools in 2025.pdf
The Necessity of Digital Forensics, the Digital Forensics Process & Laborator...
The Evolving Role of the CEO in the Age of AI
How Much Does It Cost to Build an eCommerce Website in 2025.pdf
MuleSoft Meetup: Dreamforce'25 Tour- Vibing With AI & Agents.pdf
Transforming Supply Chains with Amazon Bedrock AgentCore (AWS Swiss User Grou...
Mulesoft Meetup Online Portuguese: MCP e IA
UFCD 0797 - SISTEMAS OPERATIVOS_Unidade Completa.pptx
How to Choose the Right Vendor for ADA PDF Accessibility and Compliance in 2026
KMWorld - KM & AI Bring Collectivity, Nostalgia, & Selectivity
Lets Build a Serverless Function with Kiro
Mastering Agentic Orchestration with UiPath Maestro | Hands on Workshop
Dev Dives: Build smarter agents with UiPath Agent Builder
Top 10 AI Development Companies in UK 2025
"DISC as GPS for team leaders: how to lead a team from storming to performing...
 
[BDD 2025 - Mobile Development] Exploring Apple’s On-Device FoundationModels
Cybersecurity Prevention and Detection: Unit 2
Running Non-Cloud-Native Databases in Cloud-Native Environments_ Challenges a...
Guardrails in Action - Ensuring Safe AI with Azure AI Content Safety.pptx
Mastering UiPath Maestro – Session 2 – Building a Live Use Case - Session 2

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