SlideShare a Scribd company logo
Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge
Artificial Intelligence
Paradigm
G. Guérard
Department of Nouvelles Energies
Ecole Supérieure d’Ingénieurs Léonard de Vinci
Lecture 2
GG | A.I. 1/34
Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge
OutlineOutlineOutlineOutlineOutlineOutlineOutlineOutlineOutlineOutlineOutlineOutlineOutlineOutlineOutlineOutlineOutline
1 Recursion
2 Divide-and-Conquer
Paradigm
Example
3 Dynamic programming
Paradigm
Characteristics
Example
GG | A.I. 2/34
Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge
OverviewOverviewOverviewOverviewOverviewOverviewOverviewOverviewOverviewOverviewOverviewOverviewOverviewOverviewOverviewOverviewOverview
A recursive function calls itself directly or indirectly.
It is a programming tool, based on a non-intuitive mode of
thinking.
Recursion form the base to another paradigm:
DIVIDE-AND-CONQUER.
GG | A.I. 3/34
Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge
OverviewOverviewOverviewOverviewOverviewOverviewOverviewOverviewOverviewOverviewOverviewOverviewOverviewOverviewOverviewOverviewOverview
ITERATION:
Uses repetition structures (for, while or do...while)
Repetition through explicitly use of repetition structure
Terminates when loop-continuation conditions fail
Controls repetition by using a counter.
RECURSION:
Uses selection structures (if, if...else or switch)
Repetition through repeated method calls
Terminates when base cases are satisfied
Controls repetition by dividing problem into simpler one.
GG | A.I. 4/34
Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge
StackStackStackStackStackStackStackStackStackStackStackStackStackStackStackStackStack
To understand how recursion works, it helps to visualize
what’s going on. To help visualize, we will use a common
concept called the STACK.
Stack
A stack basically operates like a container with priority on inside
objects.
It has only two operations:
PUSH: you can push something onto the stack.
POP: you can pop something off the top of the stack.
GG | A.I. 5/34
Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge
Stacks and MethodsStacks and MethodsStacks and MethodsStacks and MethodsStacks and MethodsStacks and MethodsStacks and MethodsStacks and MethodsStacks and MethodsStacks and MethodsStacks and MethodsStacks and MethodsStacks and MethodsStacks and MethodsStacks and MethodsStacks and MethodsStacks and Methods
When you run a program, the computer creates a stack for you.
Each time you invoke a method, the method is placed on
top of the stack (PUSH).
When the method returns or exits, the method is popped
off the stack (POP).
If a method calls itself recursively, you just push another
copy of the method onto the stack.
GG | A.I. 6/34
Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge
ExampleExampleExampleExampleExampleExampleExampleExampleExampleExampleExampleExampleExampleExampleExampleExampleExample
Consider Void Count(int index) a recursion function: if(index < 2)
Count(index+1)
GG | A.I. 7/34
Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge
Another ExampleAnother ExampleAnother ExampleAnother ExampleAnother ExampleAnother ExampleAnother ExampleAnother ExampleAnother ExampleAnother ExampleAnother ExampleAnother ExampleAnother ExampleAnother ExampleAnother ExampleAnother ExampleAnother Example
Consider Int Factorial(int number) a recursion function: if (
(number == 1) || (number == 0) ) return 1; else return (number *
Factorial (number-1))
GG | A.I. 8/34
Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge
Pro. and Con.Pro. and Con.Pro. and Con.Pro. and Con.Pro. and Con.Pro. and Con.Pro. and Con.Pro. and Con.Pro. and Con.Pro. and Con.Pro. and Con.Pro. and Con.Pro. and Con.Pro. and Con.Pro. and Con.Pro. and Con.Pro. and Con.
1 More overhead than iteration;
2 More memory intensive than iteration;
3 Can also be solved iteratively;
4 Often can be implemented with only a few lines of code.
GG | A.I. 9/34
Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge
IntroductionIntroductionIntroductionIntroductionIntroductionIntroductionIntroductionIntroductionIntroductionIntroductionIntroductionIntroductionIntroductionIntroductionIntroductionIntroductionIntroduction
DIVIDE-AND-CONQUER is not a trick. It is a very useful general
purpose tool for designing efficient algorithms.
It follows those steps:
1 Divide: divide a given problem into subproblems (of
approximately equal size)
2 Conquer: solve each subproblem directly or recursively
3 Combine: and combine the solutions of the subproblems into a
global solution.
GG | A.I. 10/34
Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge
AlgorithmAlgorithmAlgorithmAlgorithmAlgorithmAlgorithmAlgorithmAlgorithmAlgorithmAlgorithmAlgorithmAlgorithmAlgorithmAlgorithmAlgorithmAlgorithmAlgorithm
The general structure of an algorithm designed by using divide
and conquer is:
GG | A.I. 11/34
Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge
MergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesort
The algorithm sorts an array of size N by splitting it into two parts of
almost equal size, recursively sorting each of them, and then merging
the two sorted subarrays back together into a fully sorted list in O(N)
time (comparing in order both array into a single array).
GG | A.I. 12/34
Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge
MergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesort
The algorithm sorts an array of size N by splitting it into two
parts of almost equal size, recursively sorting each of them, and
then merging the two sorted subarrays back together into a
fully sorted list in O(N) time.
The running time of the algorithm satisfies the Master theorem:
∀N > 1, M(N) ≤ 2M(N/2) + O(N)
wich we implies
M(N) = O(N log N).
GG | A.I. 13/34
Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge
MergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesort
Divide
log(n) divisions to split an array of size n into elements.
GG | A.I. 14/34
Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge
MergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesort
Conquer
log(n) iterations, each iterations takes O(n) time, for a total
time O(n log n)
GG | A.I. 15/34
Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge
MergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesort
Combine
Two sorted arrays can be merged in linear time into a sorted
array.
GG | A.I. 16/34
Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge
MergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesort
Example
GG | A.I. 17/34
Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge
OverviewOverviewOverviewOverviewOverviewOverviewOverviewOverviewOverviewOverviewOverviewOverviewOverviewOverviewOverviewOverviewOverview
DYNAMIC PROGRAMMING is a powerfull algorithmic design
technique. Large class of seemingly exponential problems have
a polynomial solution via dynamic programming, particularly
for optimization problems.
The main difference between greedy, D&C and DP programs
are:
Greedy: build up a solution incrementally, optimizing some
local criterion.
Divide-&-conquer: break up a problem into independent
subproblems, solve each one and combine solution.
Dynamic programming: break up a problem into a series of
overlapping subproblems, and build up solutions to larger and
larger subproblems.
GG | A.I. 18/34
Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge
Four steps of Dynamic programmingFour steps of Dynamic programmingFour steps of Dynamic programmingFour steps of Dynamic programmingFour steps of Dynamic programmingFour steps of Dynamic programmingFour steps of Dynamic programmingFour steps of Dynamic programmingFour steps of Dynamic programmingFour steps of Dynamic programmingFour steps of Dynamic programmingFour steps of Dynamic programmingFour steps of Dynamic programmingFour steps of Dynamic programmingFour steps of Dynamic programmingFour steps of Dynamic programmingFour steps of Dynamic programming
1 Define subproblems and characterize the structure of an
optimal solution (OPTIMAL SUBSTRUCTURE).
2 Recursively define the value of an optimal solution
(RECURSIVE FORMULATION).
3 TOP-DOWN: Recurse and memoize; or BOTTOM-UP:
Compute the value of an optimal solution using an
array/table.
4 Construct an OPTIMAL SOLUTION from the computed
information.
GG | A.I. 19/34
Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge
ExampleExampleExampleExampleExampleExampleExampleExampleExampleExampleExampleExampleExampleExampleExampleExampleExample
Fib(n): if n ≤ 2 return 1; else return Fib(n-1)+Fib(n-2);
Running time: M(n) = M(n − 1) + M(n − 2) + O(1) ≥
2M(n − 2) + O(1) ≥ 2n/2.
An exponential running time is bad for this kind of problem.
We could memoize some inner solution to compute the
problem.
GG | A.I. 20/34
Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge
Top-Down Dynamic ProgramTop-Down Dynamic ProgramTop-Down Dynamic ProgramTop-Down Dynamic ProgramTop-Down Dynamic ProgramTop-Down Dynamic ProgramTop-Down Dynamic ProgramTop-Down Dynamic ProgramTop-Down Dynamic ProgramTop-Down Dynamic ProgramTop-Down Dynamic ProgramTop-Down Dynamic ProgramTop-Down Dynamic ProgramTop-Down Dynamic ProgramTop-Down Dynamic ProgramTop-Down Dynamic ProgramTop-Down Dynamic Program
In this program, fib(k) only recurses first time called, for any k.
Thus, there are only n nonmemoized calls. Each memoized
calls are free, in O(1). Top-Down memoize and re-use solutions
to subproblems that help solve problem.
GG | A.I. 21/34
Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge
Bottom-Up Dynamic ProgramBottom-Up Dynamic ProgramBottom-Up Dynamic ProgramBottom-Up Dynamic ProgramBottom-Up Dynamic ProgramBottom-Up Dynamic ProgramBottom-Up Dynamic ProgramBottom-Up Dynamic ProgramBottom-Up Dynamic ProgramBottom-Up Dynamic ProgramBottom-Up Dynamic ProgramBottom-Up Dynamic ProgramBottom-Up Dynamic ProgramBottom-Up Dynamic ProgramBottom-Up Dynamic ProgramBottom-Up Dynamic ProgramBottom-Up Dynamic Program
Bottom-up dynamic program construct the solution from the last
subproblems to the problem itself. We have just to remenber the last
two fibs. Bottom-up dynamic programs follow the same scheme.
GG | A.I. 22/34
Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge
Optimal SubstructureOptimal SubstructureOptimal SubstructureOptimal SubstructureOptimal SubstructureOptimal SubstructureOptimal SubstructureOptimal SubstructureOptimal SubstructureOptimal SubstructureOptimal SubstructureOptimal SubstructureOptimal SubstructureOptimal SubstructureOptimal SubstructureOptimal SubstructureOptimal Substructure
Definition
A problem have OPTIMAL SUBSTRUCTURE when the optimal
solution of a problem contains in itself solutions for subproblems of
the same type.
If a problem presents this characteristic, we say that it respects
the optimality principle.
GG | A.I. 23/34
Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge
Overlapping SubstructureOverlapping SubstructureOverlapping SubstructureOverlapping SubstructureOverlapping SubstructureOverlapping SubstructureOverlapping SubstructureOverlapping SubstructureOverlapping SubstructureOverlapping SubstructureOverlapping SubstructureOverlapping SubstructureOverlapping SubstructureOverlapping SubstructureOverlapping SubstructureOverlapping SubstructureOverlapping Substructure
Definition
A problem is said to have OVERLAPPING SUBPROBLEMS if the
problem can be broken down into subproblems which are reused
several times or a recursive algorithm for the problem solves the same
subproblem over and over rather than always generating new
subproblems (for example by memoization).
See Fibonacci example.
GG | A.I. 24/34
Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge
Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2
1 Characterize the optimal solution of the problem.
1 Understand the problem
2 Verify if a brute force algorithm is enough (optional)
3 Generalize the problem
4 Divide the problem in subproblems of the same type
5 Verify if the problems obeys the optimality principle and
overlapping subproblems.
If the problem presents these two characteristics, we know
that dynamic programming is applicable.
GG | A.I. 25/34
Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge
Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2
2 Recursively define the optimal solution, by using optimal
solutions of subproblems
1 Recursively define the optimal solution value, exactly and
with rigour, from the solutions of subproblems of the same
type
2 Imagine that the values of optimal solutions are already
available when we need them
3 Mathematically define the recursion
GG | A.I. 26/34
Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge
Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2
3 Compute the solutions of all subproblems: top-down
1 Use the recursive function directly obtained from the
definition of the solution and keep a table with the results
already computed
2 When we need to access a value for the first time we need
to compute it, and from then on we just need to see the
already computed result.
GG | A.I. 27/34
Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge
Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2
3 Compute the solutions of all subproblems: bottom-up
1 Find the order in which the subproblems are needed, from
the smaller subproblem until we reach the global problem
and implement, using a table
2 Usually this order is the inverse to the normal order of the
recursive function that solves the problem
GG | A.I. 28/34
Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge
Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2
4 Reconstruct the optimal solution, based on the computed
values
1 Directly from the subproblems table
2 OR New table that stores the decisions in each step
GG | A.I. 29/34
Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge
MatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatches
There are n matches
In each play you can choose to remove 1, 3, or 8 matches
Whoever removes the last stones, wins the game.
GIVEN THE NUMBER OF INITIAL STONES, CAN THE PLAYER
THAT STARTS TO PLAY GUARANTEE A WIN?
GG | A.I. 30/34
Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge
MatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatches
1 Characterize the optimal solution of the problem.
In BRUTE FORCE algorithm, there are 3k possible games.
Let win(i) be a boolean value representing if we can win
when there are i matches:
win(1), win(3),win(8) are true
For the other cases:
if your play goes make the game go to winning position,
then our opponent can force your defeat.
Therefore, our position is a winning position if we can get to
a losing position.
If all possible movements lead to a winning position, then
your position is a losing one.
GG | A.I. 31/34
Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge
MatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatches
2 Recursively define the optimal solution.
win(0) = false
win(i) =
true if (win(i − 1) = false || win(i − 3) = false || win(i − 8) = false
false otherwise
GG | A.I. 32/34
Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge
MatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatches
3 Compute the solutions of all subproblems: bottom-up
For i ← 0 to n do
if((i ≥ 1 && win(i − 1) = false) || (i ≥ 3 &&
win(i − 3) = false) || (i ≥ 8 && win(i − 8) = false))
then win(i) ← true
else win(i) ← false
i 0 1 2 3 4 5 6 7 8 9 10 11 12
win(i) f t f t f t f t t t t f t
GG | A.I. 33/34
Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge
Fundamental knowledgeFundamental knowledgeFundamental knowledgeFundamental knowledgeFundamental knowledgeFundamental knowledgeFundamental knowledgeFundamental knowledgeFundamental knowledgeFundamental knowledgeFundamental knowledgeFundamental knowledgeFundamental knowledgeFundamental knowledgeFundamental knowledgeFundamental knowledgeFundamental knowledge
YOU HAVE TO KNOW BEFORE THE TUTORIAL:
1 Recursion:
1 Principle;
2 Stack: how it works;
3 Divide-and-Conquer paradigm: three steps and general structure;
4 Understand the mergesort algorithm.
2 Dynamic programming:
1 Principle;
2 Dynamic programming paradigm: four steps and bottom-up
recursion (see v2);
3 Optimal substructure;
4 Overlapping subproblems.
GG | A.I. 34/34
Ad

More Related Content

What's hot (20)

Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
Dr Shashikant Athawale
 
Data Structures and Algorithm - Module 1.pptx
Data Structures and Algorithm - Module 1.pptxData Structures and Algorithm - Module 1.pptx
Data Structures and Algorithm - Module 1.pptx
EllenGrace9
 
Building blocks of deep learning
Building blocks of deep learningBuilding blocks of deep learning
Building blocks of deep learning
Keshan Sodimana
 
Algorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IAlgorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms I
Mohamed Loey
 
Data Structure and Algorithm - Divide and Conquer
Data Structure and Algorithm - Divide and ConquerData Structure and Algorithm - Divide and Conquer
Data Structure and Algorithm - Divide and Conquer
Laguna State Polytechnic University
 
Computer architecture pipelining
Computer architecture pipeliningComputer architecture pipelining
Computer architecture pipelining
Mazin Alwaaly
 
Code optimization in compiler design
Code optimization in compiler designCode optimization in compiler design
Code optimization in compiler design
Kuppusamy P
 
Instruction Execution Cycle
Instruction Execution CycleInstruction Execution Cycle
Instruction Execution Cycle
utsav_shah
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
Nikhil Sharma
 
Back face detection
Back face detectionBack face detection
Back face detection
Pooja Dixit
 
8 QUEENS PROBLEM.pptx
8 QUEENS PROBLEM.pptx8 QUEENS PROBLEM.pptx
8 QUEENS PROBLEM.pptx
sunidhi740916
 
code optimization 1...code optimization-1221849738688969-9
code optimization 1...code optimization-1221849738688969-9code optimization 1...code optimization-1221849738688969-9
code optimization 1...code optimization-1221849738688969-9
Sanjeev Raaz
 
Datapath Design of Computer Architecture
Datapath Design of Computer ArchitectureDatapath Design of Computer Architecture
Datapath Design of Computer Architecture
Abu Zaman
 
Mathematical Analysis of Recursive Algorithm.
Mathematical Analysis of Recursive Algorithm.Mathematical Analysis of Recursive Algorithm.
Mathematical Analysis of Recursive Algorithm.
mohanrathod18
 
Operating system 22 threading issues
Operating system 22 threading issuesOperating system 22 threading issues
Operating system 22 threading issues
Vaibhav Khanna
 
Introduction to Dynamic Programming, Principle of Optimality
Introduction to Dynamic Programming, Principle of OptimalityIntroduction to Dynamic Programming, Principle of Optimality
Introduction to Dynamic Programming, Principle of Optimality
Bhavin Darji
 
CPU Scheduling in OS Presentation
CPU Scheduling in OS  PresentationCPU Scheduling in OS  Presentation
CPU Scheduling in OS Presentation
usmankiyani1
 
unit-4-dynamic programming
unit-4-dynamic programmingunit-4-dynamic programming
unit-4-dynamic programming
hodcsencet
 
All pair shortest path
All pair shortest pathAll pair shortest path
All pair shortest path
Arafat Hossan
 
Sorting
SortingSorting
Sorting
Ashim Lamichhane
 
Data Structures and Algorithm - Module 1.pptx
Data Structures and Algorithm - Module 1.pptxData Structures and Algorithm - Module 1.pptx
Data Structures and Algorithm - Module 1.pptx
EllenGrace9
 
Building blocks of deep learning
Building blocks of deep learningBuilding blocks of deep learning
Building blocks of deep learning
Keshan Sodimana
 
Algorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IAlgorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms I
Mohamed Loey
 
Computer architecture pipelining
Computer architecture pipeliningComputer architecture pipelining
Computer architecture pipelining
Mazin Alwaaly
 
Code optimization in compiler design
Code optimization in compiler designCode optimization in compiler design
Code optimization in compiler design
Kuppusamy P
 
Instruction Execution Cycle
Instruction Execution CycleInstruction Execution Cycle
Instruction Execution Cycle
utsav_shah
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
Nikhil Sharma
 
Back face detection
Back face detectionBack face detection
Back face detection
Pooja Dixit
 
8 QUEENS PROBLEM.pptx
8 QUEENS PROBLEM.pptx8 QUEENS PROBLEM.pptx
8 QUEENS PROBLEM.pptx
sunidhi740916
 
code optimization 1...code optimization-1221849738688969-9
code optimization 1...code optimization-1221849738688969-9code optimization 1...code optimization-1221849738688969-9
code optimization 1...code optimization-1221849738688969-9
Sanjeev Raaz
 
Datapath Design of Computer Architecture
Datapath Design of Computer ArchitectureDatapath Design of Computer Architecture
Datapath Design of Computer Architecture
Abu Zaman
 
Mathematical Analysis of Recursive Algorithm.
Mathematical Analysis of Recursive Algorithm.Mathematical Analysis of Recursive Algorithm.
Mathematical Analysis of Recursive Algorithm.
mohanrathod18
 
Operating system 22 threading issues
Operating system 22 threading issuesOperating system 22 threading issues
Operating system 22 threading issues
Vaibhav Khanna
 
Introduction to Dynamic Programming, Principle of Optimality
Introduction to Dynamic Programming, Principle of OptimalityIntroduction to Dynamic Programming, Principle of Optimality
Introduction to Dynamic Programming, Principle of Optimality
Bhavin Darji
 
CPU Scheduling in OS Presentation
CPU Scheduling in OS  PresentationCPU Scheduling in OS  Presentation
CPU Scheduling in OS Presentation
usmankiyani1
 
unit-4-dynamic programming
unit-4-dynamic programmingunit-4-dynamic programming
unit-4-dynamic programming
hodcsencet
 
All pair shortest path
All pair shortest pathAll pair shortest path
All pair shortest path
Arafat Hossan
 

Viewers also liked (20)

14 si(systems analysis and design )
14 si(systems analysis and design )14 si(systems analysis and design )
14 si(systems analysis and design )
Nurdin Al-Azies
 
Presentation
PresentationPresentation
Presentation
SeanTRobinson
 
Mathematical analysis of Graph and Huff amn coding
Mathematical analysis of Graph and Huff amn codingMathematical analysis of Graph and Huff amn coding
Mathematical analysis of Graph and Huff amn coding
Dr Anjan Krishnamurthy
 
Elements of dynamic programming
Elements of dynamic programmingElements of dynamic programming
Elements of dynamic programming
Tafhim Islam
 
Daa:Dynamic Programing
Daa:Dynamic ProgramingDaa:Dynamic Programing
Daa:Dynamic Programing
rupali_2bonde
 
Dynamic programming in Algorithm Analysis
Dynamic programming in Algorithm AnalysisDynamic programming in Algorithm Analysis
Dynamic programming in Algorithm Analysis
Rajendran
 
Dynamic programming - fundamentals review
Dynamic programming - fundamentals reviewDynamic programming - fundamentals review
Dynamic programming - fundamentals review
ElifTech
 
(Binary tree)
(Binary tree)(Binary tree)
(Binary tree)
almario1988
 
Dynamic pgmming
Dynamic pgmmingDynamic pgmming
Dynamic pgmming
Dr. C.V. Suresh Babu
 
latihan 3
latihan 3latihan 3
latihan 3
evanattmajaya2
 
Max Flow Problem
Max Flow ProblemMax Flow Problem
Max Flow Problem
Guillaume Guérard
 
Planned and Unplanned Presentation (1)
Planned and Unplanned Presentation (1)Planned and Unplanned Presentation (1)
Planned and Unplanned Presentation (1)
Dianne Johnson
 
Thariq - T & S
Thariq  - T & SThariq  - T & S
Thariq - T & S
Abubacker Thariq
 
Daniel Resume 2015
Daniel Resume 2015Daniel Resume 2015
Daniel Resume 2015
Daniel Wishart
 
Sachin Resume
Sachin ResumeSachin Resume
Sachin Resume
Sachin Kasana
 
Nicholas Clark - Resume-PC Tech
Nicholas Clark - Resume-PC TechNicholas Clark - Resume-PC Tech
Nicholas Clark - Resume-PC Tech
Nicholas Clark
 
Universidad estácio de sá power
Universidad estácio de sá powerUniversidad estácio de sá power
Universidad estácio de sá power
Melonita
 
Tc4 kinilitan
Tc4 kinilitanTc4 kinilitan
Tc4 kinilitan
Kate13Violeta
 
class_desriptions
class_desriptionsclass_desriptions
class_desriptions
Isaac Long
 
14 si(systems analysis and design )
14 si(systems analysis and design )14 si(systems analysis and design )
14 si(systems analysis and design )
Nurdin Al-Azies
 
Mathematical analysis of Graph and Huff amn coding
Mathematical analysis of Graph and Huff amn codingMathematical analysis of Graph and Huff amn coding
Mathematical analysis of Graph and Huff amn coding
Dr Anjan Krishnamurthy
 
Elements of dynamic programming
Elements of dynamic programmingElements of dynamic programming
Elements of dynamic programming
Tafhim Islam
 
Daa:Dynamic Programing
Daa:Dynamic ProgramingDaa:Dynamic Programing
Daa:Dynamic Programing
rupali_2bonde
 
Dynamic programming in Algorithm Analysis
Dynamic programming in Algorithm AnalysisDynamic programming in Algorithm Analysis
Dynamic programming in Algorithm Analysis
Rajendran
 
Dynamic programming - fundamentals review
Dynamic programming - fundamentals reviewDynamic programming - fundamentals review
Dynamic programming - fundamentals review
ElifTech
 
Planned and Unplanned Presentation (1)
Planned and Unplanned Presentation (1)Planned and Unplanned Presentation (1)
Planned and Unplanned Presentation (1)
Dianne Johnson
 
Nicholas Clark - Resume-PC Tech
Nicholas Clark - Resume-PC TechNicholas Clark - Resume-PC Tech
Nicholas Clark - Resume-PC Tech
Nicholas Clark
 
Universidad estácio de sá power
Universidad estácio de sá powerUniversidad estácio de sá power
Universidad estácio de sá power
Melonita
 
class_desriptions
class_desriptionsclass_desriptions
class_desriptions
Isaac Long
 
Ad

Similar to Divide&Conquer & Dynamic Programming (20)

ALGORITHMS - SHORT NOTES
ALGORITHMS - SHORT NOTESALGORITHMS - SHORT NOTES
ALGORITHMS - SHORT NOTES
suthi
 
Lecture2a algorithm
Lecture2a algorithmLecture2a algorithm
Lecture2a algorithm
mbadhi barnabas
 
lec10.pdf
lec10.pdflec10.pdf
lec10.pdf
ssuserc02bd5
 
Divide and conquer algorithm
Divide and conquer algorithmDivide and conquer algorithm
Divide and conquer algorithm
CHANDAN KUMAR
 
CH-1.1 Introduction (1).pptx
CH-1.1 Introduction (1).pptxCH-1.1 Introduction (1).pptx
CH-1.1 Introduction (1).pptx
satvikkushwaha1
 
Recursion in Java
Recursion in JavaRecursion in Java
Recursion in Java
Fulvio Corno
 
Algorithm review
Algorithm reviewAlgorithm review
Algorithm review
chidabdu
 
Session 4 .pdf
Session 4 .pdfSession 4 .pdf
Session 4 .pdf
ssuser8cda84
 
Design and analysis of algorithms Module-I.pptx
Design and analysis of algorithms Module-I.pptxDesign and analysis of algorithms Module-I.pptx
Design and analysis of algorithms Module-I.pptx
DhanushreeAN1
 
Dynamicpgmming
DynamicpgmmingDynamicpgmming
Dynamicpgmming
Muhammad Wasif
 
Introduction to dynamic programming
Introduction to dynamic programmingIntroduction to dynamic programming
Introduction to dynamic programming
Amisha Narsingani
 
2-Algorithms and Complexit data structurey.pdf
2-Algorithms and Complexit data structurey.pdf2-Algorithms and Complexit data structurey.pdf
2-Algorithms and Complexit data structurey.pdf
ishan743441
 
esign and Analysis of Algorithms Presentation.pptx
esign and Analysis of Algorithms Presentation.pptxesign and Analysis of Algorithms Presentation.pptx
esign and Analysis of Algorithms Presentation.pptx
Niraj759370
 
Dynamic programming prasintation eaisy
Dynamic programming prasintation eaisyDynamic programming prasintation eaisy
Dynamic programming prasintation eaisy
ahmed51236
 
Design & Analysis Of Algorithm
Design & Analysis Of AlgorithmDesign & Analysis Of Algorithm
Design & Analysis Of Algorithm
Computer Hardware & Trouble shooting
 
nebnznsnshsjsjsjsjsjssjsjsjsjsjsjsjdjw 2.pptx
nebnznsnshsjsjsjsjsjssjsjsjsjsjsjsjdjw 2.pptxnebnznsnshsjsjsjsjsjssjsjsjsjsjsjsjdjw 2.pptx
nebnznsnshsjsjsjsjsjssjsjsjsjsjsjsjdjw 2.pptx
Akash1d
 
Randomized Algorithm
Randomized Algorithm Randomized Algorithm
Randomized Algorithm
Sanjeev Kumar Jaiswal
 
Quantitativetechniqueformanagerialdecisionlinearprogramming 090725035417-phpa...
Quantitativetechniqueformanagerialdecisionlinearprogramming 090725035417-phpa...Quantitativetechniqueformanagerialdecisionlinearprogramming 090725035417-phpa...
Quantitativetechniqueformanagerialdecisionlinearprogramming 090725035417-phpa...
kongara
 
RCIM 2008 - Modello Scheduling
RCIM 2008 - Modello SchedulingRCIM 2008 - Modello Scheduling
RCIM 2008 - Modello Scheduling
Marco Santambrogio
 
Design & Analysis of Algorithm course .pptx
Design & Analysis of Algorithm course .pptxDesign & Analysis of Algorithm course .pptx
Design & Analysis of Algorithm course .pptx
JeevaMCSEKIOT
 
ALGORITHMS - SHORT NOTES
ALGORITHMS - SHORT NOTESALGORITHMS - SHORT NOTES
ALGORITHMS - SHORT NOTES
suthi
 
Divide and conquer algorithm
Divide and conquer algorithmDivide and conquer algorithm
Divide and conquer algorithm
CHANDAN KUMAR
 
CH-1.1 Introduction (1).pptx
CH-1.1 Introduction (1).pptxCH-1.1 Introduction (1).pptx
CH-1.1 Introduction (1).pptx
satvikkushwaha1
 
Algorithm review
Algorithm reviewAlgorithm review
Algorithm review
chidabdu
 
Design and analysis of algorithms Module-I.pptx
Design and analysis of algorithms Module-I.pptxDesign and analysis of algorithms Module-I.pptx
Design and analysis of algorithms Module-I.pptx
DhanushreeAN1
 
Introduction to dynamic programming
Introduction to dynamic programmingIntroduction to dynamic programming
Introduction to dynamic programming
Amisha Narsingani
 
2-Algorithms and Complexit data structurey.pdf
2-Algorithms and Complexit data structurey.pdf2-Algorithms and Complexit data structurey.pdf
2-Algorithms and Complexit data structurey.pdf
ishan743441
 
esign and Analysis of Algorithms Presentation.pptx
esign and Analysis of Algorithms Presentation.pptxesign and Analysis of Algorithms Presentation.pptx
esign and Analysis of Algorithms Presentation.pptx
Niraj759370
 
Dynamic programming prasintation eaisy
Dynamic programming prasintation eaisyDynamic programming prasintation eaisy
Dynamic programming prasintation eaisy
ahmed51236
 
nebnznsnshsjsjsjsjsjssjsjsjsjsjsjsjdjw 2.pptx
nebnznsnshsjsjsjsjsjssjsjsjsjsjsjsjdjw 2.pptxnebnznsnshsjsjsjsjsjssjsjsjsjsjsjsjdjw 2.pptx
nebnznsnshsjsjsjsjsjssjsjsjsjsjsjsjdjw 2.pptx
Akash1d
 
Quantitativetechniqueformanagerialdecisionlinearprogramming 090725035417-phpa...
Quantitativetechniqueformanagerialdecisionlinearprogramming 090725035417-phpa...Quantitativetechniqueformanagerialdecisionlinearprogramming 090725035417-phpa...
Quantitativetechniqueformanagerialdecisionlinearprogramming 090725035417-phpa...
kongara
 
RCIM 2008 - Modello Scheduling
RCIM 2008 - Modello SchedulingRCIM 2008 - Modello Scheduling
RCIM 2008 - Modello Scheduling
Marco Santambrogio
 
Design & Analysis of Algorithm course .pptx
Design & Analysis of Algorithm course .pptxDesign & Analysis of Algorithm course .pptx
Design & Analysis of Algorithm course .pptx
JeevaMCSEKIOT
 
Ad

Recently uploaded (20)

Evolution of Man || Palaentology || Geology
Evolution of Man || Palaentology || GeologyEvolution of Man || Palaentology || Geology
Evolution of Man || Palaentology || Geology
Abhiroop Singh
 
ICAI OpenGov Lab: A Quick Introduction | AI for Open Government
ICAI OpenGov Lab: A Quick Introduction | AI for Open GovernmentICAI OpenGov Lab: A Quick Introduction | AI for Open Government
ICAI OpenGov Lab: A Quick Introduction | AI for Open Government
David Graus
 
A tale of two Lucies: talk at the maths dept, Free University of Amsterdam
A tale of two Lucies: talk at the maths dept, Free University of AmsterdamA tale of two Lucies: talk at the maths dept, Free University of Amsterdam
A tale of two Lucies: talk at the maths dept, Free University of Amsterdam
Richard Gill
 
Share CONTROL OF BODY MOVEMENT[1]PHYSIO PRESENTATION.pptx
Share CONTROL OF BODY MOVEMENT[1]PHYSIO PRESENTATION.pptxShare CONTROL OF BODY MOVEMENT[1]PHYSIO PRESENTATION.pptx
Share CONTROL OF BODY MOVEMENT[1]PHYSIO PRESENTATION.pptx
CHUsman65
 
Year 7 unit Earth and Space slides .pptx
Year 7 unit Earth and Space slides .pptxYear 7 unit Earth and Space slides .pptx
Year 7 unit Earth and Space slides .pptx
boushrasabbagh
 
SEXUAL REPRODUCTION IN FLOWERING PLANTS.pptx
SEXUAL REPRODUCTION IN FLOWERING PLANTS.pptxSEXUAL REPRODUCTION IN FLOWERING PLANTS.pptx
SEXUAL REPRODUCTION IN FLOWERING PLANTS.pptx
dhruti94
 
Secondary metabolite ,Plants and Health Care
Secondary metabolite ,Plants and Health CareSecondary metabolite ,Plants and Health Care
Secondary metabolite ,Plants and Health Care
Nistarini College, Purulia (W.B) India
 
Subject name: Introduction to psychology
Subject name: Introduction to psychologySubject name: Introduction to psychology
Subject name: Introduction to psychology
beebussy155
 
Introduction to Black Hole and how its formed
Introduction to Black Hole and how its formedIntroduction to Black Hole and how its formed
Introduction to Black Hole and how its formed
MSafiullahALawi
 
Keynote presentation at DeepTest Workshop 2025
Keynote presentation at DeepTest Workshop 2025Keynote presentation at DeepTest Workshop 2025
Keynote presentation at DeepTest Workshop 2025
Shiva Nejati
 
Nested PCR Microteaching ppt by Nitin.pptx
Nested PCR Microteaching ppt by Nitin.pptxNested PCR Microteaching ppt by Nitin.pptx
Nested PCR Microteaching ppt by Nitin.pptx
NitinchaudharY351367
 
Pharmacologically active constituents.pdf
Pharmacologically active constituents.pdfPharmacologically active constituents.pdf
Pharmacologically active constituents.pdf
Nistarini College, Purulia (W.B) India
 
Sb- and Sn-based materials in Na-ion batteries
Sb- and Sn-based materials in Na-ion batteriesSb- and Sn-based materials in Na-ion batteries
Sb- and Sn-based materials in Na-ion batteries
MichouRatisou
 
Animal Models for Biological and Clinical Research ppt 2.pptx
Animal Models for Biological and Clinical Research ppt 2.pptxAnimal Models for Biological and Clinical Research ppt 2.pptx
Animal Models for Biological and Clinical Research ppt 2.pptx
MahitaLaveti
 
Examining Visual Attention in Gaze-Driven VR Learning: An Eye-Tracking Study ...
Examining Visual Attention in Gaze-Driven VR Learning: An Eye-Tracking Study ...Examining Visual Attention in Gaze-Driven VR Learning: An Eye-Tracking Study ...
Examining Visual Attention in Gaze-Driven VR Learning: An Eye-Tracking Study ...
Yasasi Abeysinghe
 
Seminary Administration - Class Roll - Report.pdf
Seminary Administration - Class Roll - Report.pdfSeminary Administration - Class Roll - Report.pdf
Seminary Administration - Class Roll - Report.pdf
CludioGomesdaSilva
 
Nutritional Management of Heat Stress in broiler and layers
Nutritional Management of Heat Stress in broiler and layersNutritional Management of Heat Stress in broiler and layers
Nutritional Management of Heat Stress in broiler and layers
Bihar Veterinary College, Bihar Animal Sciences University, Patna, Bihar, India
 
Presantation investigation new drug new.pdf
Presantation investigation new drug new.pdfPresantation investigation new drug new.pdf
Presantation investigation new drug new.pdf
091HarshikaModi
 
Hemorrhagic Fever from Venezuala Medical Virology.pptx
Hemorrhagic Fever from Venezuala Medical Virology.pptxHemorrhagic Fever from Venezuala Medical Virology.pptx
Hemorrhagic Fever from Venezuala Medical Virology.pptx
wamunsmith
 
Data Structures Stack and Queue Data Structures
Data Structures Stack and Queue Data StructuresData Structures Stack and Queue Data Structures
Data Structures Stack and Queue Data Structures
poongothai11
 
Evolution of Man || Palaentology || Geology
Evolution of Man || Palaentology || GeologyEvolution of Man || Palaentology || Geology
Evolution of Man || Palaentology || Geology
Abhiroop Singh
 
ICAI OpenGov Lab: A Quick Introduction | AI for Open Government
ICAI OpenGov Lab: A Quick Introduction | AI for Open GovernmentICAI OpenGov Lab: A Quick Introduction | AI for Open Government
ICAI OpenGov Lab: A Quick Introduction | AI for Open Government
David Graus
 
A tale of two Lucies: talk at the maths dept, Free University of Amsterdam
A tale of two Lucies: talk at the maths dept, Free University of AmsterdamA tale of two Lucies: talk at the maths dept, Free University of Amsterdam
A tale of two Lucies: talk at the maths dept, Free University of Amsterdam
Richard Gill
 
Share CONTROL OF BODY MOVEMENT[1]PHYSIO PRESENTATION.pptx
Share CONTROL OF BODY MOVEMENT[1]PHYSIO PRESENTATION.pptxShare CONTROL OF BODY MOVEMENT[1]PHYSIO PRESENTATION.pptx
Share CONTROL OF BODY MOVEMENT[1]PHYSIO PRESENTATION.pptx
CHUsman65
 
Year 7 unit Earth and Space slides .pptx
Year 7 unit Earth and Space slides .pptxYear 7 unit Earth and Space slides .pptx
Year 7 unit Earth and Space slides .pptx
boushrasabbagh
 
SEXUAL REPRODUCTION IN FLOWERING PLANTS.pptx
SEXUAL REPRODUCTION IN FLOWERING PLANTS.pptxSEXUAL REPRODUCTION IN FLOWERING PLANTS.pptx
SEXUAL REPRODUCTION IN FLOWERING PLANTS.pptx
dhruti94
 
Subject name: Introduction to psychology
Subject name: Introduction to psychologySubject name: Introduction to psychology
Subject name: Introduction to psychology
beebussy155
 
Introduction to Black Hole and how its formed
Introduction to Black Hole and how its formedIntroduction to Black Hole and how its formed
Introduction to Black Hole and how its formed
MSafiullahALawi
 
Keynote presentation at DeepTest Workshop 2025
Keynote presentation at DeepTest Workshop 2025Keynote presentation at DeepTest Workshop 2025
Keynote presentation at DeepTest Workshop 2025
Shiva Nejati
 
Nested PCR Microteaching ppt by Nitin.pptx
Nested PCR Microteaching ppt by Nitin.pptxNested PCR Microteaching ppt by Nitin.pptx
Nested PCR Microteaching ppt by Nitin.pptx
NitinchaudharY351367
 
Sb- and Sn-based materials in Na-ion batteries
Sb- and Sn-based materials in Na-ion batteriesSb- and Sn-based materials in Na-ion batteries
Sb- and Sn-based materials in Na-ion batteries
MichouRatisou
 
Animal Models for Biological and Clinical Research ppt 2.pptx
Animal Models for Biological and Clinical Research ppt 2.pptxAnimal Models for Biological and Clinical Research ppt 2.pptx
Animal Models for Biological and Clinical Research ppt 2.pptx
MahitaLaveti
 
Examining Visual Attention in Gaze-Driven VR Learning: An Eye-Tracking Study ...
Examining Visual Attention in Gaze-Driven VR Learning: An Eye-Tracking Study ...Examining Visual Attention in Gaze-Driven VR Learning: An Eye-Tracking Study ...
Examining Visual Attention in Gaze-Driven VR Learning: An Eye-Tracking Study ...
Yasasi Abeysinghe
 
Seminary Administration - Class Roll - Report.pdf
Seminary Administration - Class Roll - Report.pdfSeminary Administration - Class Roll - Report.pdf
Seminary Administration - Class Roll - Report.pdf
CludioGomesdaSilva
 
Presantation investigation new drug new.pdf
Presantation investigation new drug new.pdfPresantation investigation new drug new.pdf
Presantation investigation new drug new.pdf
091HarshikaModi
 
Hemorrhagic Fever from Venezuala Medical Virology.pptx
Hemorrhagic Fever from Venezuala Medical Virology.pptxHemorrhagic Fever from Venezuala Medical Virology.pptx
Hemorrhagic Fever from Venezuala Medical Virology.pptx
wamunsmith
 
Data Structures Stack and Queue Data Structures
Data Structures Stack and Queue Data StructuresData Structures Stack and Queue Data Structures
Data Structures Stack and Queue Data Structures
poongothai11
 

Divide&Conquer & Dynamic Programming

  • 1. Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge Artificial Intelligence Paradigm G. Guérard Department of Nouvelles Energies Ecole Supérieure d’Ingénieurs Léonard de Vinci Lecture 2 GG | A.I. 1/34
  • 2. Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge OutlineOutlineOutlineOutlineOutlineOutlineOutlineOutlineOutlineOutlineOutlineOutlineOutlineOutlineOutlineOutlineOutline 1 Recursion 2 Divide-and-Conquer Paradigm Example 3 Dynamic programming Paradigm Characteristics Example GG | A.I. 2/34
  • 3. Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge OverviewOverviewOverviewOverviewOverviewOverviewOverviewOverviewOverviewOverviewOverviewOverviewOverviewOverviewOverviewOverviewOverview A recursive function calls itself directly or indirectly. It is a programming tool, based on a non-intuitive mode of thinking. Recursion form the base to another paradigm: DIVIDE-AND-CONQUER. GG | A.I. 3/34
  • 4. Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge OverviewOverviewOverviewOverviewOverviewOverviewOverviewOverviewOverviewOverviewOverviewOverviewOverviewOverviewOverviewOverviewOverview ITERATION: Uses repetition structures (for, while or do...while) Repetition through explicitly use of repetition structure Terminates when loop-continuation conditions fail Controls repetition by using a counter. RECURSION: Uses selection structures (if, if...else or switch) Repetition through repeated method calls Terminates when base cases are satisfied Controls repetition by dividing problem into simpler one. GG | A.I. 4/34
  • 5. Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge StackStackStackStackStackStackStackStackStackStackStackStackStackStackStackStackStack To understand how recursion works, it helps to visualize what’s going on. To help visualize, we will use a common concept called the STACK. Stack A stack basically operates like a container with priority on inside objects. It has only two operations: PUSH: you can push something onto the stack. POP: you can pop something off the top of the stack. GG | A.I. 5/34
  • 6. Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge Stacks and MethodsStacks and MethodsStacks and MethodsStacks and MethodsStacks and MethodsStacks and MethodsStacks and MethodsStacks and MethodsStacks and MethodsStacks and MethodsStacks and MethodsStacks and MethodsStacks and MethodsStacks and MethodsStacks and MethodsStacks and MethodsStacks and Methods When you run a program, the computer creates a stack for you. Each time you invoke a method, the method is placed on top of the stack (PUSH). When the method returns or exits, the method is popped off the stack (POP). If a method calls itself recursively, you just push another copy of the method onto the stack. GG | A.I. 6/34
  • 7. Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge ExampleExampleExampleExampleExampleExampleExampleExampleExampleExampleExampleExampleExampleExampleExampleExampleExample Consider Void Count(int index) a recursion function: if(index < 2) Count(index+1) GG | A.I. 7/34
  • 8. Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge Another ExampleAnother ExampleAnother ExampleAnother ExampleAnother ExampleAnother ExampleAnother ExampleAnother ExampleAnother ExampleAnother ExampleAnother ExampleAnother ExampleAnother ExampleAnother ExampleAnother ExampleAnother ExampleAnother Example Consider Int Factorial(int number) a recursion function: if ( (number == 1) || (number == 0) ) return 1; else return (number * Factorial (number-1)) GG | A.I. 8/34
  • 9. Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge Pro. and Con.Pro. and Con.Pro. and Con.Pro. and Con.Pro. and Con.Pro. and Con.Pro. and Con.Pro. and Con.Pro. and Con.Pro. and Con.Pro. and Con.Pro. and Con.Pro. and Con.Pro. and Con.Pro. and Con.Pro. and Con.Pro. and Con. 1 More overhead than iteration; 2 More memory intensive than iteration; 3 Can also be solved iteratively; 4 Often can be implemented with only a few lines of code. GG | A.I. 9/34
  • 10. Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge IntroductionIntroductionIntroductionIntroductionIntroductionIntroductionIntroductionIntroductionIntroductionIntroductionIntroductionIntroductionIntroductionIntroductionIntroductionIntroductionIntroduction DIVIDE-AND-CONQUER is not a trick. It is a very useful general purpose tool for designing efficient algorithms. It follows those steps: 1 Divide: divide a given problem into subproblems (of approximately equal size) 2 Conquer: solve each subproblem directly or recursively 3 Combine: and combine the solutions of the subproblems into a global solution. GG | A.I. 10/34
  • 11. Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge AlgorithmAlgorithmAlgorithmAlgorithmAlgorithmAlgorithmAlgorithmAlgorithmAlgorithmAlgorithmAlgorithmAlgorithmAlgorithmAlgorithmAlgorithmAlgorithmAlgorithm The general structure of an algorithm designed by using divide and conquer is: GG | A.I. 11/34
  • 12. Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge MergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesort The algorithm sorts an array of size N by splitting it into two parts of almost equal size, recursively sorting each of them, and then merging the two sorted subarrays back together into a fully sorted list in O(N) time (comparing in order both array into a single array). GG | A.I. 12/34
  • 13. Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge MergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesort The algorithm sorts an array of size N by splitting it into two parts of almost equal size, recursively sorting each of them, and then merging the two sorted subarrays back together into a fully sorted list in O(N) time. The running time of the algorithm satisfies the Master theorem: ∀N > 1, M(N) ≤ 2M(N/2) + O(N) wich we implies M(N) = O(N log N). GG | A.I. 13/34
  • 14. Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge MergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesort Divide log(n) divisions to split an array of size n into elements. GG | A.I. 14/34
  • 15. Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge MergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesort Conquer log(n) iterations, each iterations takes O(n) time, for a total time O(n log n) GG | A.I. 15/34
  • 16. Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge MergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesort Combine Two sorted arrays can be merged in linear time into a sorted array. GG | A.I. 16/34
  • 17. Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge MergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesortMergesort Example GG | A.I. 17/34
  • 18. Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge OverviewOverviewOverviewOverviewOverviewOverviewOverviewOverviewOverviewOverviewOverviewOverviewOverviewOverviewOverviewOverviewOverview DYNAMIC PROGRAMMING is a powerfull algorithmic design technique. Large class of seemingly exponential problems have a polynomial solution via dynamic programming, particularly for optimization problems. The main difference between greedy, D&C and DP programs are: Greedy: build up a solution incrementally, optimizing some local criterion. Divide-&-conquer: break up a problem into independent subproblems, solve each one and combine solution. Dynamic programming: break up a problem into a series of overlapping subproblems, and build up solutions to larger and larger subproblems. GG | A.I. 18/34
  • 19. Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge Four steps of Dynamic programmingFour steps of Dynamic programmingFour steps of Dynamic programmingFour steps of Dynamic programmingFour steps of Dynamic programmingFour steps of Dynamic programmingFour steps of Dynamic programmingFour steps of Dynamic programmingFour steps of Dynamic programmingFour steps of Dynamic programmingFour steps of Dynamic programmingFour steps of Dynamic programmingFour steps of Dynamic programmingFour steps of Dynamic programmingFour steps of Dynamic programmingFour steps of Dynamic programmingFour steps of Dynamic programming 1 Define subproblems and characterize the structure of an optimal solution (OPTIMAL SUBSTRUCTURE). 2 Recursively define the value of an optimal solution (RECURSIVE FORMULATION). 3 TOP-DOWN: Recurse and memoize; or BOTTOM-UP: Compute the value of an optimal solution using an array/table. 4 Construct an OPTIMAL SOLUTION from the computed information. GG | A.I. 19/34
  • 20. Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge ExampleExampleExampleExampleExampleExampleExampleExampleExampleExampleExampleExampleExampleExampleExampleExampleExample Fib(n): if n ≤ 2 return 1; else return Fib(n-1)+Fib(n-2); Running time: M(n) = M(n − 1) + M(n − 2) + O(1) ≥ 2M(n − 2) + O(1) ≥ 2n/2. An exponential running time is bad for this kind of problem. We could memoize some inner solution to compute the problem. GG | A.I. 20/34
  • 21. Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge Top-Down Dynamic ProgramTop-Down Dynamic ProgramTop-Down Dynamic ProgramTop-Down Dynamic ProgramTop-Down Dynamic ProgramTop-Down Dynamic ProgramTop-Down Dynamic ProgramTop-Down Dynamic ProgramTop-Down Dynamic ProgramTop-Down Dynamic ProgramTop-Down Dynamic ProgramTop-Down Dynamic ProgramTop-Down Dynamic ProgramTop-Down Dynamic ProgramTop-Down Dynamic ProgramTop-Down Dynamic ProgramTop-Down Dynamic Program In this program, fib(k) only recurses first time called, for any k. Thus, there are only n nonmemoized calls. Each memoized calls are free, in O(1). Top-Down memoize and re-use solutions to subproblems that help solve problem. GG | A.I. 21/34
  • 22. Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge Bottom-Up Dynamic ProgramBottom-Up Dynamic ProgramBottom-Up Dynamic ProgramBottom-Up Dynamic ProgramBottom-Up Dynamic ProgramBottom-Up Dynamic ProgramBottom-Up Dynamic ProgramBottom-Up Dynamic ProgramBottom-Up Dynamic ProgramBottom-Up Dynamic ProgramBottom-Up Dynamic ProgramBottom-Up Dynamic ProgramBottom-Up Dynamic ProgramBottom-Up Dynamic ProgramBottom-Up Dynamic ProgramBottom-Up Dynamic ProgramBottom-Up Dynamic Program Bottom-up dynamic program construct the solution from the last subproblems to the problem itself. We have just to remenber the last two fibs. Bottom-up dynamic programs follow the same scheme. GG | A.I. 22/34
  • 23. Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge Optimal SubstructureOptimal SubstructureOptimal SubstructureOptimal SubstructureOptimal SubstructureOptimal SubstructureOptimal SubstructureOptimal SubstructureOptimal SubstructureOptimal SubstructureOptimal SubstructureOptimal SubstructureOptimal SubstructureOptimal SubstructureOptimal SubstructureOptimal SubstructureOptimal Substructure Definition A problem have OPTIMAL SUBSTRUCTURE when the optimal solution of a problem contains in itself solutions for subproblems of the same type. If a problem presents this characteristic, we say that it respects the optimality principle. GG | A.I. 23/34
  • 24. Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge Overlapping SubstructureOverlapping SubstructureOverlapping SubstructureOverlapping SubstructureOverlapping SubstructureOverlapping SubstructureOverlapping SubstructureOverlapping SubstructureOverlapping SubstructureOverlapping SubstructureOverlapping SubstructureOverlapping SubstructureOverlapping SubstructureOverlapping SubstructureOverlapping SubstructureOverlapping SubstructureOverlapping Substructure Definition A problem is said to have OVERLAPPING SUBPROBLEMS if the problem can be broken down into subproblems which are reused several times or a recursive algorithm for the problem solves the same subproblem over and over rather than always generating new subproblems (for example by memoization). See Fibonacci example. GG | A.I. 24/34
  • 25. Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2 1 Characterize the optimal solution of the problem. 1 Understand the problem 2 Verify if a brute force algorithm is enough (optional) 3 Generalize the problem 4 Divide the problem in subproblems of the same type 5 Verify if the problems obeys the optimality principle and overlapping subproblems. If the problem presents these two characteristics, we know that dynamic programming is applicable. GG | A.I. 25/34
  • 26. Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2 2 Recursively define the optimal solution, by using optimal solutions of subproblems 1 Recursively define the optimal solution value, exactly and with rigour, from the solutions of subproblems of the same type 2 Imagine that the values of optimal solutions are already available when we need them 3 Mathematically define the recursion GG | A.I. 26/34
  • 27. Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2 3 Compute the solutions of all subproblems: top-down 1 Use the recursive function directly obtained from the definition of the solution and keep a table with the results already computed 2 When we need to access a value for the first time we need to compute it, and from then on we just need to see the already computed result. GG | A.I. 27/34
  • 28. Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2 3 Compute the solutions of all subproblems: bottom-up 1 Find the order in which the subproblems are needed, from the smaller subproblem until we reach the global problem and implement, using a table 2 Usually this order is the inverse to the normal order of the recursive function that solves the problem GG | A.I. 28/34
  • 29. Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2Four steps of Dynamic programming v2 4 Reconstruct the optimal solution, based on the computed values 1 Directly from the subproblems table 2 OR New table that stores the decisions in each step GG | A.I. 29/34
  • 30. Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge MatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatches There are n matches In each play you can choose to remove 1, 3, or 8 matches Whoever removes the last stones, wins the game. GIVEN THE NUMBER OF INITIAL STONES, CAN THE PLAYER THAT STARTS TO PLAY GUARANTEE A WIN? GG | A.I. 30/34
  • 31. Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge MatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatches 1 Characterize the optimal solution of the problem. In BRUTE FORCE algorithm, there are 3k possible games. Let win(i) be a boolean value representing if we can win when there are i matches: win(1), win(3),win(8) are true For the other cases: if your play goes make the game go to winning position, then our opponent can force your defeat. Therefore, our position is a winning position if we can get to a losing position. If all possible movements lead to a winning position, then your position is a losing one. GG | A.I. 31/34
  • 32. Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge MatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatches 2 Recursively define the optimal solution. win(0) = false win(i) = true if (win(i − 1) = false || win(i − 3) = false || win(i − 8) = false false otherwise GG | A.I. 32/34
  • 33. Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge MatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatchesMatches 3 Compute the solutions of all subproblems: bottom-up For i ← 0 to n do if((i ≥ 1 && win(i − 1) = false) || (i ≥ 3 && win(i − 3) = false) || (i ≥ 8 && win(i − 8) = false)) then win(i) ← true else win(i) ← false i 0 1 2 3 4 5 6 7 8 9 10 11 12 win(i) f t f t f t f t t t t f t GG | A.I. 33/34
  • 34. Recursion Divide-and-Conquer Dynamic programming Fundamental Knowledge Fundamental knowledgeFundamental knowledgeFundamental knowledgeFundamental knowledgeFundamental knowledgeFundamental knowledgeFundamental knowledgeFundamental knowledgeFundamental knowledgeFundamental knowledgeFundamental knowledgeFundamental knowledgeFundamental knowledgeFundamental knowledgeFundamental knowledgeFundamental knowledgeFundamental knowledge YOU HAVE TO KNOW BEFORE THE TUTORIAL: 1 Recursion: 1 Principle; 2 Stack: how it works; 3 Divide-and-Conquer paradigm: three steps and general structure; 4 Understand the mergesort algorithm. 2 Dynamic programming: 1 Principle; 2 Dynamic programming paradigm: four steps and bottom-up recursion (see v2); 3 Optimal substructure; 4 Overlapping subproblems. GG | A.I. 34/34