Sunteți pe pagina 1din 5

DIVIDE ET IMPERA

1. Implementați următorii algoritmi folosind metoda divide et impera?


A. Merge sort
B. Binary Search
C. Quick Sort

2. Calculați suma elementelor unei liste date.


3. Scrieți o funcție care să determine dacă o listă cu elemente numere întregi este ordonată
crescător.

4. Să se numere elementele pare ale unei liste.


5. Determinați frecvența de apariție a elementelor într-o listă dată.
6. Rezolvați ecuația x3 + x - 1 pe intervalul [0,1] (i.e. returnati valoarea lui x din intervalul
dat pentru care ecuația este egală cu 0)
7. Se dă o listă cu elemente numere naturale. Să se calculeze CMMDC al elementelor listelor.

GREEDY ALGORITHMS

• o strategie de rezolvare pentru probleme de optimizare


• aplicabil unde optimul global se poate afla selectând succesiv optime locale
• permite rezolvarea problemei fără a reveni asupra alegerilor făcute pe parcurs
• folosit în multe probleme practice care necesite selecția unei mulțimi de elemente care
satisfac anumite condiții și realizează un optim

Principiu (strategia) Greedy :


• adaugă succesiv la rezultat elementul care realizează optimul local
• o decizie luată pe parcurs nu se mai modifică ulterior

• Poate furniza soluția optimă (doar pentru anumite probleme)


• o alegerea optimului local nu garantează tot timpul optimul global
• o soluție optimă dacă se găsește o modalitate de a alege (optimul local) astfel încât se
ajunge la soluție optimă
• o în unele situații se preferă o soluție, chiar și suboptimă, dar obținut în timp rezonabil
• Construiește treptat soluția (fără reveniri ca în cazul backtracking)
• Furnizează o singură soluție
• Timp de lucru polinomial

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?

Input: Four Jobs with following


deadlines and profits
JobID Deadline Profit
a 4 20
b 1 10
c 1 40
d 1 30
Output: Following is maximum
profit sequence of jobs
c, a

Input: Five Jobs with following


deadlines and profits
JobID Deadline Profit
a 2 100
b 1 19
c 2 27
d 1 25
e 3 15
Output: Following is maximum
profit sequence of jobs
c, a, e

2. Given an array of size n that has the following specifications:


A. Each element in the array contains either a policeman or a thief.
B. Each policeman can catch only one thief.
C. A policeman cannot catch a thief who is more than K units away from the
policeman.
We need to find the maximum number of thieves that can be caught.

Input : arr[] = {'P', 'T', 'T', 'P', 'T'},


k = 1.
Output : 2.
Here maximum 2 thieves can be caught, first
policeman catches first thief and second police-
man can catch either second or third thief.

Input : arr[] = {'T', 'T', 'P', 'P', 'T', 'P'},


k = 2.
Output : 3.

Input : arr[] = {'P', 'T', 'P', 'T', 'T', 'P'},


k = 3.
Output : 3.

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

Backtracking can be defined as a general algorithmic technique that considers searching


every possible combination in order to solve a computational problem.

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

There are two typical implementations of Dynamic Programming


approach: bottom-to-top and top-to-bottom.

Top-to-bottom Dynamic Programming is nothing else than ordinary recursion, enhanced with
memorizing the solutions for intermediate sub-problems. When a given sub-problem arises
second (third, fourth...) time, it is not solved from scratch, but instead the previously memorized
solution is used right away. This technique is known under the name memoization (no 'r' before
'i').

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).

In Bottom-to-top Dynamic Programming the approach is also based on storing sub-solutions in memory,
but they are solved in a different order (from smaller to bigger), and the resultant general structure of the
algorithm is not recursive. LCS algorithm is a classic Bottom-to-top DP example.

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.

S-ar putea să vă placă și