SlideShare a Scribd company logo
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 to
solve 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 optimization
problems:
     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 Closure
The 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 given
digraph 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   1
Does 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 an
intermediate node              intermediate node intermediate node                  intermediate node             intermediate node
Warshall’s Algorithm
In the kth stage: to determine R(k) is to determine if a path exists
between 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 stage
i
                          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 the
find shortest paths between every pair of vertices.               shortest path from i to
                                                                  j with each vertex
Applicable to: undirected and directed weighted graphs;           numbered no higher
no negative weight.                                               than k.
Same idea as the Warshall’s algorithm : construct
solution 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 Algorithm
D(k) : allow 1, 2, …, k to be intermediate vertices.
In the kth stage, determine whether the introduction of k as a new eligible
intermediate 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 Comments
The crucial step in designing a dynamic
programming 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 < 0

V[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]     value
return V[i, j]

More Related Content

What's hot

NONLINEAR PROGRAMMING - Lecture 1 Introduction
NONLINEAR PROGRAMMING - Lecture 1 IntroductionNONLINEAR PROGRAMMING - Lecture 1 Introduction
NONLINEAR PROGRAMMING - Lecture 1 Introduction
Olympiad
 
Fractional Knapsack Problem
Fractional Knapsack ProblemFractional Knapsack Problem
Fractional Knapsack Problem
harsh kothari
 
Scope - Static and Dynamic
Scope - Static and DynamicScope - Static and Dynamic
Scope - Static and Dynamic
Sneh Pahilwani
 
Automata theory - CFG and normal forms
Automata theory - CFG and normal formsAutomata theory - CFG and normal forms
Automata theory - CFG and normal forms
Akila Krishnamoorthy
 
Numerical Computing
Numerical Computing Numerical Computing
Numerical Computing
Er. Shiva K. Shrestha
 
「型の理論」と証明支援システム -- COQの世界
「型の理論」と証明支援システム -- COQの世界「型の理論」と証明支援システム -- COQの世界
「型の理論」と証明支援システム -- COQの世界maruyama097
 
Interval Type-2 fuzzy decision making
Interval Type-2 fuzzy decision makingInterval Type-2 fuzzy decision making
Interval Type-2 fuzzy decision making
Bob John
 
Chomsky Hierarchy.ppt
Chomsky Hierarchy.pptChomsky Hierarchy.ppt
Chomsky Hierarchy.ppt
AayushSingh233965
 
Undecidability.pptx
Undecidability.pptxUndecidability.pptx
Undecidability.pptx
PEzhumalai
 
rabbitmq Python
rabbitmq Python rabbitmq Python
rabbitmq Python
Soumil Shahsoumil
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
Yıldırım Tam
 
Product Cipher
Product CipherProduct Cipher
Product Cipher
SHUBHA CHATURVEDI
 
module4_dynamic programming_2022.pdf
module4_dynamic programming_2022.pdfmodule4_dynamic programming_2022.pdf
module4_dynamic programming_2022.pdf
Shiwani Gupta
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
Melaku Bayih Demessie
 
15 puzzle problem using branch and bound
15 puzzle problem using branch and bound15 puzzle problem using branch and bound
15 puzzle problem using branch and bound
Abhishek Singh
 
Tsp is NP-Complete
Tsp is NP-CompleteTsp is NP-Complete
Tsp is NP-Complete
Emre Can Kucukoglu
 
Representing TF and TF-IDF transformations in PMML
Representing TF and TF-IDF transformations in PMMLRepresenting TF and TF-IDF transformations in PMML
Representing TF and TF-IDF transformations in PMML
Villu Ruusmann
 
A brief introduction to mutual information and its application
A brief introduction to mutual information and its applicationA brief introduction to mutual information and its application
A brief introduction to mutual information and its application
Hyun-hwan Jeong
 
Connection Machine
Connection MachineConnection Machine
Connection Machinebutest
 

What's hot (20)

NONLINEAR PROGRAMMING - Lecture 1 Introduction
NONLINEAR PROGRAMMING - Lecture 1 IntroductionNONLINEAR PROGRAMMING - Lecture 1 Introduction
NONLINEAR PROGRAMMING - Lecture 1 Introduction
 
