0% found this document useful (0 votes)
18 views11 pages

Adalab

The document contains multiple programming experiments demonstrating various algorithms, including subset sum, N-Queens, 0/1 Knapsack, Dijkstra's shortest path, and topological sorting. Each experiment includes a C program that implements the algorithm, along with example outputs illustrating the results. The programs showcase fundamental concepts in algorithm design and problem-solving techniques.

Uploaded by

Sahana Ks
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views11 pages

Adalab

The document contains multiple programming experiments demonstrating various algorithms, including subset sum, N-Queens, 0/1 Knapsack, Dijkstra's shortest path, and topological sorting. Each experiment includes a C program that implements the algorithm, along with example outputs illustrating the results. The programs showcase fundamental concepts in algorithm design and problem-solving techniques.

Uploaded by

Sahana Ks
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Experiment-9

Program:
#include <stdio.h>

void findSubset(int S[], int n, int d, int subset[], int


size, int sum);

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;
}

void findSubset(int S[], int n, int d, int subset[], int


size, int sum)
{
if (sum == d)
{
printf("{");
for (int i = 0; i<size; i++)
{
printf("%d", subset[i]);
if (i < size-1)
{
printf(",");
}
}
printf("}\n");
return;
}
if (sum > d || n == 0)
{
return;
}
subset[size] = S[0];
findSubset(S+1, n-1, d, subset, size+1, sum+S[0]);
findSubset(S+1, n-1, d, subset, size, sum);
}

Output:

Given items in the SET: 1 2 5 6 8


Desirable 'SUM' : 9
Following are the Subsets found:
{1,2,6}
{1,8}
No more subsets found.

————————————————————————————————————————

Experiment-10

Program:

#define N 4
#include <stdbool.h>
#include <stdio.h>

// A utility function to print solution


void printSolution(int board[N][N])
{
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if(board[i][j])
printf("Q ");
else
printf(". ");
}
printf("\n");
}
}

// A utility function to check if a queen can be placed on


board[row][col].
bool isSafe(int board[N][N], int row, int col)
{
int i, j;

// Check this row on left side


for (i = 0; i < col; i++)
if (board[row][i])
return false;
// Check upper diagonal on left side
for (i = row, j = col; i >= 0 && j >= 0; i--, j--)
if (board[i][j])
return false;

// Check lower diagonal on left side


for (i = row, j = col; j >= 0 && i < N; i++, j--)
if (board[i][j])
return false;

return true;
}

// A recursive utility function to solve N


// Queen problem
bool solveNQUtil(int board[N][N], int col)
{
// Base case: If all queens are placed then return true
if (col >= N)
return true;

// Consider this column and try placing this queen in all rows
one by one
for (int i = 0; i < N; i++) {

// Check if the queen can be placed on board[i][col]


if (isSafe(board, i, col)) {

// Place this queen in board[i][col]


board[i][col] = 1;

// Recur to place rest of the queens


if (solveNQUtil(board, col + 1))
return true;

board[i][col] = 0; // BACKTRACK
}
}

// If the queen cannot be placed in any row in


// this column col then return false
return false;
}

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;
}

// Driver program to test above function


int main()
{
printf("Solution for 4-Queens Problem:\n\n");
solveNQ();
return 0;
}

Output:

Solution for 4-Queens Problem:

. . Q .
Q . . .
. . . Q
. Q . .
Experiment-8
Program: 0/1 Knapsack

#include <stdio.h>
#include <stdlib.h>

int max(int a, int b)


{
if (a > b)
return a;
else
return b;
}

void printObj(int n, int w, int dp[][w+1], int wt[])


{
int i = n, j = w;
printf("\nFollowing is the list of objects which are included
and not included in the Knapsack to earn maximum profit-\n\n");
while(i>0 && j>0)
{
if (dp[i][j] == dp[i-1][j])
{
printf("Object %d not included\n",i);
i--;
}
else
{
printf("Object %d included\n",i);
i--;
j = j - wt[i];
}
}
printf("\n");
}

int knapsackDP(int n, int m, int wt[], int pt[])


{
int i, j;
int kn_tbl[n+1][m+1];
for(i=0; i<=n; i++)
{
for(j=0; j<=m; j++)
{
if(i == 0 || j == 0)
kn_tbl[i][j] = 0;
else if(wt[i-1] <= j)
kn_tbl[i][j] = max(pt[i-1]+kn_tbl[i-1][j-wt[i-1]],
kn_tbl[i-1][j]);
else
kn_tbl[i][j] = kn_tbl[i-1][j];
}
}
printf("\nFinal Table with profit values:\n\n");
for(i=0;i<=n;i++)
{
for(j=0;j<=m;j++)
{
printf("%d\t",kn_tbl[i][j]);
}
printf("\n");
}
printObj(n, m, kn_tbl, wt);
return kn_tbl[n][m];
}

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:

Enter the number of objects: 4


Enter the maximum capacity of the knapsack: 10
Enter the Weights of 4 objects:
5
4
6
3
Enter the Profits of 4 objects:
10
40
30
50
Final Table with profit values:

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

Following is the list of objects which are included and not


included in the Knapsack to earn maximum profit-

Object 4 included
Object 3 not included
Object 2 included
Object 1 not included

The maximum Profit that can be earned is: 90


Experiment-6

Dijkstra’s Single source shortest path Algorithm

Program:
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>

// Number of vertices in the graph


#define V 9

int minDistance(int dist[], bool sptSet[])


{
// Initialize min value
int min = INT_MAX, min_index;

for (int v = 0; v < V; v++)


if (sptSet[v] == false && dist[v] <= min)
{
min = dist[v];
min_index = v;
}
return min_index;
}

void printSolution(int dist[])


{
printf("Vertex \t\t Distance from Source\n");
for (int i = 0; i < V; i++)
printf("%d \t\t\t\t %d\n", i, dist[i]);
}

void dijkstra(int graph[V][V], int src)


{
int dist[V];
bool sptSet[V];
for (int i = 0; i < V; i++)
{
dist[i] = INT_MAX;
sptSet[i] = false;
}

dist[src] = 0;

for (int count = 0; count < V - 1; count++)


{
int u = minDistance(dist, sptSet);
sptSet[u] = true;

for (int v = 0; v < V; v++)


if (!sptSet[v] && graph[u][v] && dist[u] !=
INT_MAX && dist[u] + graph[u][v] < dist[v])
dist[v] = dist[u] + graph[u][v];
}
// print the constructed distance array
printSolution(dist);
}

// 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;
}

You might also like