Documente Academic
Documente Profesional
Documente Cultură
DIVIDE ET IMPERA - Exerciții
DIVIDE ET IMPERA - Exerciții
GREEDY ALGORITHMS
1. Given an array of jobs where every job has a deadline and associated profit if the job is
finished before the deadline. It is also given that every job takes single unit of time, so the
minimum possible deadline for any job is 1. How to maximize total profit if only one job can
be scheduled at a time?
3. Given length of wall w and shelves of two lengths m and n, find the number of each type of
shelf to be used and the remaining empty space in the optimal solution so that the empty
space is minimum. The larger of the two shelves is cheaper so it is preferred. However cost is
secondary and first priority is to minimize empty space on wall.
Input : w = 24 m = 3 n = 5
Output : 3 3 0
We use three units of both shelves
and 0 space is left.
3 * 3 + 3 * 5 = 24
So empty space = 24 - 24 = 0
Another solution could have been 8 0 0
but since the larger shelf of length 5
is cheaper the former will be the answer.
Input : w = 29 m = 3 n = 9
Output : 0 3 2
0 * 3 + 3 * 9 = 27
29 - 27 = 2
Input : w = 24 m = 4 n = 7
Output : 6 0 0
6 * 4 + 0 * 7 = 24
24 - 24 = 0
BACKTRACKING
def backRec(x):
el = first(x)
x.append(el)
while el is not None:
x[-1] = el
if consistent(x):
if solution(x):
outputSolution(x)
backRec(x[:])
el = next(x)
Utilizând metoda backtracking se generează toate numerele cu câte trei cifre impare, cifre
care aparţin mulţimii {7,8,1,6,2,3}. Primele 4 soluţii generate sunt, în această ordine: 777,
771, 773, 717.
Cea de a 8-a soluţie generată este:
a) 737
b) 788
c) 717
d) 731
1. Generare permutări.
2. Generare aranjamente de n luate câte k.
3. Generare combinări de n luate câte k.
4. Generare submulțimi ale unei mulțimi.
5. Generare partiții ale unei mulțimi.
6. Problema celor n regine.
7. Problema colorării hărților.
DYNAMIC PROGRAMMING
This is actually what your example with Fibonacci sequence is supposed to illustrate. Just use the
recursive formula for Fibonacci sequence, but build the table of fib(i) values along the way, and you
get a Top-to-bottom DP algorithm for this problem (so that, for example, if you need to
calculate fib(5) second time, you get it from the table instead of calculating it again).
Bottom-to-top DP algorithms are usually more efficient, but they are generally harder (and sometimes
impossible) to build, since it is not always easy to predict which primitive sub-problems you are going to
need to solve the whole original problem, and which path you have to take from small sub-problems to
get to the final solution in the most efficient way.
Bottom up vs. Top Down:
Bottom Up - I'm going to learn programming. Then, I will start practicing. Then, I
will start taking part in contests. Then, I'll practice even more and try to improve.
After working hard like crazy, I'll be an amazing coder.
Top Down - I will be an amazing coder. How? I will work hard like crazy. How? I'll
practice more and try to improve. How? I'll start taking part in contests. Then? I'll
practicing. How? I'm going to learn programming.