0% found this document useful (0 votes)
7 views

SnehaDSAassignment

The document contains multiple C programming assignments focusing on Binary Search Trees (BST) and graph representations using adjacency matrices. Key tasks include implementing BST operations, counting nodes, copying trees, and displaying tree levels, as well as creating and analyzing graphs. Additionally, it covers sorting algorithms like Heapsort and includes functions for calculating in-degrees and out-degrees of graph vertices.

Uploaded by

gauravshinde5545
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

SnehaDSAassignment

The document contains multiple C programming assignments focusing on Binary Search Trees (BST) and graph representations using adjacency matrices. Key tasks include implementing BST operations, counting nodes, copying trees, and displaying tree levels, as well as creating and analyzing graphs. Additionally, it covers sorting algorithms like Heapsort and includes functions for calculating in-degrees and out-degrees of graph vertices.

Uploaded by

gauravshinde5545
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 57

Assignment No.

01

Set - A

Q.a) Implement a Binary search tree (BST) library (btree.h) with operations create, search,
insert, inorder, preorder and postorder. Write a menu driven program that performs the
above operations.

Ans:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* left;
struct Node* right;
} Node;
Node* createNode(int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
Node* createTree() {
return NULL;
}
Node* insert(Node* root, int data) {
if (root == NULL) {
return createNode(data);
}
if (data < root->data) {
root->left = insert(root->left, data);
} else if (data > root->data) {
root->right = insert(root->right, data);
}
return root;
}
Node* search(Node* root, int data) {
if (root == NULL || root->data == data) {
return root;
}
if (data < root->data) {
return search(root->left, data);
}
return search(root->right, data);
}
void inorder(Node* root) {
if (root != NULL) {
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
}
void preorder(Node* root) {
if (root != NULL) {
printf("%d ", root->data);
preorder(root->left);
preorder(root->right);
}
}
void postorder(Node* root) {
if (root != NULL) {
postorder(root->left);
postorder(root->right);
printf("%d ", root->data);
}
}
void displayMenu() {
printf("\nBinary Search Tree Operations:\n");
printf("1. Create Tree\n");
printf("2. Insert Element\n");
printf("3. Search Element\n");
printf("4. Inorder Traversal\n");
printf("5. Preorder Traversal\n");
printf("6. Postorder Traversal\n");
printf("7. Exit\n");
printf("Enter your choice: ");
}
int main() {
Node* root = NULL;
int choice, value;
while (1) {
displayMenu();
scanf("%d", &choice);
switch (choice) {
case 1:
root = createTree();
printf("Tree created successfully.\n");
for (int i = 0; i < 5; i++) {
printf("Enter value to insert: ");
scanf("%d", &value);
root = insert(root, value);
printf("Element %d inserted.\n", value);
}
break;
case 2:
printf("Enter value to insert: ");
scanf("%d", &value);
root = insert(root, value);
printf("Element inserted.\n");
break;
case 3:
printf("Enter value to search: ");
scanf("%d", &value);
Node* result = search(root, value);
if (result != NULL) {
printf("Element found: %d\n", result->data);
} else {
printf("Element not found.\n");
}
break;
case 4:
printf("Inorder Traversal: ");
inorder(root);
printf("\n");
break;
case 5:
printf("Preorder Traversal: ");
preorder(root);
printf("\n");
break;
case 6:
printf("Postorder Traversal: ");
postorder(root);
printf("\n");
break;
case 7:
printf("Exiting program.\n");
exit(0);
break;
default:
printf("Invalid choice! Please try again.\n");
}
}
return 0;
}
Output :
Q.b) Write a program which uses binary search tree library and counts the total nodes and
total leaf nodes in the tree.
int count(T)-returns the total number of nodes from BST
int countLeaf(T)-returns the total number of leaf nodes from BST.

Ans:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *left;
struct Node *right;
} Node;
Node* createNode(int data) {
Node *newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
Node* insert(Node* root, int data) {
if (root == NULL) {
return createNode(data);
}
if (data < root->data) {
root->left = insert(root->left, data);
} else {
root->right = insert(root->right, data);
}
return root;
}
int count(Node* root) {
if (root == NULL) {
return 0;
}
return 1 + count(root->left) + count(root->right);
}
int countLeaf(Node* root) {
if (root == NULL) {
return 0;
}
if (root->left == NULL && root->right == NULL) {
return 1;
}
return countLeaf(root->left) + countLeaf(root->right);
}
void inOrderTraversal(Node* root) {
if (root == NULL) {
return;
}
inOrderTraversal(root->left);
printf("%d ", root->data);
inOrderTraversal(root->right);
}
int main() {
Node* root = NULL;
root = insert(root, 50);
root = insert(root, 30);
root = insert(root, 20);
root = insert(root, 40);
root = insert(root, 70);
root = insert(root, 60);
root = insert(root, 80);
int totalNodes = count(root);
int leafNodes = countLeaf(root);
printf("Total nodes in the BST: %d\n", totalNodes);
printf("Total leaf nodes in the BST: %d\n", leafNodes);
printf("Values in the BST: ");
inOrderTraversal(root);
printf("\n");
return 0;
}

Output :
Set - B

Q.a) Write a C program which uses Binary search tree library and implements following
function with recursion:
Tcopy(T) create another BST which is exact copy of BST which is passed as parameter int
compare(T1, T2)-compares two hinary search trees and returns 1 if they are equal and 0
otherwise.

