0% found this document useful (0 votes)
2 views

Ch10 Dynamic Programming1

This document provides an overview of dynamic programming, including its objectives, approaches (top-down and bottom-up), and techniques such as memoization. It explains the calculation of Fibonacci numbers and binomial coefficients, as well as algorithms like Warshall's and Floyd's for graph analysis. The document also includes comparisons between dynamic programming and divide-and-conquer strategies.

Uploaded by

qx54x4q55g
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Ch10 Dynamic Programming1

This document provides an overview of dynamic programming, including its objectives, approaches (top-down and bottom-up), and techniques such as memoization. It explains the calculation of Fibonacci numbers and binomial coefficients, as well as algorithms like Warshall's and Floyd's for graph analysis. The document also includes comparisons between dynamic programming and divide-and-conquer strategies.

Uploaded by

qx54x4q55g
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 36

Ch10

Dynamic Programming-
1
Eng. Jibril Hassan (Msc IT)

Design and Analysis of Algorithms - Chapter 1


10
Objectives

• After studying this chapter you should be


able to:
• Calculate the nth Fibonacci number
• Explain the dynamic programming
approach to compute binomial coefficients
• Explain Warshall‟s and Floyd‟s algorithms

Design and Analysis of Algorithms - Chapter 2


10
Overview of Dynamic programming
• Dynamic programming is an
optimization technique for particular
classes of backtracking algorithms
which repeatedly solve sub-problems.
• When dealing with algorithms, dynamic
programming always refers to the
technique of filling in a table with values
computed from the sub-problems.

Design and Analysis of Algorithms - Chapter 3


10
Dynamic programming approach
• Dynamic programming has the following
two approaches:
• Top-down approach – In this
approach, if the solution to any problem
can be formed recursively using the
solution to its sub-problems, and if its
sub-problems are overlapping, then one
can easily memoize or store the
solutions to the sub-problems in a table.
Design and Analysis of Algorithms - Chapter 4
10
Top-down approach
• Whenever we begin solving a new
sub-problem, we first check the table
to see if it has been already solved.
• If a solution has been recorded, then
we can use it directly, otherwise we
solve the sub-problem first and add
its solution to the table.

Design and Analysis of Algorithms - Chapter 5


10
Bottom-up approach
• Bottom-up approach – In this approach, once we
formulate the solution to a problem recursively in
terms of its sub-problems, we can try
reformulating the problem in a bottom-up
fashion, i.e.
• try solving the sub-problems first and use their
solutions to arrive at solutions to bigger sub-
problems.

Design and Analysis of Algorithms - Chapter 6


10
Bottom-up approach
• This is also usually done in a table, where the
results of the sub-problems are stored in a table
and are iteratively used to generate solutions to
bigger subproblems.

Design and Analysis of Algorithms - Chapter 7


10
Concept of memoization
• Memoization has a specialized meaning in
computing.
• Memoization is the technique by which we make
a function perform faster by trading space for
time.
• It can be achieved by storing the return values of
the function in a table.
• Hence when the function is called again, the
values stored in the table are used to further
compute the values, instead of computing the
value all over again.
Design and Analysis of Algorithms - Chapter 8
10
Memoization
methodology
1) Start with a backtracking algorithm
2) Look up the problem in a table; if table has a valid
entry for it, return that value
3) Else, compute the problem recursively, and then
store the result in the table before returning the
value

Design and Analysis of Algorithms - Chapter 9


10
Dynamic programming Vs divide and conquer

• The divide-and-conquer algorithms


divide the problem into independent
subproblems, solve the sub-problems
recursively, and hence combine their
solutions to solve the original problem.
• In contrast, the dynamic programming
approach is applicable when the sub-
problems are not independent, that is,
when sub-problems share sub-problems.
Design and Analysis of Algorithms - Chapter 10
10
Dynamic programming Vs divide and conquer

• Therefore a divide-and-conquer algorithm


does more work than necessary by
repeatedly solving the common sub-
problems.
• A dynamic-programming algorithm on the
other hand solves every sub-problem just
once and then saves its answer in a table,
thereby avoiding the need to re-compute
the answer every time the sub-problem is
encountered.
Design and Analysis of Algorithms - Chapter 11
10
Fibonacci Numbers
• In the Fibonacci sequence, after the first
two numbers i.e. 0 and 1 in the
sequence, each subsequent number in
the series is equal to the sum of the
previous two numbers.
• The sequence is named after Leonardo
of Pisa, also known as Fibonacci.

Design and Analysis of Algorithms - Chapter 12


10
Fibonacci Numbers
• These fascinating numbers can be
found in the branching of trees, the
patterns on a pineapple, the florets of a
sunflower, the spirals of a pine cone,
and even in the placement of leaves on
the stems of many plants.

Design and Analysis of Algorithms - Chapter 13


