19.04.2014 Views

Algorithmes numériques et de Sherwood - UQAC

Algorithmes numériques et de Sherwood - UQAC

Algorithmes numériques et de Sherwood - UQAC

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

8INF430 <strong>Algorithmes</strong> probabilistes 1<br />

✬<br />

✩<br />

Générateur pseudo aléatoire<br />

On suppose l’existence d’un générateur <strong>de</strong> nombres aléatoires dont<br />

l’utilisation se fait à coût unitaire.<br />

Définition: Soit a < b , <strong>de</strong>ux nombres réels. La fonction<br />

uniforme( a,b) r<strong>et</strong>ourne une valeur x choisie <strong>de</strong> façon aléatoire <strong>et</strong><br />

uniforme dans l’intervalle [a, b)<br />

Définition: Si i <strong>et</strong> j sont <strong>de</strong>ux entiers, alors la fonction<br />

uniforme( a,b) r<strong>et</strong>ourne la valeur entière a ≤ v ≤ b avec probabilité<br />

1/(b − a + 1)<br />

Définition: Si S est un ensemble fini non vi<strong>de</strong>, alors uniforme(S)<br />

r<strong>et</strong>ourne la valeur v ∈ S avec probabilité 1/|S|<br />

✫<br />


8INF430 <strong>Algorithmes</strong> probabilistes 2<br />

✬<br />

Dans les années 50: Certains ordinateurs possè<strong>de</strong>nt <strong>de</strong>s dispositifs<br />

apparemment aléatoires:<br />

• compteur <strong>de</strong> particules cosmiques<br />

• bit le moins significatif <strong>de</strong> l’horloge<br />

Impopulaire car il <strong>de</strong>vient impossible <strong>de</strong> répéter l’exécution d’un<br />

calcul:<br />

• Programme plus difficile à déboguer<br />

• Comparaison <strong>de</strong> programmes plus difficile<br />

Pour certaines applications le vrai hasard est important:<br />

• loteries<br />

• cryptographie<br />

✩<br />

En pratique, on utilise <strong>de</strong>s générateur <strong>de</strong> nombres pseudo-aléatoires.<br />

✫<br />


8INF430 <strong>Algorithmes</strong> probabilistes 3<br />

✬<br />

Définition: Une séquence <strong>de</strong> nombres est dite pseudo-aléatoire si<br />

elle est générée <strong>de</strong> façon déterministe mais semble avoir été<br />

produite <strong>de</strong> façon purement aléatoire (passe avec succès certains<br />

tests statistiques).<br />

Exemple: [Métho<strong>de</strong> linéaire congruentielle]<br />

Choisir minutieusement 4 nombres:<br />

1. m: le modulo ( m > 0)<br />

2. a: le multiplicateur ( 0 ≤ a < m)<br />

3. c: le saut ( 0 ≤ c < m)<br />

4. X 0 : la valeur <strong>de</strong> départ ( 0 ≤ X 0 < m)<br />

✩<br />

La séquence <strong>de</strong> nombre pseudo-aléatoire est:<br />

✫<br />

X n+1 = (aX n + c) mod m<br />


8INF430 <strong>Algorithmes</strong> probabilistes 4<br />

✬<br />

✩<br />

Implémentation <strong>de</strong> la fonction rand()<br />

Borland C++: X n+1 = (22695477 ∗ X n + 1) mod 2 32<br />

GNU gcc: X n+1 = (69069 ∗ X n + 5) mod 2 32<br />

Visual C++: X n+1 = (134775813 ∗ X n + 1) mod 2 32<br />

Apple CarbonLib: X n+1 = (16807 ∗ X n ) mod 2 31 − 1<br />

Autres métho<strong>de</strong>s: Mersenne Twister, Blum Blum Shub, <strong>et</strong>c.<br />

✫<br />


8INF430 <strong>Algorithmes</strong> probabilistes 5<br />

✬<br />

✩<br />

Fait: La caractéristique fondamentale d’un algorithme probabiliste<br />

est qu’il peut se comporter différemment lorsqu’appelé <strong>de</strong>ux fois<br />

avec les mêmes paramètres.<br />

Définition: Le temps d’exécution espéré d’un algorithme<br />

probabiliste est le temps moyen <strong>de</strong> l’algorithme sur une entrée<br />

donnée.<br />

Remarque: Ne pas confondre temps espéré <strong>et</strong> temps moyen.<br />

Exemple: Quicksort prend un temps O(n 2 ) en pire cas <strong>et</strong><br />

O(n lg n) en moyenne.<br />

Si au début <strong>de</strong> l’algorithme, on permute aléatoirement les<br />

éléments du tableau. Cela peut se faire en temps O(n)<br />

Quelque soit l’entrée initiale, le temps espéré est O(n + n lg n).<br />

✫<br />


8INF430 <strong>Algorithmes</strong> probabilistes 6<br />

✬<br />

Classification <strong>de</strong>s algorithmes probabilistes<br />

✩<br />

<strong>Algorithmes</strong> numériques:<br />

• Utilisé pour approximer la solution à <strong>de</strong>s problèmes numériques<br />

(ex. calculer π, intégration numérique, <strong>et</strong>c.).<br />

• La précision augmente avec le temps disponible.<br />

<strong>Algorithmes</strong> <strong>de</strong> <strong>Sherwood</strong>:<br />

• Utilisé lorsqu’un algorithme déterministe fonctionne plus<br />

rapi<strong>de</strong>ment en moyenne qu’en pire cas.<br />

• Ces algorimes peuvent éliminer la différence entre bonnes <strong>et</strong><br />

mauvaises entrées.<br />

• Exemple: quicksort<br />

✫<br />


8INF430 <strong>Algorithmes</strong> probabilistes 7<br />

✬<br />

✩<br />

<strong>Algorithmes</strong> <strong>de</strong> Las Vegas:<br />

• Ces algorithmes peuvent quelque fois r<strong>et</strong>ourner un message<br />

disant qu’ils n’ont pas pu trouver la réponse.<br />

• La probabilité d’un echec peut être rendu arbitrairement p<strong>et</strong>ite<br />

en répétant l’algorithme suffisamment souvent.<br />

<strong>Algorithmes</strong> <strong>de</strong> Monte Carlo:<br />

• Ces algorithmes r<strong>et</strong>ournent toujours une réponse mais celle-ci<br />

n’est pas toujours juste.<br />

• La probabilité d’obtenir une réponse correcte augmente avec le<br />

temps disponible.<br />

✫<br />


8INF430 <strong>Algorithmes</strong> probabilistes 8<br />

✬<br />

✩<br />

<strong>Algorithmes</strong> numériques<br />

• Calcul <strong>de</strong> π<br />

• Calcul d’une intégrale définie<br />

• Comptage probabiliste<br />

✫<br />


8INF430 <strong>Algorithmes</strong> probabilistes 9<br />

✬<br />

✩<br />

Calcul <strong>de</strong> π<br />

L’expérience <strong>de</strong> Georges Louis Leclerc, Comte <strong>de</strong> Buffon, 1777:<br />

• Aiguille <strong>de</strong> 1 cm <strong>de</strong> long<br />

• Laisser tomber l’aiguille sur le sol<br />

• Sol recouvert <strong>de</strong> planches <strong>de</strong> 2cm <strong>de</strong> large<br />

Quelle est la probabilité que l’aiguille touche <strong>de</strong>ux planches?<br />

✫<br />


8INF430 <strong>Algorithmes</strong> probabilistes 10<br />

✬<br />

✩<br />

Réponse: 1/π<br />

Si on laisse tomber n aiguilles alors l’espérance du nombre<br />

d’aiguilles qui touchent <strong>de</strong>ux planches est n/π. Cela donne<br />

l’algorithme suivant:<br />

k=0<br />

pour i=1 à n faire<br />

laisser tomber une aiguille sur le sol<br />

si l’aiguille touche 2 planches alors k=k+1<br />

r<strong>et</strong>ourner n/k (puisque E[k]=n/ π)<br />

✫<br />


8INF430 <strong>Algorithmes</strong> probabilistes 11<br />

✬<br />

Comptage probabiliste<br />

✩<br />

On veut compter le nombre <strong>de</strong> truites dans un lac.<br />

On suppose qu’il est possible <strong>de</strong> pêcher <strong>de</strong>s truites (avec remise) <strong>de</strong><br />

façon aléatoire, uniforme <strong>et</strong> indépendante.<br />

On procè<strong>de</strong> <strong>de</strong> la façon suivante:<br />

répéter<br />

capturer une truite<br />

la peindre en rouge<br />

la rem<strong>et</strong>tre dans le lac<br />

jusqu’à ce qu’on recapture une truite rouge<br />

Combien <strong>de</strong> fois faut-il répéter?<br />

✫<br />


8INF430 <strong>Algorithmes</strong> probabilistes 12<br />

✬<br />

✩<br />

Soit P n,k la probabilité d’avoir une répétition lorsqu’on choisit k<br />

éléments parmi n.<br />

Fait: P n,k ≈ 1 − e −k2 /2n<br />

P n,k ≈ 1 − e −k2 /2n ≥ 1/2 ⇔ e −k2 /2n ≤ 1/2<br />

⇔ −k 2 /2n ≤ ln(1/2)<br />

⇔ k 2 /2n ≥ ln(2)<br />

⇔ k ≥ √ 2 ln(2) √ n<br />

Remarque: Cela indique qu’après avoir choisi k = ⌈1.177 √ n⌉<br />

éléments, la probabilité d’avoir une répétition dépasse 50%.<br />

Fait: Lorsque n → ∞ on a E(k) = √ πn/2 <strong>et</strong> E(k 2 ) = 2n<br />

✫<br />


8INF430 <strong>Algorithmes</strong> probabilistes 13<br />

✬<br />

✩<br />

Fait: Lorsque n → ∞ on a E(k) = √ πn/2 <strong>et</strong> E(k 2 ) = 2n<br />

fonction compter(S)<br />

k = 0<br />

T = ∅<br />

a=uniforme(S)<br />

Tantque a /∈ T faire<br />

k = k + 1<br />

T = T ∪ {a}<br />

a=uniforme(S)<br />

r<strong>et</strong>ourner k 2 /2<br />

✫<br />


8INF430 <strong>Algorithmes</strong> probabilistes 14<br />

✬<br />

✩<br />

<strong>Algorithmes</strong> <strong>de</strong> <strong>Sherwood</strong><br />

Exemple: Quicksort:<br />

procédure Pquicksort(T[1..n])<br />

mélanger(T[1..n])<br />

quicksort(T[1..n])<br />

procédure mélanger(T[1..n])<br />

pour i=1 à n-1 faire<br />

j=uniform(i,n)<br />

échanger T[i] <strong>et</strong> T[j]<br />

Temps espéré: O(n + n lg n) = O(n lg n)<br />

✫<br />


8INF430 <strong>Algorithmes</strong> probabilistes 15<br />

✬<br />

✩<br />

Hachage universel<br />

On veut m<strong>et</strong>tre en mémoire un p<strong>et</strong>it nombre d’éléments (disons n)<br />

provenant d’un grand ensemble X.<br />

Exemple: Table <strong>de</strong>s symboles d’un compilateur, mémorisation<br />

d’adresses IP, <strong>et</strong>c.<br />

On veut un accès très rapi<strong>de</strong> à ces éléments: O(1)<br />

Adressage direct: Espace O(|X|).<br />

Liste chaînée: Temps <strong>de</strong> recherche O(n).<br />

Table <strong>de</strong> hachage: Espace O(n), temps <strong>de</strong> recherche O(?)<br />

✫<br />


8INF430 <strong>Algorithmes</strong> probabilistes 16<br />

✬<br />

✩<br />

Fonction <strong>de</strong> hachage<br />

Définition: Soit N > 0. Une fonction <strong>de</strong> hachage est une fonction<br />

<strong>de</strong> la forme<br />

Exemple: h(k) = k MOD N<br />

h : X → {0, 1, ..., N − 1}<br />

Définition: Une collision se produit lorsque a ≠ b <strong>et</strong> h(a) = h(b).<br />

Une solution pour résoudre ce problème consiste à utiliser une table<br />

<strong>de</strong> listes.<br />

Fait: Si a <strong>et</strong> b sont choisit <strong>de</strong> façon aléatoire <strong>et</strong> uniforme dans X<br />

alors la probabilité d’une collision est 1/N.<br />

✫<br />


8INF430 <strong>Algorithmes</strong> probabilistes 17<br />

✬<br />

✩<br />

Fait: Aucune fonction <strong>de</strong> hachage n’est parfaite.<br />

Idée: La fonction <strong>de</strong> hachage n’est pas fixé mais choisit<br />

aléatoirement parmi un ensemble <strong>de</strong> fonctions.<br />

• Le nombre <strong>de</strong> fonctions f : X → {0, .., N − 1} est N |X| .<br />

• Si f est choisit <strong>de</strong> façon uniforme alors pour a ≠ b on a<br />

Pr(f(a) = f(b)) = 1 N<br />

• En pratique, il n’est pas possible <strong>de</strong> choisir aléatoirement une<br />

fonction f : X → {1, .., N} car N |X| est trop grand.<br />

On doit donc restreindre l’ensemble <strong>de</strong>s fonctions possibles.<br />

✫<br />


8INF430 <strong>Algorithmes</strong> probabilistes 18<br />

✬<br />

✩<br />

Classe universelle <strong>de</strong> fonctions<br />

Soit H une classe <strong>de</strong> fonctions X → {0, .., N − 1} <strong>et</strong> soit h ∈ H une<br />

fonction dans H choisit <strong>de</strong> façon aléatoire <strong>et</strong> uniforme.<br />

Alors H est une classe universelle <strong>de</strong> fonctions si pour tout a ≠ b<br />

dans X on a:<br />

Pr(h(a) = h(b)) = 1 n<br />

✫<br />


8INF430 <strong>Algorithmes</strong> probabilistes 19<br />

✬<br />

✩<br />

Exemple 1<br />

Un certain service Web nécessite <strong>de</strong> mémoriser l’adresse IP <strong>de</strong>s<br />

client actifs.<br />

• X est l’ensemble <strong>de</strong> toutes les adresses IP possibles<br />

• Chaque adresse x ∈ X est <strong>de</strong> la forme x = (x 1 , x 2 , x 3 , x 4 ) où les<br />

x i ont 8 bits (ex. x = 132.212.11.75 )<br />

• On utilise une table <strong>de</strong> hachage <strong>de</strong> n = 257 entrées<br />

(n peut être n’importe quel nombre premier).<br />

✫<br />


8INF430 <strong>Algorithmes</strong> probabilistes 20<br />

✬<br />

✩<br />

On choisit les paramètres suivants:<br />

• a = (a 1 , a 2 , a 3 , a 4 ) ∈ {0, . . . , n − 1} 4<br />

• h a : X → {0, . . . , n − 1}<br />

• h a (x) = (a 1 x 1 + a 2 x 2 + a 3 x 3 + a 4 x 4 ) mod n<br />

• H = {h a | a ∈ {0, . . . , n − 1} 4 }<br />

Fait: H est une classe universelle <strong>de</strong> fonctions<br />

✫<br />


8INF430 <strong>Algorithmes</strong> probabilistes 21<br />

✬<br />

✩<br />

Preuve: On doit montrer que si x ≠ y sont <strong>de</strong>ux éléments <strong>de</strong> X<br />

alors<br />

Pr(h a (x) = h a (y)) = 1 n<br />

Supposons que x 4 ≠ y 4 <strong>et</strong> que h a (x) = h a (y). On a<br />

3∑<br />

a i (x i − y i ) = a 4 (y 4 − x 4 ) (mod n)<br />

i=1<br />

Si la partie <strong>de</strong> gauche vaut c alors on doit avoir<br />

a 4 = c(y 4 − x 4 ) −1<br />

La probabilité que cela se produise est 1/n<br />

✫<br />


8INF430 <strong>Algorithmes</strong> probabilistes 22<br />

✬<br />

✩<br />

Exemple 2<br />

On défini h m,n : X → {0, 1, .., N − 1} <strong>de</strong> la façon suivante:<br />

• p > |X|, un nombre premier<br />

• m <strong>et</strong> n, <strong>de</strong>ux entiers < p<br />

• h m,n (x) = (mx + n mod p) mod N<br />

H = {h n,m | m, n < p}<br />

Fait: H est une classe universelle <strong>de</strong> fonctions<br />

✫<br />


8INF430 <strong>Algorithmes</strong> probabilistes 23<br />

✬<br />

✩<br />

Preuve: Soit x, y ∈ X tels que x ≠ y.<br />

Question: Combien y a-t-il <strong>de</strong> fonctions dans H telles que<br />

h(x) = h(y)?<br />

Réponse: il y a au plus p(p−1)<br />

N<br />

n.<br />

1. ≤ p possibilités pour n<br />

choix possibles pour la paire m <strong>et</strong><br />

2. ≤ p−1<br />

N<br />

possibilités pour m<br />

✫<br />


8INF430 <strong>Algorithmes</strong> probabilistes 24<br />

✬<br />

✩<br />

1.<br />

h(x) = h(y)<br />

(mx + n mod p) mod N = (my + n mod p) mod N<br />

(m(x − y) mod p) mod N = 0<br />

=⇒ p possibilités pour n<br />

✫<br />


8INF430 <strong>Algorithmes</strong> probabilistes 25<br />

✬<br />

✩<br />

2.<br />

Puisque 1 ≤ m < p <strong>et</strong> 0 ≤ x, y < |X| − 1 <strong>et</strong> x ≠ y alors<br />

m(x − y) mod p ≠ 0<br />

m(x − y) = ap + b où 0 < b < p<br />

(m(x − y) mod p) mod N = b mod N = 0<br />

On a donc b ∈ {N, 2N, · · · , ⌊ p−1<br />

N<br />

⌋N}<br />

Fait: Pour chaque b il y a exactement une valeur <strong>de</strong> m telle que<br />

m(x − y) mod p = b.<br />

=⇒ ≤ p−1<br />

N<br />

possibilités pour m<br />

✫<br />


8INF430 <strong>Algorithmes</strong> probabilistes 26<br />

✬<br />

✩<br />

Il y a donc au plus p(p−1)<br />

N<br />

fonctions h ∈ H telles que h(x) = h(y).<br />

De plus, il y a p(p − 1) fonctions dans H = {h m,n | 1 ≤ m < p <strong>et</strong><br />

0 ≤ n < p}.<br />

La probalilité d’avoir une collision est donc au plus:<br />

p(p−1)<br />

N<br />

p(p−1) = 1 N<br />

Ce qui démontre que H est une classe universelle <strong>de</strong> fonctions.<br />

✫<br />


8INF430 <strong>Algorithmes</strong> probabilistes 27<br />

✬<br />

✩<br />

✫<br />


8INF430 <strong>Algorithmes</strong> probabilistes 28<br />

✬<br />

✩<br />

✫<br />


8INF430 <strong>Algorithmes</strong> probabilistes 29<br />

✬<br />

✩<br />

✫<br />

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!