1. Here it is the code of the first program:
This program generates the list of all partitions of [7] into a file called part7.dat. Note that to generate the list of partitions of [k], we need only to exchange n=7 on the end of the program to n=k. In order to change the name of the output file, we need to change the name of the output file "part7.dat" (see main procedure) to any other name.
#include <stdio.h>
#define MAXN 40 /*max value of n*/
int n; /*partition of [n]*/
int word[MAXN]; /*word partition*/
long numberword=0; /*number word partition of length n*/
FILE *out; /*outputfile*/
void words() /*printing on the screen and in the output file the current partition*/
{
int j;
for(j=0;j<n;j++) printf("%d",word[j]+1); printf("\n");
for(j=0;j<n;j++) fprintf(out,"%d",word[j]+1); fprintf(out,"\n");
numberword++;
}
void builtwords(int l) /*generating the list of the partitions of [n]*/
{
int i,j,ma;
if (l==n) words();
else
{
ma=0; for(i=0;i<l;i++) if (ma<word[i]) ma=word[i];
for(j=0;j<ma+2;j++) {word[l]=j; builtwords(l+1);}
}
}
void main()
{
int i,s;
out=fopen("part7.dat","w"); /*defining the output file*/
n=7; /*defining the length of the partitions*/
{
numberword=0; for(i=0;i<n;i++) {word[i]=0; } builtwords(1);
printf("The number of partitions of [%d] equals %d",n,numberword);
}
printf("\n");
fclose(out);
}
2. Here it is the code of the first program: This program (below) finding the number of partitions of [n]=[11] that avoid a pattern of [7] form input file called "part7.dat" contains NN=877 patterns. The program prints the data on the screen and on the output file called "tpart7.dat". We note the following:
1. The program can be changed easily from input file to another, that is, we need to change the name of input file "part7.dat" to another name of file and take care to change the value of NN (number the patterns (lines) in the input file)
2. The program finds the number of partitions of [k]. Only we need to exchange the value of n in the main procedure from n=11 to n=k.
3. The program deals with patterns of length k=3,4,5,6,7. For example, if we would like to deals with patterns of [6] then see the following link program for patterns of [6].
#include <stdio.h>
#define MAXN 40
int NK;
int n;
int word[MAXN];
long numberword=0;
int pa3[5][3],ii;
int pa4[15][4],jj;
int pa5[52][5],kk;
int pa6[203][6],ll;
int pa7[877][7],mm,NN;
void init() /*reading the list of the partitions of [7] from the file "part7.dat"*/
{
FILE *inp;
int i,j,cc;
char ccc;
inp=fopen("part7.dat","r");
for(i=0;i<NN;i++)
{
for(j=0;j<7;j++) {fscanf(inp,"%1d",&cc); pa7[i][j]=cc;}
fscanf(inp,"%c",&ccc);
}
fclose(inp);
}
int order(int a,int b) /*order between to elements*/
{ if (a==b) return 0;
else if(a>b) return 1;
else return -1; }
/*checking if two subsequence of length 7 are order isomorphic*/
int occ7(int a1,int a2,int a3,int a4,int a5,int a6,int a7,int b1,int b2,int b3,int b4,int b5,int b6,int b7)
{
int m[21],n[21],i;
m[0]=order(a1,a2); m[1]=order(a1,a3); m[2]=order(a1,a4); m[3]=order(a1,a5); m[4]=order(a1,a6); m[5]=order(a1,a7);
m[6]=order(a2,a3); m[7]=order(a2,a4); m[8]=order(a2,a5); m[9]=order(a2,a6); m[10]=order(a2,a7);
m[11]=order(a3,a4); m[12]=order(a3,a5);m[13]=order(a3,a6);m[14]=order(a3,a7);
m[15]=order(a4,a5);m[16]=order(a4,a6);m[17]=order(a4,a7);
m[18]=order(a5,a6);m[19]=order(a5,a7);
m[20]=order(a6,a7);
n[0]=order(b1,b2); n[1]=order(b1,b3); n[2]=order(b1,b4); n[3]=order(b1,b5); n[4]=order(b1,b6); n[5]=order(b1,b7);
n[6]=order(b2,b3); n[7]=order(b2,b4); n[8]=order(b2,b5); n[9]=order(b2,b6); n[10]=order(b2,b7);
n[11]=order(b3,b4);n[12]=order(b3,b5);n[13]=order(b3,b6);n[14]=order(b3,b7);
n[15]=order(b4,b5);n[16]=order(b4,b6);n[17]=order(b4,b7);
n[18]=order(b5,b6);n[19]=order(b5,b7);
n[20]=order(b6,b7);
for(i=0;i<21;i++) if (m[i]!=n[i]) return(0);
return(1);
}
/*checking if two subsequence of length 6 are order isomorphic*/
int occ6(int a1,int a2,int a3,int a4,int a5,int a6,int b1,int b2,int b3,int b4,int b5,int b6)
{
int m[15],n[15],i;
m[0]=order(a1,a2); m[1]=order(a1,a3); m[2]=order(a1,a4); m[3]=order(a1,a5); m[4]=order(a1,a6);
m[5]=order(a2,a3); m[6]=order(a2,a4); m[7]=order(a2,a5); m[8]=order(a2,a6);
m[9]=order(a3,a4); m[10]=order(a3,a5);m[11]=order(a3,a6);
m[12]=order(a4,a5);m[13]=order(a4,a6);
m[14]=order(a5,a6);
n[0]=order(b1,b2); n[1]=order(b1,b3); n[2]=order(b1,b4); n[3]=order(b1,b5); n[4]=order(b1,b6);
n[5]=order(b2,b3); n[6]=order(b2,b4); n[7]=order(b2,b5); n[8]=order(b2,b6);
n[9]=order(b3,b4); n[10]=order(b3,b5);n[11]=order(b3,b6);
n[12]=order(b4,b5);n[13]=order(b4,b6);
n[14]=order(b5,b6);
for(i=0;i<15;i++) if (m[i]!=n[i]) return(0);
return(1);
}
/*checking if two subsequence of length 5 are order isomorphic*/
int occ5(int a1,int a2,int a3,int a4,int a5,int b1,int b2,int b3,int b4,int b5)
{
int m[10],n[10],i;
m[0]=order(a1,a2); m[1]=order(a1,a3); m[2]=order(a1,a4); m[3]=order(a1,a5);
m[4]=order(a2,a3); m[5]=order(a2,a4); m[6]=order(a2,a5);
m[7]=order(a3,a4); m[8]=order(a3,a5);
m[9]=order(a4,a5);
n[0]=order(b1,b2); n[1]=order(b1,b3); n[2]=order(b1,b4); n[3]=order(b1,b5);
n[4]=order(b2,b3); n[5]=order(b2,b4); n[6]=order(b2,b5);
n[7]=order(b3,b4); n[8]=order(b3,b5);
n[9]=order(b4,b5);
for(i=0;i<10;i++) if (m[i]!=n[i]) return(0);
return(1);
}
/*checking if two subsequence of length 4 are order isomorphic*/
int occ4(int a1,int a2,int a3,int a4,int b1,int b2,int b3,int b4)
{
int m[10],n[10],i;
m[0]=order(a1,a2); m[1]=order(a1,a3); m[2]=order(a1,a4);
m[3]=order(a2,a3); m[4]=order(a2,a4);
m[5]=order(a3,a4);
n[0]=order(b1,b2); n[1]=order(b1,b3); n[2]=order(b1,b4);
n[3]=order(b2,b3); n[4]=order(b2,b4);
n[5]=order(b3,b4);
for(i=0;i<6;i++) if (m[i]!=n[i]) return(0);
return(1);
}
/*checking if two subsequence of length 3 are order isomorphic*/
int occ3(int a1,int a2,int a3,int b1,int b2,int b3)
{
int m[10],n[10],i;
m[0]=order(a1,a2); m[1]=order(a1,a3);
m[2]=order(a2,a3);
n[0]=order(b1,b2); n[1]=order(b1,b3);
n[2]=order(b2,b3);
for(i=0;i<3;i++) if (m[i]!=n[i]) return(0);
return(1);
}
/*avoiding a partition of length 7*/
int avoidpart()
{
int i1,i2,i3,i4,i5,i6,i7;
for(i7=0;i7<n-6;i7++)
for(i6=i7+1;i6<n-5;i6++)
for(i5=i6+1;i5<n-4;i5++)
for(i4=i5+1;i4<n-3;i4++)
for(i3=i4+1;i3<n-2;i3++)
for(i2=i3+1;i2<n-1;i2++)
for(i1=i2+1;i1<n;i1++)
{
if(occ7(word[i7],word[i6],word[i5],word[i4],word[i3],word[i2],word[i1],
pa7[jj][0],pa7[jj][1],pa7[jj][2],pa7[jj][3],pa7[jj][4],pa7[jj][5],pa7[jj][6])==1) return(0);
}
return(1);
}
void words()
{
if (avoidpart()==1) numberword++; /*The current partition of [n] avoiding the current pattern*/
}
void builtwords(int l) /*generate the list of the partitions of [n]*/
{
int i,j,ma;
if (l==n) words();
else
{
ma=0; for(i=0;i<l;i++) if (ma<word[i]) ma=word[i];
for(j=0;j<ma+2;j++) {word[l]=j; builtwords(l+1);}
}
}
void main()
{
FILE *fout; /*The output file*/
int i,s;
NN=877; /*The number of the patterns of length 7 in the input file*/
init(); /*reading the input file and remember it as array pa7[][]*/
fout=fopen("tpart7.dat","w"); /*write each partition (pattern) from the input file in the output file */
/*and how many partitions of [n] that avoid this pattern. Here assumed */
/*that n=11*/
for(jj=0;jj<NN;jj++) /*scan the patterns in the array pa7[][]*/
{
/*printing the pattern on the screen and in the output file*/
printf("%5d P_n(%d%d%d%d%d%d%d)=",jj+1,pa7[jj][0],pa7[jj][1],pa7[jj][2],pa7[jj][3],pa7[jj][4],pa7[jj][5],pa7[jj][6]);
fprintf(fout,"P_n(%d%d%d%d%d%d%d)=",pa7[jj][0],pa7[jj][1],pa7[jj][2],pa7[jj][3],pa7[jj][4],pa7[jj][5],pa7[jj][6]);
n=11;
{
numberword=0; for(i=0;i<n;i++) {word[i]=0; } builtwords(1); /*finding the partitions of [n]*/
/*that avoid the pattern*/
printf("%8d,",numberword,n); /*printing the number of partitions of [n] that avoid the pattern*/
fprintf(fout,"%8d,",numberword,n);
}
printf("\n");
fprintf(fout,"\n");
}
fclose(fout);
}