0% found this document useful (0 votes)
7 views11 pages

Chap 6 - DynamicProgramming(Binomial-AllShotestPath)

The document discusses the Dynamic Programming (DP) technique, focusing on its applications in calculating binomial coefficients and finding the shortest paths in graphs. It explains the binomial coefficient formula and its recurrence relation, along with an example and the use of Pascal's Triangle for computation. Additionally, it outlines the all pairs shortest paths problem and Floyd's algorithm, detailing the complexity analysis for both techniques.

Uploaded by

3ashry.stud
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views11 pages

Chap 6 - DynamicProgramming(Binomial-AllShotestPath)

The document discusses the Dynamic Programming (DP) technique, focusing on its applications in calculating binomial coefficients and finding the shortest paths in graphs. It explains the binomial coefficient formula and its recurrence relation, along with an example and the use of Pascal's Triangle for computation. Additionally, it outlines the all pairs shortest paths problem and Floyd's algorithm, detailing the complexity analysis for both techniques.

Uploaded by

3ashry.stud
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 11

Dynamic Programming Technique

Applications of DP technique:
(ii) Binomial
(ii) Shortest Path.

Dr. Hazem M. Bahig


Dr. H. Bahig 1
Binomial Coefficient
Definition

Binomial coefficients are coefficients of the binomial formula:


(a + b)n = C(n,0)anb0 + . . . + C(n,k)an-kbk + . . . + C(n,n)a0bn

Recurrence: C(n,k) = C(n-1,k) + C(n-1,k-1) for n > k > 0


C(n,0) = 1, C(n,n) = 1 for n  0
c(n, i) = n! / (i! * (n – i)!)
Examples

C(5,3)= 5!/ (3! * 2!)= (5*4*3!)/(3! * 2!)=10

Dr. H. Bahig 2
Input • Given n and k.

Output • C(n,k).

Dr. H. Bahig 3
Binomial Coefficients
• (x + y)2 = x2 + 2xy + y2, coefficients are 1,2,1
• (x + y)3 = x3 + 3x2y + 3xy2 + y3, coefficients are 1,3,3,1
• (x + y)4 = x4 + 4x3y + 6x2y2 + 4xy3 + y4,
coefficients are 1,4,6,4,1
• (x + y)5 = x5 + 5x4y + 10x3y2 + 10x2y3 + 5xy4 + y5,
coefficients are 1,5,10,10,5,1
• The n+1 coefficients can be computed for (x + y)n according
to the formula c(n, i) = n! / (i! * (n – i)!)
for each of i = 0..n
• The repeated computation of all the factorials gets to be
expensive.

4 Dr. H. Bahig
Main Idea

• Value of C(n,k) can be computed by filling a table:

n c(n,0) c(n,1) c(n,2) c(n,3) c(n,4) c(n,5) c(n,6)


0 1
1 1 1
2 1 2 1
3 1 3 3 1
4 1 4 6 4 1
5 1 5 10 10 5 1
6 1 6 15 20 15 6 1
• Each row depends only on the preceding row
• This algorithm is known as Pascal’s Triangle
5 Dr. H. Bahig
Algorithm

Complexity Analysis

Time efficiency: Θ(n k)


Space efficiency: Θ(n k) Dr. H. Bahig 6
All pairs shortest paths
Definition
In a weighted (di)graph, find shortest paths between every pair of
vertices.

4 3
Examples 1
1
6
1 5
4
2 3

sp(1,3)=4, sp(1,2)= sp(1,4)= ∞

sp(3,1)= sp(3,2)= sp(3,4)= ∞


sp(2,1)=1, sp(2,3)=6, sp(2,4)=3

sp(4,1)=6, sp(4,2)=5, sp(4,3)=


All pairs shortest paths
Main Idea
 Construct solution through series of matrices D(0), …, D (n) using
increasing subsets of the vertices allowed as intermediate.

 On the k-th iteration, the algorithm determines shortest paths


between every pair of vertices i, j that use only vertices among 1,
…,k as intermediate
D(k)[i,j] = min {D(k-1)[i,j], D(k-1)[i,k] + D(k-1)[k,j]}
D(k-1)[i,k]
k

i
D(k-1)[k,j]
D(k-1)[i,j]

j
All pairs shortest paths
Main Idea
 Construct solution through series of matrices D(0), …, D (n) using
increasing subsets of the vertices allowed as intermediate.

 On the k-th iteration, the algorithm determines shortest paths


between every pair of vertices i, j that use only vertices among 1,
…,k as intermediate
D(k)[i,j] = min {D(k-1)[i,j], D(k-1)[i,k] + D(k-1)[k,j]}
D(k-1)[i,k]
k

i
D(k-1)[k,j]
D(k-1)[i,j]

j
Floyd’s Algorithm (example)

2
1 2 0 ∞ 3 ∞ 0 ∞ 3 ∞
6 7 2 0 ∞ ∞ 2 0 5 ∞
3 ∞ 7 0 1 ∞ 7 0 1
D(0) =
D(1) =

6 ∞ ∞ 0 6 ∞ 9 0
3 1 4

0 ∞ 3 ∞ 0 10 3 4 0 10 3 4
2 0 5 ∞ 2 0 5 6 2 0 5 6
D(2) = 9 7 0 1 D(3) = 9 7 0 1 D(4) = 7 7 0 1
6 ∞ 9 0 6 16 9 0 6 16 9 0
2
2 4 2 2
1 2 1 2
3 6 7
3 6 7 3 6 7

1 1 3
4 1 3 3 1 4
Algorithm

Complexity Analysis
Time efficiency: Θ(n3)
Space efficiency: Matrices can be written over their predecessors

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy