0% found this document useful (0 votes)
3 views18 pages

Daaf

Uploaded by

akhilnakka27
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)
3 views18 pages

Daaf

Uploaded by

akhilnakka27
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/ 18

Improved merge sort if (arr[i] < arr[j])

#include <stdio.h> temp[k++] = arr[i++];

#include <conio.h> else

#include <string.h> temp[k++] = arr[j++];

#define MAX 100 }

#define MAX_LEN 100 while (i <= m)

void merge(void *a, int l, int m, int h, int temp[k++] = arr[i++];


size, int isString) {
while (j <= h)
int i = l, j = m + 1, k = 0;
temp[k++] = arr[j++];
if (isString) {
for (i = l, k = 0; i <= h; i++, k++)
char temp[MAX][MAX_LEN];
arr[i] = temp[k];
char (*arr)[MAX_LEN] = (char
}
(*)[MAX_LEN])a;
}
while (i <= m && j <= h) {
void mergeSort(void *a, int l, int h, int
if (strcmp(arr[i], arr[j]) < 0)
size, int isString) {
strcpy(temp[k++], arr[i++]);
if (l < h) {
else
int m = (l + h) / 2;
strcpy(temp[k++], arr[j++]);
mergeSort(a, l, m, size, isString);
}
mergeSort(a, m + 1, h, size, isString);
while (i <= m)
merge(a, l, m, h, size, isString);
strcpy(temp[k++], arr[i++]);
}
while (j <= h)
}
strcpy(temp[k++], arr[j++]);
int main() {
for (i = l, k = 0; i <= h; i++, k++)
int i, n;
strcpy(arr[i], temp[k]);
char choice;
} else {
int ai[MAX];
int temp[MAX];
char ac[MAX][MAX_LEN];
int *arr = (int *)a;
clrscr();
while (i <= m && j <= h) {
printf("Sort int or string (i/s)? ");

choice = getche();

printf("\nEnter number of elements: ");

scanf("%d", &n);

if (choice == 'i') {

printf("Enter %d integers:\n", n);

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

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

mergeSort(ai, 0, n - 1, sizeof(int), 0);

printf("Sorted Integers: ");

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

printf("%d ", ai[i]);

} else {

printf("Enter %d strings:\n", n);

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

scanf("%s", ac[i]);

mergeSort(ac, 0, n - 1, sizeof(ac[0]),
1);

printf("Sorted Strings: ");

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

printf("%s ", ac[i]);

getch();

return 0;

}
Prim }

#include <stdio.h>

#include <conio.h> visited[0] = 1;

printf("\nEdge\tWeight\n");

#define MAX 10

#define INF 9999 while (ne < n) {

min = INF;

void main() { for (i = 0; i < n; i++) {

int cost[MAX][MAX], visited[MAX]; if (visited[i]) {

int i, j, n, ne, a, b, u, v, min, min_cost; for (j = 0; j < n; j++) {

if (!visited[j] && cost[i][j] <


min) {
clrscr();
min = cost[i][j];

u = i;
for (i = 0; i < MAX; i++)
v = j;
visited[i] = 0;
}

}
min_cost = 0;
}
ne = 1;
}

printf("%d - %d\t%d\n", u + 1, v + 1,
printf("Enter the number of vertices:
cost[u][v]);
");
min_cost += cost[u][v];
scanf("%d", &n);
visited[v] = 1;

ne++;
printf("Enter the adjacency matrix (use
9999 for infinity):\n"); }

for (i = 0; i < n; i++) { printf("\nMinimum Cost = %d",


min_cost);
for (j = 0; j < n; j++) {
getch();
scanf("%d", &cost[i][j]);
}
}
Kruskal’s printf("Enter edge %d (u v w): ", i +
1);
#include <stdio.h>
scanf("%d%d%d", &cost[i][0],
#include <conio.h>
&cost[i][1], &cost[i][2]);
#define MAX 30
}
int parent[MAX];
for (i = 0; i < e; i++) {
int find(int i) {
for (j = i + 1; j < e; j++) {
while (parent[i])
if (cost[i][2] > cost[j][2]) {
i = parent[i];
for (k = 0; k < 3; k++) {
return i;
temp = cost[i][k];
}
cost[i][k] = cost[j][k];
int uni(int i, int j) {
cost[j][k] = temp;
if (i != j) {
}
parent[j] = i;
}
return 1;
}
}
}
return 0;
printf("\nMinimum Cost Spanning Tree
} Edges:\n");

void main() { for (i = 0; i < e; i++) {


int n, e, u, v, i, j, a, b, min, mincost; a = find(cost[i][0]);

int cost[MAX][3]; b = find(cost[i][1]);

int temp, k; if (uni(a, b)) {

clrscr(); printf("(%d, %d) = %d\n",


cost[i][0], cost[i][1], cost[i][2]);
mincost = 0;
mincost += cost[i][2];
printf("Enter number of vertices: ");
}}
scanf("%d", &n);
printf("\nTotal Cost = %d", mincost);
printf("Enter number of edges: ");
getch();
scanf("%d", &e);
}
for (i = 0; i < e; i++) {
Dijikstra visited[src] = 1;

#include <stdio.h> for (i = 1; i < n; i++) {

#include <conio.h> min = INF;

#define MAX 10 u = -1;

#define INF 9999 for (j = 0; j < n; j++) {

void main() { if (!visited[j] && dist[j] < min) {

int cost[MAX][MAX], dist[MAX], min = dist[j];


visited[MAX], path[MAX];
u = j;
int i, j, n, src, min, u;
}
clrscr();
}
printf("Enter the number of vertices: ");
if (u == -1) break; // No more
scanf("%d", &n); reachable vertices

printf("Enter the adjacency matrix (0 for visited[u] = 1;


no edge):\n");
for (j = 0; j < n; j++) {
for (i = 0; i < n; i++) {
if (!visited[j] && dist[u] + cost[u][j]
for (j = 0; j < n; j++) { < dist[j]) {

scanf("%d", &cost[i][j]); dist[j] = dist[u] + cost[u][j];

if (cost[i][j] == 0 && i != j) path[j] = u;

cost[i][j] = INF; }}}

} printf("\nVertex\tDistance\tPrevious\n")
;
}
for (i = 0; i < n; i++) {
printf("Enter the source vertex: ");
if (dist[i] == INF)
scanf("%d", &src);
printf("%d\tINF\t\t-\n", i);
for (i = 0; i < n; i++) {
else
dist[i] = cost[src][i];
printf("%d\t%d\t\t%d\n", i, dist[i],
visited[i] = 0;
path[i]);
path[i] = src;
}
}
getch();}
dist[src] = 0;
Floyd for (k = 0; k < n; k++) {

#include <stdio.h> for (i = 0; i < n; i++) {

#include <conio.h> for (j = 0; j < n; j++) {

#define MAX 10 if (dist[i][k] != INF && dist[k][j] != INF &&

#define INF 32767 dist[i][k] + dist[k][j] < dist[i][j]) {

void printSolution(int dist[MAX][MAX], dist[i][j] = dist[i][k] + dist[k][j];


int n) {
}}}}
int i, j;
printSolution(dist, n);
printf("\nThe following matrix shows
}
the shortest distances between every
pair of vertices:\n"); void main() {

