All DAA Codes
All DAA Codes
#include<stdio.h>
int b[10],a[10];
int ctr = 0;
//merging of arrays
}
int main(){
int n,i;
printf("Enter the number of elements:\n");
scanf("%d",&n);
//array input
for (int i = 0; i < n; i++)
{
printf("Enter the element %d:\n",i+1);
scanf("%d",&a[i]);
}
mergeSort(a,0,n-1);
printf("Sorted array:\n");
for ( i = 0; i < n; i++)
{
printf("%d\n",a[i]);
}
Greedy:
#include<stdio.h>
#include<conio.h>
int coincount = 0;
coinchanging(coins , n , amount);
return 0;
}
2.
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <stdbool.h>
// Function to find the vertex with maximum bandwidth value, from the set of
vertices not yet included in max bandwidth tree
int maxBandwidthVertex(int bandwidth[], bool mstSet[]) {
int max = INT_MIN, max_index;
for (int v = 0; v < V; v++) {
if (mstSet[v] == false && bandwidth[v] >= max) {
max = bandwidth[v];
max_index = v;
}
}
return max_index;
}
int main() {
// Create a graph represented as an adjacency matrix
int graph[V][V];
// Input graph
for (int i = 0; i < V; i++)
{
for (int j = 0; j < V; j++)
{
if (i == j)
{
graph[i][j] = 0;
}
else
{
printf("Enter the weight of edge between %d and %d:\n",i,j);
scanf("%d",&graph[i][j]);
}
}
}
return 0;
}
3.
#include<stdio.h>
#include<conio.h>
float weight[50],profit[50],ratio[50];
temp = weight[j];
weight[j] = weight[i];
weight[i] = temp;
temp = profit[j];
profit[j] = profit[i];
profit[i] = temp;
}
}
}
}
void main(){
int n;
printf("Enter number of items:\n");
scanf("%d",&n);
for (int i = 0; i < n; i++)
{
printf("Enter weight of item %d:\n",i);
scanf("%f",&weight[i]);
}
int capacity;
printf("Enter capacity of knapsack:\n");
scanf("%d",&capacity);
sort(n);
knapsack(n , capacity);
}
4.
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <stdbool.h>
// Function to find the vertex with minimum key value, from the set of
vertices not yet included in MST
int minKey(int key[], bool mstSet[]) {
int min = INT_MAX, min_index;
for (int v = 0; v < V; v++) {
if (mstSet[v] == false && key[v] < min) {
min = key[v];
min_index = v;
}
}
return min_index;
}
// Update key value and parent index of the adjacent vertices of the
picked vertex
for (int v = 0; v < V; v++) {
if (graph[u][v] && mstSet[v] == false && graph[u][v] < key[v]) {
parent[v] = u;
key[v] = graph[u][v];
printf(" Updated key value of vertex %d to %d\n", v, key[v]);
}
}
}
int main() {
/* Let us create the following graph
2 3
(0)--(1)--(2)
| / \ |
6| 8/ \5 |7
| / \ |
(3)-------(4)
9 */
int graph[V][V] = {{0, 2, 0, 6, 0},
{2, 0, 3, 8, 5},
{0, 3, 0, 0, 7},
{6, 8, 0, 0, 9},
{0, 5, 7, 9, 0}};
return 0;
}
DP:
#include <stdio.h>
// Build dp table
for (int i = 0; i <= n; i++) {
for (int w = 0; w <= capacity; w++) {
if (i == 0 || w == 0)
dp[i][w] = 0;
else if (weights[i - 1] <= w) {
dp[i][w] = max(values[i - 1] + dp[i - 1][w - weights[i - 1]],
dp[i - 1][w]);
printf("dp[%d][%d] = %d\n", i, w, dp[i][w]);
}
else
dp[i][w] = dp[i - 1][w];
}
}
int main() {
int weights[] = {2, 3, 4, 5};
int values[] = {3, 4, 5, 6};
int capacity = 5;
int n = sizeof(values) / sizeof(values[0]);
return 0;
}
2.
#include <stdio.h>
#include <limits.h>
int main() {
int numVertices, numEdges, source;
printf("Enter the number of vertices: ");
scanf("%d", &numVertices);
printf("Enter the number of edges: ");
scanf("%d", &numEdges);
return 0;
}
3.
#include <stdio.h>
int min_coins_change(int amount, int denominations[], int num_denominations)
{
int dp[amount + 1];
dp[0] = 0;
// intitaialize the table with infinity for all other amounts..
for (int i = 1; i <= amount; i++)
{
dp[i] = amount + 1;
}
printf("Considering denominations: ");
for (int i = 0; i <= amount; i++)
{
printf("%d ", dp[i]);
}
printf("\n");
4.
#include <stdio.h>
#define V 5
#define INF 99999
printSolution(dist);
}
int main()
{
int graph[V][V] = { { 0, 4, INF, 5,INF },
{ INF, 0, 1, INF,6 },
{ 2,INF, 0, 3,INF },
{ INF, INF, 1, 0,2},
{ 1,INF, INF, 4,0} };
floydWarshall(graph);
return 0;
}
5.
#include <stdio.h>
#include <string.h>
return 0;
}
6.
#include <limits.h>
#include <stdio.h>
// Driver code
int main()
{
int arr[] = { 1, 2, 3, 4, 3 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function call
printf("Minimum number of multiplications is %d\n",
MatrixChainOrder(arr, 1, N - 1));
getchar();
return 0;
}
7.
#include <stdio.h>
#include <limits.h>
// Driver code
int main() {
// Example graph represented as an adjacency matrix
int graph[V][V] = {
{0, 10, 15, 20},
{10, 0, 35, 25},
{15, 35, 0, 30},
{20, 25, 30, 0}
};
// Start from the first vertex (0th vertex) and mask of visited vertices
is 1
int startingVertex = 0;
int mask = 1 << startingVertex;
return 0;
}
Backtracking:
#include <stdio.h>
#include <stdlib.h>
// Explore two branches: one where we include the job and one where we
skip the job
// Branch where we include the job
int withProfit = 0;
if (!slot[arr[index].deadline - 1]) {
// Mark the slot as occupied
slot[arr[index].deadline - 1] = 1;
// Recursively explore next job with increased profit
withProfit = jobScheduling(arr, n, slot, currentProfit +
arr[index].profit, index + 1);
// Unmark the slot after backtracking
slot[arr[index].deadline - 1] = 0;
}
int main() {
// Example jobs
struct Job arr[] = {{1, 2, 100}, {2, 1, 19}, {3, 2, 27}, {4, 1, 25}, {5,
3, 15}};
int n = sizeof(arr) / sizeof(arr[0]);
return 0;
}
2.
#include <stdio.h>
#include <stdbool.h>
#define N 4
void printSolution(int board[N][N])
{
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
printf("%d ", board[i][j]);
}
printf("\n");
}
}
bool isSafe(int board[N][N], int row, int col)
{
for (int i = 0; i < col; i++)
{
if (board[row][i])
{
return false;
}
}
for (int i = row, j = col; i >= 0 && j >= 0; i--, j--)
{
if (board[i][j])
{
return false;
}
}
for (int i = row, j = col; j >= 0 && i < N; i++, j--)
{
if (board[i][j])
{
return false;
}
}
return true;
}
bool solveNQueensUtil(int board[N][N], int col)
{
if (col >= N)
{
return true;
}
for (int i = 0; i < N; i++)
{
if (isSafe(board, i, col))
{
board[i][col] = 1;
if (solveNQueensUtil(board, col + 1))
{
return true;
}
board[i][col] = 0;
}
}
return false;
}
void solveNQueens()
{
int board[N][N] = {0};
if (solveNQueensUtil(board, 0) == false)
{
printf("Solution does not exist");
return;
}
printSolution(board);
}
int main()
{
solveNQueens();
return 0;
}
3.
#include <stdio.h>
// If we have reached the end of the array or sum exceeds the target sum,
return
if (idx >= n || currentSum > sum) {
return;
}
// Include the current element in the subset and recur with the next index
solution[idx] = set[idx];
subsetSum(set, n, sum, currentSum + set[idx], idx + 1, solution, depth +
1);
// Exclude the current element from the subset and recur with the next
index
subsetSum(set, n, sum, currentSum, idx + 1, solution, depth);
}
int main() {
int set[] = {10, 7, 5, 18, 12, 20, 15};
int n = sizeof(set) / sizeof(set[0]);
int sum = 35;
findSubsets(set, n, sum);
return 0;
}
STRING ALGORITHMS:
RABIN KARP:
#include <stdio.h>
#include <string.h>
#define d 256
// q is a prime number
#define q 101
// Function to search for the pattern pat in the string txt using Rabin-Karp algorithm
int M = strlen(pat);
int N = strlen(txt);
int i, j;
int h = 1;
h = (h * d) % q;
p = (d * p + pat[i]) % q;
t = (d * t + txt[i]) % q;
if (p == t) {
if (txt[i + j] != pat[j])
break;
// Calculate hash value for next window of text: Remove leading digit, add trailing digit
if (i < N - M) {
if (t < 0)
t = (t + q);
int main() {
search(pat, txt);
return 0;
}
Knuth moris patt:
#include <stdio.h>
#include <string.h>
// Function to compute temporary array to maintain size of suffix which is same as prefix
int i = 1;
while (i < M) {
if (pat[i] == pat[len]) {
len++;
lps[i] = len;
i++;
} else {
if (len != 0) {
} else {
lps[i] = 0;
i++;
}
}
int M = strlen(pat);
int N = strlen(txt);
// Create lps[] that will hold the longest prefix suffix values for pattern
int lps[M];
computeLPSArray(pat, M, lps);
while (i < N) {
if (pat[j] == txt[i]) {
j++;
i++;
if (j == M) {
j = lps[j - 1];
if (j != 0)
j = lps[j - 1];
else
i = i + 1;
int main() {
KMPSearch(pat, txt);
return 0;