Fractional Knapsack Problem
Fractional Knapsack ProblemFractional Knapsack Problem
Fractional Knapsack Problem
 
Scope - Static and Dynamic
Scope - Static and DynamicScope - Static and Dynamic
Scope - Static and Dynamic
 
Automata theory - CFG and normal forms
Automata theory - CFG and normal formsAutomata theory - CFG and normal forms
Automata theory - CFG and normal forms
 
Numerical Computing
Numerical Computing Numerical Computing
Numerical Computing
 
「型の理論」と証明支援システム -- COQの世界
「型の理論」と証明支援システム -- COQの世界「型の理論」と証明支援システム -- COQの世界
「型の理論」と証明支援システム -- COQの世界
 
Interval Type-2 fuzzy decision making
Interval Type-2 fuzzy decision makingInterval Type-2 fuzzy decision making
Interval Type-2 fuzzy decision making
 
Chomsky Hierarchy.ppt
Chomsky Hierarchy.pptChomsky Hierarchy.ppt
Chomsky Hierarchy.ppt
 
Undecidability.pptx
Undecidability.pptxUndecidability.pptx
Undecidability.pptx
 
rabbitmq Python
rabbitmq Python rabbitmq Python
rabbitmq Python
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
Product Cipher
Product CipherProduct Cipher
Product Cipher
 
module4_dynamic programming_2022.pdf
module4_dynamic programming_2022.pdfmodule4_dynamic programming_2022.pdf
module4_dynamic programming_2022.pdf
 
Coma
ComaComa
Coma
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
15 puzzle problem using branch and bound
15 puzzle problem using branch and bound15 puzzle problem using branch and bound
15 puzzle problem using branch and bound
 
Tsp is NP-Complete
Tsp is NP-CompleteTsp is NP-Complete
Tsp is NP-Complete
 
Representing TF and TF-IDF transformations in PMML
Representing TF and TF-IDF transformations in PMMLRepresenting TF and TF-IDF transformations in PMML
Representing TF and TF-IDF transformations in PMML
 
A brief introduction to mutual information and its application
A brief introduction to mutual information and its applicationA brief introduction to mutual information and its application
A brief introduction to mutual information and its application
 
Connection Machine
Connection MachineConnection Machine
Connection Machine
 

Similar to Algorithm chapter 8

CAPE PURE MATHEMATICS UNIT 2 MODULE 2 PRACTICE QUESTIONS
CAPE PURE MATHEMATICS UNIT 2 MODULE 2 PRACTICE QUESTIONSCAPE PURE MATHEMATICS UNIT 2 MODULE 2 PRACTICE QUESTIONS
CAPE PURE MATHEMATICS UNIT 2 MODULE 2 PRACTICE QUESTIONS
Carlon Baird
 
Convex Partitioning of a Polygon into Smaller Number of Pieces with Lowest Me...
Convex Partitioning of a Polygon into Smaller Number of Pieces with Lowest Me...Convex Partitioning of a Polygon into Smaller Number of Pieces with Lowest Me...
Convex Partitioning of a Polygon into Smaller Number of Pieces with Lowest Me...
Kasun Ranga Wijeweera
 
Research Inventy : International Journal of Engineering and Science
Research Inventy : International Journal of Engineering and ScienceResearch Inventy : International Journal of Engineering and Science
Research Inventy : International Journal of Engineering and Science
researchinventy
 
EC8352-Signals and Systems - Laplace transform
EC8352-Signals and Systems - Laplace transformEC8352-Signals and Systems - Laplace transform
EC8352-Signals and Systems - Laplace transform
NimithaSoman
 
jalalam.ppt
jalalam.pptjalalam.ppt
jalalam.ppt
HamnaAnis1
 
Chapter1polarcoordinatesandvector 150105021140-conversion-gate02
Chapter1polarcoordinatesandvector 150105021140-conversion-gate02Chapter1polarcoordinatesandvector 150105021140-conversion-gate02
Chapter1polarcoordinatesandvector 150105021140-conversion-gate02
Cleophas Rwemera
 
