Adsa Lab Manual
Adsa Lab Manual
Course Objectives:
Graph Traversals
Sorting techniques
N-Queens Problem
Job Sequencing
Experiments:
1. Construct an AVL tree for a given set of elements which are stored in a
file. And implement insert and delete operation on the constructed tree.
3. Construct Min and Max Heap using arrays, delete any element and
display
4. Implement BFT and DFT for given graph, when graph is represented by
lists.
12. Implement Travelling Sales Person problem using Branch and Bound
approach.
LTPC
0 0 3 1.5
Reference Books:
McGrawHill
1. http://cse01-iiith.vlabs.ac.in/
2. http://peterindia.net/Algorithms.html
1. Construct an AVL tree for a given set of elements which are stored in a
file. And implement insert and delete operation on the constructed tree.
Write contents of tree into a new file using in-order.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
if (newNode == NULL) {
exit(1);
newNode->data = data;
return newNode;
}
// Function to calculate the height of a node
if (node == NULL)
return 0;
return node->height;
return (a > b) ? a : b;
// Perform rotation
x->right = y;
y->left = T2;
// Update heights
return x;
}
// Function to perform left rotation
// Perform rotation
y->left = x;
x->right = T2;
// Update heights
return y;
if (node == NULL)
return 0;
if (root == NULL)
return createNode(data);
else
return rightRotate(root);
return leftRotate(root);
root->left = leftRotate(root->left);
return rightRotate(root);
root->right = rightRotate(root->right);
return leftRotate(root);
return root;
// Function to find the node with the minimum value in the tree
current = current->left;
return current;
if (root == NULL)
return root;
else {
// No child case
if (temp == NULL) {
temp = root;
root = NULL;
free(temp);
} else {
root->data = temp->data;
if (root == NULL)
return root;
return rightRotate(root);
root->left = leftRotate(root->left);
return rightRotate(root);
return leftRotate(root);
root->right = rightRotate(root->right);
return leftRotate(root);
return root;
if(root != NULL) {
inOrderTraversal(root->left);
printf("%d ",root->data);
inOrderTraversal(root->right);
if (root == NULL)
return;
freeMemory(root->left);
freeMemory(root->right);
free(root);
int main() {
int choice,value;
do
scanf("%d",&choice);
switch(choice)
scanf("%d",&value);
break;
break;
case 3: inOrderTraversal(root);
break;
case 4: freeMemory(root);
break;
}while(choice!=4);
return 0;
OUTPUT
1. Insertion
2. Deletion
3. Display
4. Exit
1. Insertion
2. Deletion
3. Display
4. Exit
1. Insertion
2. Deletion
3. Display
4. Exit
1. Insertion
2. Deletion
3. Display
4. Exit
1. Insertion
2. Deletion
3. Display
4. Exit
1. Insertion
2. Deletion
3. Display
4. Exit
1. Insertion
2. Deletion
3. Display
4. Exit
123456
1. Insertion
2. Deletion
3. Display
4. Exit
1. Insertion
2. Deletion
3. Display
4. Exit
1. Insertion
2. Deletion
3. Display
4. Exit
1. Insertion
2. Deletion
3. Display
4. Exit
456
1. Insertion
2. Deletion
3. Display
4. Exit
#include <stdio.h>
#include <stdlib.h>
int keys[MAX_KEYS];
int numKeys;
int isLeaf;
} BTreeNode;
node->numKeys = 0;
node->children[i] = NULL;
return node;
newChild->numKeys = MIN_KEYS;
if (!child->isLeaf) {
child->numKeys = MIN_KEYS;
parent->children[i + 1] = parent->children[i];
parent->children[index + 1] = newChild;
for (int i = parent->numKeys - 1; i >= index; i--) {
parent->keys[i + 1] = parent->keys[i];
parent->keys[index] = child->keys[MIN_KEYS];
parent->numKeys++;
int i = node->numKeys - 1;
if (node->isLeaf) {
node->keys[i + 1] = node->keys[i];
i--;
node->keys[i + 1] = key;
node->numKeys++;
} else {
i--;
BTreeNode* r = *root;
if (r->numKeys == MAX_KEYS) {
newRoot->children[0] = r;
splitChild(newRoot, 0, r);
int i = 0;
i++;
insertNonFull(newRoot->children[i], key);
*root = newRoot;
} else {
insertNonFull(r, key);
int i;
traverse(root->children[i]);
if (!root->isLeaf) {
traverse(root->children[i]);
int main() {
insert(&root, 10);
insert(&root, 20);
insert(&root, 5);
insert(&root, 6);
insert(&root, 12);
insert(&root, 30);
insert(&root, 7);
insert(&root, 17);
traverse(root);
return 0;
}
OUTPUT
5 6 7 10 12 17 20 30
3. Construct Min and Max Heap using arrays, delete any element and
display the content of the Heap.
#include <stdio.h>
int t = *a;
*a = *b;
*b = t;
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
// now check whether the right and left right is larger than the root or
not
largest = left;
}
if (right < n && arr[right] > arr[largest])
largest = right;
// if the root is smaller than the children then swap it with the largest
children's value
if (largest != i)
swap(&arr[i], &arr[largest]);
// again heapify that side of the heap where the root has gone
heapify(arr, n, largest);
heapify(arr, n, i);
swap(&arr[i], &arr[0]);
heapify(arr, i, 0);
}
int main()
scanf("%d",&count);
for(i=0;i<count;i++)
scanf("%d",&number[i]);
heapsort(number,count);
for(i=0;i<count;i++)
printf(" %d",number[i]);
return 0;
OUTPUT
Enter 1 element: 2
Enter 2 element: 5
Enter 3 element: 8
Enter 4 element: 10
Enter 5 element: 3
Enter 6 element: 1
Enter 7 element: 4
Enter 8 element: 6
Enter 9 element: 7
Enter 10 element: 9
#include <stdio.h>
void dfs(int v)
int i;
visited[v] = 1;
dfs(i);
int main( )
scanf("%d", &n);
for (i = 1; i <= n; i++)
visited[i] = 0;
a[i][j] = 0;
scanf("%d", &a[i][j]);
scanf("%d", &v);
dfs(v);
return 0;
OUTPUT
011000
101000
110110
001000
001001
000010
2->1
1->3
3->4
3->5
5->6
#include<stdio.h>
int queue[10];
int front=0,back=0;
queue[back] = var;
back++;
void dequeue()
queue[front] = 0;
front++;
int main()
int v,n,i,j;
// adjacenty matrix representing graph
int graph[10][10];
scanf("%d", &n);
scanf("%d", &graph[i][j]);
scanf("%d", &v);
enqueue(v);
while(front != back)
dequeue();
enqueue(i+1);
}
}
return 0;
OUTPUT
011000
101000
110110
001000
001001
000010
2132456
5. Write a program for finding the biconnected components in a given
graph.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int u;
int v;
} Edge;
// Global variables
adj[u][v] = 1;
adj[v][u] = 1;
void push(Edge e) {
stack[++top] = e;
Edge pop() {
return stack[top--];
void dfs(int u) {
visited[u] = true;
if (adj[u][v]) {
if (!visited[v]) {
parent[v] = u;
push((Edge){u, v});
dfs(v);
low[u] = (low[u] < low[v]) ? low[u] : low[v];
printf("Biconnected component:\n");
Edge e;
do {
e = pop();
printf("\n");
} else if (v != parent[u]) {
void findBiconnectedComponents() {
visited[i] = false;
parent[i] = -1;
time = 0;
dfs(i);
int main() {
// Example usage
n = 5;
m = 6;
addEdge(0, 1);
addEdge(1, 2);
addEdge(2, 0);
addEdge(1, 3);
addEdge(3, 4);
addEdge(4, 1);
findBiconnectedComponents();
return 0;
}
6. Implement Quick sort and observe the execution time for various input
sizes (Average, Worst and Best cases).
#include <stdio.h>
int t = *a;
*a = *b;
*b = t;
if(first<last)
i=first;
j=last;
while(i<j)
while(number[i]<=number[pivot]&&i<last)
i++;
while(number[j]>number[pivot])
j--;
swap(&number[i], &number[j]);
swap(&number[pivot], &number[j]);
quicksort(number,first,j-1);
quicksort(number,j+1,last);
int main()
scanf("%d",&count);
for(i=0;i<count;i++)
scanf("%d",&number[i]);
quicksort(number,0,count-1);
for(i=0;i<count;i++)
printf(" %d",number[i]);
return 0;
}
OUTPUT
Enter 1 element: 3
Enter 2 element: 6
Enter 3 element: 9
Enter 4 element: 8
Enter 5 element: 5
Enter 6 element: 2
Enter 7 element: 1
Enter 8 element: 4
Enter 9 element: 7
Enter 10 element: 10
6. Implement Merge sort and observe the execution time for various input
sizes (Average, Worst and Best cases).
#include <stdio.h>
int i, j, k, B[100];
i = low;
j = mid + 1;
k = low;
{
B[k] = A[i];
i++;
k++;
else
B[k] = A[j];
j++;
k++;
B[k] = A[i];
k++;
i++;
B[k] = A[j];
k++;
j++;
A[i] = B[i];
}
void mergeSort(int number[], int low, int high)
int mid;
if(low<high)
int main()
scanf("%d",&count);
for(i=0;i<count;i++)
scanf("%d",&number[i]);
mergeSort(number,0,count-1);
printf(" %d",number[i]);
return 0;
OUTPUT
Enter 1 element: 10
Enter 2 element: 1
Enter 3 element: 4
Enter 4 element: 7
Enter 5 element: 8
Enter 6 element: 5
Enter 7 element: 2
Enter 8 element: 3
Enter 9 element: 6
Enter 10 element: 9
#include <stdlib.h>
struct Job {
int id;
int deadline;
int profit;
};
max = arr[i].deadline;
return max;
int sequence[maxDeadline];
sequence[i] = -1;
int totalProfit = 0;
int jobsScheduled = 0;
if (sequence[j] == -1) {
sequence[j] = arr[i].id;
totalProfit += arr[i].profit;
jobsScheduled++;
break;
if (sequence[i] != -1) {
printf("%d ", sequence[i]);
int main() {
{1, 2, 100},
{2, 1, 19},
{3, 2, 27},
{4, 1, 25},
{5, 3, 15}
};
jobSequencing(arr, n);
return 0;
Jobs scheduled: 3 1 5
int i, w;
int K[n+1][W+1];
if (i==0 || w==0)
K[i][w] = 0;
else
K[i][w] = K[i-1][w];
return K[n][W];
int main()
{
int val[] = {60, 100, 120};
int W = 50;
int n = sizeof(val)/sizeof(val[0]);
return 0;
#include<stdio.h>
#include<math.h>
int a[30],count=0;
int i;
for (i=1;i<pos;i++) {
if((a[i]==a[pos])||((abs(a[i]-a[pos])==abs(i-pos))))
return 0;
return 1;
void print_sol(int n) {
int i,j;
count++;
printf("\n");
for (i=1;i<=n;i++) {
for (j=1;j<=n;j++) {
if(a[i]==j)
printf("%d", j);
void queen(int n) {
int k=1;
a[k]=0;
while(k!=0) {
a[k]=a[k]+1;
while((a[k]<=n)&&!place(k))
a[k]++;
if(a[k]<=n) {
if(k==n)
print_sol(n); else {
k++;
a[k]=0;
} else
k--;
int main() {
int i,n;
scanf("%d",&n);
queen(n);
//getch();
Output:
2413
3142
Total Solutions = 2
/* Variable Description….
fp – Final Profit
fw – Final Weight
cp – Current Profit
cw – Current Weight
*/
#include <stdio.h>
#include <conio.h>
#define max 10
int w[max],i,j,p[max];
int n,m;
float unit[max];
int y[max],x[max],fp=-1,fw;
void get()
//Advertisement
scanf(“%d”,&n);
printf(“\n Enter the Maximum capacity of the Sack: “);
scanf(“%d”,&m);
for(i=0;i<n;i++)
scanf(“%d”,&w[i]);
scanf(“%d”, &p[i]);
void show()
float s=0.0;
for(i=0;i<n;i++)
printf(“\n\t%d\t%d\t%d\t%f\t%d”,i+1,w[i],p[i],unit[i],x[i]);
for(i=0;i<n;i++)
if(x[i]==1)
printf(“%d\t”,i+1);
void sort()
int t,t1;
float t2;
for(i=0;i<n;i++)
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
t2 = unit[i];
unit[i] = unit[j];
unit[j] = t2;
t = p[i];
p[i] = p[j];
Advertisement
p[j] = t;
t1 = w[i];
w[i] = w[j];
w[j] =t1;
float b = cp;
float c = cw;
for(i=k;i<=n;i++)
{
c = c+w[i];
if( c < m)
b = b +p[i];
else
return b;
if(cw+w[k] <= m)
y[k] = 1;
if(k <= n)
knapsack(k+1,cp+p[k],cw+w[k]);
fp = cp+p[k];
fw = cw+w[k];
for(j=0;j<=k;j++)
x[j] = y[j];
y[k] = 0;
if( k <= n)
knapsack(k+1,cp,cw);
fp = cp;
Advertisement
fw = cw;
for(j=0;j<=k;j++)
x[j] = y[j];
void main()
clrscr();
printf(“\n\t\t —————————————–“);
get();
sort();
knapsack(0,0.0,0.0);
show();
getch();
Output:
——————————————-
1 1 11 11.000000 1
2 11 21 1.909091 0
3 21 31 1.476190 1
12. Implement Travelling Sales Person problem using Branch and Bound
approach.
#include<conio.h>
void get() {
int i, j;
scanf("%d", &n);
scanf("%d", &a[i][j]);
visited[i] = 0;
printf("\n\n");
int i, ncity;
visited[city] = 1;
ncity = least(city);
if (ncity == 999) {
ncity = 0;
return;
mincost(ncity);
int least(int c) {
int i, nc = 999;
kmin = a[c][i];
nc = i;
if (min != 999)
cost += kmin;
return nc;
void put() {
printf("\n\nMinimum cost:");
printf("%d", cost);
void main() {
get();
mincost(0);
put();
}
Output:
$ gcc TSPBranchBound.c
$ ./a.out
99 10 15 20 99 8
5 99 9 10 8 99
6 13 99 12 99 5
8 8 9 99 6 99
99 10 99 6 99 99
10 99 5 99 99 99
Minimum cost:46