Algorithmes et Arbres, 2`eme Semestre 2012/2013 PARTIEL

Transcription

Algorithmes et Arbres, 2`eme Semestre 2012/2013 PARTIEL
Algorithmes et Arbres, 2ème Semestre 2012/2013
PARTIEL ALGORITHMES ET ARBRES (CONCEPTION ALGORITHMIQUE)
Durée : 2h, tous documents et calculatrices autorisés, téléphones interdits.
Les réponses peuvent être données en français ou en anglais.
Le barème est donné à titre indicatif.
Exercice 1. (Théorème maı̂tre), 2 points
Dans les 4 récurrences suivants, indiquer s’il est possible d’utiliser le théorème maı̂tre, et donner
l’asymptotique dans les cas traitables.
1. u(n) = 2u(⌊n/2⌋) + u(⌈n/2⌉) + n
2. u(n) = 2u(⌊n/2⌋) + u(⌈n/3⌉) + n
3. u(n) = u(⌊n/2⌋) + ln(n)
4. u(n) = u(⌊n/2⌋) + u(⌈n/2⌉)
Exercice 2. (Récurrence) 2 points
On cherche à retrouver le pseudo-code du programme mystère qui affiche à l’écran les résultats
suivant :
mystère(1) = 1
mystère(2) = 2 1 1
mystère(3) = 3 2 1 1 2 1 1
mystère(4) = 4 3 2 1 1 2 1 1 3 2 1 1 2 1 1
mystère(5) = 5 4 3 2 1 1 2 1 1 3 2 1 1 2 1 1 4 3 2 1 1 2 1 1 3 2 1 1 2 1 1
1. Écrire un algorithme récursif qui pour un entier n affiche à l’écran mystère(n).
2. Prouver par récurrence que mystère(n) affiche 2n − 1 nombre à l’écran.
Remarque : On considère que les nombres à plusieurs chiffres comme 10 ou 666 ne comptent que
comme un seul nombre.
Exercice 3. (Invariant de boucle) 2 points
Voici un pseudo-code permettant de calculer le n-ième terme de la suite récurrente :
f (n) = f (n − 1) + f (n − 2) + f (n − 3) et f (0) = f (1) = f (2) = 1.
Algorithme 1 : CalculF
Entrées : un entier n
Sorties : la valeur de f (n)
1 v0 = 1;
2 v1 = 1;
3 v2 = 1;
4 pour k allant de 3 à n faire
5
temp = v2 ;
6
v2 = v0 + v1 + v2 ;
7
v0 = v1 ;
8
v1 = temp;
9
Renvoyer v2
1. Donner un invariant de boucle permettant de montrer que le pseudo-code CalculF (n) renvoie
f (n).
2. Donner (sans preuve) la complexité de l’algorithme en nombre d’additions.
1
Algorithmes et Arbres
2
English Version
Duration : 2h, paper documents and calculators allowed, telephones forbidden. Answers can be
given in French or English.
Points repartition is given as an indication.
Exercise 1. (Master theorem), 2 points
In the 4 following recurrences, specify if it is possible to use the master theorem and give the asymptotic when possible.
1. u(n) = 2u(⌊n/2⌋) + u(⌈n/2⌉) + n
2. u(n) = 2u(⌊n/2⌋) + u(⌈n/3⌉) + n
3. u(n) = u(⌊n/2⌋) + ln(n)
4. u(n) = u(⌊n/2⌋) + u(⌈n/2⌉)
Exercise 2. (Recurrence) 2 points
We want to find the pseudo-code of the program mystère that displayed the following results :
mystère(1) = 1
mystère(2) = 2 1 1
mystère(3) = 3 2 1 1 2 1 1
mystère(4) = 4 3 2 1 1 2 1 1 3 2 1 1 2 1 1
mystère(5) = 5 4 3 2 1 1 2 1 1 3 2 1 1 2 1 1 4 3 2 1 1 2 1 1 3 2 1 1 2 1 1
1. Write an recursive algorithm which takes a integer n and displays mystère(n).
2. Prove by induction that mystère(n) displays 2n − 1 numbers.
Remark : We consider that several digits numbers like 10 or 666 count as a single number.
Exercise 3. (Loop Invariant) 2 points
Here is a pseudo-code which computes the n-th term of the recursive sequence :
f (n) = f (n − 1) + f (n − 2) + f (n − 3) et f (0) = f (1) = f (2) = 1.
Algorithme 2 : CalculF
Entrées : an integer n
Sorties : the value of f (n)
1 v0 = 1;
2 v1 = 1;
3 v2 = 1;
4 pour k from 3 to n faire
5
temp = v2 ;
6
v2 = v0 + v1 + v2 ;
7
v0 = v1 ;
8
v1 = temp;
9
Return v2
1. Give a loop invariant enabling to show that the pseudo-code CalculF (n) returns f (n).
2. Give (without proof) the complexity of the algorithm in number of additions.
L2 Année 2012/2013
3
Exercice 4. Des sous (Diviser pour régner) 6 points
On dispose d’un ensemble P de n pièces indistinguables p1 , ..., pn de poids respectif π1 , ..., πn et
d’une balance à plateaux. On cherche
à déterminer quelle est la pièce la plus lourde. Les poids
P
vérifient la propriété suivante :
πk < πn .
k<n
1. Montrer que pn est la pièce la plus lourde.
2. Montrer que pour tout partition de P en deux ensembles P1 et P2 , en mettant P1 et P2 sur les
deux plateaux, la balance penche toujours du coté où se trouve la pièce la plus lourde.
3. En déduire une algorithme de type diviser pour régner permettant de trouver la pièce pn .
4. Soit C(n) le nombre de pesées que nécessite cet algorithme pour trouver pn , donner une récurrence
pour C(n).
5. Donner une formule explicite pour C(2n ).
Exercice 5. Existence d’un nombre à valeur bornée (Diviser pour régner) 5 points
On cherche à écrire un algorithme qui prend en entrée un tableau trié T et deux nombres a et b,
avec a < b, qui renvoie Vrai s’il existe un nombre c dans le tableau tel que a < c < b et Faux sinon.
1. Donner un algorithme naı̈f qui effectue 2n comparaisons pour un tableau de taille n dans le pire
des cas.
On veut trouver un algorithme de type diviser pour régner plus efficace. Dans la suite, la taille du
tableau T est notée n.
2. Si T [⌊ n2 ⌋] ≤ a, dans quelle partie du tableau c peut-il se trouver s’il existe ?
3. Même question avec T [⌊ n2 ⌋] ≥ b.
4. Y a-t-il d’autres cas possibles ? Si oui, comment les traiter ?
5. Donner un algorithme du type diviser pour régner en vous aidant des questions précédentes.
Quelle est sa complexité ?
Exercice 6. (Récurrence plus méchante) 4 points
Soit la suite (an )n∈N , dont les premiers termes sont :
a0 = 0
a1 = 1
a2 = 0 3
a3 = 1 0 3
a4 = 4 0 3 4
a5 = 1 0 3 3
a6 = 4 0 3 4 1 0 3 3
a7 = 7 1 0 3 3 7
a8 = 4 0 3 4 1 0 3 3 3
a9 = 7 1 0 3 3 7 4 0 3 4 1 0 3 3 3
a10 = 10 4 0 3 4 1 0 3 3 3 10
a11 = 7 1 0 3 3 7 4 0 3 4 1 0 3 3 3 3
a12 = 10 4 0 3 4 1 0 3 3 3 10 7 1 0 3 3 7 4 0 3 4 1 0 3 3 3 3
a13 = 13 7 1 0 3 3 7 4 0 3 4 1 0 3 3 3 3 13
1. Donner les formules de récurrences pour a3n ,a3n+1 et a3n+2 .
2. Combien de fois le nombre 3 apparaı̂t-il dans a3n ?
Remarque : On considère que les nombres à plusieurs chiffres comme 13 ou 338 ne contiennent pas
le nombre 3. Par exemple, le nombre 3 apparaı̂t 7 fois dans a13 .
4
Algorithmes et Arbres
Exercise 4. Coins (Divide and conquer) 6 points
We have a set P of n indistinguishable coins p1 , ..., pn of respective weights π1 , ..., πn and a balance
scale.
P We seek to determine which is the heaviest coin. Weights satisfy the following property :
πk < πn .
k<n
1. Prove that pn is the heaviest coin.
2. Prove that for every partition of P is two sets P1 and P2 , with P1 on one plate and P2 on the
other, the scale always bends to the side where the heaviest coin is. 3. Derive an algorithm type
divide-and-conquer enabling to find the coin pn .
4. Let C(n) be the number of weightings required by this algorithm to find pn , give a recurrence for
C(n).
5. Give an explicit formula for C(2n ).
Exercise 5. Existence of a number of bounded value (Divide and conquer) 5 points
We try to write an algorithm that takes as input a sorted array T and two numbers a and b with
a < b, which returns True if there is a number c in the table such as a < c < b and False otherwise.
1. Give a naive algorithm that performs 2n comparisons for an array of size n in the worst case.
On want to find a more efficient algorithm type divide-and-conquer. In the following, the size of the
array T is denoted n.
2. If T [⌊ n2 ⌋] ≤ a, in which part of the array c can it be if it exists ?
3. Same question with T [⌊ n2 ⌋] ≥ b.
4. Is there any other possible case ? If yes, how can we treat them ?
5. Give an algorithm type divide-and-conquer with the help of preceding questions. What is its
complexity ?
Exercise 6. (Challenging Recurrence) 4 points
Let (an )n∈N be the sequence whose first terms are :
a0 = 0
a1 = 1
a2 = 0 3
a3 = 1 0 3
a4 = 4 0 3 4
a5 = 1 0 3 3
a6 = 4 0 3 4 1 0 3 3
a7 = 7 1 0 3 3 7
a8 = 4 0 3 4 1 0 3 3 3
a9 = 7 1 0 3 3 7 4 0 3 4 1 0 3 3 3
a10 = 10 4 0 3 4 1 0 3 3 3 10
a11 = 7 1 0 3 3 7 4 0 3 4 1 0 3 3 3 3
a12 = 10 4 0 3 4 1 0 3 3 3 10 7 1 0 3 3 7 4 0 3 4 1 0 3 3 3 3
a13 = 13 7 1 0 3 3 7 4 0 3 4 1 0 3 3 3 3 13
1. Give recurrence formulæfor a3n ,a3n+1 and a3n+2 .
2. How many time the number 3 appears in a3n ?
Remark : We consider that several digits numbers like 13 or 338 do not include the number 3. In
particular, the number 3 appears 7 time in a13 .