0% found this document useful (0 votes)
36 views17 pages

Ads Lab Manual (Micro)

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)
36 views17 pages

Ads Lab Manual (Micro)

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/ 17

WEEK 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>

// AVL Tree node


struct Node {
int key;
struct Node* left;
struct Node* right;
int height;
};

// Function to get height of the node


int getHeight(struct Node* n)
{
if (n == NULL)
return 0;
return n->height;
}

// Function to create a new node


struct Node* createNode(int key)
{
struct Node* node
= (struct Node*)malloc(sizeof(struct Node));
node->key = key;
node->left = NULL;
node->right = NULL;
node->height = 1; // New node is initially added at leaf
return node;
}

// Utility function to get the maximum of two integers


int max(int a, int b) { return (a > b) ? a : b; }

// Function to get balance factor of a node


int getBalanceFactor(struct Node* n)
{
if (n == NULL)
return 0;
return getHeight(n->left) - getHeight(n->right);
}

// Right rotation function


struct Node* rightRotate(struct Node* y)
{
struct Node* x = y->left;
struct Node* T2 = x->right;
// Perform rotation
x->right = y;
y->left = T2;

// Update heights
y->height
= max(getHeight(y->left), getHeight(y->right)) + 1;
x->height
= max(getHeight(x->left), getHeight(x->right)) + 1;

return x;
}

// Left rotation function


struct Node* leftRotate(struct Node* x)
{
struct Node* y = x->right;
struct Node* T2 = y->left;

// Perform rotation
y->left = x;
x->right = T2;

// Update heights
x->height
= max(getHeight(x->left), getHeight(x->right)) + 1;
y->height
= max(getHeight(y->left), getHeight(y->right)) + 1;

return y;
}

// Function to insert a key into AVL tree


struct Node* insert(struct Node* node, int key)
{
// 1. Perform standard BST insertion
if (node == NULL)
return createNode(key);

if (key < node->key)


node->left = insert(node->left, key);
else if (key > node->key)
node->right = insert(node->right, key);
else // Equal keys are not allowed in BST
return node;

// 2. Update height of this ancestor node


node->height = 1
+ max(getHeight(node->left),
getHeight(node->right));
// 3. Get the balance factor of this ancestor node to
// check whether this node became unbalanced
int balance = getBalanceFactor(node);

// 4. If the node becomes unbalanced, then there are 4


// cases

// Left Left Case


if (balance > 1 && key < node->left->key)
return rightRotate(node);

// Right Right Case


if (balance < -1 && key > node->right->key)
return leftRotate(node);

// Left Right Case


if (balance > 1 && key > node->left->key) {
node->left = leftRotate(node->left);
return rightRotate(node);
}

// Right Left Case


if (balance < -1 && key < node->right->key) {
node->right = rightRotate(node->right);
return leftRotate(node);
}

// Return the (unchanged) node pointer


return node;
}

// Function to perform preorder traversal of AVL tree


void inOrder(struct Node* root)
{
if (root != NULL) {
inOrder(root->left);
printf("%d ", root->key);
inOrder(root->right);
}
}

// Main function
int main()
{
struct Node* root = NULL;

// Inserting nodes
root = insert(root, 1);
root = insert(root, 2);
root = insert(root, 4);
root = insert(root, 5);
root = insert(root, 6);
root = insert(root, 3);

// Print preorder traversal of the AVL tree


printf("Inorder traversal of AVL tree: ");
inOrder(root);

return 0;
}

OUT PUT:

Inorder traversal of AVL tree: 1 2 3 4 5 6


WEEK 4: Write a C Program to Implement BFT and DFT forgiven graph, when graph is
represented by
a) Adjacency Matrix b) Adjacency Lists
#include <stdio.h>

#include <stdlib.h>

#define MAX 100

int adjMatrix[MAX][MAX];

int visited[MAX];

int queue[MAX], front = -1, rear = -1;

// Enqueue operation for BFS

void enqueue(int vertex)

if (rear == MAX - 1)

printf("\nQueue Overflow");

else

if (front == -1)

front = 0;

rear = rear + 1;

queue[rear] = vertex;

// Dequeue operation for BFS

int dequeue()

int vertex;

if (front == -1 || front > rear)

printf("\nQueue Underflow");

return -1;

else

