SlideShare a Scribd company logo
CSC 421: Applied
Algorithms and Structures
Week 6
“Programming isn't about what you know; it's about what you can figure out.” – Chris Pine
“An algorithm must be seen to be believed.” - Donald Knuth
Fibonacci pseudocode
Naïve recursive version (Θ 2𝑛 )
Fibo(n)
if n = 0 then return 1
return Fibo(n-1)+Fibo(n-2)
DP smart recursive version (Θ 𝑛 )
Fibo(n, f)
if f[n] exists then return f[n]
if n = 0 or n = 1 then
f[n] = 1
return 1
sum = Fibo(n-1) + Fibo(n-2)
f[n] = sum
return sum
Fibonacci pseudocode
DP iterative version (Θ 𝑛 )
Fibo(n)
f[0] = f[1] = 1
for i = 2 to n
f[i] = f[i-1] + f[i-2]
return f[n]
Dynamic programming
Calculating Fibonacci numbers
• The usual recurrence:
𝐹𝑖𝑏𝑜 𝑛 =
1 𝑖𝑓 𝑛 = 0 ∨ 𝑛 = 1
𝐹𝑖𝑏𝑜 𝑛 − 1 + 𝐹𝑖𝑏𝑜(𝑛 − 2) 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒
• Algorithm that calculates using this recurrence
• Runtime recurrence: 𝑇 𝑛 = 𝑇 𝑛 − 1 + 𝑇 𝑛 − 2 + 1
• Memoization (table of previously calculated values)
• Best way: Only keep track of last two values
Fibonacci (naive) call tree
Fibonacci (memoized) call tree
Catalan numbers
• How many different strings of 𝑛 pairs of balanced parentheses?
• Recurrence formula:
𝐶 𝑛 =
1 𝑖𝑓 𝑛 = 0
𝑖=1
𝑛
𝐶𝑖−1𝐶𝑛−𝑖 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒
• 𝐶 0 = 1
• 𝐶 1 = 𝐶 0 𝐶 0 = 1
• 𝐶 2 = 𝐶 0 𝐶 1 + 𝐶 1 𝐶 0 = 2
• 𝐶 3 = 𝐶 0 𝐶 2 + 𝐶 1 𝐶 1 + 𝐶 2 𝐶 0 = 5
• 𝐶 4 = 𝐶 0 𝐶 3 + 𝐶 1 𝐶 2 + 𝐶 2 𝐶 1 + 𝐶 3 𝐶 0 = 14
• 𝐶 5 = 𝐶 0 𝐶 4 + 𝐶 1 𝐶 3 + 𝐶 2 𝐶 2 + 𝐶 3 𝐶 1 + 𝐶 4 𝐶 0 = 42
• First few values of the sequence: 1, 1, 2, 5, 14, 42, 132, ….
Catalan numbers
• Naïve pseudocode:
CATALAN(n)
if n = 0 then return 1
sum = 0
for i = 1 to n
sum += CATALAN(i-1) * CATALAN(n-i)
return sum
• Top-down approach
• Bottom-up approach using memoization
Catalan numbers
• Dynamic programming pseudocode:
Catalan(n)
table[0] = 1
for i = 1 to n+1
sum = 0
for j = 0 to i-1
sum += table[j]*table[i-j-1]
table[i] = sum
return table[n]
Catalan numbers
• Other formulas:
𝐶 𝑛 =
1
𝑛 + 1
2𝑛
𝑛
=
2𝑛 !
𝑛 + 1 ! 𝑛!
𝐶 𝑛 =
2 2𝑛 − 1
𝑛 + 1
𝐶(𝑛 − 1)
Chessboard traversal
Given an 𝑛 × 𝑛 chessboard 𝑝 where each square has a profit associated with it. The
problem is to find the path from the top row to the bottom row that maximizes the
total profit. A move from a square may only go to a square in the next row down that
it touches, that is, one down and one to the left, one straight down, or one down and
one to the right.
See example on the whiteboard.
Chessboard traversal
This is a maximization optimization problem. You are given an 𝑛 × 𝑛 table 𝑝 of profits.
Here is top down recursive definition of maximal profit:
𝑞(𝑖, 𝑗) =
0 𝑗 < 1 or 𝑗 > 𝑛
𝑝[𝑖, 𝑗] if 𝑖 = 1
𝑝 𝑖, 𝑗 + max{𝑞(𝑖 − 1, 𝑗 − 1), 𝑞(𝑖 − 1, 𝑗), 𝑞(𝑖 − 1, 𝑗 + 1)} otherwise
q(i,j)
if j < 1 or j > n then return 0
else if i = 1 then return p[i,j]
else return p[i,j] + max(q(i-1,j-1), q(i-1,j), q(i-1,j+1))
This algorithm would take Θ(2𝑛
) time. Show this with a recursion tree.
Chessboard traversal
Instead of beginning by trying to compute the maximal profit path of length 𝑛, we will
compute maximal profits for paths of length 1, then paths of length 2, and so forth.
We’ll use a 2-dimensional table called 𝑞.
The value of 𝑞[𝑖, 𝑗] is the maximum profit one can earn for every path that ends at
square 𝑖, 𝑗.
Represent the computation of 𝑞 with an 𝑛 × 𝑛 table. Fill the table row-by-row. Begin
by filling row 1 with the values from the profit table (𝑝).
Developing a dynamic programming
solution
a) Formulate the problem recursively.
a) Specification.
b) Solution.
b) Build solutions to your recurrence from the bottom up.
a) Identify the sub-problems.
b) Choose a memoization data structure.
c) Identify dependencies.
d) Find a good evaluation order.
e) Analyze space and running time.
f) Write down the algorithm.
Edit distance
• This is a minimization optimization problem.
• Note the representation showing both strings and the edit operations, for example:
𝐴 𝐿 𝐺 𝑂 𝑅 𝐼 𝑇 𝐻 𝑀
𝐴 𝐿 𝑇 𝑅 𝑈 𝐼 𝑆 𝑇 𝐼 𝐶
𝑑 𝑠 𝑖 𝑖 𝑠 𝑠
• Look at the steps in creating a dynamic programming solution to a problem.
• Specify those for the edit distance problem.
Rod cutting problem
Given a rod of length 𝑛 and a price chart for segments of length up to 𝑛. Determine
which cut maximizes the total price.
Given a rod of length 4. What is the optimal set of cuts?
Rod cutting problem
We can simplify the optimization by realizing that we need to know where the leftmost
cut will be in an optimal cutting of the rod. Given that, we get the following recurrence
relation for calculating 𝑟[𝑘], the maximum profit obtainable from a rod of length 𝑘:
𝑟 𝑘 =
0 if 𝑘 = 0
max
1≤𝑖≤𝑘
{𝑝 𝑖 + 𝑟 𝑘 − 𝑖 } otherwise
The index 𝑖 in the maximum calculation is the location of the leftmost cut.
Calculating this recurrence from the top-down is done by the pseudocode on the next
slide.
Recursive top-down solution
Analysis of that solution
The recurrence relation is:
𝑇 𝑛 = 1 +
𝑗=0
𝑛−1
𝑇(𝑗)
This is Θ(2𝑛
).
Need a better way. Let’s solve the problem bottom-up.
Iterative bottom-up solution
Analysis of that solution
Two nested loops:
𝑗=1
𝑛
𝑖=1
𝑗
1 =
𝑗=1
𝑛
𝑗 =
𝑛 𝑛 + 1
2
= Θ(𝑛2)
How does the bottom-up approach work?
The array element 𝑝[𝑖] holds the price for a segment of length 𝑖. The array element
𝑟[𝑖] holds the optimal price for a rod of length 𝑖.
Compute the optimal solution for a rod of length 1.
Compute the optimal solution for a rod of length 2.
…
Computing the optimal solution for a rod of length 𝑗 means trying each of the possible
𝑗 − 1 cuts. We think of each cut as being the first one (leftmost).
Longest common subsequence
In the longest-common-subsequence problem, we are given two sequences:
𝑋 = 〈𝑥1, 𝑥2, … , 𝑥𝑚〉, 𝑌 = 〈𝑦1, 𝑦2, … , 𝑦𝑛〉
We wish to find a maximum-length common subsequence 𝑍 of 𝑋 and 𝑌.
LCS: definition of subsequence
Given a sequence
𝑋 = 〈 𝑥1, 𝑥2, … , 𝑥𝑚〉
Another sequence
𝑍 = 〈𝑧1, 𝑧2, … , 𝑧𝑘〉
is a subsequence of 𝑋 if there exists a strictly increasing sequence 〈𝑖1, 𝑖2, … , 𝑖𝑘〉 of
indices of 𝑋 such that for all 𝑗 = 1, 2, … , 𝑘, we have 𝑥𝑖𝑗
= 𝑧𝑗.
Notation: 𝑋𝑗 is the first 𝑗 characters of sequence 𝑋.
Also, assume that 𝑋 has 𝑚 characters, 𝑌 has 𝑛 characters, and 𝑍 has 𝑘 characters.
LCS: optimal substructure
Given sequences 𝑋 and 𝑌 and longest common subsequence 𝑍:
• If 𝑥𝑚 = 𝑦𝑛, then 𝑧𝑘 = 𝑥𝑚 = 𝑦𝑛 and 𝑍𝑘−1 is an LCS of 𝑋𝑚−1 and 𝑌𝑛−1.
• If 𝑥𝑚 ≠ 𝑦𝑛, then 𝑧𝑘 ≠ 𝑥𝑚 implies that 𝑍 is an LCS of 𝑋𝑚−1 and 𝑌.
• If 𝑥𝑚 ≠ 𝑦𝑛, then 𝑧𝑘 ≠ 𝑦𝑛 implies that 𝑍 is an LCS of 𝑋 and 𝑌𝑛−1.
The LCS problem has optimal substructure.
LCS: recursive solution
We can use the optimal substructure formulas to construct a recurrence relation for
𝑐[𝑖, 𝑗], which is the length of the LCS for the pair 𝑋𝑖 and 𝑌
𝑗:
𝑐 𝑖, 𝑗 =
0 𝑖𝑓 𝑖 = 0 𝑜𝑟 𝑗 = 0
𝑐 𝑖 − 1, 𝑗 − 1 + 1 𝑖𝑓 𝑖, 𝑗 > 0 𝑎𝑛𝑑 𝑥𝑖 = 𝑦𝑗
max{𝑐 𝑖, 𝑗 − 1 , 𝑐 𝑖 − 1, 𝑗 } 𝑖𝑓 𝑖, 𝑗 > 0 𝑎𝑛𝑑 𝑥𝑖 ≠ 𝑦𝑗
There are many possible sub-problems but the formula only considers a few.
LCS: computing using DP
Note that the 𝑐 values may be placed into a table of size 𝑚 × 𝑛.
Index the rows from the top to bottom 0 to 𝑚, index the columns from left to right 0 to
𝑛. Note that computing 𝑐[𝑖, 𝑗] requires table values to its left, above it, or left and
above.
So fill the table row-by-row starting at row 0.
On the board: An example with the sequences 𝑋 = 〈𝐴, 𝐵, 𝐶, 𝐵, 𝐷, 𝐴, 𝐵〉 and 𝑌 =
〈𝐵, 𝐷, 𝐶, 𝐴, 𝐵, 𝐴〉.
LCS algorithm
LCS algorithm: running time
Nested loops, the outside one executing 𝑚 iterations, the inside one executing 𝑛
iterations: Θ(𝑚𝑛).
LCS: printing a LCS
Assembly line traversal
There are two assembly lines
We want to minimize the cost of going from the entry point to the exit point. That is,
we want to minimize the total of the costs associated with each station and each
transition from line to the other.
Assembly line traversal
With 𝑛 stations, there are 2𝑛 possible paths (once again, the number of 𝑛-bit binary
strings comes in handy).
Here’s a recurrence that defines an optimal solution for a station on the first line:
𝑓1 𝑗 =
𝑒1 + 𝑎1,1 𝑖𝑓 𝑗 = 1
min{𝑓1 𝑗 − 1 + 𝑎1,𝑗, 𝑓2 𝑗 − 1 + 𝑡2,𝑗−1 + 𝑎1,𝑗} 𝑖𝑓 𝑗 > 1
The first term in the min expression is when the optimal path comes to station 𝑆1,𝑗
from station 𝑆1,𝑗−1; the second term when it comes from station 𝑆2,𝑗−1.
Assembly line traversal
Treating the recurrence relation as a way to define values in a pair of tables, fill each
table bottom-up, that is, starting at the table entry at position 1, rather than filling it by
starting at position 𝑛.
Let 𝑓1 1 = 𝑒1 + 𝑎1,1 and 𝑓2 1 = 𝑒2 + 𝑎2,1.
Have a for-loop indexing 𝑗 from 2 to 𝑛. Inside the for-loop compute 𝑓1[𝑗] and 𝑓2[𝑗].
From the recurrence, each of these relies only values in the table to the left, that is,
values that have already been computed.
Assembly line traversal
34
Here’s an example with actual values:

More Related Content

Similar to Applied Algorithms and Structures week999 (20)

Dynamic programming - fundamentals review
Dynamic programming - fundamentals reviewDynamic programming - fundamentals review
Dynamic programming - fundamentals review
ElifTech
 
Dynamic_methods_Greedy_algorithms_11.ppt
Dynamic_methods_Greedy_algorithms_11.pptDynamic_methods_Greedy_algorithms_11.ppt
Dynamic_methods_Greedy_algorithms_11.ppt
Gautam873893
 
Learn about dynamic programming and how to design algorith
Learn about dynamic programming and how to design algorithLearn about dynamic programming and how to design algorith
Learn about dynamic programming and how to design algorith
MazenulIslamKhan
 
Lecture11
Lecture11Lecture11
Lecture11
Nv Thejaswini
 
DynamicProgramming.pdf
DynamicProgramming.pdfDynamicProgramming.pdf
DynamicProgramming.pdf
ssuser3a8f33
 
Chap08alg
Chap08algChap08alg
Chap08alg
Munkhchimeg
 
Chap08alg
Chap08algChap08alg
Chap08alg
Munhchimeg
 
Daa chapter 2
Daa chapter 2Daa chapter 2
Daa chapter 2
B.Kirron Reddi
 
dynamic programming complete by Mumtaz Ali (03154103173)
dynamic programming complete by Mumtaz Ali (03154103173)dynamic programming complete by Mumtaz Ali (03154103173)
dynamic programming complete by Mumtaz Ali (03154103173)
Mumtaz Ali
 
Unit 2_final DESIGN AND ANALYSIS OF ALGORITHMS.pdf
Unit 2_final DESIGN AND ANALYSIS OF ALGORITHMS.pdfUnit 2_final DESIGN AND ANALYSIS OF ALGORITHMS.pdf
Unit 2_final DESIGN AND ANALYSIS OF ALGORITHMS.pdf
saiscount01
 
d0a2de03-27d3-4ca2-9ac6-d83440657a6c.ppt
d0a2de03-27d3-4ca2-9ac6-d83440657a6c.pptd0a2de03-27d3-4ca2-9ac6-d83440657a6c.ppt
d0a2de03-27d3-4ca2-9ac6-d83440657a6c.ppt
SrishaUrala
 
Sienna 10 dynamic
Sienna 10 dynamicSienna 10 dynamic
Sienna 10 dynamic
chidabdu
 
Cs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer keyCs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer key
appasami
 
Design and Analysis of Algorithm-Lecture.pptx
Design and Analysis of Algorithm-Lecture.pptxDesign and Analysis of Algorithm-Lecture.pptx
Design and Analysis of Algorithm-Lecture.pptx
bani30122004
 
unit-4-dynamic programming
unit-4-dynamic programmingunit-4-dynamic programming
unit-4-dynamic programming
hodcsencet
 
DynamicProgramming.ppt
DynamicProgramming.pptDynamicProgramming.ppt
DynamicProgramming.ppt
DavidMaina47
 
Skiena algorithm 2007 lecture18 application of dynamic programming
Skiena algorithm 2007 lecture18 application of dynamic programmingSkiena algorithm 2007 lecture18 application of dynamic programming
Skiena algorithm 2007 lecture18 application of dynamic programming
zukun
 
Chapter_8kkkaaaallalalalakakakakakakakak.pptx
Chapter_8kkkaaaallalalalakakakakakakakak.pptxChapter_8kkkaaaallalalalakakakakakakakak.pptx
Chapter_8kkkaaaallalalalakakakakakakakak.pptx
MhmdMk10
 
Sienna 3 bruteforce
Sienna 3 bruteforceSienna 3 bruteforce
Sienna 3 bruteforce
chidabdu
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
Amit Kumar Rathi
 
Dynamic programming - fundamentals review
Dynamic programming - fundamentals reviewDynamic programming - fundamentals review
Dynamic programming - fundamentals review
ElifTech
 
Dynamic_methods_Greedy_algorithms_11.ppt
Dynamic_methods_Greedy_algorithms_11.pptDynamic_methods_Greedy_algorithms_11.ppt
Dynamic_methods_Greedy_algorithms_11.ppt
Gautam873893
 
Learn about dynamic programming and how to design algorith
Learn about dynamic programming and how to design algorithLearn about dynamic programming and how to design algorith
Learn about dynamic programming and how to design algorith
MazenulIslamKhan
 
DynamicProgramming.pdf
DynamicProgramming.pdfDynamicProgramming.pdf
DynamicProgramming.pdf
ssuser3a8f33
 
dynamic programming complete by Mumtaz Ali (03154103173)
dynamic programming complete by Mumtaz Ali (03154103173)dynamic programming complete by Mumtaz Ali (03154103173)
dynamic programming complete by Mumtaz Ali (03154103173)
Mumtaz Ali
 
Unit 2_final DESIGN AND ANALYSIS OF ALGORITHMS.pdf
Unit 2_final DESIGN AND ANALYSIS OF ALGORITHMS.pdfUnit 2_final DESIGN AND ANALYSIS OF ALGORITHMS.pdf
Unit 2_final DESIGN AND ANALYSIS OF ALGORITHMS.pdf
saiscount01
 
d0a2de03-27d3-4ca2-9ac6-d83440657a6c.ppt
d0a2de03-27d3-4ca2-9ac6-d83440657a6c.pptd0a2de03-27d3-4ca2-9ac6-d83440657a6c.ppt
d0a2de03-27d3-4ca2-9ac6-d83440657a6c.ppt
SrishaUrala
 
Sienna 10 dynamic
Sienna 10 dynamicSienna 10 dynamic
Sienna 10 dynamic
chidabdu
 
Cs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer keyCs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer key
appasami
 
Design and Analysis of Algorithm-Lecture.pptx
Design and Analysis of Algorithm-Lecture.pptxDesign and Analysis of Algorithm-Lecture.pptx
Design and Analysis of Algorithm-Lecture.pptx
bani30122004
 
unit-4-dynamic programming
unit-4-dynamic programmingunit-4-dynamic programming
unit-4-dynamic programming
hodcsencet
 
DynamicProgramming.ppt
DynamicProgramming.pptDynamicProgramming.ppt
DynamicProgramming.ppt
DavidMaina47
 
Skiena algorithm 2007 lecture18 application of dynamic programming
Skiena algorithm 2007 lecture18 application of dynamic programmingSkiena algorithm 2007 lecture18 application of dynamic programming
Skiena algorithm 2007 lecture18 application of dynamic programming
zukun
 
Chapter_8kkkaaaallalalalakakakakakakakak.pptx
Chapter_8kkkaaaallalalalakakakakakakakak.pptxChapter_8kkkaaaallalalalakakakakakakakak.pptx
Chapter_8kkkaaaallalalalakakakakakakakak.pptx
MhmdMk10
 
Sienna 3 bruteforce
Sienna 3 bruteforceSienna 3 bruteforce
Sienna 3 bruteforce
chidabdu
 

Recently uploaded (20)

QUIZ-O-FORCE PRELIMINARY ANSWER SLIDE.pptx
QUIZ-O-FORCE PRELIMINARY ANSWER SLIDE.pptxQUIZ-O-FORCE PRELIMINARY ANSWER SLIDE.pptx
QUIZ-O-FORCE PRELIMINARY ANSWER SLIDE.pptx
Sourav Kr Podder
 
How to Configure Subcontracting in Odoo 18 Manufacturing
How to Configure Subcontracting in Odoo 18 ManufacturingHow to Configure Subcontracting in Odoo 18 Manufacturing
How to Configure Subcontracting in Odoo 18 Manufacturing
Celine George
 
Critical Thinking and Bias with Jibi Moses
Critical Thinking and Bias with Jibi MosesCritical Thinking and Bias with Jibi Moses
Critical Thinking and Bias with Jibi Moses
Excellence Foundation for South Sudan
 
How to Setup Renewal of Subscription in Odoo 18
How to Setup Renewal of Subscription in Odoo 18How to Setup Renewal of Subscription in Odoo 18
How to Setup Renewal of Subscription in Odoo 18
Celine George
 
QUIZ-O-FORCE FINAL SET BY SUDIPTA & SUBHAM.pptx
QUIZ-O-FORCE FINAL SET BY SUDIPTA & SUBHAM.pptxQUIZ-O-FORCE FINAL SET BY SUDIPTA & SUBHAM.pptx
QUIZ-O-FORCE FINAL SET BY SUDIPTA & SUBHAM.pptx
Sourav Kr Podder
 
0b - THE ROMANTIC ERA: FEELINGS AND IDENTITY.pptx
0b - THE ROMANTIC ERA: FEELINGS AND IDENTITY.pptx0b - THE ROMANTIC ERA: FEELINGS AND IDENTITY.pptx
0b - THE ROMANTIC ERA: FEELINGS AND IDENTITY.pptx
Julián Jesús Pérez Fernández
 
Uterine Prolapse, causes type and classification,its managment
Uterine Prolapse, causes type and classification,its managmentUterine Prolapse, causes type and classification,its managment
Uterine Prolapse, causes type and classification,its managment
Ritu480198
 