for (i = 0; i < n; i++) { int graph[MAX][MAX];

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

if (dist[i][j] == INF) clrscr();

printf(" INF "); printf("Enter the number of vertices: ");

else scanf("%d", &n);

printf("%4d ", dist[i][j]); printf("Enter the adjacency matrix


(enter 0 if no edge exists):\n");
}
for (i = 0; i < n; i++) {
printf("\n");
for (j = 0; j < n; j++) {
}
scanf("%d", &w);
}
if (i != j && w == 0)
void floydWarshall(int
graph[MAX][MAX], int n) { graph[i][j] = INF;

int dist[MAX][MAX]; else

int i, j, k; graph[i][j] = w;

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

for (j = 0; j < n; j++) { floydWarshall(graph, n);

dist[i][j] = graph[i][j]; getch();

}} }
0/1 knapsack void main() {

#include <stdio.h> int values[MAX_N], weights[MAX_N];

#include <conio.h> int W, n, i;

#define MAX_N 10 int result;

#define MAX_W 50 clrscr();

int knapsack(int W, int weights[], int printf("Enter the number of items: ");
values[], int n) {
scanf("%d", &n);
int dp[MAX_N + 1][MAX_W + 1];
printf("Enter the values of items:\n");
int i, w;
for (i = 0; i < n; i++) {
for (i = 0; i <= n; i++) {
printf("Value[%d]: ", i + 1);
for (w = 0; w <= W; w++) {
scanf("%d", &values[i]);
if (i == 0 || w == 0) {
}
dp[i][w] = 0;
printf("Enter the weights of items:\n");
} else if (weights[i - 1] <= w) {
for (i = 0; i < n; i++) {
if (values[i - 1] + dp[i - 1][w -
printf("Weight[%d]: ", i + 1);
weights[i - 1]] > dp[i - 1][w]) {
scanf("%d", &weights[i]);
dp[i][w] = values[i - 1] + dp[i -
1][w - weights[i - 1]]; }

} else { printf("Enter the capacity of the


knapsack: ");
dp[i][w] = dp[i - 1][w];
scanf("%d", &W);
}
result = knapsack(W, weights, values, n);
} else {
printf("\nMaximum value in knapsack
dp[i][w] = dp[i - 1][w];
of capacity %d = %d", W, result);
}

}
getch();
}
}
return dp[n][W];

}
Bellmanford return;

#include <stdio.h> }}

#include <conio.h> printf("\nVertex\tDistance from


Source\n");
#define MAX 20
for (i = 0; i < V; i++) {
#define INF 9999
if (dist[i] == INF)
void bellmanFord(int graph[][3], int V, int
E, int src) { printf("%d\tINF\n", i);

int dist[MAX]; else

int i, j, u, v, weight; printf("%d\t%d\n", i, dist[i]);

for (i = 0; i < V; i++) }}

dist[i] = INF; void main() {

dist[src] = 0; int graph[MAX][3];

for (i = 1; i <= V - 1; i++) { int V, E, i, src;

for (j = 0; j < E; j++) { clrscr();

u = graph[j][0]; printf("Enter the number of vertices: ");

v = graph[j][1]; scanf("%d", &V);

weight = graph[j][2]; printf("Enter the number of edges: ");

if (dist[u] != INF && dist[u] + scanf("%d", &E);


weight < dist[v]) {
printf("Enter edges as: source
dist[v] = dist[u] + weight; destination weight\n");

}}} for (i = 0; i < E; i++) {

for (j = 0; j < E; j++) { printf("Edge %d: ", i + 1);

u = graph[j][0]; scanf("%d%d%d", &graph[i][0],


&graph[i][1], &graph[i][2]);}
v = graph[j][1];
printf("Enter the source vertex: ");
weight = graph[j][2];
scanf("%d", &src);
if (dist[u] != INF && dist[u] + weight <
dist[v]) { bellmanFord(graph, V, E, src);

printf("\nGraph contains a getch();


negative weight cycle");
}
tsp }

#include <stdio.h> void printPath() {

#include <conio.h> int mask = 1, pos = 0, nextCity;

#define MAX 10 printf("\nOptimal Path: %d", pos);

#define INF 9999 while (mask != (1 << V) - 1) {

int graph[MAX][MAX], dp[1 << nextCity = path[mask][pos];


MAX][MAX], path[1 << MAX][MAX];
printf(" -> %d", nextCity);
int V;
mask |= (1 << nextCity);
int tsp(int mask, int pos) {
pos = nextCity;
int city, newMask, newCost;
}
if (mask == (1 << V) - 1)
printf(" -> 0\n");
return graph[pos][0];
}
if (dp[mask][pos] != -1)
void init() {
return dp[mask][pos];
int i, j;
dp[mask][pos] = INF;
for (i = 0; i < (1 << V); i++)
path[mask][pos] = -1;
for (j = 0; j < V; j++) {
for (city = 0; city < V; city++) {
dp[i][j] = -1;
if ((mask & (1 << city)) == 0) {
path[i][j] = -1;
newMask = mask | (1 << city);
}}
newCost = graph[pos][city] +
void main() {
tsp(newMask, city);
int i, j, minCost;
printf("Visited Mask: %2d (0x%X),
Current City: %d, Next City: %d, Cost: clrscr();
%d\n", mask, mask, pos, city, newCost);
printf("Enter number of cities: ");
if (newCost < dp[mask][pos]) {
scanf("%d", &V);
dp[mask][pos] = newCost;
if (V > MAX) {
path[mask][pos] = city;
printf("Maximum cities supported:
} } } %d", MAX);

return dp[mask][pos]; getch();


return;

printf("Enter the cost matrix (use %d for


INF):\n", INF);

for (i = 0; i < V; i++) {

for (j = 0; j < V; j++) {

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

init();

minCost = tsp(1, 0); // Start from city 0

printf("\nMinimum cost of TSP: %d\n",


minCost);

printPath(); // Show optimal path

getch();

}
N queen

#include <stdio.h> return 1;}

#include <conio.h> int solveNQueens(int row) {

#define MAX 15 int col;

int board[MAX][MAX]; if (row >= N)

int N; return 1;

void printBoard() { for (col = 0; col < N; col++) {

int i, j; if (isSafe(row, col)) {

printf("\nSolution to the %d-Queens board[row][col] = 1;


problem:\n", N);
if (solveNQueens(row + 1))
for (i = 0; i < N; i++) {
return 1;
for (j = 0; j < N; j++) {
board[row][col] = 0; // Backtrack
printf("%d ", board[i][j]);
}}
}
return 0;}
printf("\n");
void main() {
}}
int i, j;
int isSafe(int row, int col) {
clrscr();
int i, j;
printf("Entervalue of N (1 to %d): ",MAX);
for (i = 0; i < row; i++)
scanf("%d", &N);
if (board[i][col] == 1)
if (N < 1 || N > MAX) {
return 0;
printf("Invalid board size.\n");
for (i = row, j = col; i >= 0 && j >= 0; i--,
getch();
j--)
return;}
if (board[i][j] == 1)
for (i = 0; i < N; i++)
return 0;
for (j = 0; j < N; j++)
for (i = row, j = col; i >= 0 && j < N; i--,
j++) board[i][j] = 0;

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

return 0; printBoard();
else

printf("No solution exists for N = %d\n",


N);

getch();

}
Number of subsets

#include <stdio.h> void main() {

#include <conio.h> int i;

#define MAX 10 clrscr();

int n, target; printf("Enter number of elements (max


%d): ", MAX);
int set[MAX];
scanf("%d", &n);
int solution[MAX];
if (n > MAX) {
void printSolution() {
printf("Too many elements!\n");
int i;
getch();
printf("{ ");
return;
for (i = 0; i < n; i++) {
}
if (solution[i] == 1) {
printf("Enter elements:\n");
printf("%d ", set[i]);
for (i = 0; i < n; i++) {
}}
scanf("%d", &set[i]);
printf("}\n");
solution[i] = 0;
}
}
void sumOfSubsets(int i, int sum) {
printf("Enter target sum: ");
if (sum == target) {
scanf("%d", &target);
printSolution();
printf("\nSubsets whose sum equals
return;
%d:\n", target);
}
sumOfSubsets(0, 0);
if (sum > target || i == n)

return;
getch();
solution[i] = 1;
}
sumOfSubsets(i + 1, sum + set[i]);

solution[i] = 0;

sumOfSubsets(i + 1, sum);

}
Graph color }

#include <stdio.h> void printSolution() {

#include <conio.h> int i;

#define MAX 10 printf("\nSolution exists:\n");

int graph[MAX][MAX]; for (i = 0; i < V; i++) {

int color[MAX]; printf("Vertex %d --> Color %d\n", i,


color[i]);
int V;
}}
int isSafe(int v, int c) {
void main() {
int i;
int m, i, j;
for (i = 0; i < V; i++) {
clrscr();
if (graph[v][i] == 1 && color[i] == c) {
printf("Enter number of vertices (max
return 0;
%d): ", MAX);
}}
scanf("%d", &V);
return 1;
if (V > MAX) {
}
printf("Too many vertices!\n");
int graphColoring(int m, int v) {
getch();
int c;
return;
if (v == V) {
}
return 1;
printf("Enter the adjacency
} matrix:\n");

for (c = 1; c <= m; c++) { for (i = 0; i < V; i++) {

if (isSafe(v, c)) { for (j = 0; j < V; j++) {

color[v] = c; scanf("%d", &graph[i][j]);

if (graphColoring(m, v + 1)) }}

return 1; printf("Enter the number of colors: ");

color[v] = 0; // Backtrack scanf("%d", &m);

}} for (i = 0; i < V; i++) {


return 0; color[i] = 0;
}

if (graphColoring(m, 0)) {

printSolution();

} else {

printf("Solution does not exist.\n");

getch();

}
Hamilton cycle hamiltonian(pos + 1);

#include <stdio.h> visited[v] = 0;

#include <conio.h> }}}

#define MAX 10 void main() {

int V; int i, j;

int graph[MAX][MAX]; clrscr();

int path[MAX]; printf("Enter number of vertices (max


%d): ", MAX);
int visited[MAX];
scanf("%d", &V);
void printPath() {
if (V > MAX || V <= 0) {
int i;
printf("Invalid number of vertices!\n");
for (i = 0; i < V; i++) {
getch();
printf("%d", path[i]);
return;
if (i < V - 1)
}
printf(" -> ");
printf("Enter the adjacency matrix (0 or
}
1):\n");
printf("\n");
for (i = 0; i < V; i++) {
}
for (j = 0; j < V; j++) {
void hamiltonian(int pos) {
scanf("%d", &graph[i][j]);
int v;
}}
if (pos == V) {
printf("\nHamiltonian paths are:\n");
printPath();
for (i = 0; i < V; i++) {
return;
for (j = 0; j < V; j++)
}
visited[j] = 0;
for (v = 0; v < V; v++) {
path[0] = i;
if (!visited[v] && graph[path[pos -
visited[i] = 1;
1]][v]) {
hamiltonian(1);
path[pos] = v;
}
visited[v] = 1;
getch(); }
knapsack greedy printf("Selected full item with
Profit = %d, Weight = %d\n",
#include <stdio.h>
items[i].profit, items[i].weight);
#include <stdlib.h>
} else {
struct Item {
int remaining = capacity -
int profit; currentWeight;

int weight; totalProfit += items[i].profit *


((float)remaining / items[i].weight);
float ratio;
printf("Selected fraction of item
};
with Profit = %d, Weight = %d (fraction
int compare(const void *a, const void *b) taken: %d/%d)\n",
{
items[i].profit, items[i].weight,
struct Item *item1 = (struct Item *)a; remaining, items[i].weight);
struct Item *item2 = (struct Item *)b; currentWeight = capacity;
if (item1->ratio < item2->ratio) break;
return 1; }
else if (item1->ratio > item2->ratio) }
return -1; printf("\nTotal weight of selected
items: %d\n", currentWeight);
return 0;
return totalProfit;
}
}
float knapsack(struct Item items[], int n,
int capacity) { int main() {
int i; int n, capacity, i;
float totalProfit = 0.0; printf("Enter the number of items: ");
int currentWeight = 0; scanf("%d", &n);
printf("\nStep-by-step selection:\n"); struct Item* items = (struct
Item*)malloc(n * sizeof(struct Item));
for (i = 0; i < n; i++) {
if (items == NULL) {
if (currentWeight + items[i].weight
<= capacity) { printf("Memory allocation
failed.\n");
currentWeight += items[i].weight;
return 1;
totalProfit += items[i].profit;
}

printf("Enter the capacity of the float maxProfit = knapsack(items, n,


knapsack: "); capacity);

scanf("%d", &capacity); printf("\nMaximum Profit = %.2f\n",


maxProfit);
for (i = 0; i < n; i++) {
free(items);
printf("Enter profit and weight for
item %d:\n", i + 1); return 0;

printf("Profit: "); }

scanf("%d", &items[i].profit);

printf("Weight: ");

scanf("%d", &items[i].weight);

items[i].ratio = (float)items[i].profit /
items[i].weight;

printf("\nOriginal Items:\n");

printf("Item\tProfit\tWeight\tRatio\n");

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

printf("%c\t%d\t%d\t%.2f\n", 'A' + i,
items[i].profit, items[i].weight,
items[i].ratio);

qsort(items, n, sizeof(struct Item),


compare);

printf("\nItems sorted by Profit/Weight


ratio:\n");

printf("Item\tProfit\tWeight\tRatio\n");

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

printf("%c\t%d\t%d\t%.2f\n", 'A' + i,
items[i].profit, items[i].weight,
items[i].ratio);

You might also like