0% found this document useful (0 votes)
17 views

BKT2

The document describes the general backtracking algorithm (BKT) for generating permutations and combinations. It provides the conditions for permutations as assigning each value xk from 1 to n where no values are repeated. For combinations, the conditions are assigning values xk from 1 to n-m+k where subsequent values must be greater than the previous. Pseudocode and C++ implementations of the BKT algorithm are provided for generating permutations and combinations respectively.

Uploaded by

Camelia Bălan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

BKT2

The document describes the general backtracking algorithm (BKT) for generating permutations and combinations. It provides the conditions for permutations as assigning each value xk from 1 to n where no values are repeated. For combinations, the conditions are assigning values xk from 1 to n-m+k where subsequent values must be greater than the previous. Pseudocode and C++ implementations of the BKT algorithm are provided for generating permutations and combinations respectively.

Uploaded by

Camelia Bălan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 1

Conditii permutari:

BKT - SCHEM GENERAL


void BKT(int k)
{
int i;
for(i=val.ini;i<=val.fin;i++)
{
x[k]=i;
if(valid(k))if(sol(k))afis();
else BKT(k+1);
}
}
int main()
{

BKT(1);
}

1. 1<=xk<=n , k=1,n BKT


2. xk!=xi, i=1, k-1 VALID
3. k=n SOL

Conditii combinari:
1. 1<=xk<=n-m+k, k=1,m
2. k>1, xk>xk-1 VALID
3. k=m SOL

BKT

GENERAREA COMBINRILOR
GENERAREA PERMUTRILOR
#include<iostream.h>
int x[100],n;
int valid(int k)
{
int i;
for(i=1;i<=k-1;i++)
if(x[k]==x[i])return 0;
return 1;
}
int sol(int k)
{
return(k==n);
}
void afis()
{
int i;
for(i=1;i<=n;i++)cout<<x[i]<<" ";
cout<<"\n";
}
void BKT(int k)
{
int i;
for(i=1;i<=n;i++)
{
x[k]=i;
if(valid(k))if(sol(k))afis();
else BKT(k+1);
}
}
int main()
{
cin>>n;
BKT(1);
return 0;
}

#include<iostream.h>
int x[100],n,m;
int valid(int k)
{
if(k>1&&x[k]<=x[k-1])return 0;
return 1;
}
int sol(int k)
{
return (k==m);
}
void afis()
{
int i;
cout<<"{";
for(i=1;i<=m-1;i+
+)cout<<x[i]<<",";
cout<<x[m]<<"}\n";
}
void BKT(int k)
{
int i;
for(i=1;i<=n-m+k;i++)
{
x[k]=i;
if(valid(k))if(sol(k))afis();
else
BKT(k+1);
}
}
int main()
{
cin>>n>>m;
BKT(1);
return 0;
}

You might also like