How to Setup Lunch in Odoo 18 - Odoo guides
How to Setup Lunch in Odoo 18 - Odoo guidesHow to Setup Lunch in Odoo 18 - Odoo guides
How to Setup Lunch in Odoo 18 - Odoo guides
Celine George
 
QUIZ-O-FORCE 3.0 FINAL SET BY SOURAV .pptx
QUIZ-O-FORCE 3.0 FINAL SET BY SOURAV .pptxQUIZ-O-FORCE 3.0 FINAL SET BY SOURAV .pptx
QUIZ-O-FORCE 3.0 FINAL SET BY SOURAV .pptx
Sourav Kr Podder
 
LDMMIA About me 2025 Edition 3 College Volume
LDMMIA About me 2025 Edition 3 College VolumeLDMMIA About me 2025 Edition 3 College Volume
LDMMIA About me 2025 Edition 3 College Volume
LDM & Mia eStudios
 
How to Manage Orders in Odoo 18 Lunch - Odoo Slides
How to Manage Orders in Odoo 18 Lunch - Odoo SlidesHow to Manage Orders in Odoo 18 Lunch - Odoo Slides
How to Manage Orders in Odoo 18 Lunch - Odoo Slides
Celine George
 
Quiz-E-Mataram (Under 20 Quiz Set) .pptx
Quiz-E-Mataram (Under 20 Quiz Set) .pptxQuiz-E-Mataram (Under 20 Quiz Set) .pptx
Quiz-E-Mataram (Under 20 Quiz Set) .pptx
SouptikUkil
 
Types of Actions in Odoo 18 - Odoo Slides
Types of Actions in Odoo 18 - Odoo SlidesTypes of Actions in Odoo 18 - Odoo Slides
Types of Actions in Odoo 18 - Odoo Slides
Celine George
 
[2025] Qualtric XM-EX-EXPERT Study Plan | Practice Questions + Exam Details
[2025] Qualtric XM-EX-EXPERT Study Plan | Practice Questions + Exam Details[2025] Qualtric XM-EX-EXPERT Study Plan | Practice Questions + Exam Details
[2025] Qualtric XM-EX-EXPERT Study Plan | Practice Questions + Exam Details
Jenny408767
 
What are the Features & Functions of Odoo 18 SMS Marketing
What are the Features & Functions of Odoo 18 SMS MarketingWhat are the Features & Functions of Odoo 18 SMS Marketing
What are the Features & Functions of Odoo 18 SMS Marketing
Celine George
 
A Brief Introduction About Jack Lutkus
A Brief Introduction About  Jack  LutkusA Brief Introduction About  Jack  Lutkus
A Brief Introduction About Jack Lutkus
Jack Lutkus
 
How to Use Owl Slots in Odoo 17 - Odoo Slides
How to Use Owl Slots in Odoo 17 - Odoo SlidesHow to Use Owl Slots in Odoo 17 - Odoo Slides
How to Use Owl Slots in Odoo 17 - Odoo Slides
Celine George
 
THE FEMALE POPE IN SAINT PETER'S BASILICA
THE FEMALE POPE IN SAINT PETER'S BASILICATHE FEMALE POPE IN SAINT PETER'S BASILICA
THE FEMALE POPE IN SAINT PETER'S BASILICA
Claude LaCombe
 
Unit 4 Reverse Engineering Tools Functionalities & Use-Cases.pdf
Unit 4  Reverse Engineering Tools  Functionalities & Use-Cases.pdfUnit 4  Reverse Engineering Tools  Functionalities & Use-Cases.pdf
Unit 4 Reverse Engineering Tools Functionalities & Use-Cases.pdf
ChatanBawankar
 
"Orthoptera: Grasshoppers, Crickets, and Katydids pptx
"Orthoptera: Grasshoppers, Crickets, and Katydids pptx"Orthoptera: Grasshoppers, Crickets, and Katydids pptx
"Orthoptera: Grasshoppers, Crickets, and Katydids pptx
Arshad Shaikh
 
QUIZ-O-FORCE PRELIMINARY ANSWER SLIDE.pptx
QUIZ-O-FORCE PRELIMINARY ANSWER SLIDE.pptxQUIZ-O-FORCE PRELIMINARY ANSWER SLIDE.pptx
QUIZ-O-FORCE PRELIMINARY ANSWER SLIDE.pptx
Sourav Kr Podder
 
How to Configure Subcontracting in Odoo 18 Manufacturing
How to Configure Subcontracting in Odoo 18 ManufacturingHow to Configure Subcontracting in Odoo 18 Manufacturing
How to Configure Subcontracting in Odoo 18 Manufacturing
Celine George
 
How to Setup Renewal of Subscription in Odoo 18
How to Setup Renewal of Subscription in Odoo 18How to Setup Renewal of Subscription in Odoo 18
How to Setup Renewal of Subscription in Odoo 18
Celine George
 
QUIZ-O-FORCE FINAL SET BY SUDIPTA & SUBHAM.pptx
QUIZ-O-FORCE FINAL SET BY SUDIPTA & SUBHAM.pptxQUIZ-O-FORCE FINAL SET BY SUDIPTA & SUBHAM.pptx
QUIZ-O-FORCE FINAL SET BY SUDIPTA & SUBHAM.pptx
Sourav Kr Podder
 
Uterine Prolapse, causes type and classification,its managment
Uterine Prolapse, causes type and classification,its managmentUterine Prolapse, causes type and classification,its managment
Uterine Prolapse, causes type and classification,its managment
Ritu480198
 
How to Setup Lunch in Odoo 18 - Odoo guides
How to Setup Lunch in Odoo 18 - Odoo guidesHow to Setup Lunch in Odoo 18 - Odoo guides
How to Setup Lunch in Odoo 18 - Odoo guides
Celine George
 