Ans :
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* left;
struct Node* right;
} Node;
Node* createNode(int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
if (newNode == NULL) {
printf("Memory allocation failed\n");
exit(1);
}
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
}
Node* insert(Node* root, int data) {
if (root == NULL) {
return createNode(data);
}
if (data < root->data) {
root->left = insert(root->left, data);
} else if (data > root->data) {
root->right = insert(root->right, data);
}
return root;
}
Node* Tcopy(Node* root) {
if (root == NULL) {
return NULL;
}
Node* newNode = createNode(root->data);
newNode->left = Tcopy(root->left);
newNode->right = Tcopy(root->right);
return newNode;
}
int compare(Node* root1, Node* root2) {
if (root1 == NULL && root2 == NULL) {
return 1;
}
if (root1 == NULL || root2 == NULL) {
return 0;
return (root1->data == root2->data) && compare(root1->left, root2->left) && compare(root1-
>right, root2->right);
}
void inorder(Node* root) {
if (root != NULL) {
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
}
int main() {
Node* root1 = NULL;
root1 = insert(root1, 50);
root1 = insert(root1, 30);
root1 = insert(root1, 20);
root1 = insert(root1, 40);
root1 = insert(root1, 70);
root1 = insert(root1, 60);
root1 = insert(root1, 80);
Node* root2 = Tcopy(root1);
printf("Inorder traversal of T1: ");
inorder(root1);
printf("\n");
printf("Inorder traversal of T2 (copy of T1): ");
inorder(root2);
printf("\n");
if (compare(root1, root2)) {
printf("Both trees are equal.\n");
} else {
printf("Both trees are not equal.\n");
}
return 0;
}

Output :
Assignment No. 02

Set - A

Q.1) Write a C program which uses Binary search tree library and displays nodes at each
level,count of node at each level and total levels in the tree.
Ans :

#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* left;
struct Node* right;
};
struct Node* createNode(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->left = newNode->right = NULL;
return newNode;
}
struct Node* insert(struct Node* root, int value) {
if (root == NULL) {
return createNode(value);
}
if (value < root->data) {
root->left = insert(root->left, value);
} else {
root->right = insert(root->right, value);
}
return root;
}
void levelOrder(struct Node* root) {
if (root == NULL) return;
struct Node* queue[100];
int front = 0, rear = 0;
queue[rear++] = root;
int level = 0;
while (front < rear) {
int nodeCount = rear - front;
printf("Level %d: ", level);
while (nodeCount > 0) {
struct Node* currentNode = queue[front++];
printf("%d ", currentNode->data);
if (currentNode->left != NULL) {
queue[rear++] = currentNode->left;
}
if (currentNode->right != NULL) {
queue[rear++] = currentNode->right;
}
nodeCount--;
}
printf("\n");
level++;
}
}
int countLevels(struct Node* root) {
if (root == NULL) {
return 0;
}
int leftHeight = countLevels(root->left);
int rightHeight = countLevels(root->right);
return (leftHeight > rightHeight ? leftHeight : rightHeight) + 1;
}
void countNodesAtEachLevel(struct Node* root) {
if (root == NULL) return;
struct Node* queue[100];
int front = 0, rear = 0;
queue[rear++] = root;
int level = 0;
while (front < rear) {
int nodeCount = rear - front;
printf("Number of nodes at Level %d: %d\n", level, nodeCount);
while (nodeCount > 0) {
struct Node* currentNode = queue[front++];

if (currentNode->left != NULL) {
queue[rear++] = currentNode->left;
}
if (currentNode->right != NULL) {
queue[rear++] = currentNode->right;
}
nodeCount--;
}
level++;
}
}
int main() {
struct Node* root = NULL;
root = insert(root, 50);
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);
levelOrder(root);
countNodesAtEachLevel(root);
int totalLevels = countLevels(root);
printf("Total number of levels in the tree: %d\n", totalLevels);
return 0;
}
OUTPUT :

Set - B

a) Write a program to sort n randomly generated elements using Heapsort


method.
Ans:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
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) {
int temp = arr[i];
arr[i] = arr[largest];
arr[largest] = temp;
heapify(arr, n, largest);
}
}
void heapSort(int arr[], int n) {
for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);
for (int i = n - 1; i > 0; i--) {
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
heapify(arr, i, 0);
}
}
int main() {
int n;
srand(time(0));
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
for (int i = 0; i < n; i++) {
arr[i] = rand() % 100;
}
printf("Before sorting: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
heapSort(arr, n);
printf("After sorting: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}

OUTPUT :
Assignment No. 03

Set - A

a) Write a C program that accepts the vertices and edges of a graph and stores it as an
adjacency matrix. Display the adjacency matrix.
Ans :

#include <stdio.h>
int main() {
int vertices, edges, i, j, u, v;
printf("Enter the number of vertices: ");
scanf("%d", &vertices);

int adjMatrix[vertices][vertices];

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


for(j = 0; j < vertices; j++) {
adjMatrix[i][j] = 0;
}
}

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


scanf("%d", &edges);

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


printf("Enter edge (u v): ");
scanf("%d %d", &u, &v);
adjMatrix[u][v] = 1;
adjMatrix[v][u] = 1;
}

printf("Adjacency Matrix:\n");
for(i = 0; i < vertices; i++) {
for(j = 0; j < vertices; j++) {
printf("%d ", adjMatrix[i][j]);
}
printf("\n");
}

return 0;
}