Applied Calculus Chapter 1 polar coordinates and vector
Applied Calculus Chapter  1 polar coordinates and vectorApplied Calculus Chapter  1 polar coordinates and vector
Applied Calculus Chapter 1 polar coordinates and vector
J C
 
Lect4 ellipse
Lect4 ellipseLect4 ellipse
Lect4 ellipse
BCET
 
Lecture 23 loop transfer function
Lecture 23 loop transfer functionLecture 23 loop transfer function
Lecture 23 loop transfer function
bennedy ningthoukhongjam
 
Signals and systems assignment help
Signals and systems assignment helpSignals and systems assignment help
Signals and systems assignment help
Matlab Assignment Experts
 
IJCER (www.ijceronline.com) International Journal of computational Engineerin...
IJCER (www.ijceronline.com) International Journal of computational Engineerin...IJCER (www.ijceronline.com) International Journal of computational Engineerin...
IJCER (www.ijceronline.com) International Journal of computational Engineerin...ijceronline
 
Dsp U Lec04 Discrete Time Signals & Systems
Dsp U   Lec04 Discrete Time Signals & SystemsDsp U   Lec04 Discrete Time Signals & Systems
Dsp U Lec04 Discrete Time Signals & Systems
taha25
 
Direct methods for the solution of systems of linear equations
Direct methods for the solution of systems of linear equationsDirect methods for the solution of systems of linear equations
Direct methods for the solution of systems of linear equationsNORAIMA
 
04-Laplace Transform and Its Inverse.pptx
04-Laplace Transform and Its  Inverse.pptx04-Laplace Transform and Its  Inverse.pptx
04-Laplace Transform and Its Inverse.pptx
HananShayibo
 
Data sparse approximation of the Karhunen-Loeve expansion
Data sparse approximation of the Karhunen-Loeve expansionData sparse approximation of the Karhunen-Loeve expansion
Data sparse approximation of the Karhunen-Loeve expansion
Alexander Litvinenko
 

Similar to Algorithm chapter 8 (20)

Cc10 3 geo_har
Cc10 3 geo_harCc10 3 geo_har
Cc10 3 geo_har
 
CAPE PURE MATHEMATICS UNIT 2 MODULE 2 PRACTICE QUESTIONS
CAPE PURE MATHEMATICS UNIT 2 MODULE 2 PRACTICE QUESTIONSCAPE PURE MATHEMATICS UNIT 2 MODULE 2 PRACTICE QUESTIONS
CAPE PURE MATHEMATICS UNIT 2 MODULE 2 PRACTICE QUESTIONS
 
Demo
DemoDemo
Demo
 
Demo
DemoDemo
Demo
 
Demo
DemoDemo
Demo
 
Convex Partitioning of a Polygon into Smaller Number of Pieces with Lowest Me...
Convex Partitioning of a Polygon into Smaller Number of Pieces with Lowest Me...Convex Partitioning of a Polygon into Smaller Number of Pieces with Lowest Me...
Convex Partitioning of a Polygon into Smaller Number of Pieces with Lowest Me...
 
Research Inventy : International Journal of Engineering and Science
Research Inventy : International Journal of Engineering and ScienceResearch Inventy : International Journal of Engineering and Science
Research Inventy : International Journal of Engineering and Science
 
EC8352-Signals and Systems - Laplace transform
EC8352-Signals and Systems - Laplace transformEC8352-Signals and Systems - Laplace transform
EC8352-Signals and Systems - Laplace transform
 
www.ijerd.com
www.ijerd.comwww.ijerd.com
www.ijerd.com
 
jalalam.ppt
jalalam.pptjalalam.ppt
jalalam.ppt
 
Chapter1polarcoordinatesandvector 150105021140-conversion-gate02
Chapter1polarcoordinatesandvector 150105021140-conversion-gate02Chapter1polarcoordinatesandvector 150105021140-conversion-gate02
Chapter1polarcoordinatesandvector 150105021140-conversion-gate02
 
Applied Calculus Chapter 1 polar coordinates and vector
Applied Calculus Chapter  1 polar coordinates and vectorApplied Calculus Chapter  1 polar coordinates and vector
Applied Calculus Chapter 1 polar coordinates and vector
 
