ADA Lab
ADA Lab
for(i=0;i<n;i++)
ratio[i]=profit[i]/weight[i];
temp = weight[j];
weight[j] = weight[i];
weight[i] = temp;
temp = profit[j];
profit[j] = profit[i];
profit[i] = temp;
}
output:-
Enter the number of items :4
Enter Weight and Profit for item[0] :
2
12
Enter Weight and Profit for item[1] :
1
10
Enter Weight and Profit for item[2] :
3
20
Enter Weight and Profit for item[3] :
2
15
Enter the capacity of knapsack :
5
Knapsack problems using Greedy Algorithm:
// A utility function to check if a queen can be placed on board[row][col]. Note that this
// function is called when "col" queens are already placed in columns from 0 to col -1.
// So we need to check only left side for attacking queens
bool isSafe(int board[N][N], int row, int col)
{
int i, j;
return true;
}
// Consider this column and try placing this queen in all rows one by one
for (int i = 0; i < N; i++) {
//If placing queen in board[i][col] doesn't lead to a solution, then remove queen from board[i][col]
board[i][col] = 0; // BACKTRACK
}
}
// This function solves the N Queen problem using Backtracking. It mainly uses solveNQUtil() to
// solve the problem. It returns false if queens cannot be placed, otherwise, return true and
// prints placement of queens in the form of 1s . Please note that there may be more than one
// solutions, this function prints one of the feasible solutions.
bool solveNQ()
{
int board[N][N] = { { 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 } };
if (solveNQUtil(board, 0) == false) {
printf("Solution does not exist");
return false;
}
printSolution(board);
return true;
}
#include<stdio.h>
#include<conio.h>
#include<math.h>
int a[30],count=0;
int place(int pos)
{
int i;
for (i=1;i<pos;i++)
{
if((a[i]==a[pos])||((abs(a[i]-a[pos])==abs(i-pos))))
return 0;
}
return 1;
}
void print_sol(int n)
{
int i,j;
count++;
printf("\n\nSolution #%d:\n",count);
for (i=1;i<=n;i++)
{
for (j=1;j<=n;j++)
{
if(a[i]==j)
printf("Q\t");
else printf("*\t");
}
printf("\n");
}
}
void queen(int n)
{
int k=1;
a[k]=0;
while(k!=0)
{
a[k]=a[k]+1;
while((a[k]<=n)&&!place(k))
a[k]++;
if(a[k]<=n)
{
if(k==n)
print_sol(n);
else
{
k++;
a[k]=0;
}
}
else k--;
}
}
void main()
{
int i,n;
printf("Enter the number of Queens\n");
scanf("%d",&n);
queen(n);
printf("\nTotal solutions=%d",count);
getch();
}
Output:-
Enter the number of Queens
4
Solution #1:
* Q * *
* * * Q
Q * * *
* * Q *
Solution #2:
* * Q *
Q * * *
* * * Q
* Q * *
Total solutions=2
// WRITE A PROGRAM TO IMPLEMENT GREEDY ALGORITHM FOR JOB SEQUENCING WITH DEADLINES
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
// Driver's code
int main()
{
Job arr[] = { { 'a', 2, 100 },
{ 'b', 1, 19 },
{ 'c', 2, 27 },
{ 'd', 1, 25 },
{ 'e', 3, 15 } };
int n = sizeof(arr) / sizeof(arr[0]);
printf(
"Following is maximum profit sequence of jobs \n");
// Function call
printJobScheduling(arr, n);
return 0;
}
Output:-
Following is maximum profit sequence of jobs
cae
Process returned 0 (0x0) execution time : 0.100 s
OR
#include <stdio.h>
#define MAX 100
typedef struct Job
{
char id[5];
int deadline;
int profit;
} Job;
int main(void)
{
//variables
int i, j;
//temp
Job temp;
//number of jobs
int n = 5;
jobSequencingWithDeadline(jobs, n);
return 0;
}
//required jobs
printf("\nRequired Jobs: ");
for(i = 1; i <= dmax; i++)
{
printf("%s", jobs[timeslot[i]].id);
//required profit
maxprofit = 0;
for(i = 1; i <= dmax; i++)
{
maxprofit += jobs[timeslot[i]].profit;
}
printf("\nMax Profit: %d\n", maxprofit);
}
void main()
{
char ele[MAX][MAX];
int w[MAX][MAX], c[MAX][MAX], r[MAX][MAX], p[MAX], q[MAX];
int temp=0, root, min, min1, n;
int i,j,k,b;
clrscr();
printf("Enter the number of elements:");
scanf("%d",&n);
printf("\n");
for(i=1; i <= n; i++)
{
printf("Enter the Element of %d:",i);
scanf("%d",&p[i]);
}
printf("\n");
for(i=0; i <= n; i++)
{
printf("Enter the Probability of %d:",i);
scanf("%d",&q[i]);
}
printf("W\t\tC\t\tR\n");
for(i=0; i <= n; i++)
{
for(j=0; j <= n; j++)
{
if(i == j)
{
w[i][j] = q[i];
c[i][j] = 0;
r[i][j] = 0;
printf("W[%d][%d]: %d\tC[%d][%d]: %d\tR[%d][%d]: %d\n",i,j,w[i][j],i,j,c[i][j],i,j,r[i][j]);
}
}
}
printf("\n");
for(b=0; b < n; b++)
{
for(i=0,j=b+1; j < n+1 && i < n+1; j++,i++)
{
}
printf("\n");
}
printf("Minimum cost = %d\n",c[0][n]);
root = r[0][n];
printf("Root = %d \n",root);
getch();
}
/* Output
Enter the number of elements: 4
Minimum cost = 32
Root = 2
OR
// A naive recursive implementation of optimal binary
// search tree problem
#include <stdio.h>
#include <limits.h>
OUTPUT:-
Cost of Optimal BST is 142
Process returned 0 (0x0) execution time : 0.047 s