introduzione al corso - polito.it...recursion 9 tecniche di programmazione a.a. 2018/2019 divide et...
TRANSCRIPT
![Page 1: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/1.jpg)
Recursion
Tecniche di Programmazione – A.A. 2018/2019
![Page 2: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/2.jpg)
Summary
1. Definition and divide-and-conquer strategies
2. Recursion: design tips
3. Simple recursive algorithms
1. Fibonacci numbers
2. Dicothomic search
3. X-Expansion
4. Anagrams
5. Knapsack
4. Recursive vs Iterative strategies
5. More complex examples of recursive algorithms
1. Knight’s Tour
2. Proposed exercises
A.A. 2018/2019Tecniche di programmazione2
![Page 3: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/3.jpg)
Definition and divide-and-conquer
strategies
Recursion
![Page 4: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/4.jpg)
Why recursion?
A.A. 2018/2019Tecniche di programmazione4
Divide et impera
Systematic exploration/enumeration
Handling recursive data structures
![Page 5: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/5.jpg)
Definition
A method (or a procedure or a function) is defined as
recursive when:
Inside its definition, we have a call to the same method
(procedure, function)
Or, inside its definition, there is a call to another method that,
directly or indirectly, calls the method itself
An algorithm is said to be recursive when it is based on
recursive methods (procedures, functions)
A.A. 2018/2019Tecniche di programmazione5
![Page 6: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/6.jpg)
A.A. 2018/2019Tecniche di programmazione6
![Page 7: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/7.jpg)
Example: Factorial
public long recursiveFactorial(long N) {
long result = 1 ;
if ( N == 0 )
return 1 ;
else {
result = recursiveFactorial(N-1) ;
result = N * result ;
return result ;
}
}
0!≝1
∀N≥1:N!≝N× N−1 !
A.A. 2018/2019Tecniche di programmazione7
![Page 8: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/8.jpg)
Motivation
Many problems lend themselves, naturally, to a recursive
description:
We define a method to solve sub-problems similar to the initial
one, but smaller
We define a method to combine the partial solutions into the
overall solution of the original problem
Divide et impera
Gaius Julius Caesar
A.A. 2018/2019Tecniche di programmazione8
![Page 9: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/9.jpg)
Recursion
A.A. 2018/2019Tecniche di programmazione9
Divide et Impera
Split a problem P into {Qi} where Qi are still complex, yet simpler instances of the same problem.
Solve {Qi }, then merge the solutions
Merge & split must be “simple”
A.k.a., Divide ’n Conquer
Exploration
Systematic procedure to enumerate all possible solutions
Solutions (built stepwise)
Paths
Permutations
Combinations
Divide et Impera, by “dividing” the possible solutions
![Page 10: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/10.jpg)
Divide et Impera – Divide and Conquer
Solution = Solve ( Problem ) ;
Solve ( Problem ) {
List<SubProblem> subProblems = Divide ( Problem ) ;
For ( each subP[i] in subProblems ) {
SubSolution[i] = Solve ( subP[i] ) ;
}
Solution = Combine ( SubSolution[ 1..N ] ) ;
return Solution ;
}
A.A. 2018/2019Tecniche di programmazione10
![Page 11: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/11.jpg)
Divide et Impera – Divide and Conquer
Solution = Solve ( Problem ) ;
Solve ( Problem ) {
List<SubProblem> subProblems = Divide ( Problem ) ;
For ( each subP[i] in subProblems ) {
SubSolution[i] = Solve ( subP[i] ) ;
}
Solution = Combine ( SubSolution[ 1..N ] ) ;
return Solution ;
}
“a” sub-problems, each
“b” times smaller than
the initial problem
recursive call
A.A. 2018/2019Tecniche di programmazione11
![Page 12: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/12.jpg)
How to stop recursion?
Recursion must not be infinite
Any algorithm must always terminate!
After a sufficient nesting level, sub-problems become so
small (and so easy) to be solved:
Trivially (ex: sets of just one element)
Or, with methods different from recursion
A.A. 2018/2019Tecniche di programmazione12
![Page 13: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/13.jpg)
Warnings
Always remember the “termination condition”
Ensure that all sub-problems are strictly “smaller” than
the initial problem
A.A. 2018/2019Tecniche di programmazione13
![Page 14: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/14.jpg)
Divide et Impera – Divide and Conquer
Solve ( Problem ) {
if( problem is trivial )
Solution = Solve_trivial ( Problem ) ;
else {
List<SubProblem> subProblems = Divide ( Problem ) ;
For ( each subP[i] in subProblems ) {
SubSolution[i] = Solve ( subP[i] ) ;
}
Solution = Combine ( SubSolution[ 1..N ] ) ;
}
return Solution ;
}
do recursion
A.A. 2018/2019Tecniche di programmazione14
![Page 15: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/15.jpg)
What about complexity?
A.A. 2018/2019Tecniche di programmazione15
a = number of sub-problems for a problem
b = how smaller sub-problems are than the original one
n = size of the original problem
T(n) = complexity of Solve
…our unknown complexity function
Θ(1) = complexity of Solve_trivial
…otherwise it wouldn’t be trivial
D(n) = complexity of Divide
C(n) = complexity of Combine
![Page 16: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/16.jpg)
Divide et Impera – Divide and Conquer
Solve ( Problem ) {
if( problem is trivial )
Solution = Solve_trivial ( Problem ) ;
else {
List<SubProblem> subProblems = Divide ( Problem ) ;
For ( each subP[i] in subProblems ) {
SubSolution[i] = Solve ( subP[i] ) ;
}
Solution = Combine ( SubSolution[ 1..a ] ) ;
}
return Solution ;
}
T(n)
Θ(1)
D(n)
C(n)
T(n/b)
a times
A.A. 2018/2019Tecniche di programmazione16
![Page 17: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/17.jpg)
Complexity computation
T(n) =
(1) for n c
D(n) + a T(n/b) + C(n) for n > c
Recurrence Equation not easy to solve in the general case
Special case:
If D(n)+C(n)=(n)
We obtain T(n) = (n log n).
A.A. 2018/2019Tecniche di programmazione17
![Page 18: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/18.jpg)
Exploration
Explore ( S ) {
List<Step> steps = PossibleSteps ( Problem, S ) ;
for ( each p in steps ) {
S.Do ( p )
Explore ( S ) ;
S.Undo ( p ) ;
}
}
A.A. 2018/2019Tecniche di programmazione18
![Page 19: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/19.jpg)
Exploration
Explore ( S ) {
List<Step> steps = PossibleSteps ( Problem, S ) ;
for ( each p in steps ) {
S.Do ( p )
Explore ( S ) ;
S.Undo ( p ) ;
}
}
A.A. 2018/2019Tecniche di programmazione19
Backtrack!
The “status” of the
problem
Local variable
“Try” the step
Recursion
![Page 20: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/20.jpg)
Design tips
Recursion
![Page 21: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/21.jpg)
Goal
1. Analysis of a problem to be solved with recursive
techniques
2. Identification of the main design choices
3. Identification of the main implementation strategies
A.A. 2018/2019Tecniche di programmazione21
![Page 22: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/22.jpg)
Analizzare il problema
A.A. 2018/2019Tecniche di programmazione22
Come imposto in generale la ricorsione?
Che cosa mi rappresenta il "livello"?
Com'è fatta una soluzione parziale?
Com'è fatta una soluzione totale?
![Page 23: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/23.jpg)
Generale le possibili soluzioni
A.A. 2018/2019Tecniche di programmazione23
Qual è la regola per generare tutte le soluzioni del
livello+1 a partire da una soluzione parziale del livello
corrente?
Come faccio a riconoscere se una soluzione parziale è
anche completa? (terminazione con successo)
Come viene avviata la ricorsione (livello 0)?
![Page 24: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/24.jpg)
Identificare le soluzioni valide
A.A. 2018/2019Tecniche di programmazione24
Data una soluzione parziale, come faccio a
sapere se è valida (e quindi continuare)?
sapere se non è valida (e quindi terminare la ricorsione)?
nb. magari non posso…
Data una soluzione completa, come faccio a
sapere se è valida?
sapere se non è valida?
Cosa devo fare con le soluzioni complete valide?
Fermarmi alla prima?
Generarle e memorizzarle tutte?
Contarle?
![Page 25: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/25.jpg)
Progettare le strutture dati
A.A. 2018/2019Tecniche di programmazione25
Qual è la struttura dati per memorizzare una soluzione
(parziale o completa)?
Qual è la struttura dati per memorizzare lo stato della
ricerca (della ricorsione)?
![Page 26: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/26.jpg)
Scheletro del codice
A.A. 2018/2019Tecniche di programmazione26
![Page 27: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/27.jpg)
Riempire lo scheletro (del codice)
A.A. 2018/2019Tecniche di programmazione27
Blocco Frammento di codice
A
B
C
D
E
![Page 28: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/28.jpg)
Recursion myths
A.A. 2018/2019Tecniche di programmazione28
Recursive algorithms are O(n log n)
Recursive algorithms are better than non-recursive ones
Recursive algorithms can be coded quickly
![Page 29: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/29.jpg)
Simple recursive algorithms
Recursion
![Page 30: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/30.jpg)
Schedina Totocalcio
A.A. 2018/2019Tecniche di programmazione30
![Page 31: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/31.jpg)
Classi
A.A. 2018/2019Tecniche di programmazione31
![Page 32: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/32.jpg)
Fibonacci Numbers
Problem:
Compute the N-th Fibonacci Number
Definition:
FIBN+1 = FIBN + FIBN-1 for N>0
FIB1 = 1
FIB0 = 0
A.A. 2018/2019Tecniche di programmazione32
![Page 33: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/33.jpg)
Recursive solution
public long recursiveFibonacci(long N) {if(N==0)
return 0 ;if(N==1)
return 1 ;
long left = recursiveFibonacci(N-1) ;long right = recursiveFibonacci(N-2) ;
return left + right ;}
Fib(0) = 0Fib(1) = 1Fib(2) = 1Fib(3) = 2Fib(4) = 3Fib(5) = 5
A.A. 2018/2019Tecniche di programmazione33
![Page 34: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/34.jpg)
Analysis
FIB(5)
FIB(4)FIB(3)
A.A. 2018/2019Tecniche di programmazione34
![Page 35: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/35.jpg)
Analysis
FIB(5)
FIB(4)FIB(3)
FIB(1) FIB(2)
FIB(0) FIB(1)
A.A. 2018/2019Tecniche di programmazione35
![Page 36: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/36.jpg)
Analysis
FIB(5)
FIB(4)FIB(3)
FIB(1) FIB(2)
FIB(0) FIB(1)
FIB(3) FIB(2)
FIB(1) FIB(2)
FIB(0) FIB(1) FIB(0)
FIB(1)
A.A. 2018/2019Tecniche di programmazione36
![Page 37: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/37.jpg)
Analysis
FIB(5)
FIB(4)FIB(3)
FIB(1) FIB(2)
FIB(0) FIB(1)
FIB(3) FIB(2)
FIB(1) FIB(2)
FIB(0) FIB(1) FIB(0)
FIB(1)
A.A. 2018/2019Tecniche di programmazione37
Complexity?
![Page 38: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/38.jpg)
Example: dichotomic search
Problem
Determine whether an element x is present inside an ordered
vector v[N]
Approach
Divide the vector in two halves
Compare the middle element with x
Reapply the problem over one of the two halves (left or right,
depending on the comparison result)
The other half may be ignored, since the vector is ordered
A.A. 2018/2019Tecniche di programmazione38
![Page 39: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/39.jpg)
Example
1 3 4 6 8 9 11 12v 4x
A.A. 2018/2019Tecniche di programmazione39
![Page 40: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/40.jpg)
Example
1 3 4 6 8 9 11 12v 4x
y
y<xyx
A.A. 2018/2019Tecniche di programmazione40
![Page 41: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/41.jpg)
Example
1 3 4 6 8 9 11 12v 4x
1 3 4 6 8 9 11 12
1 3 4 6
4 6
y
y<xyx
A.A. 2018/2019Tecniche di programmazione41
![Page 42: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/42.jpg)
Solution
public int find(int[] v, int a, int b, int x)
{
if(b-a == 0) { // trivial case
if(v[a]==x) return a ; // found
else return –1 ; // not found
}
int c = (a+b) / 2 ; // splitting point
if(v[c] >= x)
return find(v, a, c, x) ;
else return find(v, c+1, b, x) ;
}
A.A. 2018/2019Tecniche di programmazione42
![Page 43: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/43.jpg)
Solution
public int find(int[] v, int a, int b, int x)
{
if(b-a == 0) { // trivial case
if(v[a]==x) return a ; // found
else return –1 ; // not found
}
int c = (a+b) / 2 ; // splitting point
if(v[c] >= x)
return find(v, a, c, x) ;
else return find(v, c+1, b, x) ;
}
Beware of integer-arithmetic
approximations!
A.A. 2018/2019Tecniche di programmazione43
![Page 44: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/44.jpg)
Quick reference
A.A. 2018/2019Tecniche di programmazione44
![Page 45: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/45.jpg)
Exercise: Value X
When working with Boolean functions, we often use the
symbol X, meaning that a given variable may have
indifferently the value 0 or 1.
Example: in the OR function, the result is 1 when the
inputs are 01, 10 or 11. More compactly, if the inputs are
X1 or 1X.
A.A. 2018/2019Tecniche di programmazione45
![Page 46: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/46.jpg)
X-Expansion
We want to devise an algorithm that, given a binary string
that includes characters 0, 1 and X, will compute all the
possible combinations implied by the given string.
Example: given the string 01X0X, algorithm must compute
the following combinations
01000
01001
01100
01101
A.A. 2018/2019Tecniche di programmazione46
![Page 47: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/47.jpg)
Solution
We may devise a recursive algorithm that explores the
complete ‘tree’ of possible compatible combinations:
Transforming each X into a 0, and then into a 1
For each transformation, we recursively seek other X in the
string
The number of final combinations (leaves of the tree) is
equal to 2N, if N is the number of X.
The tree height is N+1.
A.A. 2018/2019Tecniche di programmazione47
![Page 48: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/48.jpg)
Combinations tree
01X0X
0100X 0110X
01000 01001 01100 01101
A.A. 2018/2019Tecniche di programmazione48
![Page 49: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/49.jpg)
Exercise: Anagram
Given a word, find all possible anagrams of that word
Find all permutations of the elements in a set
Permutations are N!
E.g.: «Dog» → dog, dgo, god, gdo, odg, ogd
A.A. 2018/2019Tecniche di programmazione49
![Page 50: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/50.jpg)
Anagrams: recursion tree
A.A. 2018/2019Tecniche di programmazione50
“”
D,O,G
Letters yet to be
considered
Part of word already
constructed
![Page 51: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/51.jpg)
Anagrams: recursion tree
A.A. 2018/2019Tecniche di programmazione51
“”
D,O,G
“D”
O,G
“O”
D,G
“G”
D,O
Letters yet to be
considered
Part of word already
constructed
![Page 52: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/52.jpg)
Anagrams: recursion tree
A.A. 2018/2019Tecniche di programmazione52
“”
D,O,G
“D”
O,G
“O”
D,G
“G”
D,O
“DO”
G
“DG”
O
“DOG” “DGO”
Letters yet to be
considered
Part of word already
constructed
![Page 53: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/53.jpg)
Anagrams: recursion tree
A.A. 2018/2019Tecniche di programmazione53
“”
D,O,G
“D”
O,G
“O”
D,G
“G”
D,O
“DO”
G
“DG”
O
“DOG” “DGO”
“OD”
G
“OG”
D
“ODG” “OGD”
“GD”
O
“GO”
D
“GDO” “GOD”
Letters yet to be
considered
Part of word already
constructed
![Page 54: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/54.jpg)
Anagrams: problem variants
A.A. 2018/2019Tecniche di programmazione54
Generate only anagrams that are “valid” words
At the end of recursion, check the dictionary
During recursion, check whether the current prefix exists in
the dictionary
Handle words with multiple letters: avoid duplicate
anagrams
E.g., “seas” → seas and seas are the same word
Generate all and, at the end or recursion, check if repeated
Constrain, during recursion, duplicate letters to always appear
in the same order (e.g, s alwaws before s)
http://wordsmith.org/anagram/index.html
![Page 55: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/55.jpg)
The Knapsack Problem
A.A. 2018/2019Tecniche di programmazione55
![Page 56: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/56.jpg)
The Knapsack Problem
A.A. 2018/2019Tecniche di programmazione56
Input: Weight of N items {w1, w2, ..., wn}Cost of N items {c1, c2, ..., cn}Knapsack limit S
Output: Selection for knapsack: {x1,x2,…xn}where xi {0,1}.
Sample input:
wi={1,1,2,4,12}
ci ={1,2,2,10,4}
S=15
![Page 57: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/57.jpg)
The Knapsack Problem
A.A. 2018/2019Tecniche di programmazione57
Max 15
0
5
0
12
12
2
2 3 0
5 2 0710 39
5 01131029
2
33
5 55 5
7777
13 2 14 3 011
11 11 11
![Page 58: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/58.jpg)
A.A. 2018/2019Tecniche di programmazione58
8 2 5 5 6 7 3 9
1 2 4 1 9 2 3 1
2 2 5 2 42 7 9 7
8 2 5 6 6 6 3 9
1 2 4 1 9 2 3 1
2 7 1 1 4 7 8 9
2 3 5 3 1 8 9 9
8 2 3 1 6 7 3 9
![Page 59: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/59.jpg)
A.A. 2018/2019Tecniche di programmazione59
4 2 5 5 3 7 3 9
1 2 4 1 9 2 3 1
2 2 5 2 42 7 1 3
8 2 5 6 1 1 1 9
1 2 4 1 9 2 3 1
2 7 1 1 4 7 8 2
2 3 5 3 1 8 9 9
8 2 3 1 6 7 3 9
![Page 60: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/60.jpg)
Exercise: Binomial Coefficient
Compute the Binomial Coefficient (n m) exploiting the
recurrence relations (derived from Tartaglia’s triangle):
=
=
−+
−
−=
nmn
n
n
n
m
n
m
n
m
n
0,0
10
1
1
1
A.A. 2018/2019Tecniche di programmazione60
![Page 61: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/61.jpg)
Exercise: Determinant
Compute the determinant of a square matrix
Remind that:
det( M 1x1 ) = m1,1
det( M NxN ) = sum of the products of all elements of a row
(or column), times the determinants of the (N-1)x(N-1) sub-
matrices obtained by deleting the row and column containing
the multiplying element, with alternating signs (-1)(i+j).
Laplace’s Formula, at
http://en.wikipedia.org/wiki/Determinant
A.A. 2018/2019Tecniche di programmazione61
![Page 62: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/62.jpg)
Recursive vs Iterative strategies
Recursion
![Page 63: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/63.jpg)
Recursion and iteration
Every recursive program can always be implemented in
an iterative manner
The best solution, in terms of efficiency and code clarity,
depends on the problem
A.A. 2018/2019Tecniche di programmazione63
![Page 64: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/64.jpg)
Example: Factorial (iterative)
public long iterativeFactorial(long N) {
long result = 1 ;
for (long i=2; i<=N; i++)
result = result * i ;
return result ;
}
0!≝1
∀N≥1:N!≝N× N−1 !
A.A. 2018/2019Tecniche di programmazione64
![Page 65: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/65.jpg)
Fibonacci (iterative)
public long iterativeFibonacci(long N) {if(N==0) return 0 ;if(N==1) return 1 ;
// now we know that N >= 2long i = 2 ;long fib1 = 1 ; // fib(N-1)long fib2 = 0 ; // fib(N-1)
while( i<=N ) {long fib = fib1 + fib2 ;fib2 = fib1 ;fib1 = fib ;i++ ;
}
return fib1 ;}
A.A. 2018/2019Tecniche di programmazione65
![Page 66: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/66.jpg)
Dichotomic search (iterative)
A.A. 2018/2019Tecniche di programmazione66
public int findIterative(int[] v, int x) {int a = 0 ;int b = v.length-1 ;
while( a != b ) {int c = (a + b) / 2; // middle pointif (v[c] >= x) {// v[c] is too large -> search leftb = c ;
} else {// v[c] is too small -> search righta = c+1 ;
}}if (v[a] == x)return a;
elsereturn -1;
}
![Page 67: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/67.jpg)
Exercises
Create an iterative version for the computation of the
binomial coefficient (n m).
Analyze a possible iterative version for computing the
determinant of a matrix. What are the difficulties?
Can you find a simple iterative solution for the X-
Expansion problem? And for the Anagram problem?
A.A. 2018/2019Tecniche di programmazione67
![Page 68: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/68.jpg)
More complex examples of recursive
algorithms
Recursion
![Page 69: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/69.jpg)
Knight’s tour
Consider a NxN chessboard, with the Knight moving
according to Chess rules
The Knight may move in 8 different cells
We want to find a sequence of moves for the Knight
where
All cells in the chessboard
are visited
Each cell is touched exactly once
The starting point is arbitrary
A.A. 2018/2019Tecniche di programmazione69
![Page 70: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/70.jpg)
Analysis
Assume N=4
1
A.A. 2018/2019Tecniche di programmazione70
![Page 71: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/71.jpg)
Move 1
1
2,0,0
Level of the next move
to try
Coordinates of the last
move
A.A. 2018/2019Tecniche di programmazione71
![Page 72: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/72.jpg)
Move 2
2
1
2,0,0
3,2,1
A.A. 2018/2019Tecniche di programmazione72
![Page 73: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/73.jpg)
Move 3
3
2
1
2,0,0
3,2,1
4,3,3
A.A. 2018/2019Tecniche di programmazione73
![Page 74: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/74.jpg)
Move 38
6 3
5 2 9
10 4 7
1 8 11
13,3,0
2,0,0
3,2,1
4,3,3
5,1,2
6,2,0
7,3,2
8,1,3
9,0,1
10,2,2
11,0,3
12,1,1
13,2,3
14,3,1
15,1,0
16,0,2
14,3,1
15,1,0
16,3,1
11,1,0
12,3,1
13,2,3
14,0,2 14,1,1
15,0,3 15,3,0
12,0,2
A.A. 2018/2019Tecniche di programmazione108
![Page 75: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/75.jpg)
Complexity
A.A. 2018/2019Tecniche di programmazione109
The number of possible moves, at each step, is at most 8.
The number of steps is N2.
The solution tree has a number of nodes
8N^2.
In the worst case
The solution is in the right-most leave of the solution tree
The tree is complete
The number of recursive calls, in the worst case, is
therefore (8N^2).
![Page 76: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/76.jpg)
Implementation
A.A. 2018/2019Tecniche di programmazione110
![Page 77: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/77.jpg)
The N Queens
Consider a NxN chessboard, and N Queens that may act
according to the chess rules
Find a position for the N queens, such that no Queen is
able to attack any other Queen
A.A. 2018/2019Tecniche di programmazione111
?
?
![Page 78: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/78.jpg)
Domino game
A.A. 2018/2019Tecniche di programmazione112
Consider the game of Domino, composed of two-sided
pieces: each side is labeled with a number from 0 to 6. All
combinations of number pairs are represented exactly
once.
Find the longest possible sequence of pieces, such that
consecutive pieces have the same value on the adjacent
sides.
![Page 79: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/79.jpg)
Resources
A.A. 2018/2019Tecniche di programmazione113
Algorithms in a Nutshell, By George T. Heineman, Gary
Pollice, Stanley Selkow, O'Reilly Media
![Page 80: Introduzione al corso - polito.it...Recursion 9 Tecniche di programmazione A.A. 2018/2019 Divide et Impera Split a problem P into {Qi} where Qi are still complex, yet simpler instances](https://reader031.vdocuments.site/reader031/viewer/2022022809/5e4de720441e0804612f3393/html5/thumbnails/80.jpg)
Licenza d’uso
A.A. 2018/2019Tecniche di programmazione114
Queste diapositive sono distribuite con licenza Creative Commons“Attribuzione - Non commerciale - Condividi allo stesso modo (CC BY-NC-SA)”
Sei libero: di riprodurre, distribuire, comunicare al pubblico, esporre in pubblico,
rappresentare, eseguire e recitare quest'opera
di modificare quest'opera
Alle seguenti condizioni: Attribuzione — Devi attribuire la paternità dell'opera agli autori
originali e in modo tale da non suggerire che essi avallino te o il modo in cui tu usi l'opera.
Non commerciale — Non puoi usare quest'opera per fini commerciali.
Condividi allo stesso modo — Se alteri o trasformi quest'opera, o se la usi per crearne un'altra, puoi distribuire l'opera risultante solo con una licenza identica o equivalente a questa.
http://creativecommons.org/licenses/by-nc-sa/3.0/