Lect4 ellipse
Lect4 ellipseLect4 ellipse
Lect4 ellipse
 
Lecture 23 loop transfer function
Lecture 23 loop transfer functionLecture 23 loop transfer function
Lecture 23 loop transfer function
 
Signals and systems assignment help
Signals and systems assignment helpSignals and systems assignment help
Signals and systems assignment help
 
IJCER (www.ijceronline.com) International Journal of computational Engineerin...
IJCER (www.ijceronline.com) International Journal of computational Engineerin...IJCER (www.ijceronline.com) International Journal of computational Engineerin...
IJCER (www.ijceronline.com) International Journal of computational Engineerin...
 
Dsp U Lec04 Discrete Time Signals & Systems
Dsp U   Lec04 Discrete Time Signals & SystemsDsp U   Lec04 Discrete Time Signals & Systems
Dsp U Lec04 Discrete Time Signals & Systems
 
Direct methods for the solution of systems of linear equations
Direct methods for the solution of systems of linear equationsDirect methods for the solution of systems of linear equations
Direct methods for the solution of systems of linear equations
 
04-Laplace Transform and Its Inverse.pptx
04-Laplace Transform and Its  Inverse.pptx04-Laplace Transform and Its  Inverse.pptx
04-Laplace Transform and Its Inverse.pptx
 
Data sparse approximation of the Karhunen-Loeve expansion
Data sparse approximation of the Karhunen-Loeve expansionData sparse approximation of the Karhunen-Loeve expansion
Data sparse approximation of the Karhunen-Loeve expansion
 

More from chidabdu

Sienna 12 huffman
Sienna 12 huffmanSienna 12 huffman
Sienna 12 huffmanchidabdu
 
Sienna 11 graphs
Sienna 11 graphsSienna 11 graphs
Sienna 11 graphschidabdu
 
Sienna 10 dynamic
Sienna 10 dynamicSienna 10 dynamic
Sienna 10 dynamicchidabdu
 
Sienna 9 hashing
Sienna 9 hashingSienna 9 hashing
Sienna 9 hashingchidabdu
 
Sienna 8 countingsorts
Sienna 8 countingsortsSienna 8 countingsorts
Sienna 8 countingsortschidabdu
 
Sienna 7 heaps
Sienna 7 heapsSienna 7 heaps
Sienna 7 heapschidabdu
 
Sienna 6 bst
Sienna 6 bstSienna 6 bst
Sienna 6 bstchidabdu
 
Sienna 5 decreaseandconquer
Sienna 5 decreaseandconquerSienna 5 decreaseandconquer
Sienna 5 decreaseandconquerchidabdu
 
Sienna 4 divideandconquer
Sienna 4 divideandconquerSienna 4 divideandconquer
Sienna 4 divideandconquerchidabdu
 
Sienna 3 bruteforce
Sienna 3 bruteforceSienna 3 bruteforce
Sienna 3 bruteforcechidabdu
 
Sienna 2 analysis
Sienna 2 analysisSienna 2 analysis
Sienna 2 analysischidabdu
 
Sienna 1 intro
Sienna 1 introSienna 1 intro
Sienna 1 introchidabdu
 
Sienna 13 limitations
Sienna 13 limitationsSienna 13 limitations
Sienna 13 limitationschidabdu
 
Unit 3 basic processing unit
Unit 3   basic processing unitUnit 3   basic processing unit
Unit 3 basic processing unitchidabdu
 
Unit 5 I/O organization
Unit 5   I/O organizationUnit 5   I/O organization
Unit 5 I/O organizationchidabdu
 
Algorithm chapter 1
Algorithm chapter 1Algorithm chapter 1
Algorithm chapter 1chidabdu
 
Algorithm chapter 11
Algorithm chapter 11Algorithm chapter 11
Algorithm chapter 11chidabdu
 
Algorithm chapter 10
Algorithm chapter 10Algorithm chapter 10
Algorithm chapter 10chidabdu
 
Algorithm chapter 9
Algorithm chapter 9Algorithm chapter 9
Algorithm chapter 9chidabdu
 
Algorithm chapter 7
Algorithm chapter 7Algorithm chapter 7
Algorithm chapter 7chidabdu
 