{
vertex = queue[front];

front = front + 1;

return vertex;

// Breadth-First Traversal using Adjacency Matrix

void BFTMatrix(int n, int startVertex)

int i,vertex;

enqueue(startVertex);

visited[startVertex] = 1;

printf("BFS: ");

while (front <= rear)

vertex = dequeue();

printf("%d ", vertex);

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

if (adjMatrix[vertex][i] == 1 && !visited[i])

enqueue(i);

visited[i] = 1;

printf("\n");

// Depth-First Traversal using Adjacency Matrix

void DFTMatrix(int n, int vertex)

int i;

printf("%d ", vertex);


visited[vertex] = 1;

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

if (adjMatrix[vertex][i] == 1 && !visited[i])

DFTMatrix(n, i);

int main()

int n, i, j, startVertex;

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

scanf("%d", &n);

printf("Enter the adjacency matrix:\n");

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

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

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

printf("Enter the start vertex: ");

scanf("%d", &startVertex);

// BFS

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

visited[i] = 0; // Reset visited array

BFTMatrix(n, startVertex);

// DFS

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

visited[i] = 0; // Reset visited array

printf("DFS: ");
DFTMatrix(n, startVertex);

printf("\n");

scanf("%d",&n);

return 0;

OUTPUT:
Week 8
write a c program to Implement Job sequencing with deadlines using Greedy
strategy.

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

typedef struct Jobs


{
char id; // Jobs Id
int dead; // Deadline of Jobs
int profit; // Profit if Jobs is over before or on deadline
} Jobs;

int compare(const void* a, const void* b)


{
Jobs* temp1 = (Jobs*)a;
Jobs* temp2 = (Jobs*)b;
return (temp2->profit - temp1->profit);
}

int min(int num1, int num2)


{
return (num1 > num2) ? num2 : num1;
}
int main()
{
int i,j;
Jobs arr[] =
{
{ 'a', 2, 100 },
{ 'b', 2, 20 },
{ 'c', 1, 40 },
{ 'd', 3, 35 },
{ 'e', 1, 25 }
};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Following is maximum profit sequence of Jobs: \n");
qsort(arr, n, sizeof(Jobs), compare);
int result[n];
bool slot[n];

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


slot[i] = false;

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

for ( j = min(n, arr[i].dead) - 1; j >= 0; j--)


{

// Free slot found


if (slot[j] == false)
{
result[j] = i;
slot[j] = true;
break;
}
}
}
for ( i = 0; i < n; i++)
if (slot[i])
printf("%c ", arr[result[i]].id);
getch();
return 0;
}
OUTPUT:
Week 9

Write a c program to solve 0/1 Knapsack problem Using Dynamic Programming.

#include <stdio.h>
int max(int a, int b)
{
return (a > b)? a : b;
}
// Returns the maximum value that can be put in a knapsack of capacity W
int knapsack(int W, int wt[], int val[], int n)
{
int i, w;
int K[n+1][W+1];

// Build table K[][] in bottom up manner


for (i = 0; i <= n; i++)
{
for (w = 0; w <= W; w++)
{
if (i==0 || w==0)
K[i][w] = 0;
else if (wt[i-1] <= w)
K[i][w] = max(val[i-1] + K[i-1][w-wt[i-1]], K[i-1][w]);
else
K[i][w] = K[i-1][w];
}
}

return K[n][W];
}

int main()
{
int val[] = {60, 100, 120};
int wt[] = {10, 20, 30};
int W = 50;
int n = sizeof(val)/sizeof(val[0]);
printf("\nValue = %d", knapsack(W, wt, val, n));
getch();
return 0;
}
OUTPUT:
//WEEK -10
//C program to solve N Queen Problem using backtracking

#define N 4
#include <stdbool.h>
#include <stdio.h>

void printSolution(int board[N][N])


{
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if(board[i][j])
printf("Q ");
else
printf(". ");
}
printf("\n");
}
}

bool isSafe(int board[N][N], int row, int col)


{
int i, j;

// Check this row on left side


for (i = 0; i < col; i++)
if (board[row][i])
return false;

// Check upper diagonal on left side


for (i = row, j = col; i >= 0 && j >= 0; i--, j--)
if (board[i][j])
return false;

// Check lower diagonal on left side


for (i = row, j = col; j >= 0 && i < N; i++, j--)
if (board[i][j])
return false;
return true;
}

bool solveNQUtil(int board[N][N], int col)


{
if (col >= N)
return true;

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

// Check if the queen can be placed on


// board[i][col]
if (isSafe(board, i, col)) {

// Place this queen in board[i][col]


board[i][col] = 1;

// Recur to place rest of the queens


if (solveNQUtil(board, col + 1))
return true;

board[i][col] = 0; // BACKTRACK
}
}

return false;
}

bool solveNQ()
{
int board[N][N] = { { 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 } };

if (solveNQUtil(board, 0) == false) {
printf("Solution does not exist");
return false;
}
printSolution(board);
return true;
}

// Driver program to test above function


int main()
{
solveNQ();
return 0;
}

OUTPUT:
. . Q .
Q . . .
. . . Q
. Q . .
WEEK 11
Write a C program Using Backtracking strategy to solve 0/1 Knapsack problem
#include <stdio.h>
#define MAX_ITEMS 100
int maxProfit = 0;
int n;
int W;
int weights[MAX_ITEMS];
int values[MAX_ITEMS];

void knapsack(int index, int currentWeight, int currentProfit)


{
// Base case: if we've considered all items
if (index == n)
{
if (currentProfit > maxProfit)
{
maxProfit = currentProfit;
}
return;
}

// Exclude the current item and move to the next item


knapsack(index + 1, currentWeight, currentProfit);

// Include the current item if it doesn't exceed capacity


if (currentWeight + weights[index] <= W)
{
knapsack(index + 1, currentWeight + weights[index], currentProfit + values[index]);
}
}

int main()
{
int i;
printf("Enter the number of items: ");
scanf("%d", &n);
printf("Enter the maximum weight capacity of the knapsack: ");
scanf("%d", &W);

printf("Enter the weights and values of each item:\n");


for (i = 0; i < n; i++)
{
printf("Item %d - Weight: ", i + 1);
scanf("%d", &weights[i]);
printf("Item %d - Value: ", i + 1);
scanf("%d", &values[i]);
}

// Start the backtracking process from the first item


knapsack(0, 0, 0);

printf("Maximum profit: %d\n", maxProfit);


getch();
return 0;
}

OUTPUT:

You might also like