Output :
b) Write a C program that accepts the vertices and edges of a graph and store it as an
adjacency matrix. Implement functions to print indegree, outdegree and total degree of all
vertices of graph.
Ans :

#include <stdio.h>
void printIndegree(int adjMatrix[][10], int vertices) {
int i, j;
printf("Indegree of vertices:\n");
for(i = 0; i < vertices; i++) {
int indegree = 0;
for(j = 0; j < vertices; j++) {
indegree += adjMatrix[j][i];
}
printf("Vertex %d: %d\n", i, indegree);
}
}

void printOutdegree(int adjMatrix[][10], int vertices) {


int i, j;
printf("Outdegree of vertices:\n");
for(i = 0; i < vertices; i++) {
int outdegree = 0;
for(j = 0; j < vertices; j++) {
outdegree += adjMatrix[i][j];
}
printf("Vertex %d: %d\n", i, outdegree);
}
}

void printTotalDegree(int adjMatrix[][10], int vertices) {


int i;
printf("Total degree of vertices:\n");
for(i = 0; i < vertices; i++) {
int totalDegree = 0;
for(int j = 0; j < vertices; j++) {
totalDegree += adjMatrix[i][j] + adjMatrix[j][i];
}
printf("Vertex %d: %d\n", i, totalDegree);
}
}

int main() {
int vertices, edges, i, u, v;
printf("Enter the number of vertices: ");
scanf("%d", &vertices);

int adjMatrix[vertices][vertices];

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


for(int j = 0; j < vertices; j++) {
adjMatrix[i][j] = 0;
}
}

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


scanf("%d", &edges);

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


printf("Enter edge (u v): ");
scanf("%d %d", &u, &v);
adjMatrix[u][v] = 1;
}

printIndegree(adjMatrix, vertices);
printOutdegree(adjMatrix, vertices);
printTotalDegree(adjMatrix, vertices);

return 0;
}

Output :
Set-B

a) Write a C program that accepts the vertices and edges of a graph and store it as an
adjacency matrix. Implement function to traverse the graph using Breadth First Search (BFS)
traversal.
Ans :

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

void BFS(int adjMatrix[][10], int vertices, int start) {


int queue[10], front = -1, rear = -1, visited[10] = {0};
queue[++rear] = start;
visited[start] = 1;

while(front < rear) {


int current = queue[++front];
printf("%d ", current);

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


if(adjMatrix[current][i] == 1 && visited[i] == 0) {
queue[++rear] = i;
visited[i] = 1;
}
}
}
printf("\n");
}

int main() {
int vertices, edges, u, v;
printf("Enter the number of vertices: ");
scanf("%d", &vertices);

int adjMatrix[vertices][vertices];

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


for(int j = 0; j < vertices; j++) {
adjMatrix[i][j] = 0;
}
}

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


scanf("%d", &edges);

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


printf("Enter edge (u v): ");
scanf("%d %d", &u, &v);
adjMatrix[u][v] = 1;
adjMatrix[v][u] = 1;
}
printf("BFS Traversal starting from vertex 0:\n");
BFS(adjMatrix, vertices, 0);

return 0;
}
Output :

b) Write a C program that accepts the vertices and edges of a graph and store it as an
adjacency matrix. Implement function to traverse the graph using Depth First Search (BFS)
traversal.
Ans:
#include <stdio.h>
void DFS(int adjMatrix[][10], int vertices, int start, int visited[]) {
printf("%d ", start);
visited[start] = 1;

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


if(adjMatrix[start][i] == 1 && !visited[i]) {
DFS(adjMatrix, vertices, i, visited);
}
}
}

int main() {
int vertices, edges, u, v;
printf("Enter the number of vertices: ");
scanf("%d", &vertices);

int adjMatrix[vertices][vertices];

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


for(int j = 0; j < vertices; j++) {
adjMatrix[i][j] = 0;
}
}

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


scanf("%d", &edges);
for(int i = 0; i < edges; i++) {
printf("Enter edge (u v): ");
scanf("%d %d", &u, &v);
adjMatrix[u][v] = 1;
adjMatrix[v][u] = 1;
}

int visited[vertices];
for(int i = 0; i < vertices; i++) {
visited[i] = 0;
}

printf("DFS Traversal starting from vertex 0:\n");


DFS(adjMatrix, vertices, 0, visited);
printf("\n");

return 0;
}

Output :
Assignment No. 04

Set-A

a) Write a C program that accepts the vertices and edges of a graph. Create adjacency
list and display the adjacency list.
Ans :

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

struct Node {
int vertex;
struct Node* next;
};

