Untitled Copy
Untitled Copy
#include <stdio.h>
#include <limits.h>
#define MAX 20
#define INF INT_MAX
int n;
int distance[MAX][MAX];
int cost_table[1 << MAX][MAX];
int travelling_salesman(int mask, int current) {
if (mask == (1 << n) - 1)
return distance[current][0];
if (cost_table[mask][current] != -1)
return cost_table[mask][current];
int min_cost = INF;
for (int city = 0; city < n; city++) {
if (!(mask & (1 << city))) {
int new_cost = distance[current][city] +
travelling_salesman(mask | (1 << city), city);
if (new_cost < min_cost)
min_cost = new_cost;
}
}
return cost_table[mask][current] = min_cost;
}
int main() {
printf("Enter the number of places: ");
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", &distance[i][j]);
for (int i = 0; i < (1 << n); i++)
for (int j = 0; j < n; j++)
cost_table[i][j] = -1;
printf("The minimum cost to visit all places and return to the starting
point is: %d\n", travelling_salesman(1, 0));
return 0;
}
*****
2)Sum of subset:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int subset[MAX];
int sum;
int level;
};
if (method == 'F') {
currentNode = queue[front++];
} else {
currentNode = queue[--rear];
}
if (currentNode.sum == targetSum) {
printf("Subset with sum %d: { ", targetSum);
for (int i = 0; i < currentNode.level; i++) {
if (currentNode.subset[i]) {
printf("%d ", set[i]);
}
}
printf("}\n");
solutionFound = 1;
if (method == 'L') break;
}
if (currentNode.level < n) {
struct Node child1 = currentNode;
child1.level++;
queue[rear++] = child1;
if (!solutionFound) {
printf("No subset with sum %d found.\n", targetSum);
}
}
int main() {
int set[MAX];
int n;
int targetSum;
*****
#include <stdio.h>
#include <stdlib.h>
int main() {
struct Graph* graph = (struct Graph*)malloc(sizeof(struct Graph));
graph->V = 7; // Number of vertices
graph->E = 9; // Number of edges
return 0;
}
*****
****
5)Knapsack PROBLEM:
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int weight;
int profit;
float ratio; // profit-to-weight ratio
} Item;
int i;
float totalProfit = 0.0;
for (i = 0; i < n; i++) {
if (capacity == 0)
break;
if (items[i].weight <= capacity) {
totalProfit += items[i].profit;
capacity -= items[i].weight;
} else {
totalProfit += items[i].profit * ((float)capacity / items[i].weight);
capacity = 0;
}
}
return totalProfit;
}
int main() {
int n;
printf("Enter the number of items: ");
scanf("%d", &n);
Item items[n];
int capacity;
printf("Enter the weights and profits of each item:\n");
for (int i = 0; i < n; i++) {
printf("Item %d - Weight: ", i + 1);
scanf("%d", &items[i].weight);
printf("Item %d - Profit: ", i + 1);
scanf("%d", &items[i].profit);
items[i].ratio = (float)items[i].profit / items[i].weight;
}
printf("Enter the capacity of the knapsack: ");
scanf("%d", &capacity);
printf("Maximum profit (using profit-to-weight ratio): %.2f\n",
fractionalKnapsack(capacity, items, n, compareByRatio));
printf("Maximum profit (using profit): %.2f\n",
fractionalKnapsack(capacity, items, n, compareByProfit));
printf("Maximum profit (using weight): %.2f\n",
fractionalKnapsack(capacity, items, n, compareByWeight));
return 0;
}
*****
6)Graph coloring:
#include <stdio.h>
#define MAX_V 100
int graph[MAX_V][MAX_V];
int colour[MAX_V];
int n;
int graphcolouring(int m) {
for (int i = 0; i < n; i++) {
colour[i] = 0;
}
if (!graphcolouruntil(m, 0)) {
printf("Solution does not exist\n");
return 0;
}
printf("\nSolution exists\n");
for (int i = 0; i < n; i++) {
printf("Vertex %d ----> Colour %d\n", i + 1, colour[i]);
}
return 1;
}
int main() {
printf("22BBS0256 - Sanjai K\n");
int c;
printf("Enter the number of vertices: ");
scanf("%d", &n);
graphcolouring(c);
return 0;
}
*****
7)Ford fulkerson:
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#define MAX_NODES 1000
#define INF 1000000000
int capacity[MAX_NODES][MAX_NODES];
int flow[MAX_NODES][MAX_NODES];
int n;
queue[rear++] = source;
visited[source] = true;
parent[source] = -1;
max_flow += path_flow;
}
return max_flow;
}
int main() {
printf("22BBS0256 - Sanjai K\n");
n = 9; // Number of nodes
memset(capacity, 0, sizeof(capacity)); // Initialize capacity matrix to
0
*****
8)Djikstra algorithm:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#define V 7
int main() {
int graph[V][V] = {
{0, 3, 0, 2, 0, 0, 6},
{0, 0, 6, 0, 1, 0, 0},
{0, 0, 0, 0, 0, 1, 0},
{0, 2, 0, 0, 3, 0, 0},
{0, 0, 0, 0, 0, 4, 0},
{0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 2, 0}
};
char startNode = 'S';
int src = mapNode(startNode);
dijkstra(graph, src);
return 0;
}
*****
9)Bellman ford:
#include <stdio.h>
#include <limits.h>
struct Edge {
int src, dest, weight;
};
int main() {
printf("22BBS0256 - Sanjai K\n");
bellmanFord(edges, 0);
return 0;
}