Download as pdf or txt
Download as pdf or txt
You are on page 1of 63

Program of binary tree algo :

#include <stdio.h>

#include <stdlib.h>

struct node {

int key;

struct node *left, *right;

};

struct node *newNode(int item) {

struct node *temp = (struct node *)malloc(sizeof(struct node));

temp->key = item;

temp->left = temp->right = NULL;

return temp;

void inorder(struct node *root) {

if (root != NULL) {

inorder(root->left);

printf("%d -> ", root->key);

inorder(root->right);

struct node *insert(struct node *node, int key) {

if (node == NULL) return newNode(key);

if (key < node->key)

node->left = insert(node->left, key);

else

node->right = insert(node->right, key);

return node;

struct node *minValueNode(struct node *node) {

struct node *current = node;

while (current && current->left != NULL)

current = current->left;

return current;

}
struct node *deleteNode(struct node *root, int key) {

if (root == NULL) return root;

if (key < root->key)

root->left = deleteNode(root->left, key);

else if (key > root->key)

root->right = deleteNode(root->right, key);

else {

if (root->left == NULL) {

struct node *temp = root->right;

free(root);

return temp;

} else if (root->right == NULL) {

struct node *temp = root->left;

free(root);

return temp;

struct node *temp = minValueNode(root->right);

root->key = temp->key;

root->right = deleteNode(root->right, temp->key);

return root;

int main() {

struct node *root = NULL;

root = insert(root, 8);

root = insert(root, 3);

root = insert(root, 1);

root = insert(root, 6);

root = insert(root, 7);

root = insert(root, 10);

root = insert(root, 14);

root = insert(root, 4);

printf("Inorder traversal: ");

inorder(root);

printf("\nAfter deleting 10\n");


root = deleteNode(root, 10);

printf("Inorder traversal: ");

inorder(root);

oUtPUt :
Program of bUcket sort algo:
#include <stdio.h>

int getMax(int a[], int n)

int max = a[0];

for (int i = 1; i < n; i++)

if (a[i] > max)

max = a[i];

return max;

void bucket(int a[], int n)

int max = getMax(a, n);

int bucket[max], i;

for (int i = 0; i <= max; i++)

bucket[i] = 0;

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

bucket[a[i]]++;

for (int i = 0, j = 0; i <= max; i++)

while (bucket[i] > 0)

a[j++] = i;

bucket[i]--;

void printArr(int a[], int n)

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


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

int main()

int a[] = {54, 12, 84, 57, 69, 41, 9, 5};

int n = sizeof(a) / sizeof(a[0]);

printf("Before sorting array elements are - \n");

printArr(a, n);

bucket(a, n);

printf("\nAfter sorting array elements are - \n");

printArr(a, n);

oUtPUt :
Program of coUnting sort algo:
#include <stdio.h>

#include <string.h>

#define RANGE 255

void countSort(char arr[])

char output[strlen(arr)];

int count[RANGE + 1], i;

memset(count, 0, sizeof(count));

for (i = 0; arr[i]; ++i)

++count[arr[i]];

for (i = 1; i <= RANGE; ++i)

count[i] += count[i - 1];

for (i = 0; arr[i]; ++i) {

output[count[arr[i]] - 1] = arr[i];

--count[arr[i]];

for (i = 0; arr[i]; ++i)

arr[i] = output[i];

int main()

char arr[] = "geeksforgeeks";

countSort(arr);

printf("Sorted character array is %s", arr);

return 0;

oUtPUt:
Program of heaP sort algo:
#include <stdio.h>

void swap(int* a, int* b)

int temp = *a;

*a = *b;

*b = temp;

void heapify(int arr[], int N, int i)

int largest = i;

int left = 2 * i + 1;

int right = 2 * i + 2;

if (left < N && arr[left] > arr[largest])

largest = left;

if (right < N && arr[right] > arr[largest])

largest = right;

if (largest != i) {

swap(&arr[i], &arr[largest]);

heapify(arr, N, largest);

void heapSort(int arr[], int N)

// Build max heap

for (int i = N / 2 - 1; i >= 0; i--)

heapify(arr, N, i);

// Heap sort

for (int i = N - 1; i >= 0; i--) {

swap(&arr[0], &arr[i]);

heapify(arr, i, 0);

} }

void printArray(int arr[], int N)


{

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

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

printf("\n");

int main()

int arr[] = { 12, 11, 13, 5, 6, 7 };

int N = sizeof(arr) / sizeof(arr[0]);

heapSort(arr, N);

printf("Sorted array is\n")

printArray(arr, N);

oUtPUt:
Program of insertion sort algo:
#include <math.h>

#include <stdio.h>

void insertionSort(int arr[], int n)

int i, key, j;

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

key = arr[i];

j = i - 1;

while (j >= 0 && arr[j] > key) {

arr[j + 1] = arr[j];

j = j - 1;

arr[j + 1] = key;

void printArray(int arr[], int n)

int i;

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

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

printf("\n");

int main()

int arr[] = { 12, 11, 13, 5, 6 };

int n = sizeof(arr) / sizeof(arr[0]);

insertionSort(arr, n);

printArray(arr, n);

return 0; }

oUtPUt:
Program of merge sort algo:
#include <stdio.h>

#include <stdlib.h>

void merge(int arr[], int l, int m, int r)

int i, j, k;

int n1 = m - l + 1;

int n2 = r - m;

int L[n1], R[n2];

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

L[i] = arr[l + i];

for (j = 0; j < n2; j++)

R[j] = arr[m + 1 + j];

i = 0;

j = 0;

k = l;

while (i < n1 && j < n2) {

if (L[i] <= R[j]) {

arr[k] = L[i];

i++;

else {

arr[k] = R[j];

j++;

k++;

while (i < n1) {

arr[k] = L[i];

i++;

k++;

while (j < n2) {

arr[k] = R[j];
j++;

k++;

void mergeSort(int arr[], int l, int r)

if (l < r)

int m = l + (r - l) / 2;

mergeSort(arr, l, m);

mergeSort(arr, m + 1, r);

merge(arr, l, m, r);

void printArray(int A[], int size)

{ int i;

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

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

printf("\n"); }

int main()

{ int arr[] = { 12, 11, 13, 5, 6, 7 };

int arr_size = sizeof(arr) / sizeof(arr[0]);

printf("Given array is \n");

printArray(arr, arr_size);

mergeSort(arr, 0, arr_size - 1);

printf("\nSorted array is \n");

printArray(arr, arr_size);

return 0;}

oUtPUt:
Program of qUick sort algo:
#include<stdio.h>

void swap(int* a, int* b)

int t = *a;

*a = *b;

*b = t;

int partition(int arr[], int low, int high)

int pivot = arr[high]; // pivot

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);

void quickSort(int arr[], int low, int high)

if (low < high) {

int pi = partition(arr, low, high);

quickSort(arr, low, pi - 1);

quickSort(arr, pi + 1, high);

void printArray(int arr[], int size)

int i;
for (i = 0; i < size; i++)

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

printf("\n");

int main()

int arr[] = { 10, 7, 8, 9, 1, 5 };

int n = sizeof(arr) / sizeof(arr[0]);

quickSort(arr, 0, n - 1);

printf("Sorted array:");

printArray(arr, n);

return 0;

oUtPUt:
Program of radix sort algo:
#include <stdio.h>

int getMax(int a[], int n) {

int max = a[0];

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

if(a[i] > max)

max = a[i];

return max;

void countingSort(int a[], int n, int place)

int output[n + 1];

int count[10] = {0};

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

count[(a[i] / place) % 10]++;

for (int i = 1; i < 10; i++)

count[i] += count[i - 1];

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

output[count[(a[i] / place) % 10] - 1] = a[i];

count[(a[i] / place) % 10]--;

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

a[i] = output[i];

void radixsort(int a[], int n) {

int max = getMax(a, n);

for (int place = 1; max / place > 0; place *= 10)

countingSort(a, n, place);

void printArray(int a[], int n) {

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

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

} printf("\n"); }
int main() {

int a[] = {181, 289, 390, 121, 145, 736, 514, 888, 122};

int n = sizeof(a) / sizeof(a[0]);

printf("Before sorting array elements are - \n");

printArray(a,n);

radixsort(a, n);

printf("After applying Radix sort, the array elements are - \n");

printArray(a, n);

oUtPUt:
Program of red black tree insert algo:
#include <stdio.h>

#include <stdlib.h>

struct node {

int d;

int c;

struct node* p;

struct node* r;

struct node* l;

};

struct node* root = NULL;

struct node* bst(struct node* trav,

struct node* temp)

if (trav == NULL)

return temp;

if (temp->d < trav->d)

trav->l = bst(trav->l, temp);

trav->l->p = trav;

else if (temp->d > trav->d)

trav->r = bst(trav->r, temp);

trav->r->p = trav;

return trav;

void rightrotate(struct node* temp)

struct node* left = temp->l;

temp->l = left->r;

if (temp->l)

temp->l->p = temp;
left->p = temp->p;

if (!temp->p)

root = left;

else if (temp == temp->p->l)

temp->p->l = left;

else

temp->p->r = left;

left->r = temp;

temp->p = left;

void leftrotate(struct node* temp)

struct node* right = temp->r;

temp->r = right->l;

if (temp->r)

temp->r->p = temp;

right->p = temp->p;

if (!temp->p)

root = right;

else if (temp == temp->p->l)

temp->p->l = right;

else

temp->p->r = right;

right->l = temp;

temp->p = right;

void fixup(struct node* root, struct node* pt)

struct node* parent_pt = NULL;

struct node* grand_parent_pt = NULL;

while ((pt != root) && (pt->c != 0)

&& (pt->p->c == 1))

parent_pt = pt->p;

grand_parent_pt = pt->p->p;
if (parent_pt == grand_parent_pt->l)

struct node* uncle_pt = grand_parent_pt->r;

if (uncle_pt != NULL && uncle_pt->c == 1)

grand_parent_pt->c = 1;

parent_pt->c = 0;

uncle_pt->c = 0;

pt = grand_parent_pt;

else {

if (pt == parent_pt->r) {

leftrotate(parent_pt);

pt = parent_pt;

parent_pt = pt->p;

rightrotate(grand_parent_pt);

int t = parent_pt->c;

parent_pt->c = grand_parent_pt->c;

grand_parent_pt->c = t;

pt = parent_pt;

else {

struct node* uncle_pt = grand_parent_pt->l;

if ((uncle_pt != NULL) && (uncle_pt->c == 1))

grand_parent_pt->c = 1;

parent_pt->c = 0;

uncle_pt->c = 0;

pt = grand_parent_pt;

else {

if (pt == parent_pt->l) {

rightrotate(parent_pt);
pt = parent_pt;

parent_pt = pt->p;

leftrotate(grand_parent_pt);

int t = parent_pt->c;

parent_pt->c = grand_parent_pt->c;

grand_parent_pt->c = t;

pt = parent_pt;

root->c = 0;

void inorder(struct node* trav)

if (trav == NULL)

return;

inorder(trav->l);

printf("%d ", trav->d);

inorder(trav->r);

int main()

int n = 7;

int a[7] = { 7, 6, 5, 4, 3, 2, 1 };

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

struct node* temp

= (struct node*)malloc(sizeof(struct node));

temp->r = NULL;

temp->l = NULL;

temp->p = NULL;

temp->d = a[i];

temp->c = 1;

root = bst(root, temp);


fixup(root, temp);

printf("Inorder Traversal of Created Tree\n");

inorder(root);

return 0;

oUtPUt:
Program of shell sort algo:
#include <stdio.h>

int shell(int a[], int n)

{ for (int interval = n/2; interval > 0; interval /= 2)

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

int temp = a[i];

int j;

for (j = i; j >= interval && a[j - interval] > temp; j -= interval)

a[j] = a[j - interval];

a[j] = temp;

} }

return 0; }

void printArr(int a[], int n)

int i;

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

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

int main()

{ int a[] = { 33, 31, 40, 8, 12, 17, 25, 42 };

int n = sizeof(a) / sizeof(a[0]);

printf("Before sorting array elements are - \n");

printArr(a, n);

shell(a, n);

printf("\nAfter applying shell sort, the array elements are - \n");

printArr(a, n);

return 0; }

oUtPUt:
Program of knaPsack Problem algo:
#include<stdio.h>

int max(int a, int b) {

if(a>b){

return a;

} else {

return b;

} }

int knapsack(int W, int wt[], int val[], int n) {

int i, w;

int knap[n+1][W+1];

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

for (w = 0; w <= W; w++) {

if (i==0 || w==0)

knap[i][w] = 0;

else if (wt[i-1] <= w)

knap[i][w] = max(val[i-1] + knap[i-1][w-wt[i-1]], knap[i-1][w]);

else

knap[i][w] = knap[i-1][w];

} }

return knap[n][W]; }

int main() {

int val[] = {20, 25, 40};

int wt[] = {25, 20, 30};

int W = 50;

int n = sizeof(val)/sizeof(val[0]);

printf("The solution is : %d", knapsack(W, wt, val, n));

return 0;

oUtPUt:
Program of traVelling salesman Problem
algo:
#include<stdio.h>

int ary[10][10],completed[10],n,cost=0;

void takeInput()

int i,j;

printf("Enter the number of villages: ");

scanf("%d",&n);

printf("\nEnter the Cost Matrix\n");

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

printf("\nEnter Elements of Row: %d\n",i+1);

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

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

completed[i]=0;

printf("\n\nThe cost list is:");

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

printf("\n");

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

printf("\t%d",ary[i][j]);

void mincost(int city)

int i,ncity;

completed[city]=1;

printf("%d--->",city+1);

ncity = least(city);

if(ncity==999)

ncity=0;

printf("%d",ncity+1);
cost+=ary[city][ncity];

return;

mincost(ncity);

int least(int c)

int i,nc=999;

int min=999,kmin;

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

if((ary[c][i]!=0)&&(completed[i]==0))

if(ary[c][i]+ary[i][c] < min)

min=ary[i][0]+ary[c][i];

kmin=ary[c][i];

nc=i;

if(min!=999)

cost+=kmin;

return nc;

int main()

takeInput();

printf("\n\nThe Path is:\n");

mincost(0); //passing 0 because starting vertex

printf("\n\nMinimum cost is %d\n ",cost);

return 0;

}
oUtPUt:
Program of red black tree delete algo:
#include <stdio.h>

#include <stdlib.h>

enum nodeColor {

RED,

BLACK

};

struct rbNode {

int data, color;

struct rbNode *link[2];

};

struct rbNode *root = NULL;

// Create a red-black tree

struct rbNode *createNode(int data) {

struct rbNode *newnode;

newnode = (struct rbNode *)malloc(sizeof(struct rbNode));

newnode->data = data;

newnode->color = RED;

newnode->link[0] = newnode->link[1] = NULL;

return newnode;

// Insert an node

void insertion(int data) {

struct rbNode *stack[98], *ptr, *newnode, *xPtr, *yPtr;

int dir[98], ht = 0, index;

ptr = root;

if (!root) {

root = createNode(data);

return;

stack[ht] = root;

dir[ht++] = 0;

while (ptr != NULL) {

if (ptr->data == data) {

printf("Duplicates Not Allowed!!\n");


return;

index = (data - ptr->data) > 0 ? 1 : 0;

stack[ht] = ptr;

ptr = ptr->link[index];

dir[ht++] = index;

stack[ht - 1]->link[index] = newnode = createNode(data);

while ((ht >= 3) && (stack[ht - 1]->color == RED)) {

if (dir[ht - 2] == 0) {

yPtr = stack[ht - 2]->link[1];

if (yPtr != NULL && yPtr->color == RED) {

stack[ht - 2]->color = RED;

stack[ht - 1]->color = yPtr->color = BLACK;

ht = ht - 2;

} else {

if (dir[ht - 1] == 0) {

yPtr = stack[ht - 1];

} else {

xPtr = stack[ht - 1];

yPtr = xPtr->link[1];

xPtr->link[1] = yPtr->link[0];

yPtr->link[0] = xPtr;

stack[ht - 2]->link[0] = yPtr;

xPtr = stack[ht - 2];

xPtr->color = RED;

yPtr->color = BLACK;

xPtr->link[0] = yPtr->link[1];

yPtr->link[1] = xPtr;

if (xPtr == root) {

root = yPtr;

} else {

stack[ht - 3]->link[dir[ht - 3]] = yPtr;

break; }
} else {

yPtr = stack[ht - 2]->link[0];

if ((yPtr != NULL) && (yPtr->color == RED)) {

stack[ht - 2]->color = RED;

stack[ht - 1]->color = yPtr->color = BLACK;

ht = ht - 2;

} else {

if (dir[ht - 1] == 1) {

yPtr = stack[ht - 1];

} else {

xPtr = stack[ht - 1];

yPtr = xPtr->link[0];

xPtr->link[0] = yPtr->link[1];

yPtr->link[1] = xPtr;

stack[ht - 2]->link[1] = yPtr;

xPtr = stack[ht - 2];

yPtr->color = BLACK;

xPtr->color = RED;

xPtr->link[1] = yPtr->link[0];

yPtr->link[0] = xPtr;

if (xPtr == root) {

root = yPtr;

} else {

stack[ht - 3]->link[dir[ht - 3]] = yPtr;

break;

}}}

root->color = BLACK;

// Delete a node

void deletion(int data) {

struct rbNode *stack[98], *ptr, *xPtr, *yPtr;

struct rbNode *pPtr, *qPtr, *rPtr;

int dir[98], ht = 0, diff, i;

enum nodeColor color;


if (!root) {

printf("Tree not available\n");

return;

ptr = root;

while (ptr != NULL) {

if ((data - ptr->data) == 0)

break;

diff = (data - ptr->data) > 0 ? 1 : 0;

stack[ht] = ptr;

dir[ht++] = diff;

ptr = ptr->link[diff];

if (ptr->link[1] == NULL) {

if ((ptr == root) && (ptr->link[0] == NULL)) {

free(ptr);

root = NULL;

} else if (ptr == root) {

root = ptr->link[0];

free(ptr);

} else {

stack[ht - 1]->link[dir[ht - 1]] = ptr->link[0];

} else {

xPtr = ptr->link[1];

if (xPtr->link[0] == NULL) {

xPtr->link[0] = ptr->link[0];

color = xPtr->color;

xPtr->color = ptr->color;

ptr->color = color;

if (ptr == root) {

root = xPtr;

} else {

stack[ht - 1]->link[dir[ht - 1]] = xPtr;

dir[ht] = 1;
stack[ht++] = xPtr;

} else {

i = ht++;

while (1) {

dir[ht] = 0;

stack[ht++] = xPtr;

yPtr = xPtr->link[0];

if (!yPtr->link[0])

break;

xPtr = yPtr;

dir[i] = 1;

stack[i] = yPtr;

if (i > 0)

stack[i - 1]->link[dir[i - 1]] = yPtr;

yPtr->link[0] = ptr->link[0];

xPtr->link[0] = yPtr->link[1];

yPtr->link[1] = ptr->link[1];

if (ptr == root) {

root = yPtr;

color = yPtr->color;

yPtr->color = ptr->color;

ptr->color = color;

if (ht < 1)

return;

if (ptr->color == BLACK) {

while (1) {

pPtr = stack[ht - 1]->link[dir[ht - 1]];

if (pPtr && pPtr->color == RED) {

pPtr->color = BLACK;

break;

} if (ht < 2)

break;
if (dir[ht - 2] == 0) {

rPtr = stack[ht - 1]->link[1];

if (!rPtr)

break;

if (rPtr->color == RED) {

stack[ht - 1]->color = RED;

rPtr->color = BLACK;

stack[ht - 1]->link[1] = rPtr->link[0];

rPtr->link[0] = stack[ht - 1];

if (stack[ht - 1] == root) {

root = rPtr;

} else {

stack[ht - 2]->link[dir[ht - 2]] = rPtr;

dir[ht] = 0;

stack[ht] = stack[ht - 1];

stack[ht - 1] = rPtr;

ht++;

rPtr = stack[ht - 1]->link[1];

if ((!rPtr->link[0] || rPtr->link[0]->color == BLACK) &&

(!rPtr->link[1] || rPtr->link[1]->color == BLACK)) {

rPtr->color = RED;

} else {

if (!rPtr->link[1] || rPtr->link[1]->color == BLACK) {

qPtr = rPtr->link[0];

rPtr->color = RED;

qPtr->color = BLACK;

rPtr->link[0] = qPtr->link[1];

qPtr->link[1] = rPtr;

rPtr = stack[ht - 1]->link[1] = qPtr;

rPtr->color = stack[ht - 1]->color;

stack[ht - 1]->color = BLACK;

rPtr->link[1]->color = BLACK;

stack[ht - 1]->link[1] = rPtr->link[0];


rPtr->link[0] = stack[ht - 1];

if (stack[ht - 1] == root) {

root = rPtr;

} else {

stack[ht - 2]->link[dir[ht - 2]] = rPtr;

break;

} else {

rPtr = stack[ht - 1]->link[0];

if (!rPtr)

break;

if (rPtr->color == RED) {

stack[ht - 1]->color = RED;

rPtr->color = BLACK;

stack[ht - 1]->link[0] = rPtr->link[1];

rPtr->link[1] = stack[ht - 1];

if (stack[ht - 1] == root) {

root = rPtr;

} else {

stack[ht - 2]->link[dir[ht - 2]] = rPtr;

dir[ht] = 1;

stack[ht] = stack[ht - 1];

stack[ht - 1] = rPtr;

ht++;

rPtr = stack[ht - 1]->link[0];

if ((!rPtr->link[0] || rPtr->link[0]->color == BLACK) &&

(!rPtr->link[1] || rPtr->link[1]->color == BLACK)) {

rPtr->color = RED;

} else {

if (!rPtr->link[0] || rPtr->link[0]->color == BLACK) {

qPtr = rPtr->link[1];

rPtr->color = RED;

qPtr->color = BLACK;
rPtr->link[1] = qPtr->link[0];

qPtr->link[0] = rPtr;

rPtr = stack[ht - 1]->link[0] = qPtr;

rPtr->color = stack[ht - 1]->color;

stack[ht - 1]->color = BLACK;

rPtr->link[0]->color = BLACK;

stack[ht - 1]->link[0] = rPtr->link[1];

rPtr->link[1] = stack[ht - 1];

if (stack[ht - 1] == root) {

root = rPtr;

} else {

stack[ht - 2]->link[dir[ht - 2]] = rPtr;

break;} }

ht--;

}}}

// Print the inorder traversal of the tree

void inorderTraversal(struct rbNode *node) {

if (node) {

inorderTraversal(node->link[0]);

printf("%d ", node->data);

inorderTraversal(node->link[1]);

return;

// Driver code

int main() {

int ch, data;

while (1) {

printf("1. Insertion\t2. Deletion\n");

printf("3. Traverse\t4. Exit");

printf("\nEnter your choice:");

scanf("%d", &ch);

switch (ch) {

case 1:
printf("Enter the element to insert:");

scanf("%d", &data);

insertion(data);

break;

case 2:

printf("Enter the element to delete:");

scanf("%d", &data);

deletion(data);

break;

case 3:

inorderTraversal(root);

printf("\n");

break;

case 4:

exit(0);

default:

printf("Not available\n");

break;

} printf("\n"); }

return 0;}

oUtPUt:
Program of n qUeen Problem Using
backtracking algo:
#include<stdio.h>

#include<math.h>

int board[20],count;

int main()

int n,i,j;

void queen(int row,int n);

printf(" - N Queens Problem Using Backtracking -");

printf("\n\nEnter number of Queens:");

scanf("%d",&n);

queen(1,n);

return 0;

//function for printing the solution

void print(int n)

int i,j;

printf("\n\nSolution %d:\n\n",++count);

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

printf("\t%d",i);

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

printf("\n\n%d",i);

for(j=1;j<=n;++j) //for nxn board

if(board[i]==j)

printf("\tQ"); //queen at i,j position

else

printf("\t-"); //empty slot

}}}

/*funtion to check conflicts

If no conflict for desired postion returns 1 otherwise returns 0*/

int place(int row,int column){


int i;

for(i=1;i<=row-1;++i)

//checking column and digonal conflicts

if(board[i]==column)

return 0;

else

if(abs(board[i]-column)==abs(i-row))

return 0;

return 1; //no conflicts

//function to check for proper positioning of queen

void queen(int row,int n){

int column;

for(column=1;column<=n;++column){

if(place(row,column)){

board[row]=column; //no conflicts so place queen

if(row==n) //dead end

print(n); //printing the board configuration

else //try queen with next position

queen(row+1,n);

}}}

oUtPUt:
Program of btree algo:
#include <stdio.h>

#include <stdlib.h>

#define MAX 3

#define MIN 2

struct BTreeNode {

int val[MAX + 1], count;

struct BTreeNode *link[MAX + 1];

};

struct BTreeNode *root;

// Create a node

struct BTreeNode *createNode(int val, struct BTreeNode *child) {

struct BTreeNode *newNode;

newNode = (struct BTreeNode *)malloc(sizeof(struct BTreeNode));

newNode->val[1] = val;

newNode->count = 1;

newNode->link[0] = root;

newNode->link[1] = child;

return newNode;

// Insert node

void insertNode(int val, int pos, struct BTreeNode *node,

struct BTreeNode *child) {

int j = node->count;

while (j > pos) {

node->val[j + 1] = node->val[j];

node->link[j + 1] = node->link[j];

j--;

node->val[j + 1] = val;

node->link[j + 1] = child;

node->count++;

// Split node

void splitNode(int val, int *pval, int pos, struct BTreeNode *node,
struct BTreeNode *child, struct BTreeNode **newNode) {

int median, j;

if (pos > MIN)

median = MIN + 1;

else

median = MIN;

*newNode = (struct BTreeNode *)malloc(sizeof(struct BTreeNode));

j = median + 1;

while (j <= MAX) {

(*newNode)->val[j - median] = node->val[j];

(*newNode)->link[j - median] = node->link[j];

j++;

node->count = median;

(*newNode)->count = MAX - median;

if (pos <= MIN) {

insertNode(val, pos, node, child);

} else {

insertNode(val, pos - median, *newNode, child);

*pval = node->val[node->count];

(*newNode)->link[0] = node->link[node->count];

node->count--;

// Set the value

int setValue(int val, int *pval,

struct BTreeNode *node, struct BTreeNode **child) {

int pos;

if (!node) {

*pval = val;

*child = NULL;

return 1;

if (val < node->val[1]) {

pos = 0;

} else {
for (pos = node->count;

(val < node->val[pos] && pos > 1); pos--)

if (val == node->val[pos]) {

printf("Duplicates are not permitted\n");

return 0;

if (setValue(val, pval, node->link[pos], child)) {

if (node->count < MAX) {

insertNode(*pval, pos, node, *child);

} else {

splitNode(*pval, pval, pos, node, *child, child);

return 1;

return 0;

// Insert the value

void insert(int val) {

int flag, i;

struct BTreeNode *child;

flag = setValue(val, &i, root, &child);

if (flag)

root = createNode(i, child);

// Search node

void search(int val, int *pos, struct BTreeNode *myNode) {

if (!myNode) {

return;}

if (val < myNode->val[1]) {

*pos = 0;

} else {

for (*pos = myNode->count;

(val < myNode->val[*pos] && *pos > 1); (*pos)--):


if (val == myNode->val[*pos]) {

printf("%d is found", val);

return;}}

search(val, pos, myNode->link[*pos]);

return;}

// Traverse then nodes

void traversal(struct BTreeNode *myNode) {

int i;

if (myNode) {

for (i = 0; i < myNode->count; i++) {

traversal(myNode->link[i]);

printf("%d ", myNode->val[i + 1])}

traversal(myNode->link[i]);}}

int main() {

int val, ch;

insert(8);

insert(9);

insert(10);

insert(11);

insert(15);

insert(16);

insert(17);

insert(18);

insert(20);

insert(23);

traversal(root);

printf("\n");

search(11, &ch, root);}

oUtPUt:
Program of fibonacci heaP algo:
#include <math.h>

#include <stdbool.h>

#include <stdio.h>

#include <stdlib.h>

typedef struct _NODE {

int key;

int degree;

struct _NODE *left_sibling;

struct _NODE *right_sibling;

struct _NODE *parent;

struct _NODE *child;

bool mark;

bool visited;

} NODE;

typedef struct fibanocci_heap {

int n;

NODE *min;

int phi;

int degree;

} FIB_HEAP;

FIB_HEAP *make_fib_heap();

void insertion(FIB_HEAP *H, NODE *new, int val);

NODE *extract_min(FIB_HEAP *H);

void consolidate(FIB_HEAP *H);

void fib_heap_link(FIB_HEAP *H, NODE *y, NODE *x);

NODE *find_min_node(FIB_HEAP *H);

void decrease_key(FIB_HEAP *H, NODE *node, int key);

void cut(FIB_HEAP *H, NODE *node_to_be_decrease, NODE *parent_node);

void cascading_cut(FIB_HEAP *H, NODE *parent_node);

void Delete_Node(FIB_HEAP *H, int dec_key);

FIB_HEAP *make_fib_heap() {

FIB_HEAP *H;

H = (FIB_HEAP *)malloc(sizeof(FIB_HEAP));

H->n = 0; H->min = NULL;


H->phi = 0;

H->degree = 0;

return H;}

// Printing the heap

void print_heap(NODE *n) {

NODE *x;

for (x = n;; x = x->right_sibling) {

if (x->child == NULL) {

printf("node with no child (%d) \n", x->key);

} else {

printf("NODE(%d) with child (%d)\n", x->key, x->child->key);

print_heap(x->child);

if (x->right_sibling == n) {

break; } }}

// Inserting nodes

void insertion(FIB_HEAP *H, NODE *new, int val) {

new = (NODE *)malloc(sizeof(NODE));

new->key = val;

new->degree = 0;

new->mark = false;

new->parent = NULL;

new->child = NULL;

new->visited = false;

new->left_sibling = new;

new->right_sibling = new;

if (H->min == NULL) {

H->min = new;

} else {

H->min->left_sibling->right_sibling = new;

new->right_sibling = H->min;

new->left_sibling = H->min->left_sibling;

H->min->left_sibling = new;

if (new->key < H->min->key) {

H->min = new; } }

(H->n)++;}
// Find min node

NODE *find_min_node(FIB_HEAP *H) {

if (H == NULL) {

printf(" \n Fibonacci heap not yet created \n");

return NULL;

} else

return H->min;}

// Union operation

FIB_HEAP *unionHeap(FIB_HEAP *H1, FIB_HEAP *H2) {

FIB_HEAP *Hnew;

Hnew = make_fib_heap();

Hnew->min = H1->min;

NODE *temp1, *temp2;

temp1 = Hnew->min->right_sibling;

temp2 = H2->min->left_sibling;

Hnew->min->right_sibling->left_sibling = H2->min->left_sibling;

Hnew->min->right_sibling = H2->min;

H2->min->left_sibling = Hnew->min;

temp2->right_sibling = temp1;

if ((H1->min == NULL) || (H2->min != NULL && H2->min->key < H1->min->key))

Hnew->min = H2->min;

Hnew->n = H1->n + H2->n;

return Hnew;}

// Calculate the degree

int cal_degree(int n) {

int count = 0;

while (n > 0) {

n = n / 2;

count++; }

return count;}

// Consolidate function

void consolidate(FIB_HEAP *H) {

int degree, i, d;

degree = cal_degree(H->n);

NODE *A[degree], *x, *y, *z;

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


A[i] = NULL; }

x = H->min;

do {

d = x->degree;

while (A[d] != NULL) {

y = A[d];

if (x->key > y->key) {

NODE *exchange_help;

exchange_help = x;

x = y;

y = exchange_help;

if (y == H->min)

H->min = x;

fib_heap_link(H, y, x);

if (y->right_sibling == x)

H->min = x;

A[d] = NULL;

d++;

A[d] = x;

x = x->right_sibling;

} while (x != H->min);

H->min = NULL;

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

if (A[i] != NULL) {

A[i]->left_sibling = A[i];

A[i]->right_sibling = A[i];

if (H->min == NULL) {

H->min = A[i];

} else {

H->min->left_sibling->right_sibling = A[i];

A[i]->right_sibling = H->min;

A[i]->left_sibling = H->min->left_sibling;

H->min->left_sibling = A[i];

if (A[i]->key < H->min->key) {


H->min = A[i]; } }

if (H->min == NULL) {

H->min = A[i];

} else if (A[i]->key < H->min->key) {

H->min = A[i]; }}}}

// Linking

void fib_heap_link(FIB_HEAP *H, NODE *y, NODE *x) {

y->right_sibling->left_sibling = y->left_sibling;

y->left_sibling->right_sibling = y->right_sibling;

if (x->right_sibling == x)

H->min = x;

y->left_sibling = y;

y->right_sibling = y;

y->parent = x;

if (x->child == NULL) {

x->child = y;

y->right_sibling = x->child;

y->left_sibling = x->child->left_sibling;

x->child->left_sibling->right_sibling = y;

x->child->left_sibling = y;

if ((y->key) < (x->child->key))

x->child = y;

(x->degree)++;

// Extract min

NODE *extract_min(FIB_HEAP *H) {

if (H->min == NULL)

printf("\n The heap is empty");

else {

NODE *temp = H->min;

NODE *pntr;

pntr = temp;

NODE *x = NULL;

if (temp->child != NULL) {

x = temp->child;
do {

pntr = x->right_sibling;

(H->min->left_sibling)->right_sibling = x;

x->right_sibling = H->min;

x->left_sibling = H->min->left_sibling;

H->min->left_sibling = x;

if (x->key < H->min->key)

H->min = x;

x->parent = NULL;

x = pntr;

} while (pntr != temp->child);

(temp->left_sibling)->right_sibling = temp->right_sibling;

(temp->right_sibling)->left_sibling = temp->left_sibling;

H->min = temp->right_sibling;

if (temp == temp->right_sibling && temp->child == NULL)

H->min = NULL;

else {

H->min = temp->right_sibling;

consolidate(H);

H->n = H->n - 1;

return temp;

return H->min;

void cut(FIB_HEAP *H, NODE *node_to_be_decrease, NODE *parent_node) {

NODE *temp_parent_check;

if (node_to_be_decrease == node_to_be_decrease->right_sibling)

parent_node->child = NULL;

node_to_be_decrease->left_sibling->right_sibling = node_to_be_decrease->right_sibling;

node_to_be_decrease->right_sibling->left_sibling = node_to_be_decrease->left_sibling;

if (node_to_be_decrease == parent_node->child)

parent_node->child = node_to_be_decrease->right_sibling;

(parent_node->degree)--;

node_to_be_decrease->left_sibling = node_to_be_decrease;
node_to_be_decrease->right_sibling = node_to_be_decrease;

H->min->left_sibling->right_sibling = node_to_be_decrease;

node_to_be_decrease->right_sibling = H->min;

node_to_be_decrease->left_sibling = H->min->left_sibling;

H->min->left_sibling = node_to_be_decrease;

node_to_be_decrease->parent = NULL;

node_to_be_decrease->mark = false;

void cascading_cut(FIB_HEAP *H, NODE *parent_node) {

NODE *aux;

aux = parent_node->parent;

if (aux != NULL) {

if (parent_node->mark == false) {

parent_node->mark = true;

} else {

cut(H, parent_node, aux);

cascading_cut(H, aux); } }}

void decrease_key(FIB_HEAP *H, NODE *node_to_be_decrease, int new_key) {

NODE *parent_node;

if (H == NULL) {

printf("\n FIbonacci heap not created ");

return;

if (node_to_be_decrease == NULL) {

printf("Node is not in the heap");

else {

if (node_to_be_decrease->key < new_key) {

printf("\n Invalid new key for decrease key operation \n ");

} else {

node_to_be_decrease->key = new_key;

parent_node = node_to_be_decrease->parent;

if ((parent_node != NULL) && (node_to_be_decrease->key < parent_node->key)) {

printf("\n cut called");

cut(H, node_to_be_decrease, parent_node);

printf("\n cascading cut called");


cascading_cut(H, parent_node);

if (node_to_be_decrease->key < H->min->key) {

H->min = node_to_be_decrease; }}}}

void *find_node(FIB_HEAP *H, NODE *n, int key, int new_key) {

NODE *find_use = n;

NODE *f = NULL;

find_use->visited = true;

if (find_use->key == key) {

find_use->visited = false;

f = find_use;

decrease_key(H, f, new_key);

if (find_use->child != NULL) {

find_node(H, find_use->child, key, new_key);

if ((find_use->right_sibling->visited != true)) {

find_node(H, find_use->right_sibling, key, new_key);

find_use->visited = false;

FIB_HEAP *insertion_procedure() {

FIB_HEAP *temp;

int no_of_nodes, ele, i;

NODE *new_node;

temp = (FIB_HEAP *)malloc(sizeof(FIB_HEAP));

temp = NULL;

if (temp == NULL) {

temp = make_fib_heap();

printf(" \n enter number of nodes to be insert = ");

scanf("%d", &no_of_nodes);

for (i = 1; i <= no_of_nodes; i++) {

printf("\n node %d and its key value = ", i);

scanf("%d", &ele);

insertion(temp, new_node, ele); }


return temp;}

void Delete_Node(FIB_HEAP *H, int dec_key) {

NODE *p = NULL;

find_node(H, H->min, dec_key, -5000);

p = extract_min(H);

if (p != NULL)

printf("\n Node deleted");

else

printf("\n Node not deleted:some error");

int main(int argc, char **argv) {

NODE *new_node, *min_node, *extracted_min, *node_to_be_decrease, *find_use;

FIB_HEAP *heap, *h1, *h2;

int operation_no, new_key, dec_key, ele, i, no_of_nodes;

heap = (FIB_HEAP *)malloc(sizeof(FIB_HEAP));

heap = NULL;

while (1) {

printf(" \n Operations \n 1. Create Fibonacci heap \n 2. Insert nodes into fibonacci


heap \n 3. Find min \n 4. Union \n 5. Extract min \n 6. Decrease key \n 7.Delete node \n 8.
print heap \n 9. exit \n enter operation_no = ");

scanf("%d", &operation_no);

switch (operation_no) {

case 1:

heap = make_fib_heap();

break;

case 2:

if (heap == NULL) {

heap = make_fib_heap();

printf(" enter number of nodes to be insert = ");

scanf("%d", &no_of_nodes);

for (i = 1; i <= no_of_nodes; i++) {

printf("\n node %d and its key value = ", i);

scanf("%d", &ele);

insertion(heap, new_node, ele);

break;
case 3:

min_node = find_min_node(heap);

if (min_node == NULL)

printf("No minimum value");

else

printf("\n min value = %d", min_node->key);

break;

case 4:

if (heap == NULL) {

printf("\n no FIbonacci heap created \n ");

break;

h1 = insertion_procedure();

heap = unionHeap(heap, h1);

printf("Unified Heap:\n");

print_heap(heap->min);

break;

case 5:

if (heap == NULL)

printf("Empty Fibonacci heap");

else {

extracted_min = extract_min(heap);

printf("\n min value = %d", extracted_min->key);

printf("\n Updated heap: \n");

print_heap(heap->min);

break;

case 6:

if (heap == NULL)

printf("Fibonacci heap is empty");

else {

printf(" \n node to be decreased = ");

scanf("%d", &dec_key);

printf(" \n enter the new key = ");

scanf("%d", &new_key);

find_use = heap->min;
find_node(heap, find_use, dec_key, new_key);

printf("\n Key decreased- Corresponding heap:\n");

print_heap(heap->min);

break;

case 7:

if (heap == NULL)

printf("Fibonacci heap is empty");

else {

printf(" \n Enter node key to be deleted = ");

scanf("%d", &dec_key);

Delete_Node(heap, dec_key);

printf("\n Node Deleted- Corresponding heap:\n");

print_heap(heap->min);

break;

case 8:

print_heap(heap->min);

break;

case 9:

free(new_node);

free(heap);

exit(0);

default:

printf("Invalid choice "); }}}


Program of longest common sUbseqUence algo:
#include <stdio.h>

#include <string.h>

int i, j, m, n, LCS_table[20][20];

char S1[20] = "ACADB", S2[20] = "CBDA", b[20][20];

void lcsAlgo() {

m = strlen(S1);

n = strlen(S2);

// Filling 0's in the matrix

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

LCS_table[i][0] = 0;

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

LCS_table[0][i] = 0;

// Building the mtrix in bottom-up way

for (i = 1; i <= m; i++)

for (j = 1; j <= n; j++) {

if (S1[i - 1] == S2[j - 1]) {

LCS_table[i][j] = LCS_table[i - 1][j - 1] + 1;

} else if (LCS_table[i - 1][j] >= LCS_table[i][j - 1]) {

LCS_table[i][j] = LCS_table[i - 1][j];

} else {

LCS_table[i][j] = LCS_table[i][j - 1];

int index = LCS_table[m][n];

char lcsAlgo[index + 1];

lcsAlgo[index] = '\0';

int i = m, j = n;

while (i > 0 && j > 0) {

if (S1[i - 1] == S2[j - 1]) {

lcsAlgo[index - 1] = S1[i - 1];

i--;

j--;

index--; }

else if (LCS_table[i - 1][j] > LCS_table[i][j - 1])


i--;

else

j--;

// Printing the sub sequences

printf("S1 : %s \nS2 : %s \n", S1, S2);

printf("LCS: %s", lcsAlgo);

int main() {

lcsAlgo();

printf("\n");

oUtPUt:
Program of hUffman coding algo:
#include <stdio.h>

#include <stdlib.h>

#define MAX_TREE_HT 50

struct MinHNode {

char item;

unsigned freq;

struct MinHNode *left, *right;

};

struct MinHeap {

unsigned size;

unsigned capacity;

struct MinHNode **array;

};

// Create nodes

struct MinHNode *newNode(char item, unsigned freq) {

struct MinHNode *temp = (struct MinHNode *)malloc(sizeof(struct MinHNode));

temp->left = temp->right = NULL;

temp->item = item;

temp->freq = freq;

return temp;

// Create min heap

struct MinHeap *createMinH(unsigned capacity) {

struct MinHeap *minHeap = (struct MinHeap *)malloc(sizeof(struct MinHeap));

minHeap->size = 0;

minHeap->capacity = capacity;

minHeap->array = (struct MinHNode **)malloc(minHeap->capacity * sizeof(struct MinHNode


*));

return minHeap;

// Function to swap

void swapMinHNode(struct MinHNode **a, struct MinHNode **b) {

struct MinHNode *t = *a;

*a = *b;

*b = t;}
// Heapify

void minHeapify(struct MinHeap *minHeap, int idx) {

int smallest = idx;

int left = 2 * idx + 1;

int right = 2 * idx + 2;

if (left < minHeap->size && minHeap->array[left]->freq < minHeap->array[smallest]->freq)

smallest = left;

if (right < minHeap->size && minHeap->array[right]->freq < minHeap->array[smallest]-


>freq)

smallest = right;

if (smallest != idx) {

swapMinHNode(&minHeap->array[smallest], &minHeap->array[idx]);

minHeapify(minHeap, smallest); }}

// Check if size if 1

int checkSizeOne(struct MinHeap *minHeap) {

return (minHeap->size == 1);

// Extract min

struct MinHNode *extractMin(struct MinHeap *minHeap) {

struct MinHNode *temp = minHeap->array[0];

minHeap->array[0] = minHeap->array[minHeap->size - 1];

--minHeap->size;

minHeapify(minHeap, 0);

return temp;

// Insertion function

void insertMinHeap(struct MinHeap *minHeap, struct MinHNode *minHeapNode) {

++minHeap->size;

int i = minHeap->size - 1;

while (i && minHeapNode->freq < minHeap->array[(i - 1) / 2]->freq) {

minHeap->array[i] = minHeap->array[(i - 1) / 2];

i = (i - 1) / 2; }

minHeap->array[i] = minHeapNode;}

void buildMinHeap(struct MinHeap *minHeap) {

int n = minHeap->size - 1;

int i;
for (i = (n - 1) / 2; i >= 0; --i)

minHeapify(minHeap, i);

int isLeaf(struct MinHNode *root) {

return !(root->left) && !(root->right);

struct MinHeap *createAndBuildMinHeap(char item[], int freq[], int size) {

struct MinHeap *minHeap = createMinH(size);

for (int i = 0; i < size; ++i)

minHeap->array[i] = newNode(item[i], freq[i]);

minHeap->size = size;

buildMinHeap(minHeap);

return minHeap;

struct MinHNode *buildHuffmanTree(char item[], int freq[], int size) {

struct MinHNode *left, *right, *top;

struct MinHeap *minHeap = createAndBuildMinHeap(item, freq, size);

while (!checkSizeOne(minHeap)) {

left = extractMin(minHeap);

right = extractMin(minHeap);

top = newNode('$', left->freq + right->freq);

top->left = left;

top->right = right;

insertMinHeap(minHeap, top);

} return extractMin(minHeap);

// Print the array

void printArray(int arr[], int n) {

int i;

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

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

printf("\n");

void printHCodes(struct MinHNode *root, int arr[], int top)

if (root->left)

{ arr[top] = 0;
printHCodes(root->left, arr, top + 1);

if (root->right)

arr[top] = 1;

printHCodes(root->right, arr, top + 1);

if (isLeaf(root))

printf(" %c | ", root->item);

printArray(arr, top); }}

// Wrapper function

void HuffmanCodes(char item[], int freq[], int size) {

struct MinHNode *root = buildHuffmanTree(item, freq, size);

int arr[MAX_TREE_HT], top = 0;

printHCodes(root, arr, top);

}}

int main() {

char arr[] = {'A', 'B', 'C', 'D'};

int freq[] = {5, 1, 6, 3};

int size = sizeof(arr) / sizeof(arr[0]);

printf(" Char | Huffman code ");

printf("\n--------------------\n");

HuffmanCodes(arr, freq, size);

}
Program of hamiltonian cycle
#include<stdio.h>

#define V 5

void printSolution(int path[]);

bool isSafe(int v, bool graph[V][V], int path[], int pos)

if (graph [ path[pos-1] ][ v ] == 0)

return false;

for (int i = 0; i < pos; i++)

if (path[i] == v)

return false;

return true;

bool hamCycleUtil(bool graph[V][V], int path[], int pos)

if (pos == V)

if ( graph[ path[pos-1] ][ path[0] ] == 1 )

return true;

else

return false;

for (int v = 1; v < V; v++)

if (isSafe(v, graph, path, pos))

path[pos] = v;

if (hamCycleUtil (graph, path, pos+1) == true)

return true;

path[pos] = -1;

} }

return false;

bool hamCycle(bool graph[V][V])

{ int *path = new int[V];


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

path[i] = -1;

path[0] = 0;

if ( hamCycleUtil(graph, path, 1) == false )

printf("\nSolution does not exist");

return false;

printSolution(path);

return true;

void printSolution(int path[])

printf ("Solution Exists:"

" Following is one Hamiltonian Cycle \n");

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

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

printf(" %d ", path[0]);

printf("\n");

int main()

bool graph1[V][V] = {{0, 1, 0, 1, 0},

{1, 0, 1, 1, 1},

{0, 1, 0, 0, 1},

{1, 1, 0, 0, 1},

{0, 1, 1, 1, 0},

};

hamCycle(graph1);

bool graph2[V][V] = {{0, 1, 0, 1, 0},

{1, 0, 1, 1, 1},

{0, 1, 0, 0, 1},

{1, 1, 0, 0, 0},

{0, 1, 1, 0, 0},

};

// Print the solution


hamCycle(graph2);

return 0;

oUtPUt:
Program of actiVity selection Problem:
#include <stdio.h>

void printMaxActivities(int s[], int f[], int n)

{ int i, j;

printf("Following activities are selected n");

i = 0;

printf("%d ", i);

for (j = 1; j < n; j++) {

if (s[j] >= f[i]) {

printf("%d ", j);

i = j; }}}

int main()

int s[] = { 1, 3, 0, 5, 8, 5 };
index

s.no Program date sign


1. BINARY SEARCH TREE

2. BUCKET SORT

3. COUNTING SORT

4. HEAP SORT
5. INSERTION SORT

6. MERGE SORT

7. QUICK SORT
8. RADIX SORT

9. RBT INSERT

10. SHELL SORT

11. KNAPSACK PROBLEM

12. TRAVELLING SALESMAN PROBLEM

13. RBT DELETE

14. N QUEEN PROBLEM USING BACKTRACKING

15. B-TREE

16. FIBONACCI HEAP

17. LCS

18. HUFFMAN CODING

19. HAMILTONIAN CYCLE

20. ACTIVITY SELECTION PROBLEM


int f[] = { 2, 4, 6, 7, 9, 9 };

int n = sizeof(s) / sizeof(s[0]);

printMaxActivities(s, f, n);

return 0;

oUtPUt:

You might also like