void addEdge(struct Node* adjList[], int u, int v) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->vertex = v;
newNode->next = adjList[u];
adjList[u] = newNode;

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


newNode->vertex = u;
newNode->next = adjList[v];
adjList[v] = newNode;
}

void printAdjList(struct Node* adjList[], int vertices) {


for (int i = 0; i < vertices; i++) {
struct Node* temp = adjList[i];
printf("Vertex %d: ", i);
while (temp != NULL) {
printf("%d ", temp->vertex);
temp = temp->next;
}
printf("\n");
}
}

int main() {
int vertices, edges, u, v;
printf("Enter the number of vertices: ");
scanf("%d", &vertices);

struct Node* adjList[vertices];


for (int i = 0; i < vertices; i++) {
adjList[i] = NULL;
}

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


scanf("%d", &edges);

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


printf("Enter edge (u v): ");
scanf("%d %d", &u, &v);
addEdge(adjList, u, v);
}

printAdjList(adjList, vertices);

return 0;
}
Output :

b) Write a C program that accepts the vertices and edges of a graph. Create adjacency list.
Implement functions to print indegree, outdegree and total degree of all vertex of graph.
Ans :
#include <stdio.h>
#include <stdlib.h>
struct Node {
int vertex;
struct Node* next;
};
void addEdge(struct Node* adjList[], int u, int v) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->vertex = v;
newNode->next = adjList[u];
adjList[u] = newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->vertex = u;
newNode->next = adjList[v];
adjList[v] = newNode;
}
void printIndegree(struct Node* adjList[], int vertices) {
int indegree[vertices];
for (int i = 0; i < vertices; i++) indegree[i] = 0;
for (int i = 0; i < vertices; i++) {
struct Node* temp = adjList[i];
while (temp != NULL) {
indegree[temp->vertex]++;
temp = temp->next;
}
}
printf("Indegree of vertices:\n");
for (int i = 0; i < vertices; i++) {
printf("Vertex %d: %d\n", i, indegree[i]);
}
}
void printOutdegree(struct Node* adjList[], int vertices) {
int outdegree[vertices];
for (int i = 0; i < vertices; i++) outdegree[i] = 0;
for (int i = 0; i < vertices; i++) {
struct Node* temp = adjList[i];
while (temp != NULL) {
outdegree[i]++;
temp = temp->next;
}
}
printf("Outdegree of vertices:\n");
for (int i = 0; i < vertices; i++) {
printf("Vertex %d: %d\n", i, outdegree[i]);
}
}
void printTotalDegree(struct Node* adjList[], int vertices) {
int totalDegree[vertices];
for (int i = 0; i < vertices; i++) totalDegree[i] = 0;
for (int i = 0; i < vertices; i++) {
struct Node* temp = adjList[i];
while (temp != NULL) {
totalDegree[i]++;
totalDegree[temp->vertex]++;
temp = temp->next;
}
}
printf("Total degree of vertices:\n");
for (int i = 0; i < vertices; i++) {
printf("Vertex %d: %d\n", i, totalDegree[i]);
}
}
int main() {
int vertices, edges, u, v;
printf("Enter the number of vertices: ");
scanf("%d", &vertices);
struct Node* adjList[vertices];
for (int i = 0; i < vertices; i++) adjList[i] = NULL;
printf("Enter the number of edges: ");
scanf("%d", &edges);
for (int i = 0; i < edges; i++) {
printf("Enter edge (u v): ");
scanf("%d %d", &u, &v);
addEdge(adjList, u, v);
}
printIndegree(adjList, vertices);
printOutdegree(adjList, vertices);
printTotalDegree(adjList, vertices);
return 0;
}
Output :
Set-B

a) Write a C program that accepts the vertices and edges of a graph and store it as an
adjacency list. Implement function to traverse the graph using Breadth First Search (BFS)
traversal.
Ans :
#include <stdio.h>
#include <stdlib.h>
struct Node {
int vertex;
struct Node* next;
};
void addEdge(struct Node* adjList[], int u, int v) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->vertex = v;
newNode->next = adjList[u];
adjList[u] = newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->vertex = u;
newNode->next = adjList[v];
adjList[v] = newNode;
}
void BFS(struct Node* adjList[], int vertices, int start) {
int visited[vertices];
for (int i = 0; i < vertices; i++) visited[i] = 0;
int queue[vertices], front = 0, rear = 0;
queue[rear++] = start;
visited[start] = 1;
while (front < rear) {
int current = queue[front++];
printf("%d ", current);
struct Node* temp = adjList[current];
while (temp != NULL) {
if (!visited[temp->vertex]) {
queue[rear++] = temp->vertex;
visited[temp->vertex] = 1;
}
temp = temp->next;
}
}
printf("\n");
}
int main()
int vertices, edges, u, v;
printf("Enter the number of vertices: ");
scanf("%d", &vertices);
struct Node* adjList[vertices];
for (int i = 0; i < vertices; i++) adjList[i] = NULL
printf("Enter the number of edges: ");
scanf("%d", &edges);
for (int i = 0; i < edges; i++) {
printf("Enter edge (u v): ");
scanf("%d %d", &u, &v);
addEdge(adjList, u, v);
}
printf("BFS Traversal starting from vertex 0:\n");
BFS(adjList, vertices, 0);
return 0;
}
Output :