QUIZ-O-FORCE 3.0 FINAL SET BY SOURAV .pptx
QUIZ-O-FORCE 3.0 FINAL SET BY SOURAV .pptxQUIZ-O-FORCE 3.0 FINAL SET BY SOURAV .pptx
QUIZ-O-FORCE 3.0 FINAL SET BY SOURAV .pptx
Sourav Kr Podder
 
LDMMIA About me 2025 Edition 3 College Volume
LDMMIA About me 2025 Edition 3 College VolumeLDMMIA About me 2025 Edition 3 College Volume
LDMMIA About me 2025 Edition 3 College Volume
LDM & Mia eStudios
 
How to Manage Orders in Odoo 18 Lunch - Odoo Slides
How to Manage Orders in Odoo 18 Lunch - Odoo SlidesHow to Manage Orders in Odoo 18 Lunch - Odoo Slides
How to Manage Orders in Odoo 18 Lunch - Odoo Slides
Celine George
 
Quiz-E-Mataram (Under 20 Quiz Set) .pptx
Quiz-E-Mataram (Under 20 Quiz Set) .pptxQuiz-E-Mataram (Under 20 Quiz Set) .pptx
Quiz-E-Mataram (Under 20 Quiz Set) .pptx
SouptikUkil
 
Types of Actions in Odoo 18 - Odoo Slides
Types of Actions in Odoo 18 - Odoo SlidesTypes of Actions in Odoo 18 - Odoo Slides
Types of Actions in Odoo 18 - Odoo Slides
Celine George
 
[2025] Qualtric XM-EX-EXPERT Study Plan | Practice Questions + Exam Details
[2025] Qualtric XM-EX-EXPERT Study Plan | Practice Questions + Exam Details[2025] Qualtric XM-EX-EXPERT Study Plan | Practice Questions + Exam Details
[2025] Qualtric XM-EX-EXPERT Study Plan | Practice Questions + Exam Details
Jenny408767
 
What are the Features & Functions of Odoo 18 SMS Marketing
What are the Features & Functions of Odoo 18 SMS MarketingWhat are the Features & Functions of Odoo 18 SMS Marketing
What are the Features & Functions of Odoo 18 SMS Marketing
Celine George
 
A Brief Introduction About Jack Lutkus
A Brief Introduction About  Jack  LutkusA Brief Introduction About  Jack  Lutkus
A Brief Introduction About Jack Lutkus
Jack Lutkus
 
How to Use Owl Slots in Odoo 17 - Odoo Slides
How to Use Owl Slots in Odoo 17 - Odoo SlidesHow to Use Owl Slots in Odoo 17 - Odoo Slides
How to Use Owl Slots in Odoo 17 - Odoo Slides
Celine George
 
THE FEMALE POPE IN SAINT PETER'S BASILICA
THE FEMALE POPE IN SAINT PETER'S BASILICATHE FEMALE POPE IN SAINT PETER'S BASILICA
THE FEMALE POPE IN SAINT PETER'S BASILICA
Claude LaCombe
 
Unit 4 Reverse Engineering Tools Functionalities & Use-Cases.pdf
Unit 4  Reverse Engineering Tools  Functionalities & Use-Cases.pdfUnit 4  Reverse Engineering Tools  Functionalities & Use-Cases.pdf
Unit 4 Reverse Engineering Tools Functionalities & Use-Cases.pdf
ChatanBawankar
 
"Orthoptera: Grasshoppers, Crickets, and Katydids pptx
"Orthoptera: Grasshoppers, Crickets, and Katydids pptx"Orthoptera: Grasshoppers, Crickets, and Katydids pptx
"Orthoptera: Grasshoppers, Crickets, and Katydids pptx
Arshad Shaikh
 

