0% found this document useful (0 votes)
19 views

DAA_9-12

Daa

Uploaded by

shivadubey2002
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)
19 views

DAA_9-12

Daa

Uploaded by

shivadubey2002
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
You are on page 1/ 10

PROGRAM-9

AIM: Write a program to implement fractional knapsack problem


using Greedy strategy.
#include <stdio.h>
typedef struct {
int weight;
int value;
float ratio;
} Item;
void calculateRatio(Item items[], int n) {
for (int i = 0; i < n; i++) {
items[i].ratio = (float)items[i].value / items[i].weight;
}}
int compare(const void *a, const void *b) {
Item *item1 = (Item *)a;
Item *item2 = (Item *)b;
if (item1->ratio > item2->ratio) {
return -1;
} else if (item1->ratio < item2->ratio) {
return 1; }
return 0;}
float fractionalKnapsack(Item items[], int n, int capacity) {
qsort(items, n, sizeof(Item), compare);
int currentWeight = 0;
float totalValue = 0.0;
for (int i = 0; i < n; i++) {
if (currentWeight + items[i].weight <= capacity) {
currentWeight += items[i].weight;
totalValue += items[i].value;
} else {
int remainingWeight = capacity - currentWeight;
totalValue += items[i].value * ((float)remainingWeight / items[i].weight);
break;
}}
return totalValue;}
int main() {
int n, capacity
printf("Enter number of items: ");
scanf("%d", &n);
printf("Enter knapsack capacity: ");
scanf("%d", &capacity);
Item items[n];
for (int i = 0; i < n; i++) {
printf("Enter weight and value of item %d: ", i + 1);
scanf("%d %d", &items[i].weight, &items[i].value);}
calculateRatio(items, n);
float maxValue = fractionalKnapsack(items, n, capacity)
printf("Maximum value that can be obtained: %.2f\n", maxValue);
return 0;
}

OUTPUT:
PROGRAM – 10
AIM: Write a program to implement travelling salesperson problem.
#include <stdio.h>

#include <limits.h>

#define MAX_CITIES 10

int n;

int dist[MAX_CITIES][MAX_CITIES];

int calculateTourLength(int tour[]) {

int totalDistance = 0;

for (int i = 0; i < n - 1; i++) {

totalDistance += dist[tour[i]][tour[i + 1]];

totalDistance += dist[tour[n - 1]][tour[0]];

return totalDistance;}

void tsp(int tour[], int pos, int *minDistance, int bestTour[]) {

if (pos == n) {

int currentDistance = calculateTourLength(tour);

if (currentDistance < *minDistance) {

*minDistance = currentDistance;

for (int i = 0; i < n; i++) {

bestTour[i] = tour[i];

return;}

for (int i = pos; i < n; i++) {

int temp = tour[pos];

tour[pos] = tour[i];

tour[i] = temp;

tsp(tour, pos + 1, minDistance, bestTour);

temp = tour[pos];
tour[pos] = tour[i];

tour[i] = temp;

}}

int main() {

printf("Enter number of cities: ");

scanf("%d", &n);

printf("Enter the distance matrix:\n");

for (int i = 0; i < n; i++) {

for (int j = 0; j < n; j++) {

scanf("%d", &dist[i][j]);

int tour[MAX_CITIES], bestTour[MAX_CITIES];

int minDistance = INT_MAX;

for (int i = 0; i < n; i++) {

tour[i] = i;}

tsp(tour, 0, &minDistance, bestTour);

printf("Minimum distance: %d\n", minDistance);

printf("Best tour: ");

for (int i = 0; i < n; i++) {

printf("%d ", bestTour[i]);}

printf("\n");

return 0;}

OUTPUT:
PROGRAM-11
AIM: Write a program to find the minimum cost spanning tree using Kruskal’s algorithm.
#include <stdio.h>
#include <stdlib.h>
#define MAX_EDGES 100
#define MAX_VERTICES 20
typedef struct {
int u, v, weight;
} Edge;
typedef struct {
int parent;
int rank;
} Subset;
int find(Subset subsets[], int i) {
if (subsets[i].parent != i) {
subsets[i].parent = find(subsets, subsets[i].parent);
}
return subsets[i].parent;
}
void unionSets(Subset subsets[], int x, int y) {
int xroot = find(subsets, x);
int yroot = find(subsets, y);
if (xroot != yroot) {
if (subsets[xroot].rank < subsets[yroot].rank) {
subsets[xroot].parent = yroot;
} else if (subsets[xroot].rank > subsets[yroot].rank) {
subsets[yroot].parent = xroot;
} else {
subsets[yroot].parent = xroot;
subsets[xroot].rank++;
}
}
}
int compareEdges(const void *a, const void *b) {
return ((Edge *)a)->weight - ((Edge *)b)->weight;
}
void kruskal(Edge edges[], int V, int E) {
qsort(edges, E, sizeof(Edge), compareEdges);
Subset subsets[V];
for (int i = 0; i < V; i++) {
subsets[i].parent = i;
subsets[i].rank = 0;
}
Edge result[V - 1];
int e = 0;
int i = 0;
while (e < V - 1 && i < E) {
Edge currentEdge = edges[i++];
int x = find(subsets, currentEdge.u);
int y = find(subsets, currentEdge.v);
if (x != y) {
result[e++] = currentEdge;
unionSets(subsets, x, y);
}
}
printf("Minimum Cost Spanning Tree (MST):\n");
int minCost = 0;
for (int i = 0; i < e; i++) {
printf("%d -- %d == %d\n", result[i].u, result[i].v, result[i].weight);
minCost += result[i].weight;
}
printf("Total cost of MST: %d\n", minCost);
}
int main() {
int V, E;
printf("Enter number of vertices: ");
scanf("%d", &V);
printf("Enter number of edges: ");
scanf("%d", &E);
Edge edges[E];
printf("Enter the edges (u, v, weight):\n");
for (int i = 0; i < E; i++) {
scanf("%d %d %d", &edges[i].u, &edges[i].v, &edges[i].weight);
}
kruskal(edges, V, E);
return 0;
}
OUTPUT:
PROGRAM-12
AIM: Write a Program to implement N Queen Problem using Backtracking.
#include <stdio.h>

#include <stdbool.h>

#define MAX 100

int board[MAX][MAX];

int N;

void printBoard() {

for (int i = 0; i < N; i++) {

for (int j = 0; j < N; j++) {

if (board[i][j] == 1)

printf(" Q ");

else

printf(" . ");

printf("\n");

}bool isSafe(int row, int col) {

// Check the column

for (int i = 0; i < row; i++) {

if (board[i][col] == 1) {

return false;

} }

for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) {

if (board[i][j] == 1) {

return false;

for (int i = row - 1, j = col + 1; i >= 0 && j < N; i--, j++) {


if (board[i][j] == 1) {

return false;

return true;

bool solveNQueens(int row) {

if (row == N) {

return true;

for (int col = 0; col < N; col++) {

if (isSafe(row, col)) {

// Place the queen

board[row][col] = 1;

if (solveNQueens(row + 1)) {

return true;

board[row][col] = 0;

return false;

int main() {

printf("Enter the number of queens (N): ");

scanf("%d", &N);

for (int i = 0; i < N; i++) {

for (int j = 0; j < N; j++) {

board[i][j] = 0;
}

if (solveNQueens(0)) {

printf("Solution:\n");

printBoard();

} else {

printf("No solution exists for %d queens.\n", N);

return 0;

OUTPUT:

You might also like