Démonstration 5 0. 1. 2.

Transcription

Démonstration 5 0. 1. 2.
IFT2125 – Introduction à l’algorithmique
Automne 2015
Démonstration 5
Démonstrateur: Michael Blondin
0.
Solutions du devoir 1.
1.
À une certaine époque, l’unité monétaire du Portugal était constituée de pièces de 1, 2 12 , 5,
10, 20, 25 et 50 escudos. L’algorithme vorace suivant rend-il toujours la monnaie de façon
optimale pour ce système monétaire ?
def monnaie(pieces, montant):
t = [0] * len(pieces)
x = montant
for i in reversed(range(len(pieces))):
t[i] = int(x // pieces[i])
x
= int(x % pieces[i])
return t
Vous pouvez supposer qu’une infinité de pièces sont à votre disposition et que chaque montant
est entier.
2.
Considérez un système monétaire constitué de pièces de c1 , c2 , . . . , ck ∈ N>0 telles que c1 = 1
et ci ≥ 2ci−1 pour tout 1 < i ≤ k. L’algorithme vorace monnaie de la question précédente
rend-il toujours la monnaie de façon optimale pour un tel système monétaire ? Vous pouvez
supposer qu’une infinité de pièces sont à votre disposition et que chaque montant est entier.
1
3.
Considérons le problème d’ordonnancement suivant. Nous avons un serveur qui doit traiter
k clients au total et qui ne peut traiter qu’un client à la fois. Le temps requis pour traiter
le client i est ti . Nous cherchons à minimiser le temps moyen d’attente des clients dans le
système. Donnez un algorithme pour ce problème.
2
English version for Ph.D. students
0.
Homework 1 solutions.
1.
At some point in history, the currency of Portugal was made of coins of 1, 2 12 , 5, 10, 20,
25 and 50 escudos. For this currency, is the following greedy algorithm always optimal for
change making ?
def monnaie(pieces, montant):
t = [0] * len(pieces)
x = montant
for i in reversed(range(len(pieces))):
t[i] = int(x // pieces[i])
x
= int(x % pieces[i])
return t
Suppose you have an unlimited supply of coins of each denomination and each amount is an
integer.
2.
Consider a currency made of coins of c1 , c2 , . . . , ck ∈ N>0 such that c1 = 1 et ci ≥ 2ci−1 for
every 1 < i ≤ k. For such a currency, is the previous greedy algorithm monnaie always optimal for change making ? Suppose you have an unlimited supply of coins of each denomination
and each amount is an integer.
3.
Read Section 6.6.1 (Brassard & Bratley pp. 205–207) about minimizing time in systems with
greedy scheduling.
3

Documents pareils

Démonstration 5 1. 2. 3.

Démonstration 5 1. 2. 3. for i in reversed(xrange(len(p))): t[i] = int(x // p[i]) x = int(x % p[i]) return t Suppose you have an unlimited supply of coins of each denomination and each amount is an integer.

Plus en détail