Applied Algorithms and Structures week999

  • 1. CSC 421: Applied Algorithms and Structures Week 6 “Programming isn't about what you know; it's about what you can figure out.” – Chris Pine “An algorithm must be seen to be believed.” - Donald Knuth
  • 2. Fibonacci pseudocode Naïve recursive version (Θ 2𝑛 ) Fibo(n) if n = 0 then return 1 return Fibo(n-1)+Fibo(n-2) DP smart recursive version (Θ 𝑛 ) Fibo(n, f) if f[n] exists then return f[n] if n = 0 or n = 1 then f[n] = 1 return 1 sum = Fibo(n-1) + Fibo(n-2) f[n] = sum return sum
  • 3. Fibonacci pseudocode DP iterative version (Θ 𝑛 ) Fibo(n) f[0] = f[1] = 1 for i = 2 to n f[i] = f[i-1] + f[i-2] return f[n]
  • 4. Dynamic programming Calculating Fibonacci numbers • The usual recurrence: 𝐹𝑖𝑏𝑜 𝑛 = 1 𝑖𝑓 𝑛 = 0 ∨ 𝑛 = 1 𝐹𝑖𝑏𝑜 𝑛 − 1 + 𝐹𝑖𝑏𝑜(𝑛 − 2) 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒 • Algorithm that calculates using this recurrence • Runtime recurrence: 𝑇 𝑛 = 𝑇 𝑛 − 1 + 𝑇 𝑛 − 2 + 1 • Memoization (table of previously calculated values) • Best way: Only keep track of last two values
  • 7. Catalan numbers • How many different strings of 𝑛 pairs of balanced parentheses? • Recurrence formula: 𝐶 𝑛 = 1 𝑖𝑓 𝑛 = 0 𝑖=1 𝑛 𝐶𝑖−1𝐶𝑛−𝑖 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒 • 𝐶 0 = 1 • 𝐶 1 = 𝐶 0 𝐶 0 = 1 • 𝐶 2 = 𝐶 0 𝐶 1 + 𝐶 1 𝐶 0 = 2 • 𝐶 3 = 𝐶 0 𝐶 2 + 𝐶 1 𝐶 1 + 𝐶 2 𝐶 0 = 5 • 𝐶 4 = 𝐶 0 𝐶 3 + 𝐶 1 𝐶 2 + 𝐶 2 𝐶 1 + 𝐶 3 𝐶 0 = 14 • 𝐶 5 = 𝐶 0 𝐶 4 + 𝐶 1 𝐶 3 + 𝐶 2 𝐶 2 + 𝐶 3 𝐶 1 + 𝐶 4 𝐶 0 = 42 • First few values of the sequence: 1, 1, 2, 5, 14, 42, 132, ….
  • 8. Catalan numbers • Naïve pseudocode: CATALAN(n) if n = 0 then return 1 sum = 0 for i = 1 to n sum += CATALAN(i-1) * CATALAN(n-i) return sum • Top-down approach • Bottom-up approach using memoization
  • 9. Catalan numbers • Dynamic programming pseudocode: Catalan(n) table[0] = 1 for i = 1 to n+1 sum = 0 for j = 0 to i-1 sum += table[j]*table[i-j-1] table[i] = sum return table[n]
  • 10. Catalan numbers • Other formulas: 𝐶 𝑛 = 1 𝑛 + 1 2𝑛 𝑛 = 2𝑛 ! 𝑛 + 1 ! 𝑛! 𝐶 𝑛 = 2 2𝑛 − 1 𝑛 + 1 𝐶(𝑛 − 1)
  • 11. Chessboard traversal Given an 𝑛 × 𝑛 chessboard 𝑝 where each square has a profit associated with it. The problem is to find the path from the top row to the bottom row that maximizes the total profit. A move from a square may only go to a square in the next row down that it touches, that is, one down and one to the left, one straight down, or one down and one to the right. See example on the whiteboard.
  • 12. Chessboard traversal This is a maximization optimization problem. You are given an 𝑛 × 𝑛 table 𝑝 of profits. Here is top down recursive definition of maximal profit: 𝑞(𝑖, 𝑗) = 0 𝑗 < 1 or 𝑗 > 𝑛 𝑝[𝑖, 𝑗] if 𝑖 = 1 𝑝 𝑖, 𝑗 + max{𝑞(𝑖 − 1, 𝑗 − 1), 𝑞(𝑖 − 1, 𝑗), 𝑞(𝑖 − 1, 𝑗 + 1)} otherwise q(i,j) if j < 1 or j > n then return 0 else if i = 1 then return p[i,j] else return p[i,j] + max(q(i-1,j-1), q(i-1,j), q(i-1,j+1)) This algorithm would take Θ(2𝑛 ) time. Show this with a recursion tree.
  • 13. Chessboard traversal Instead of beginning by trying to compute the maximal profit path of length 𝑛, we will compute maximal profits for paths of length 1, then paths of length 2, and so forth. We’ll use a 2-dimensional table called 𝑞. The value of 𝑞[𝑖, 𝑗] is the maximum profit one can earn for every path that ends at square 𝑖, 𝑗. Represent the computation of 𝑞 with an 𝑛 × 𝑛 table. Fill the table row-by-row. Begin by filling row 1 with the values from the profit table (𝑝).
  • 14. Developing a dynamic programming solution a) Formulate the problem recursively. a) Specification. b) Solution. b) Build solutions to your recurrence from the bottom up. a) Identify the sub-problems. b) Choose a memoization data structure. c) Identify dependencies. d) Find a good evaluation order. e) Analyze space and running time. f) Write down the algorithm.
  • 15. Edit distance • This is a minimization optimization problem. • Note the representation showing both strings and the edit operations, for example: 𝐴 𝐿 𝐺 𝑂 𝑅 𝐼 𝑇 𝐻 𝑀 𝐴 𝐿 𝑇 𝑅 𝑈 𝐼 𝑆 𝑇 𝐼 𝐶 𝑑 𝑠 𝑖 𝑖 𝑠 𝑠 • Look at the steps in creating a dynamic programming solution to a problem. • Specify those for the edit distance problem.
  • 16. Rod cutting problem Given a rod of length 𝑛 and a price chart for segments of length up to 𝑛. Determine which cut maximizes the total price. Given a rod of length 4. What is the optimal set of cuts?
  • 17. Rod cutting problem We can simplify the optimization by realizing that we need to know where the leftmost cut will be in an optimal cutting of the rod. Given that, we get the following recurrence relation for calculating 𝑟[𝑘], the maximum profit obtainable from a rod of length 𝑘: 𝑟 𝑘 = 0 if 𝑘 = 0 max 1≤𝑖≤𝑘 {𝑝 𝑖 + 𝑟 𝑘 − 𝑖 } otherwise The index 𝑖 in the maximum calculation is the location of the leftmost cut. Calculating this recurrence from the top-down is done by the pseudocode on the next slide.
  • 19. Analysis of that solution The recurrence relation is: 𝑇 𝑛 = 1 + 𝑗=0 𝑛−1 𝑇(𝑗) This is Θ(2𝑛 ). Need a better way. Let’s solve the problem bottom-up.
  • 21. Analysis of that solution Two nested loops: 𝑗=1 𝑛 𝑖=1 𝑗 1 = 𝑗=1 𝑛 𝑗 = 𝑛 𝑛 + 1 2 = Θ(𝑛2)
  • 22. How does the bottom-up approach work? The array element 𝑝[𝑖] holds the price for a segment of length 𝑖. The array element 𝑟[𝑖] holds the optimal price for a rod of length 𝑖. Compute the optimal solution for a rod of length 1. Compute the optimal solution for a rod of length 2. … Computing the optimal solution for a rod of length 𝑗 means trying each of the possible 𝑗 − 1 cuts. We think of each cut as being the first one (leftmost).
  • 23. Longest common subsequence In the longest-common-subsequence problem, we are given two sequences: 𝑋 = 〈𝑥1, 𝑥2, … , 𝑥𝑚〉, 𝑌 = 〈𝑦1, 𝑦2, … , 𝑦𝑛〉 We wish to find a maximum-length common subsequence 𝑍 of 𝑋 and 𝑌.
  • 24. LCS: definition of subsequence Given a sequence 𝑋 = 〈 𝑥1, 𝑥2, … , 𝑥𝑚〉 Another sequence 𝑍 = 〈𝑧1, 𝑧2, … , 𝑧𝑘〉 is a subsequence of 𝑋 if there exists a strictly increasing sequence 〈𝑖1, 𝑖2, … , 𝑖𝑘〉 of indices of 𝑋 such that for all 𝑗 = 1, 2, … , 𝑘, we have 𝑥𝑖𝑗 = 𝑧𝑗. Notation: 𝑋𝑗 is the first 𝑗 characters of sequence 𝑋. Also, assume that 𝑋 has 𝑚 characters, 𝑌 has 𝑛 characters, and 𝑍 has 𝑘 characters.
  • 25. LCS: optimal substructure Given sequences 𝑋 and 𝑌 and longest common subsequence 𝑍: • If 𝑥𝑚 = 𝑦𝑛, then 𝑧𝑘 = 𝑥𝑚 = 𝑦𝑛 and 𝑍𝑘−1 is an LCS of 𝑋𝑚−1 and 𝑌𝑛−1. • If 𝑥𝑚 ≠ 𝑦𝑛, then 𝑧𝑘 ≠ 𝑥𝑚 implies that 𝑍 is an LCS of 𝑋𝑚−1 and 𝑌. • If 𝑥𝑚 ≠ 𝑦𝑛, then 𝑧𝑘 ≠ 𝑦𝑛 implies that 𝑍 is an LCS of 𝑋 and 𝑌𝑛−1. The LCS problem has optimal substructure.
  • 26. LCS: recursive solution We can use the optimal substructure formulas to construct a recurrence relation for 𝑐[𝑖, 𝑗], which is the length of the LCS for the pair 𝑋𝑖 and 𝑌 𝑗: 𝑐 𝑖, 𝑗 = 0 𝑖𝑓 𝑖 = 0 𝑜𝑟 𝑗 = 0 𝑐 𝑖 − 1, 𝑗 − 1 + 1 𝑖𝑓 𝑖, 𝑗 > 0 𝑎𝑛𝑑 𝑥𝑖 = 𝑦𝑗 max{𝑐 𝑖, 𝑗 − 1 , 𝑐 𝑖 − 1, 𝑗 } 𝑖𝑓 𝑖, 𝑗 > 0 𝑎𝑛𝑑 𝑥𝑖 ≠ 𝑦𝑗 There are many possible sub-problems but the formula only considers a few.
  • 27. LCS: computing using DP Note that the 𝑐 values may be placed into a table of size 𝑚 × 𝑛. Index the rows from the top to bottom 0 to 𝑚, index the columns from left to right 0 to 𝑛. Note that computing 𝑐[𝑖, 𝑗] requires table values to its left, above it, or left and above. So fill the table row-by-row starting at row 0. On the board: An example with the sequences 𝑋 = 〈𝐴, 𝐵, 𝐶, 𝐵, 𝐷, 𝐴, 𝐵〉 and 𝑌 = 〈𝐵, 𝐷, 𝐶, 𝐴, 𝐵, 𝐴〉.
  • 29. LCS algorithm: running time Nested loops, the outside one executing 𝑚 iterations, the inside one executing 𝑛 iterations: Θ(𝑚𝑛).
  • 31. Assembly line traversal There are two assembly lines We want to minimize the cost of going from the entry point to the exit point. That is, we want to minimize the total of the costs associated with each station and each transition from line to the other.
  • 32. Assembly line traversal With 𝑛 stations, there are 2𝑛 possible paths (once again, the number of 𝑛-bit binary strings comes in handy). Here’s a recurrence that defines an optimal solution for a station on the first line: 𝑓1 𝑗 = 𝑒1 + 𝑎1,1 𝑖𝑓 𝑗 = 1 min{𝑓1 𝑗 − 1 + 𝑎1,𝑗, 𝑓2 𝑗 − 1 + 𝑡2,𝑗−1 + 𝑎1,𝑗} 𝑖𝑓 𝑗 > 1 The first term in the min expression is when the optimal path comes to station 𝑆1,𝑗 from station 𝑆1,𝑗−1; the second term when it comes from station 𝑆2,𝑗−1.
  • 33. Assembly line traversal Treating the recurrence relation as a way to define values in a pair of tables, fill each table bottom-up, that is, starting at the table entry at position 1, rather than filling it by starting at position 𝑛. Let 𝑓1 1 = 𝑒1 + 𝑎1,1 and 𝑓2 1 = 𝑒2 + 𝑎2,1. Have a for-loop indexing 𝑗 from 2 to 𝑛. Inside the for-loop compute 𝑓1[𝑗] and 𝑓2[𝑗]. From the recurrence, each of these relies only values in the table to the left, that is, values that have already been computed.
  • 34. Assembly line traversal 34 Here’s an example with actual values: