Adalab
Adalab
Program:
#include <stdio.h>
int main()
{
int S[] = {1, 2, 3};
int n = sizeof(S) / sizeof(S[0]);
int d = 3;
int subset[n];
int size = 0;
int sum = 0;
printf("Given items in the SET: ");
for(int i=0;i<n;i++)
printf("%d ",S[i]);
printf("\nDesirable 'SUM' : %d",d);
printf("\nFollowing are the Subsets found: \n");
findSubset(S, n, d, subset, size, sum);
if (size == 0)
{
printf("No more subsets found.\n");
}
return 0;
}
Output:
————————————————————————————————————————
Experiment-10
Program:
#define N 4
#include <stdbool.h>
#include <stdio.h>
return true;
}
// Consider this column and try placing this queen in all rows
one by one
for (int i = 0; i < N; i++) {
board[i][col] = 0; // BACKTRACK
}
}
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;
}
Output:
. . Q .
Q . . .
. . . Q
. Q . .
Experiment-8
Program: 0/1 Knapsack
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n, m, i;
printf("Enter the number of objects: ");
scanf("%d", &n);
int pt[n], wt[n];
printf("Enter the maximum capacity of the knapsack: ");
scanf("%d", &m);
printf("Enter the Weights of %d objects:\n",n);
for(i=0; i<n; i++)
scanf("%d", &wt[i]);
printf("Enter the Profits of %d objects:\n",n);
for(i=0; i<n; i++)
scanf("%d", &pt[i]);
printf("The maximum Profit that can be earned is: %d\n",
knapsackDP(n, m, wt, pt));
return 0;
}
Output:
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 10 10 10 10 10 10
0 0 0 0 40 40 40 40 40 50 50
0 0 0 0 40 40 40 40 40 50 70
0 0 0 50 50 50 50 90 90 90 90
Object 4 included
Object 3 not included
Object 2 included
Object 1 not included
Program:
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
dist[src] = 0;
// driver's code
int main()
{
int graph[V][V] = { { 0, 4, 0, 0, 0, 0, 0, 8, 0 },
{ 4, 0, 8, 0, 0, 0, 0, 11, 0 },
{ 0, 8, 0, 7, 0, 4, 0, 0, 2 },
{ 0, 0, 7, 0, 9, 14, 0, 0, 0 },
{ 0, 0, 0, 9, 0, 10, 0, 0, 0 },
{ 0, 0, 4, 14, 10, 0, 2, 0, 0 },
{ 0, 0, 0, 0, 0, 2, 0, 1, 6 },
{ 8, 11, 0, 0, 0, 0, 1, 0, 7 },
{ 0, 0, 2, 0, 0, 0, 6, 7, 0 } };
dijkstra(graph, 0);
return 0;
}
Experiment-7
Topological Ordering
Program:
#include <stdio.h>
#define MAX 100
int adj[MAX][MAX];
int visited[MAX];
int n;
int order[MAX], idx;
void DFS(int v)
{
visited[v] = 1;
int i;
for(i=0; i<n; i++)
{
if(adj[v][i] && !visited[i])
{
DFS(i);
}
}
order[--idx] = v;
}
void topological_sort()
{
int i;
idx = n;
for(i=0; i<n; i++)
{
visited[i] = 0;
}
for(i=0; i<n; i++)
{
if(!visited[i])
{
DFS(i);
}
}
}
int main()
{
int i, j;
printf("Enter the number of vertices: ");
scanf("%d", &n);
printf("Enter the adjacency matrix:\n");
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
scanf("%d", &adj[i][j]);
}
}
topological_sort();
printf("Topological ordering of vertices:\n");
for(i=0; i<n; i++)
{
printf("%d ", order[i]);
}
printf("\n");
return 0;
}