Problem 1: Young tableau
An m × n Young tableau is an m × n matrix such that the entries of each row are in sorted order
from left to right and the entries of each column are in sorted order from top to bottom. Some of the
entries of a Young tableau may be ∞, which we treat as nonexistent elements. Thus a Young tableau
can be used to hold r ≤ mn numbers.
Here’s an example of a 4x4 Young tableau containing the elements {9, 16, 3, 2, 4, 8, 5, 14, 12}. Note that
this is not unique.
∞
2 4 9
∞
3 8 16
∞ ∞
5 14
∞ ∞ ∞
12
//the young tableau problem
#include<stdio.h>
#define M 4
#define N 4
#define BIGGEST 9999
int matrix[M][N]={{0,1,2,4},
{0,2,3,5},
{1,3,4,0},
{0,4,5,6}};
void print()
{
int i=0,j;
for(;i<M;i++)
{
for(j=0;j<N;j++)
printf("%d ",matrix[i][j]);
printf("\n");
}
}
void youngify(int a[M][N],int x,int y)
{
int smallx=x;
int smally=y;
if((x+1)<M&&a[x+1][y]<a[x][y])
{
smallx=x+1;
smally=y;
}
if((y+1)<N&&a[smallx][smally]>a[x][y+1])
{
smallx=x;
smally=y+1;
}
if(smallx!=x||smally!=y)
{
int temp=a[smallx][smally];
a[smallx][smally]=a[x][y];
a[x][y]=temp;
youngify(a,smallx,smally);
}
}
int extract_min()
{
int x=matrix[0][0];//get the miniest
matrix[0][0]=BIGGEST;
youngify(matrix,0,0);
return x;
}
void decreasekey(int a[M][N],int x,int y,int d)
{
if(a[x][y]<=d) return;
a[x][y]=d;
int threshold=BIGGEST;
int largestx=x;
int largesty=y;
int temp;
while((x>1||y>1)&&a[x][y]<threshold)
{
temp=a[x][y];
a[x][y]=a[largestx][largesty];
a[largestx][largesty]=temp;
x=largestx;
y=largesty;
if(x-1>=0&&a[largestx][largesty]<a[x-1][y])
{
largestx=x-1;
largesty=y;
}
if(y-1>=0&&a[largestx][largesty]<a[x][y-1])
{
largestx=x;
largesty=y-1;
}
threshold=a[largestx][largesty];
}
}
void insert(int a[M][N],int d)
{
decreasekey(a,M-1,N-1,d);
}
int main(int argc,char** argv)
{
print();
int i=extract_min();
printf("After Extract %d\n",i);
print();
printf("After Insert %d\n",5);
insert(matrix,5);
print();
return 0;
}
An m × n Young tableau is an m × n matrix such that the entries of each row are in sorted order
from left to right and the entries of each column are in sorted order from top to bottom. Some of the
entries of a Young tableau may be ∞, which we treat as nonexistent elements. Thus a Young tableau
can be used to hold r ≤ mn numbers.
Here’s an example of a 4x4 Young tableau containing the elements {9, 16, 3, 2, 4, 8, 5, 14, 12}. Note that
this is not unique.
∞
2 4 9
∞
3 8 16
∞ ∞
5 14
∞ ∞ ∞
12
//the young tableau problem
#include<stdio.h>
#define M 4
#define N 4
#define BIGGEST 9999
int matrix[M][N]={{0,1,2,4},
{0,2,3,5},
{1,3,4,0},
{0,4,5,6}};
void print()
{
int i=0,j;
for(;i<M;i++)
{
for(j=0;j<N;j++)
printf("%d ",matrix[i][j]);
printf("\n");
}
}
void youngify(int a[M][N],int x,int y)
{
int smallx=x;
int smally=y;
if((x+1)<M&&a[x+1][y]<a[x][y])
{
smallx=x+1;
smally=y;
}
if((y+1)<N&&a[smallx][smally]>a[x][y+1])
{
smallx=x;
smally=y+1;
}
if(smallx!=x||smally!=y)
{
int temp=a[smallx][smally];
a[smallx][smally]=a[x][y];
a[x][y]=temp;
youngify(a,smallx,smally);
}
}
int extract_min()
{
int x=matrix[0][0];//get the miniest
matrix[0][0]=BIGGEST;
youngify(matrix,0,0);
return x;
}
void decreasekey(int a[M][N],int x,int y,int d)
{
if(a[x][y]<=d) return;
a[x][y]=d;
int threshold=BIGGEST;
int largestx=x;
int largesty=y;
int temp;
while((x>1||y>1)&&a[x][y]<threshold)
{
temp=a[x][y];
a[x][y]=a[largestx][largesty];
a[largestx][largesty]=temp;
x=largestx;
y=largesty;
if(x-1>=0&&a[largestx][largesty]<a[x-1][y])
{
largestx=x-1;
largesty=y;
}
if(y-1>=0&&a[largestx][largesty]<a[x][y-1])
{
largestx=x;
largesty=y-1;
}
threshold=a[largestx][largesty];
}
}
void insert(int a[M][N],int d)
{
decreasekey(a,M-1,N-1,d);
}
int main(int argc,char** argv)
{
print();
int i=extract_min();
printf("After Extract %d\n",i);
print();
printf("After Insert %d\n",5);
insert(matrix,5);
print();
return 0;
}