Professional Documents
Culture Documents
Recursive and Non-Recursive Programming Methods: Course 2 - English Classes
Recursive and Non-Recursive Programming Methods: Course 2 - English Classes
Overview
2.1. BACKTRACKING METHOD
2.2. DIVIDE ET IMPERA METHOD
explicit rules that restraints the values set for each component
implicit rules that determines which combinations from the
solution space satisfies the criteria function
6
Characteristics:
Number of components, n;
Space values dimension, h;
Other data necessary to elaborate the algorithm
10
11
Recursive solution
void Backtracking(int k)
{
// for all posible values of component xk
for(int i=1; i <= h; i++)
{
x[k] = i;
// if the partial solution is possible
if ( posibil(k)) {
// if ready solution
if ( k == n-1)
afiseaza_solutia(X);
else
Backtracking(k+1); // recursive call
}
}
}
13
In main( ):
// init
Backtracking(0);
14
15
int posibil(int);
void afis_sol(void);
int x[DIM],n;
void main(void)
{
int k,h;
printf("\nIntrodu numarul de culori ale unui drapel(benzi,
<=DIM=5)\n");
scanf("%d",&n);
printf("Introdu numarul maxim de culori posibile ale unei
benzi \n");
scanf("%d",&h);
16
k=0;
x[k]=0;
do
{
while(x[k] < h) // mai sunt valori posibile pentru
// componenta k
{
x[k]++; //trec la urmatoarea valoare
if(posibil(k))
if(k==(n-1)) afis_sol(); //e gata solutie
else {k++; x[k]=0; }
// trec la urmatoarea
// componenta cu
// valori de la zero
}//while
k--; // nu mai sunt valori pentru componenta k. Revin la
// componenta k-1
}while(!(k<0));
// m-am intors mai mult decit se putea,
// sau k>=0
}//main
17
int posibil(int k)
{
if(k==0)return 1;
if(x[k-1]==x[k]) return 0;
return 1;
}//posibil
void afis_sol(void)
{
for(int i=0;i<n;i++)
19
void dr_rec(int k)
{
for(int i=1;i<=h;i++)
{ x[k]=i;
if(posibil(k))
if(k==(n-1)) afis_sol(); //solutie finala
else dr_rec(k+1); //trec la urmatoarea
//componenta
}
}//dr_rec
20
21
do {
while(x[k] < n)
{
x[k]++;
if(Posibil(k, sum)) {
if(sum == n) // solutia finala
Afis_sol(k);
else {
k++;
x[k] = 0;
}
}
} // bucla valori componenta
k--; // pas inapoi
} while(!(k < 0)); // bucla componente
}
22
23
void Back_SubSet(int k)
{
int sum;
x[k] = 0;
// pentru toate valorile pe care le poate lua x[k]
while(x[k] < n)
{
x[k]++;
if(Posibil(k, sum))
// solutie posibila
{
if(sum == n)
// solutie completa
Afis_sol(k);
else
Back_SubSet(k+1);
}
}
}
24
25
26
void main(void)
{
int k;
printf("Introdu dim matrice costuri(nr.orase) <=7\n");
scanf("%d",&n);
printf("Introdu si afis matricea costurilor C\n");
cit(COST,n);
afis(COST,n);
cost_M=(n+1)*(long)max_cost(COST,n)+1;
// printf("Cost maxim= %ld\n",cost_M);
k=0;
printf("Introdu orasul initial (0 ~ n-1)");
scanf("%d",&x[k]);
gene(1);
afis_sol(cost_M); //e gata solutie
}//main
27
int posibil(int k)
{
if(k==0)return 1;
if(COST[x[k-1]][x[k]]!=0){//drum direct
for(int i=0;i<k;i++)//orasul nu a mai fost ales
if(x[k]==x[i]) return 0;
return 1;
}
return 0;
}//posibil
28
void gene(int k)
{
for(int i=0;i<n;i++)// valori intre 0 si n-1
{
x[k]=i;
if(posibil(k))
if((k==(n-1))&& (COST[x[n-1]][x[0]]!=0)){
C=0;// stabilire cost pentru solutia determinata
for(int i=0;i<n-1;i++) C+=COST[x[i]][x[i+1]];
C+=COST[x[n-1]][x[0]];
if(C<cost_M){
for(int i=0;i<n;i++)
Y[i]=x[i];
cost_M=C; //salvare solutie cost minim
}
}
else gene(k+1);
} //for
} // end gene
29
Conclusions:
The solution is determined by a systematic
search in the solution space
We build the solution vector, component by
component and we test at each step if the
partial obtained vector is able to be
considered in the final solution:
If yes, we continue
If no, the partial obtained vector is ignored
30
Binary search
We consider a vector of ordered elements. We
want to find an element (key) from the vector.
We decompose the problem:
We compare the key with the value from the
middle
If are equals, we found the position in the vector
If not, we know which elements (left or right
middle) are used to continue the searching
process
We continue in this mode till we find the
position of the element, or the element is not in
the vector arriving at an empty vector
35
Searching value: 23
36
Searching value : 88
37
The seraching function will return the position from the vector
where is the element, or value (-1) if it is not founded:
int CautareBinara(int *p, int inc, int sfr, int val)//recursive
{
int mij;
mij = (inc + sfr)/2;
if(p[mij] == val)
return mij;
38
Hanoi Tower
41
42
43
44
45
47
49
#define MAX 8
void citire_sir(int*, int);
void tip_sir(int*, int);
int m_max(int*, int, int);
void main( )
{
int n, Maxim;
int vect[MAX];
cout << "Numarul de elemente mai mic de " << MAX <<"
este=";
cin >> n;
cout << "Introduceti cele n elemente\n";
citire_sir(vect,n);
Maxim=m_max(vect, 0, n);
cout << "Maximul sirului este " << Maxim <<'\n';
cout <<"Tipareste elementele sirului\n";
tip_sir(vect,n);
}
50
51
52