Important Algorithms MAKAUT (Former WBUT)
Important Algorithms MAKAUT (Former WBUT)
PROBLEM STATEMENT:
The knapsack problem in combinatorial optimization states:
Given a set of items, each with a weight and a value, determine
the number of each item to include in a collection so that the
total weight is less than or equal to a given limit and the total
value is as large as possible.
ALGORITHM:
Algo_Greedy_Knapsack (m,n)
{
C PROGRAM:
# include<stdio.h>
void knapsack(int n, float weight[], float profit[], float capacity) {
float x[20], tp = 0; int i, j, u;
u = capacity;
for (i = 0; i < n; i++)
x[i] = 0.0;
for (i = 0; i < n; i++) {
if (weight[i] > u)
break;
else {
x[i] = 1.0; tp = tp+profit[i];
u = u - weight[i];
}
} if (i < n)
x[i] = u / weight[i];
tp = tp + (x[i] * profit[i]);
printf("\nThe result vector is:- ");
for (i = 0; i < n; i++)
printf("%f\t", x[i]);
printf("\nMaximum profit is:- %f", tp);
}
int main() {
float weight[20], profit[20], capacity;
int num, i, j; float
ratio[20], temp;
printf("\nEnter the no. of objects:- ");
scanf("%d", &num);
near[i]=k;
}
near[k]=near[l]=0;
for i=2 to v-1 do
{
Let j be an index such that near[j]!=0 and
cost[j,near[j]] is minimum; t[i,1]=j;
t[1,2]=near[j];
min.cost=min.cost+cost[j,near[j]];
near[j]=0; for k=1 to v do
if (near[k]!=0 and cost[k,near[k]]>cost[k,j])
then near[k]=j;
}
return min.cost;
}
C PROGRAM:
#include<stdio.h>
#include<conio.h>
int n, cost[10][10];
void prim() {
int i, j, startVertex, endVertex;
int k, nr[10], temp, minimumCost = 0, tree[10][3];
int temp = cost[0][0];
for (i = 0; i < n; i++)
{
k = j;
}
}
tree[i][0] = k;
tree[i][1] = nr[k]; tree[i][2] = cost[k][nr[k]];
minimumCost = minimumCost + cost[k][nr[k]];
nr[k] = 100;
for (j = 0; j < n; j++)
{
if (nr[j] != 100 && cost[j][nr[j]] > cost[j][k])
nr[j] = k;
}
temp = 99;
}
printf("\nThe min spanning tree is:\n");
for (i = 0; i < n - 1; i++)
{
for (j = 0; j < 3; j++)
printf("%d\t", tree[i][j]);
printf("\n");
}
printf("\nMin cost : %d", minimumCost);
}
void main() {
int i, j;
clrscr();
printf("Enter the no. of vertices :");
scanf("%d", &n);
printf("\nEnter the costs of edges in matrix form :\n");
for (i = 0; i < n; i++)
p=1;
min.cost=0.0;
for i=1 to E do
{
j=Find(edgelist[i].vertex1);
k=Find(edgelist[i].vertex2);
if(j!=k)
{
t[p][1]=edgelist[i].vertex1;
t[p][2]=edgelist[i].vertex2;
p=p+1;
min.cost=mincost+edgelist[i].cost;
union(j,k);
}
}
if (p!=(v-1)) then print: No spanning tree; else
return min.cost;
}
C PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h> int
i,j,k,a,b,u,v,n,ne=1;
int min,mincost=0,cost[9][9],parent[9];
int find(int); int uni(int,int);
void main()
{
clrscr();
printf("\nEnter the no. of vertices: ");
scanf("%d",&n);
printf("\nEnter the cost adjacency matrix:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=999;
}
}
printf("The edges of Minimum Cost Spanning Tree are:\n");
while(ne < n)
{
for(i=1,min=999;i<=n;i++)
{
for(j=1;j <= n;j++)
{
if(cost[i][j] < min)
{
min=cost[i][j];
a=u=i;
b=v=j;
}
}
}
u=find(u);
v=find(v);
if(uni(u,v))
{
printf("%d edge (%d,%d) =%d\n",ne++,a,b,min);
mincost +=min;
}
cost[a][b]=cost[b][a]=999;
}
printf("\nMinimum cost = %d\n",mincost);
getch();
}
int find(int i)
{
while(parent[i])
i=parent[i];
return i;
}
int uni(int i,int j)
{
if(i!=j)
{
parent[j]=i;
return 1;
}
return 0;
}
Algo_Place (k,i)
{
for j=1 to k-1 do
if((x[j]=i) or (Abs(x[j]-i)=Abs(j-i))) then return FALSE;
return TRUE;
}
C PROGRAM:
#include<stdio.h>
#include<stdlib.h>
#include<math.h> int
chess_board[20], count; int
display(int); void
queen_function(int, int); int
placeholder(int, int);
void queen_function(int row_value, int limit)
{
int column_value;
for(column_value = 1; column_value <= limit;
column_value++)
{
if(placeholder(row_value, column_value))
{
chess_board[row_value] = column_value;
if(row_value == limit)
{
display(limit);
}
else
{
queen_function(row_value + 1, limit);
}
}
}
}
int placeholder(int row_value, int column_value)
{
int count;
for(count = 1; count <= row_value - 1; count++)
{
if(chess_board[count] == column_value)
{
return 0;
}
else
{
if(abs(chess_board[count] - column_value) ==
abs(count - row_value))
{
return 0;
}
}
}
return 1;
}
int display(int limit)
{
int m, n;
printf("\n\n\tPossible Solution %d:\n\n", ++count);
for(m = 1; m <= limit; m++)
{
printf("\t[%d]", m);
}
for(m = 1; m <= limit; m++)
{
printf("\n\n[%d]", m);
for(n = 1; n <= limit; n++)
{
if(chess_board[m] == n)
{
printf("\tQ");
}
else
{
printf("\t*");
}
}
}
}
void main()
{
int limit;
printf("\nEnter Number of Queens:\t");
scanf("%d", &limit);
if(limit <= 3)
{
printf("\nNumber should be greater than 3 to form a
Matrix\n");
}
else
{
queen_function(1, limit);
}
printf("\n\n");
}
}
for k=1 to n do
{
for i=1 to n do
{
A[i][j]=min(A[i][j], A[i][k]+ A[k][j]);
}
}}
C PROGRAM:
#include<stdio.h>
#include<stdlib.h>
#define MAX 10
void floyd(int);
int w[MAX][MAX], d[MAX][MAX][MAX];
void main()
{
int i, j,k,v;
clrscr();
printf("Enter the no. of vertices in the graph: \n");
scanf("%d",&v);
printf("Enter the weight matrix: \n");
for(i=1;i<=v;i++)
{
for(j=1;j<=v;j++)
scanf("%d",&w[i][j]);
}
floyd(v);
getch();
}
void floyd(int v)
{
int k, i,j;
k=0; for(i=1;i<=v;i++)
{
for(j=1;j<=v;j++)
d[k][i][j]=w[i][j];
}
for(k=1;k<=v;k++)
{
for(i=1;i<=v;i++)
{
for(j=1;j<=v;j++)
d[k][i][j]=min(d[k-1][i][j], d[k-1][i][k]+ d[k1][k][j]);
}
}
for(k=0;k<=v;k++){
printf(" k=%d \n",k);
for(i=1;i<=v;i++)
{
printf("\n");
for(j=1;j<=v;j++)
printf("\t %d",d[k][i][j]);
}
printf("\n \n ");
}
}
m[i][j]=q;
s[i][j]=k;
}
}
}
}
}
C PROGRAM:
#include<stdio.h>
int chain(int index[20][20],int chain_matrix[],int i,int j);
void print_order(int index[20][20],int i,int j);
int main()
{
int size,i,value; int
chain_matrix[100];
int index[20][20];
printf("Enter the number of matrices: ");
scanf("%d",&size);
printf("\nEnter the dimensions of %d matrices:\n",size);
printf("\n");
for(i=0;i<size+1;i++)
{
printf("Enter the dimensions of the matrix: ");
scanf("%d",&chain_matrix[i]);
}
value=chain(index,chain_matrix,1,size);
printf("\nNumber of multiplication: %d\n\n",value);
printf("\nThe Optimal order is: ");
print_order(index,1,size);
getch();
return 0;
}
int chain(int index[20][20],int chain_matrix[],int i,int j)
{
if(i==j) return 0;
int k,count; int min=1000000;
for (k=i;k<j;k++)
{
count=chain(index,chain_matrix,i,k)+chain(index,
chain_matrix,k+1,j)+chain_matrix[i1]*chain_matrix[k]*
chain_matrix[j];
if(count<min)
{
min=count;
index[i][j]=k;
}
}
return min;
}
void print_order(int index[20][20],int i,int j)
{
if(i==j)
printf("M%d",i);
else
{
printf("(");
print_order(index,i,index[i][j]);
print_order(index,index[i][j]+1,j);
printf(")");
}
}
if
(a[m]=x)
return m;
if (a[m]>x)
return Binary_Search (a, x, l, m-1);
else
return Binary_Search (a, x, m+1, h);
}
C PROGRAM:
#include<stdio.h>
#include<stdlib.h>
#define size 10
int binsearch(int[], int, int, int);
int main()
{
int num, i, key, position;
int low, high, list[size];
printf("\nEnter the total number of elements:\n");
scanf("%d", &num);
printf("\nEnter the elements of list:\n");
for (i = 0; i < num; i++)
{
scanf("%d", &list[i]);
}
low = 0; high = num - 1;
printf("\nEnter element to be searched:\n");
scanf("%d", &key);
position = binsearch(list, key, low, high);
if (position != -1) {
printf("\nNumber present at %d", (position + 1));
} else
printf("\n The number is not present in the list");
return (0);
}
int binsearch(int a[], int x, int low, int high)
{ int mid;
if (low > high)
return -1;
{
b[i]=a[j];
h=h+1;
}
else
{
b[i]=a[j];
j=j+1;
}
i=i+1;
}
if(h>mid) then
for k=j to high do
{
b[i]=a[k];
i=i+1;
}
else
for k=h to mid do
{
b[i]=a[k];
i=i+1;
}
for k = low to high do a[k] = b[k];
}
C PROGRAM:
#include<stdio.h>
#include<conio.h> int
a[50];
void merge(int,int,int);
void merge_sort(int low,int high)
{
int mid;
if(low<high){
mid=(low+high)/2;
merge_sort(low,mid);
merge_sort(mid+1,high);
merge(low,mid,high);
}
}
void merge(int low,int mid,int high)
{
int h,i,j,b[50],k; h=low; i=low;
j=mid+1;
while((h<=mid)&&(j<=high))
{
if(a[h]<=a[j])
{
b[i]=a[h];
h++;
}
else{ b[i]=a[j];
j++;
}
i++;
}
if(h>mid){
for(k=j;k<=high;k++)
{
b[i]=a[k];
i++;
}
}
else{
for(k=h;k<=mid;k++)
{
b[i]=a[k];
i++;
}
}
for(k=low;k<=high;k++)
a[k]=b[k];
}
int main()
{
int num,i;
printf("\nEnter the total numbers: ");
scanf("%d",&num);
printf("\nEnter %d numbers: \n",num);
for(i=1;i<=num;i++)
{
scanf("%d",&a[i]);
}
merge_sort(1,num);
printf("\nSORTED ORDER: \n");
for(i=1;i<=num;i++)
printf("\t%d",a[i]);
getch();
}
r=k;
//find position r after which i will be included in j[]
while ((d[j[r]]>d[i])and((d[j[r]]>r))
do r=r-1;
if((d[j[r]]>r)
{
//insert i into j[r+1]
for q=k to r+1 step -1 do
j[q+1]=j[q];
j[r+1]=i;
k=k+1;
}
}
return k;
}
C PROGRAM:
#include <stdio.h>
#define MAX 100
struct Job { int id;
int deadline;
int profit;
};
void jobSequencingWithDeadline(struct Job jobs[], int n);
int minValue(int x, int y) { if(x < y) return x;
return y;
}
void main() {
int i, j;
struct Job jobs[5];
for(i=0;i<5;i++){ printf("\nEnter
job ID: "); scanf("%d",&jobs[i].id);
printf("Enter job deadline: ");
scanf("%d",&jobs[i].deadline);
printf("Enter profit: ");
scanf("%d",&jobs[i].profit);
}
struct Job temp;
int n = 5;
}
}
for(i = 1; i <= dmax; i++) {
timeslot[i] = -1;
}
printf("\nMaximum deadline: %d\n", dmax);
for(i = 1; i <= n; i++)
{
k = minValue(dmax, jobs[i - 1].deadline);
while(k >= 1) {
if(timeslot[k] == -1)
{
timeslot[k] = i-1;
filledTimeSlot++;
break;
}
k--;
}
if(filledTimeSlot == dmax) {
break;
}
}
printf("\nRequired Jobs: ");
for(i = 1; i <= dmax; i++)
{
printf("%d", jobs[timeslot[i]].id);
if(i < dmax)
{
printf(" --> ");
}
}
maxprofit = 0;
for(i = 1; i <= dmax; i++)
{
maxprofit += jobs[timeslot[i]].profit;
}
printf("\n\nMax Profit: %d\n", maxprofit);
}
}//End of Algorithm
C PROGRAM:
#include<stdio.h>
#include<conio.h>
int m=0,n=4;
int cal(int temp[10][10],int t[10][10])
{
int i,j,m=0;
for(i=0;i < n;i++)
for(j=0;j < n;j++)
{
if(temp[i][j]!=t[i][j])
m++;
}
return m;
}
int check(int a[10][10],int t[10][10])
{
int i,j,f=1;
for(i=0;i < n;i++)
for(j=0;j < n;j++)
if(a[i][j]!=t[i][j])
f=0;
return f;
}
void main()
{
int p,i,j,n=4,a[10][10],t[10][10],temp[10][10],r[10][10];
int m=0,x=0,y=0,d=1000,dmin=0,l=0;
clrscr();
printf("\nEnter the matrix to be solved,space with zero :\n");
for(i=0;i < n;i++)
for(j=0;j < n;j++)
scanf("%d",&a[i][j]);
printf("\nEnter the target matrix,space with zero :\n");
for(i=0;i < n;i++)
for(j=0;j < n;j++)
scanf("%d",&t[i][j]);
printf("\nEntered Matrix is :\n");
for(i=0;i < n;i++)
{
for(j=0;j < n;j++)
printf("%d\t",a[i][j]);
printf("\n");
}
printf("\nTarget Matrix is :\n");
for(i=0;i < n;i++)
{
for(j=0;j < n;j++)
printf("%d\t",t[i][j]);
printf("\n");
}
while(!(check(a,t)))
{
l++;
d=1000;
for(i=0;i < n;i++)
for(j=0;j < n;j++)
{
if(a[i][j]==0)
{
x=i;
y=j;
}
}
//To move upwards
for(i=0;i < n;i++)
for(j=0;j < n;j++)
temp[i][j]=a[i][j];
if(x!=0)
{
p=temp[x][y];
temp[x][y]=temp[x-1][y];
temp[x-1][y]=p;
}
m=cal(temp,t);
dmin=l+m;
if(dmin < d)
{
d=dmin;
for(i=0;i < n;i++)
for(j=0;j < n;j++)
r[i][j]=temp[i][j];
}
//To move downwards
for(i=0;i < n;i++)
for(j=0;j < n;j++)
temp[i][j]=a[i][j];
if(x!=n-1)
{
p=temp[x][y];
temp[x][y]=temp[x+1][y];
temp[x+1][y]=p;
}
m=cal(temp,t);
dmin=l+m;
if(dmin < d)
{
d=dmin;
for(i=0;i < n;i++)
for(j=0;j < n;j++)
r[i][j]=temp[i][j];
}
//To move right side
for(i=0;i < n;i++)
for(j=0;j < n;j++)
temp[i][j]=a[i][j];
if(y!=n-1)
{
p=temp[x][y];
temp[x][y]=temp[x][y+1];
temp[x][y+1]=p;
}
m=cal(temp,t);
dmin=l+m;
if(dmin < d)
{
d=dmin;
for(i=0;i < n;i++)
for(j=0;j < n;j++)
r[i][j]=temp[i][j];
}
//To move left
for(i=0;i < n;i++)
else
{
//large problem
mid=(i+j)/2;
//solving sub-problems
Max_Min (i,mid,max,min);
Max_Min (mid+1,j,max1,min1);
//combining solutions
if(max<max1) then max=max1;
if(min>min1) then min=min1;
}
}
C PROGRAM:
#include<stdio.h> int
a[50],max,min; void
find(int i,int n){ int
mid,max1,min1; if(i==n)
max=min=a[i];
else
if(i==n-1)
if(a[i]<=a[n])
{
max=a[n];
min=a[i];
}
else
{
max=a[i];
min=a[n];
}
else{
mid=(i+n)/2;
find(i,mid);
max1=max;
min1=min;
find(mid+1,n);
if(max<max1)
max=max1;
if(min>min1) min=min1;
}
}
int main()
{
int i,n;
printf("Enter the size of the array: ");
scanf("%d",&n);
printf("Enter the elements in the array:\n\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
max=min=a[0];
find(0,n-1);
printf("\nMinimum: %d",min);
printf("\nMaximum: %d",max);
return 0;
}