More from chidabdu (20)

Sienna 12 huffman
Sienna 12 huffmanSienna 12 huffman
Sienna 12 huffman
 
Sienna 11 graphs
Sienna 11 graphsSienna 11 graphs
Sienna 11 graphs
 
Sienna 10 dynamic
Sienna 10 dynamicSienna 10 dynamic
Sienna 10 dynamic
 
Sienna 9 hashing
Sienna 9 hashingSienna 9 hashing
Sienna 9 hashing
 
Sienna 8 countingsorts
Sienna 8 countingsortsSienna 8 countingsorts
Sienna 8 countingsorts
 
Sienna 7 heaps
Sienna 7 heapsSienna 7 heaps
Sienna 7 heaps
 
Sienna 6 bst
Sienna 6 bstSienna 6 bst
Sienna 6 bst
 
Sienna 5 decreaseandconquer
Sienna 5 decreaseandconquerSienna 5 decreaseandconquer
Sienna 5 decreaseandconquer
 
Sienna 4 divideandconquer
Sienna 4 divideandconquerSienna 4 divideandconquer
Sienna 4 divideandconquer
 
Sienna 3 bruteforce
Sienna 3 bruteforceSienna 3 bruteforce
Sienna 3 bruteforce
 
Sienna 2 analysis
Sienna 2 analysisSienna 2 analysis
Sienna 2 analysis
 
Sienna 1 intro
Sienna 1 introSienna 1 intro
Sienna 1 intro
 
Sienna 13 limitations
Sienna 13 limitationsSienna 13 limitations
Sienna 13 limitations
 
Unit 3 basic processing unit
Unit 3   basic processing unitUnit 3   basic processing unit
Unit 3 basic processing unit
 
Unit 5 I/O organization
Unit 5   I/O organizationUnit 5   I/O organization
Unit 5 I/O organization
 
Algorithm chapter 1
Algorithm chapter 1Algorithm chapter 1
Algorithm chapter 1
 
Algorithm chapter 11
Algorithm chapter 11Algorithm chapter 11
Algorithm chapter 11
 
Algorithm chapter 10
Algorithm chapter 10Algorithm chapter 10
Algorithm chapter 10
 
Algorithm chapter 9
Algorithm chapter 9Algorithm chapter 9
Algorithm chapter 9
 
Algorithm chapter 7
Algorithm chapter 7Algorithm chapter 7
Algorithm chapter 7
 

Recently uploaded

Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Jeffrey Haguewood
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
Product School
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
Elena Simperl
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
Sri Ambati
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Tobias Schneck
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Inflectra
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Product School
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
DianaGray10
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
DianaGray10
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Thierry Lestable
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
Guy Korland
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
ODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User Group
CatarinaPereira64715
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Product School
 

Recently uploaded (20)

Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
ODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User Group
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 

Algorithm chapter 8

  • 2. Dynamic Programming Dynamic Programming is a general algorithm design technique. “Programming” here means “planning”. Invented by American mathematician Richard Bellman in the 1950s to solve 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 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 optimization problems: 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 Closure The 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
  • 8. Warshall’s Algorithm • Main idea: Use a bottom-up method to construct the transitive closure of a given digraph 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 1 Does 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 an intermediate node intermediate node intermediate node intermediate node intermediate node
  • 9. Warshall’s Algorithm In the kth stage: to determine R(k) is to determine if a path exists between 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 stage i 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: All pairs shortest paths All pairs shortest paths problem: In a weighted graph, dij(k) = length of the find shortest paths between every pair of vertices. shortest path from i to j with each vertex Applicable to: undirected and directed weighted graphs; numbered no higher no negative weight. than k. Same idea as the Warshall’s algorithm : construct solution 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 Algorithm D(k) : allow 1, 2, …, k to be intermediate vertices. In the kth stage, determine whether the introduction of k as a new eligible intermediate 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 Comments The crucial step in designing a dynamic programming algorithm: Deriving a recurrence relating a solution to the problem’s current instance with solutions of its smaller ( and overlapping) subinstances.
  • 13. 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?
  • 14. 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 < 0 V[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] value return V[i, j]