10
Fibonacci Numbers
• Fibonacci numbers are one example of
patterns that have interested
mathematicians through the ages.
• A real life example of a Fibonacci sequence
is given below
• Beginning with a single pair of rabbits, if
every month each productive pair bears a
new pair, which becomes productive when
they are 1 month old, how many rabbits
will there be after n months?
Design and Analysis of Algorithms - Chapter 14
10
Fibonacci Algorithms

Design and Analysis of Algorithms - Chapter 15


10
Fibonacci Algorithms

Design and Analysis of Algorithms - Chapter 16


10
Binomial Coefficients
• The binomial coefficient is the number of ways of picking k
unordered outcomes from n possibilities. It is also known as
a combination or combinatorial number.
• The binomial coefficient is represented as and can be read as
“n choose k”
• The value of the binomial coefficient is given by equation Eq:
10.

Design and Analysis of Algorithms - Chapter 17


10
Computation of binomial
coefficients
• Pascal’s triangle
• Pascal‟s triangle helps you determine the
coefficients which arise in binomial expansion.
• For example consider equation Eq: 10.7

• You might notice that the coefficients are


numbers in the row of Pascal‟s triangle shown in
the Figure 10.3.

Design and Analysis of Algorithms - Chapter 18


10
Pascal’s triangle

Design and Analysis of Algorithms - Chapter 19


10
Dynamic programming approach

• Computing a binomial coefficient is a


standard example of applying dynamic
programming to a non-optimization
problem.
• You may recall from your studies of
elementary combinatorics that the
binomial coefficient, denoted

Design and Analysis of Algorithms - Chapter 20


10
Dynamic programming approach

Design and Analysis of Algorithms - Chapter 21


10
Warshall’s and Floyd’s Algorithm
• Transitive closure
• The transitive closure of a directed graph
(digraph) with n vertices is an n × n matrix such
that T [i, j] is true if and only if, j is reachable from
i by some path.
• Our aim is to figure the transitive closure of a
directed graph shown in Figure 10.5 where 10.5
(a) is the digraph, 10.5 (b) the adjacency matrix,
and 10.5 (c) the transitive closure.
• The adjacency matrix is a restriction of matrix T
if we only allow paths of length 1.
Design and Analysis of Algorithms - Chapter 22
10
Warshall’s and Floyd’s Algorithm

Design and Analysis of Algorithms - Chapter 23


10
Overview of Warshall’s
and Floyd’s algorithm
• The sub-problems here are
connectivity/distance information where
only a subset of vertices is allowed in the
paths.
• They are very useful to obtain information
about all pairs of vertices even though they
have complexity θ(n3 ).
• There are other algorithms if we only want
the information about one pair, (e.g. DFS
for reachability).
Design and Analysis of Algorithms - Chapter 24
10
Warshall’s algorithm

Design and Analysis of Algorithms - Chapter 25


10
Warshall’s algorithm

Design and Analysis of Algorithms - Chapter 26


10
Design and Analysis of Algorithms - Chapter 27
10
Floyd’s algorithm
• We can generate the distance matrix
with an algorithm that is very similar to
Warshall‟s algorithm. It is called Floyd‟s
algorithm, after its inventor R. Floyd.
• It is applicable to both undirected and
directed weighted graphs provided that
they do not contain a cycle of negative
length.

Design and Analysis of Algorithms - Chapter 28


10
Floyd’s algorithm

Design and Analysis of Algorithms - Chapter 29


10
Floyd’s Algorithm

Design and Analysis of Algorithms - Chapter 30


10
Floyd’s Algorithm

Design and Analysis of Algorithms - Chapter 31


10
Terminal Questions
1. Differentiate between dynamic programming
and Divide and Conquer techniques.
2. Write the algorithm to find the n th Fibonacci
number.
3. Explain the dynamic programming approach
to find binomial coefficients.
4. What is Warshall‟s algorithm to find the
transitive closure?
• 5. Explain the Floyd‟s algorithm to find the
shortest path.
Design and Analysis of Algorithms - Chapter 32
10
Self Assessment
Questions
1. The Top-down, and bottom-up are the two approaches
to dynamic programming.
2. Memoization is a technique to store answers to sub-
problems in a table.
3. The Divide and conquer algorithm is similar to the
dynamic approach.
4. The asymptotic running time when we first run to
calculate the nth Fibonacci number is O(n).

Design and Analysis of Algorithms - Chapter 33


10
Self Assessment
Questions
6. To compute the nth Fibonacci number we followed the
Bottom-up dynamic approach.
7. What formula can we use to find the value of the binomial
coefficient?
8. The gamma function allows the binomial
coefficient to be generalized to non-integer arguments like 9.
Binomial coefficients are a study of Complex n and k.

Design and Analysis of Algorithms - Chapter 34


10
Self Assessment
Questions
10. Both Warshall‟s and Floyd‟s algorithms have the
run time as . _θ(n3 )__
11. The Warshall‟s algorithm is used to solve
problems. Transitive closure
12. The Floyd‟s algorithm is used to solve Shortest
path _ problems.

Design and Analysis of Algorithms - Chapter 35


10
END

Design and Analysis of Algorithms - Chapter 36


10

You might also like