b) Write a C program that accepts the vertices and edges of a graph and store it as an
adjacency list. Implement function to traverse the graph using Depth First Search (BFS)
traversal.
Ans:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int vertex;
struct Node* next;
};
void addEdge(struct Node* adjList[], int u, int v) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->vertex = v;
newNode->next = adjList[u];
adjList[u] = newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->vertex = u;
newNode->next = adjList[v];
adjList[v] = newNode;
}
void DFS(struct Node* adjList[], int vertices, int start, int visited[]) {
visited[start] = 1;
printf("%d ", start);
struct Node* temp = adjList[start];
while (temp != NULL) {
if (!visited[temp->vertex]) {
DFS(adjList, vertices, temp->vertex, visited);
}
temp = temp->next;
}
}
int main() {
int vertices, edges, u, v;
printf("Enter the number of vertices: ");
scanf("%d", &vertices);
struct Node* adjList[vertices];
for (int i = 0; i < vertices; i++) adjList[i] = NULL;
printf("Enter the number of edges: ");
scanf("%d", &edges);
for (int i = 0; i < edges; i++) {
printf("Enter edge (u v): ");
scanf("%d %d", &u, &v);
addEdge(adjList, u, v);
}
int visited[vertices];
for (int i = 0; i < vertices; i++) visited[i] = 0;
printf("DFS Traversal starting from vertex 0:\n");
DFS(adjList, vertices, 0, visited);
printf("\n");
return 0;
}
Output :
Assignment No. 05

Set-A

a) Write a C program for the implementation of Topological sorting.


Ans :

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

struct Node {
int vertex;
struct Node* next;
};

void addEdge(struct Node* adjList[], int u, int v) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->vertex = v;
newNode->next = adjList[u];
adjList[u] = newNode;
}

void DFS(struct Node* adjList[], int vertex, int visited[], int stack[], int* top) {
visited[vertex] = 1;

struct Node* temp = adjList[vertex];


while (temp != NULL) {
if (!visited[temp->vertex]) {
DFS(adjList, temp->vertex, visited, stack, top);
}
temp = temp->next;
}

stack[++(*top)] = vertex;
}

void topologicalSort(struct Node* adjList[], int vertices) {


int visited[vertices];
int stack[vertices];
int top = -1;

for (int i = 0; i < vertices; i++) visited[i] = 0;

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


if (!visited[i]) {
DFS(adjList, i, visited, stack, &top);
}
}

printf("Topological Sorting: ");


for (int i = top; i >= 0; i--) {
printf("%d ", stack[i]);
}
printf("\n");
}

int main() {
int vertices, edges, u, v;
printf("Enter the number of vertices: ");
scanf("%d", &vertices);

struct Node* adjList[vertices];


for (int i = 0; i < vertices; i++) adjList[i] = NULL;

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


scanf("%d", &edges);

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


printf("Enter edge (u v): ");
scanf("%d %d", &u, &v);
addEdge(adjList, u, v);
}

topologicalSort(adjList, vertices);

return 0;
}

Output :

b)Write a C program for the Implementation of Prim's Minimum spanning tree algorithm.
Ans :
#include <stdio.h>
#include <limits.h>
#define MAX 10
int minKey(int key[], int visited[], int vertices) {
int min = INT_MAX, minIndex;
for (int i = 0; i < vertices; i++) {
if (visited[i] == 0 && key[i] < min) {
min = key[i];
minIndex = i;
}
}
return minIndex;
}

void primMST(int graph[MAX][MAX], int vertices) {


int parent[vertices];
int key[vertices];
int visited[vertices];

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


key[i] = INT_MAX;
visited[i] = 0;
}

key[0] = 0;
parent[0] = -1;

for (int count = 0; count < vertices - 1; count++) {


int u = minKey(key, visited, vertices);
visited[u] = 1;

for (int v = 0; v < vertices; v++) {


if (graph[u][v] && visited[v] == 0 && graph[u][v] < key[v]) {
parent[v] = u;
key[v] = graph[u][v];
}
}
}

printf("Edge Weight\n");
for (int i = 1; i < vertices; i++) {
printf("%d - %d %d\n", parent[i], i, graph[i][parent[i]]);
}
}

int main() {
int vertices, graph[MAX][MAX];

printf("Enter number of vertices: ");


scanf("%d", &vertices);

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


for (int i = 0; i < vertices; i++) {
for (int j = 0; j < vertices; j++) {
scanf("%d", &graph[i][j]);
}
}
primMST(graph, vertices);
return 0;
}

Output :
Set-B

a) Write a C program for the Implementation of Kruskal's Minimum spanning tree


algorithm.
Ans :

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

struct Edge {
int u, v, weight;
};

int find(int parent[], int i) {


if (parent[i] == -1)
return i;
return find(parent, parent[i]);
}

void unionSet(int parent[], int x, int y) {


int xroot = find(parent, x);
int yroot = find(parent, y);
if (xroot != yroot)
parent[xroot] = yroot;
}

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


