• Aucun résultat trouvé

Data Structures and Algorithms

N/A
N/A
Protected

Academic year: 2022

Partager "Data Structures and Algorithms"

Copied!
2
0
0

Texte intégral

(1)

Data Structures and Algorithms

Tutorial 2: Shuffling an Array

Naive Shuffle

Consider the following algorithm to Shuffle an array: draw elements randomly from the original arrayA, and mark taken elements using an arrayCso we don’t take them away. To chose an element, just call RANDOM to generate a new index as long as the corresponding element has already been taken. We will assume that calls to RANDOMuse a constant time to return values following a uniform distribution.

Input: an array A

Output: a shuffled copy ofA NAIVESHUFFLE(A)

1 n←length(A)

1 fori←1 tondoC[i]←0 2 fori←1 tondo

3 do

4 j←RANDOM(1,n) 5 untilC[j] =0

6 C[j]←1 7 B[i]← A[j] 8 returnB Questions:

1. Explain why this algorithm may not terminate.

2. Among the cases where the algorithm do terminate, what is the run-time complexity of the best case scenario to shuffle an array of sizenwith this algorithm?

3. Lettibe arandom variable(orstochastic variable) denoting the number of calls to RANDOMduring thei-th iteration.

(a) The probability that the second iteration makes only one call to RANDOMis Pr{t2 =1}= n−1

n

because there aren−1 possible free values to choose from then. What is the probability Pr{ti+1 = k+1} to make k+1 calls to RANDOM (that means k unlucky random calls followed by 1 good call) afterivalues have already been taken?

(b) Deduce the expected value E[ti+1−1].

(c) Finally give the order (using Θ notation) of the average number of calls to RANDOM:

ni=1E[ti].

4. Among all scenarios what is the probability of getting a best case?

1

(2)

The Modern Fisher-Yates Shuffle

This shuffle is also known as theKnuth Shuffle.

Input: an array A

Output: the array Ashuffled in place FISHERYATESSHUFFLE(A)

1 n←length(A) 2 fori←1 ton−1 do 3 j←RANDOM(i,n) 4 A[i]↔ A[j] Questions:

1. Explain why this algorithm always terminates.

2. What is the run-time complexity of this algorithm?

3. How can we justify that this algorithm is unbiased? (i.e., That it can generate all permutation with equal chance.)

Inside-Out Fisher-Yates Shuffle

Sometimes you do not want an array to be shuffle in place. This version of the algorithm will shuffle the array as it is being copied.

Input: an array A

Output: a shuffled copy ofA

INSIDEOUTFISHERYATESSHUFFLE(A) 1 n←length(A)

2 B[1]← A[1] 3 fori←2 tondo 4 j←RANDOM(1,i) 5 B[i]←B[j]

6 B[j]←A[j] 7 returnB Questions:

1. Is this algorithm isunbiased?

2. What is its run-time complexity?

3. Such a shuffle could also be done by copying A on B and then calling FISHERYATESSHUT-

TLE(B). What would be the complexity of doing that?

4. Why would we prefer INSIDEOUTFISHERYATESSHUFFLEover copy+FISHERYATESSHUTTLE?

2

Références

Documents relatifs

(5) Bhaskaracharya et al [4] have shown that maximizing conflict satisfaction is a reasonably effective heuristic for exploiting intra- statement storage reuse

In Section 2, we introduce Horn clauses, the transforma- tion of our running example, and Galois connections, in Section 3, we formally give our data abstraction technique, in

L’archive ouverte pluridisciplinaire HAL, est destinée au dépôt et à la diffusion de documents scientifiques de niveau recherche, publiés ou non, émanant des

The characteristics for the five most repeated prokaryote genomes are pre- sented in Table 1. These short genome sequences have been extensively studied and are well known

This method proposes to complement low-cost inertial sensors commonly used in navigation techniques (accelerometers and gyroscopes [5], [2]) with a specific device consisting of

and predict its expected impact, the research project Abysound aims at creating a monitoring system composed of an acoustic array located in deep waters near the noisy zones to

This identification is carried out by machine learning thanks to two different approaches: a supervised model that differentiates drone signals from noise ones in a

Pruning based on phased alleles versus allele counts gave similar results, both with SNPrune and PLINK, but when the analysis used allele counts, it was considerably faster, for