Lab Fat Daa
Lab Fat Daa
#include<stdio.h>
int main(){
int start[5]={1,5,12,251};
int finish[5]={10,13,23,24};
int ac vi es = sizeof(start)/sizeof(start[0]);
int i, j;
i = 0;
prin ("%d\n", i);
for (j = 1; j < ac vi es; j++){
if (start[j] >= finish[i]){
prin ("%d\n", j);
i = j;
}
}
return 0;
}
2. Huffman Coding
#include <stdio.h>
#include <stdlib.h>
#define MAX_TREE_HT 100
struct MinHeapNode {
char data;
unsigned freq;
struct MinHeapNode *le , *right;
};
struct MinHeap {
unsigned size;
unsigned capacity;
struct MinHeapNode** array;
};
struct MinHeapNode* newNode(char data, unsigned freq)
{
struct MinHeapNode* temp = (struct MinHeapNode*)malloc(
sizeof(struct MinHeapNode));
temp->le = temp->right = NULL;
temp->data = data;
temp->freq = freq;
return temp;
}
struct MinHeap* createMinHeap(unsigned capacity)
{
4. LCSS
#include<stdio.h>
#include<string.h>
int i,j,m,n, x[20][20];
char A1[20]="BDCB",A2[20]="BACDB",b[20][20];
void sub(){
m=strlen(A1);
n=strlen(A2);
for(i=0;i<=m;i++)
x[i][0]=0;
for(i=0;i<=n;i++)
x[0][i]=0;
for(i=1;i<=m;i++)
for(j=1;j<=n;j++){
if(A1[i-1]==A2[j-1]){
x[i][j]=x[i-1][j-1]+1;
}
else if(x[i-1][j]>=x[i][j-1]){
x[i][j]=x[i-1][j];
}
else{
x[i][j]=x[i][j-1];
}
}
int index=x[m][n];
char sub[21];
sub[index]='\0';
int i=m,j=n;
while(i>0&&j>0){
if(A1[i-1]==A2[j-1]){
sub[index-1]=A1[i-1];
i--;
j--;
index--;
}
else if(x[i-1][j]>x[i][j-1])
i--;
else
j--;
}
prin ("LCS=%s\n\n\n",sub);
int k=strlen(sub);
prin ("Length=%d",k);
}
int main(){
sub();
prin ("\n");
}
5. Maximum Subarray
#include <stdio.h>
#include <limits.h>
6. Karatsuba Algorithm
#include <stdio.h>
#include <math.h>
long pow10(int n) {
long result = 1;
for (int i = 0; i < n; i++) {
result *= 10;
}
return result;
}
if (n == 1) {
return X * Y;
}
int main() {
long X = 12;
long Y = 4;
return 0;
}
7. Backtracking Nqueens
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
bool isSafe(int **board, int row, int col, int n) {
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 solveNQueens(int **board, int col, int n) {
if (col == n) {
return true;
}
for (int i = 0; i < n; i++) {
if (isSafe(board, i, col, n)) {
board[i][col] = 1;
if (solveNQueens(board, col + 1, n)) {
return true;
}
board[i][col] = 0;
}
}
return false;
}
void printBoard(int **board, int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
prin ("%d ", board[i][j]);
}
prin ("\n");
}
}
int main() {
int n;
prin ("Enter the number of queens: ");
scanf("%d", &n);
int **board = malloc(n * sizeof(int *));
for (int i = 0; i < n; i++) {
board[i] = malloc(n * sizeof(int));
for (int j = 0; j < n; j++) {
board[i][j] = 0;
}
}
if (solveNQueens(board, 0, n)) {
printBoard(board, n);
} else {
prin ("No solu on exists.\n");
}
for (int i = 0; i < n; i++) {
free(board[i]);
}
free(board);
return 0;
}
9. KMP
#include<stdio.h>
#include<string.h>
void x(char *pat,int M,int *lps)
{
int len=0;
int i=1;
lps[0]=0;
while(i<M)
{
if(pat[i]==pat[len])
{
len++;
lps[i]=len;
i++;
}
else
{
if(len!=0)
{
len=lps[len-1];
}
else{
lps[i]=0;
i++;
}
}
}
}
void y(char *pat,char *txt)
{
int M=strlen(pat);
int N=strlen(txt);
int lps[M];
x(pat,M,lps);
int i=0;
int j=0;
while(i<N)
{
if(pat[j]==txt[i]){
j++;
i++;
}
if(j==M){
prin ("The match is found:%d-%d\n",i-j+1,i);
j=lps[j-1];
}else if(i<N && pat[j]!=txt[i]){
if(j!=0)
j=lps[j-1];
else
i=i+1;
}
}
}
int main()
{
char txt[100]="aaabbbaba",pat[100]="baba";
y(pat,txt);
}
10. Rabin Karp
#include <stdio.h>
#include <string.h>
// Calculate the hash value of the pa ern and the ini al hash value of the first window in the text
for (int i = 0; i < pa ernLength; i++) {
pa ernHash = (d * pa ernHash + pa ern[i]) % q;
textHash = (d * textHash + text[i]) % q;
}
// Slide the pa ern over the text one by one and compare their hash values
for (int i = 0; i <= textLength - pa ernLength; i++) {
// If the hash values match, compare the pa ern and the current window character by character
if (pa ernHash == textHash) {
int j;
for (j = 0; j < pa ernLength; j++) {
if (text[i+j] != pa ern[j])
break;
}
if (j == pa ernLength)
prin ("Pa ern found at index %d\n", i);
}
// Calculate the hash value for the next window in the text
if (i < textLength - pa ernLength) {
textHash = (d * (textHash - text[i] * h) + text[i+pa ernLength]) % q;
if (textHash < 0) // In case we get a nega ve hash value
textHash = (textHash + q);
}
}
}
int main() {
char text[] = "ABABDABACDABABCABAB";
char pa ern[] = "ABABCABAB";
int q = 101; // A prime number
rabinKarp(pa ern, text, q);
return 0;
}
#define V 4
// Solves the all-pairs shortest path problem using Floyd Warshall algorithm
void floydWarshall (int graph[][V])
{
int dist[V][V], i, j, k;
printSolu on(dist);
}
return visited[t];
}
return maxFlow;
}
int main() {
int graph[N][N] = { {0, 16, 13, 0, 0, 0},
{0, 0, 10, 12, 0, 0},
{0, 4, 0, 0, 14, 0},
{0, 0, 9, 0, 0, 20},
{0, 0, 0, 7, 0, 4},
{0, 0, 0, 0, 0, 0}
};
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef struct {
int x, y;
} Point;
int main() {
Point points[] = {{0, 3}, {1, 1}, {2, 2}, {4, 4}, {0, 0}, {1, 2}, {3, 1}, {3, 3}};
int n = sizeof(points)/sizeof(points[0]);
convex_hull(points, n);
return 0;
}
typedef struct {
int x, y;
} Point;
int main() {
Point points[] = {{0, 3}, {1, 1}, {2, 2}, {4, 4}, {0, 0}, {1, 2}, {3, 1}, {3, 3}};
int n = sizeof(points)/sizeof(points[0]);
convex_hull(points, n);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include < me.h>
void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}
int par on(int arr[], int low, int high) {
int pivot = arr[high];
int i = low - 1;
for (int j = low; j <= high - 1; j++) {
if (arr[j] < pivot) {
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i+1], &arr[high]);
return (i+1);
}
int randomPar on(int arr[], int low, int high) {
srand( me(NULL));
int random = low + rand() % (high - low);
swap(&arr[random], &arr[high]);
return par on(arr, low, high);
}
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pi = randomPar on(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
int main() {
int arr[] = {67, 0, 23, 11, 1, 21, 65, 63, 50, 33};
int n = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, n-1);
for (int i = 0; i < n; i++) {
prin ("%d ", arr[i]);
}
return 0;
}
#include <stdio.h>
int main() {
int id[] = {0, 1, 2, 3}; // candidate IDs
int rank[] = {5, 7, 9, 10}; // candidate ranks
int current_rank = -1; // current assistant rank
int num_candidates = sizeof(rank) / sizeof(rank[0]); // number of candidates
int ci = 10; // cost to interview a candidate
int ch = 100; // cost to hire a candidate
int hire;
return 0;
}
struct Item {
int value;
int weight;
};
int main() {
struct Item items[] = {{10, 2}, {5, 3}, {15, 5},{7,7},{6,1},{18,4},{3,1}};
int capacity = 15;
int n = sizeof(items) / sizeof(items[0]);
double result = frac onalKnapsack(capacity, items, n);
prin ("Maximum value in Knapsack = %.2f", result);
return 0;
}
// Ini alize first column as true. It's possible to make a sum of 0 with any number of elements
for (int i = 0; i <= n; i++)
subset[i][0] = true;
// Ini alize first row as false. It's not possible to make a non-zero sum with an empty set
for (int i = 1; i <= sum; i++)
subset[0][i] = false;
int main()
{
int set[] = {3, 34, 4, 12, 5, 2};
int n = sizeof(set)/sizeof(set[0]);
int sum = 9;
if (subsetSum(set, n, sum))
prin ("There exists a subset of the given set with the given sum\n");
else
prin ("There does not exist a subset of the given set with the given sum\n");
return 0;
}
19. Naïve
#include<stdio.h>
#include<string.h>
int main()
{
char text[100]="aaabbbaba",pa ern[100]="baba";
int n=strlen(text);
int m=strlen(pa ern);
for(int i=0;i<=n-m;i++){
int j;
for(j=0;j<m;j++){
if(text[i+j]!=pa ern[j]){
break;
}
}
if(j==m){
prin ("The match is found:%d-%d\n",i+1,i+m);
}
}
}