return ((struct Edge*)a)->weight - ((struct Edge*)b)->weight;
}

void kruskal(int vertices, struct Edge edges[], int edgesCount) {


int parent[vertices];
for (int i = 0; i < vertices; i++)
parent[i] = -1;
qsort(edges, edgesCount, sizeof(struct Edge), compare);

printf("Edge Weight\n");
for (int i = 0; i < edgesCount; i++) {
int u = edges[i].u;
int v = edges[i].v;
int setU = find(parent, u);
int setV = find(parent, v);

if (setU != setV) {
printf("%d - %d %d\n", u, v, edges[i].weight);
unionSet(parent, setU, setV);
}
}
}

int main() {
int vertices, edgesCount;
printf("Enter number of vertices: ");
scanf("%d", &vertices);

printf("Enter number of edges: ");


scanf("%d", &edgesCount);

struct Edge edges[edgesCount];


printf("Enter edges (u v weight):\n");
for (int i = 0; i < edgesCount; i++) {
scanf("%d %d %d", &edges[i].u, &edges[i].v, &edges[i].weight);
}

kruskal(vertices, edges, edgesCount);

return 0;
}

Output :
Assignment No. 06

Set - A
a)Write a C program for the implementation of Dijkstra's shortest path algorithm for finding
shortest path from a given source vertex using adjacency cost matrix.
Ans :

#include <stdio.h>
#include <limits.h>
#define MAX 10
int minDistance(int dist[], int sptSet[], int vertices) {
int min = INT_MAX, minIndex;
for (int i = 0; i < vertices; i++) {
if (sptSet[i] == 0 && dist[i] < min) {
min = dist[i];
minIndex = i;
}
}
return minIndex;
}
void dijkstra(int graph[MAX][MAX], int vertices, int src) {
int dist[vertices], sptSet[vertices];
for (int i = 0; i < vertices; i++) {
dist[i] = INT_MAX;
sptSet[i] = 0;
}
dist[src] = 0;
for (int count = 0; count < vertices - 1; count++) {
int u = minDistance(dist, sptSet, vertices);
sptSet[u] = 1;

for (int v = 0; v < vertices; v++) {


if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX && dist[u] + graph[u][v] < dist[v])
{
dist[v] = dist[u] + graph[u][v];
}
}
}

printf("Vertex Distance from Source\n");


for (int i = 0; i < vertices; i++) {
printf("%d %d\n", i, dist[i]);
}
}
int main() {
int vertices, graph[MAX][MAX];
int src;

printf("Enter number of vertices: ");


scanf("%d", &vertices);

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


for (int i = 0; i < vertices; i++) {
for (int j = 0; j < vertices; j++) {
scanf("%d", &graph[i][j]);
}
}

printf("Enter source vertex: ");


scanf("%d", &src);

dijkstra(graph, vertices, src);

return 0;
}
Output :

Set - B

a)Write a C program for the implementation of Floyd Warshall's algorithm for finding all
pairs shortest path using adjacency cost matrix.
Ans :

#include <stdio.h>
#include <limits.h>

#define MAX 10

void floydWarshall(int graph[MAX][MAX], int vertices) {


int dist[vertices][vertices];

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


for (int j = 0; j < vertices; j++) {
dist[i][j] = graph[i][j];
}
}

for (int k = 0; k < vertices; k++) {


for (int i = 0; i < vertices; i++) {
for (int j = 0; j < vertices; j++) {
if (dist[i][k] != INT_MAX && dist[k][j] != INT_MAX && dist[i][k] + dist[k][j] < dist[i]
[j]) {
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
}

printf("Shortest distances between every pair of vertices:\n");


for (int i = 0; i < vertices; i++) {
for (int j = 0; j < vertices; j++) {
if (dist[i][j] == INT_MAX) {
printf("INF ");
} else {
printf("%d ", dist[i][j]);
}
}
printf("\n");
}
}
int main() {
int vertices, graph[MAX][MAX];
printf("Enter number of vertices: ");
scanf("%d", &vertices);
printf("Enter adjacency cost matrix:\n");
for (int i = 0; i < vertices; i++) {
for (int j = 0; j < vertices; j++) {
scanf("%d", &graph[i][j]);
if (graph[i][j] == 0 && i != j) {
graph[i][j] = INT_MAX;
}
}
}
floydWarshall(graph, vertices);
return 0;
}
Output:
Assignment No. 07
Set - A

a) Write a program to implement various types of hash functions which are used to place the
data in a hash table
a. Division Method
b. Mid Square Method
c. Digit Folding Method
Accept n values from the user and display appropriate message in case of collision for each of
the above functions.
Ans :

#include <stdio.h>
#include <math.h>

#define SIZE 10

int divisionMethod(int key) {


return key % SIZE;
}

int midSquareMethod(int key) {


int square = key * key;
int digits = (int)log10(square) + 1;
int middle = square / (int)pow(10, digits / 2) % SIZE;
return middle;
}

int digitFoldingMethod(int key) {


int sum = 0;
while (key > 0) {
sum += key % 1000;
key /= 1000;
}
return sum % SIZE;
}

void insert(int hashTable[], int key, int (*hashFunction)(int), const char *method) {
int index = hashFunction(key);
if (hashTable[index] != -1) {
printf("Collision occurred at index %d for key %d using %s method\n", index, key, method);
} else {
hashTable[index] = key;
printf("Key %d inserted at index %d using %s method\n", key, index, method);
}
}

int main() {
int hashTable[SIZE];
for (int i = 0; i < SIZE; i++) {
hashTable[i] = -1;
}
int n;
printf("Enter number of values to insert: ");
scanf("%d", &n);

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


int key;
char method[20];
printf("Enter key %d: ", i + 1);
scanf("%d", &key);
printf("Choose hash method (division, mid_square, digit_folding): ");
scanf("%s", method);

if (strcmp(method, "division") == 0) {
insert(hashTable, key, divisionMethod, "division");
} else if (strcmp(method, "mid_square") == 0) {
insert(hashTable, key, midSquareMethod, "mid_square");
} else if (strcmp(method, "digit_folding") == 0) {
insert(hashTable, key, digitFoldingMethod, "digit_folding");
} else {
printf("Invalid method selected\n");
}
}

return 0;
}
Output :
Set - B

a) Write a menu driven program to implement hash table using array (insert, search, delete,
display). Use any of the above-mentioned hash functions. In case of collision apply linear
probing.
Ans :

#include <stdio.h>
#define SIZE 10

int hashTable[SIZE];

int divisionMethod(int key) {


return key % SIZE;
}

void insert(int key) {


int index = divisionMethod(key);
while (hashTable[index] != -1) {
index = (index + 1) % SIZE;
}
hashTable[index] = key;
printf("Key %d inserted at index %d\n", key, index);
}

void search(int key) {


int index = divisionMethod(key);
int start = index;
while (hashTable[index] != -1) {
if (hashTable[index] == key) {
printf("Key %d found at index %d\n", key, index);
return;
}
index = (index + 1) % SIZE;
if (index == start) break;
}
printf("Key %d not found\n", key);
}

void delete(int key) {


int index = divisionMethod(key);
int start = index;
while (hashTable[index] != -1) {
if (hashTable[index] == key) {
hashTable[index] = -1;
printf("Key %d deleted from index %d\n", key, index);
return;
}
index = (index + 1) % SIZE;
if (index == start) break;
}
printf("Key %d not found to delete\n", key);
}

void display() {
printf("Hash Table: ");
for (int i = 0; i < SIZE; i++) {
if (hashTable[i] != -1)
printf("[%d] ", hashTable[i]);
else
printf("[ ] ");
}
printf("\n");
}

int main() {
for (int i = 0; i < SIZE; i++) {
hashTable[i] = -1;
}
int choice, key;

do {
printf("\nMenu:\n");
printf("1. Insert\n");
printf("2. Search\n");
printf("3. Delete\n");
printf("4. Display\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter key to insert: ");
scanf("%d", &key);
insert(key);
break;
case 2:
printf("Enter key to search: ");
scanf("%d", &key);
search(key);
break;
case 3:
printf("Enter key to delete: ");
scanf("%d", &key);
delete(key);
break;
case 4:
display();
break;
case 5:
printf("Exiting...\n");
break;
default:
printf("Invalid choice\n");
}
} while (choice != 5);

return 0;
}
Output :
b)Write a menu driven program to implement hash table using array (insert, search, delete,
display). Use any of the above-mentioned hash functions. In case of collision apply quadratic
probing.
Ans :

#include <stdio.h>

#define SIZE 10

int hashTable[SIZE];

int divisionMethod(int key) {


return key % SIZE;
}

void insert(int key) {


int index = divisionMethod(key);
int i = 0;
while (hashTable[(index + i * i) % SIZE] != -1) {
i++;
if (i == SIZE) {
printf("Hash table is full\n");
return;
}
}
hashTable[(index + i * i) % SIZE] = key;
printf("Key %d inserted\n", key);
}

void search(int key) {


int index = divisionMethod(key);
int i = 0;
while (hashTable[(index + i * i) % SIZE] != -1) {
if (hashTable[(index + i * i) % SIZE] == key) {
printf("Key %d found\n", key);
return;
}
i++;
if (i == SIZE) break;
}
printf("Key %d not found\n", key);
}

void delete(int key) {


int index = divisionMethod(key);
int i = 0;
while (hashTable[(index + i * i) % SIZE] != -1) {
if (hashTable[(index + i * i) % SIZE] == key) {
hashTable[(index + i * i) % SIZE] = -1;
printf("Key %d deleted\n", key);
return;
}
i++;
if (i == SIZE) break;
}
printf("Key %d not found to delete\n", key);
}

void display() {
printf("Hash Table: ");
for (int i = 0; i < SIZE; i++) {
if (hashTable[i] != -1)
printf("[%d] ", hashTable[i]);
else
printf("[ ] ");
}
printf("\n");
}

int main() {
for (int i = 0; i < SIZE; i++) {
hashTable[i] = -1;
}
int choice, key;

do {
printf("\nMenu:\n");
printf("1. Insert\n");
printf("2. Search\n");
printf("3. Delete\n");
printf("4. Display\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter key to insert: ");
scanf("%d", &key);
insert(key);
break;
case 2:
printf("Enter key to search: ");
scanf("%d", &key);
search(key);
break;
case 3:
printf("Enter key to delete: ");
scanf("%d", &key);
delete(key);
break;
case 4:
display();
break;
case 5:
printf("Exiting...\n");
break;
default:
printf("Invalid choice\n");
}
} while (choice != 5);

return 0;
}
Output:
Assignment No. 08

Set-A

a)Implement hash table using singly linked lists. Write a menu driven program to perform
operations on the hash table (insert, search, delete, display). Select appropriate hashing
function. In case of collision, use separate chaining.
Ans :

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

#define SIZE 10

struct Node {
int key;
struct Node* next;
};

struct Node* hashTable[SIZE];

int divisionMethod(int key) {


return key % SIZE;
}

void insert(int key) {


int index = divisionMethod(key);
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->key = key;
newNode->next = hashTable[index];
hashTable[index] = newNode;
printf("Key %d inserted\n", key);
}

void search(int key) {


int index = divisionMethod(key);
struct Node* temp = hashTable[index];
while (temp != NULL) {
if (temp->key == key) {
printf("Key %d found\n", key);
return;
}
temp = temp->next;
}
printf("Key %d not found\n", key);
}

void delete(int key) {


int index = divisionMethod(key);
struct Node* temp = hashTable[index];
struct Node* prev = NULL;
while (temp != NULL) {
if (temp->key == key) {
if (prev == NULL) {
hashTable[index] = temp->next;
} else {
prev->next = temp->next;
}
free(temp);
printf("Key %d deleted\n", key);
return;
}
prev = temp;
temp = temp->next;
}
printf("Key %d not found to delete\n", key);
}
void display() {
for (int i = 0; i < SIZE; i++) {
printf("Index %d: ", i);
struct Node* temp = hashTable[i];
while (temp != NULL) {
printf("%d -> ", temp->key);
temp = temp->next;
}
printf("NULL\n");
}
}
int main() {
for (int i = 0; i < SIZE; i++) {
hashTable[i] = NULL;
}
int choice, key;
do {
printf("\nMenu:\n");
printf("1. Insert\n");
printf("2. Search\n");
printf("3. Delete\n");
printf("4. Display\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter key to insert: ");
scanf("%d", &key);
insert(key);
break;
case 2:
printf("Enter key to search: ");
scanf("%d", &key);
search(key);
break;
case 3:
printf("Enter key to delete: ");
scanf("%d", &key);
delete(key);
break;
case 4:
display();
break;
case 5:
printf("Exiting...\n");
break;
default:
printf("Invalid choice\n");
}
} while (choice != 5);

return 0;
}
Output :
set_B

a)Implement hash table using doubly linked lists. Write a menu driven program to perform
operations on the hash table (insert, search, delete, display). Select appropriate hashing
function. In case of collision, use separate chaining.
Ans :

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

#define SIZE 10

struct Node {
int key;
struct Node* prev;
struct Node* next;
};

struct Node* hashTable[SIZE];

int divisionMethod(int key) {


return key % SIZE;
}

void insert(int key) {


int index = divisionMethod(key);
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->key = key;
newNode->prev = NULL;
newNode->next = hashTable[index];
if (hashTable[index] != NULL) {
hashTable[index]->prev = newNode;
}
hashTable[index] = newNode;
printf("Key %d inserted\n", key);
}

void search(int key) {


int index = divisionMethod(key);
struct Node* temp = hashTable[index];
while (temp != NULL) {
if (temp->key == key) {
printf("Key %d found\n", key);
return;
}
temp = temp->next;
}
printf("Key %d not found\n", key);
}

void delete(int key) {


int index = divisionMethod(key);
struct Node* temp = hashTable[index];
while (temp != NULL) {
if (temp->key == key) {
if (temp->prev != NULL) {
temp->prev->next = temp->next;
} else {
hashTable[index] = temp->next;
}
if (temp->next != NULL) {
temp->next->prev = temp->prev;
}
free(temp);
printf("Key %d deleted\n", key);
return;
}
temp = temp->next;
}
printf("Key %d not found to delete\n", key);
}

void display() {
for (int i = 0; i < SIZE; i++) {
printf("Index %d: ", i);
struct Node* temp = hashTable[i];
while (temp != NULL) {
printf("%d <-> ", temp->key);
temp = temp->next;
}
printf("NULL\n");
}
}

int main() {
for (int i = 0; i < SIZE; i++) {
hashTable[i] = NULL;
}

int choice, key;

do {
printf("\nMenu:\n");
printf("1. Insert\n");
printf("2. Search\n");
printf("3. Delete\n");
printf("4. Display\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter key to insert: ");
scanf("%d", &key);
insert(key);
break;
case 2:
printf("Enter key to search: ");
scanf("%d", &key);
search(key);
break;
case 3:
printf("Enter key to delete: ");
scanf("%d", &key);
delete(key);
break;
case 4:
display();
break;
case 5:
printf("Exiting...\n");
break;
default:
printf("Invalid choice\n");
}
} while (choice != 5);

return 0;
}
Output :

You might also like