Ds Programs
Ds Programs
Ds Programs
main() {
TNODE *root = NULL; /* Main Program */
int opn, elem, n, i;
do {
clrscr();
printf("n ### Binary Search Tree Operations ### nn");
printf("n Press 1-Creation of BST");
printf("n 2-Traverse in Inorder");
printf("n 3-Traverse in Preorder");
printf("n 4-Traverse in Postorder");
printf("n 5-Exitn");
printf("n Your option ? ");
scanf("%d", &opn);
switch (opn) {
case 1:
root = NULL;
printf("nnBST for How Many Nodes ?");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
printf("nRead the Data for Node %d ?", i);
scanf("%d", &elem);
root = CreateBST(root, elem);
}
printf("nBST with %d nodes is ready to Use!!n", n);
break;
case 2:
printf("n BST Traversal in INORDER n");
Inorder(root);
break;
case 3:
printf("n BST Traversal in PREORDER n");
Preorder(root);
break;
case 4:
printf("n BST Traversal in POSTORDER n");
Postorder(root);
break;
case 5:
printf("nn Terminating nn");
break;
default:
printf("nnInvalid Option !!! Try Again !! nn");
break;
}
printf("nnnn Press a Key to Continue . . . ");
getch();
} while (opn != 5);
}
TNODE *CreateBST(TNODE *root, int elem) {
if (root == NULL) {
root = (TNODE *) malloc(sizeof(TNODE));
root->left = root->right = NULL;
root->data = elem;
return root;
} else {
if (elem < root->data)
root->left = CreateBST(root->left, elem);
else if (elem > root->data)
root->right = CreateBST(root->right, elem);
else
printf(" Duplicate Element !! Not Allowed !!!");
return (root);
}
}
void Inorder(TNODE *root) {
if (root != NULL) {
Inorder(root->left);
printf(" %d ", root->data);
Inorder(root->right);
}
}
#include <stdio.h>
#include <stdlib.h>
struct node {
int vertex;
struct node* next;
};
struct node* createNode(int v);
struct Graph {
int numVertices;
int* visited;
// We need int** to store a two dimensional array.
// Similary, we need struct node** to store an array of Linked lists
struct node** adjLists;
};
void DFS(struct Graph* graph, int vertex) {
struct node* adjList = graph->adjLists[vertex];
struct node* temp = adjList;
graph->visited[vertex] = 1;
printf("Visited %d \n", vertex);
if (graph->visited[connectedVertex] == 0) {
DFS(graph, connectedVertex);
}
temp = temp->next;
}
}
// Create a node
struct node* createNode(int v) {
struct node* newNode = malloc(sizeof(struct node));
newNode->vertex = v;
newNode->next = NULL;
return newNode;
}
// Create graph
struct Graph* createGraph(int vertices) {
struct Graph* graph = malloc(sizeof(struct Graph));
graph->numVertices = vertices;
graph->adjLists = malloc(vertices * sizeof(struct node*));
graph->visited = malloc(vertices * sizeof(int));
int i;
for (i = 0; i < vertices; i++) {
graph->adjLists[i] = NULL;
graph->visited[i] = 0;
}
return graph;
}
// Add edge
void addEdge(struct Graph* graph, int src, int dest) {
// Add edge from src to dest
struct node* newNode = createNode(dest);
newNode->next = graph->adjLists[src];
graph->adjLists[src] = newNode;
// Add edge from dest to src
newNode = createNode(src);
newNode->next = graph->adjLists[dest];
graph->adjLists[dest] = newNode;
}
// Print the graph
void printGraph(struct Graph* graph) {
int v;
for (v = 0; v < graph->numVertices; v++) {
struct node* temp = graph->adjLists[v];
printf("\n Adjacency list of vertex %d\n ", v);
while (temp) {
printf("%d -> ", temp->vertex);
temp = temp->next;
}
printf("\n");
}
}
int main() {
struct Graph* graph = createGraph(4);
addEdge(graph, 0, 1);
addEdge(graph, 0, 2);
addEdge(graph, 1, 2);
addEdge(graph, 2, 3);
printGraph(graph);
DFS(graph, 2);
return 0;
}
Output:
Adjacency list of vertex 0
2 -> 1 ->
Adjacency list of vertex 1
2 -> 0 ->
Adjacency list of vertex 2
3 -> 1 -> 0 ->
Adjacency list of vertex 3
2 ->
Visited 2
Visited 3
Visited 1
Visited 0
13) b)Write a C program for finding the Breadth First Search of a graph
#include <stdio.h>
#include <stdlib.h>
#define SIZE 40
struct queue {
int items[SIZE];
int front;
int rear;
};
struct queue* createQueue();
void enqueue(struct queue* q, int);
int dequeue(struct queue* q);
void display(struct queue* q);
int isEmpty(struct queue* q);
void printQueue(struct queue* q);
struct node {
int vertex;
struct node* next;
};
struct node* createNode(int);
struct Graph {
int numVertices;
struct node** adjLists;
int* visited;
};
void bfs(struct Graph* graph, int startVertex) {
struct queue* q = createQueue();
graph->visited[startVertex] = 1;
enqueue(q, startVertex);
while (!isEmpty(q)) {
printQueue(q);
int currentVertex = dequeue(q);
printf("Visited %d\n", currentVertex);
while (temp) {
int adjVertex = temp->vertex;
if (graph->visited[adjVertex] == 0) {
graph->visited[adjVertex] = 1;
enqueue(q, adjVertex);
}
temp = temp->next;
}
}
}
// Creating a node
struct node* createNode(int v) {
struct node* newNode = malloc(sizeof(struct node));
newNode->vertex = v;
newNode->next = NULL;
return newNode;
}
// Creating a graph
struct Graph* createGraph(int vertices) {
struct Graph* graph = malloc(sizeof(struct Graph));
graph->numVertices = vertices;
graph->adjLists = malloc(vertices * sizeof(struct node*));
graph->visited = malloc(vertices * sizeof(int));
int i;
for (i = 0; i < vertices; i++) {
graph->adjLists[i] = NULL;
graph->visited[i] = 0;
}
return graph;
}
// Add edge
void addEdge(struct Graph* graph, int src, int dest) {
// Add edge from src to dest
struct node* newNode = createNode(dest);
newNode->next = graph->adjLists[src];
graph->adjLists[src] = newNode;
// Create a queue
struct queue* createQueue() {
struct queue* q = malloc(sizeof(struct queue));
q->front = -1;
q->rear = -1;
return q;
}
// Check if the queue is empty
int isEmpty(struct queue* q) {
if (q->rear == -1)
return 1;
else
return 0;
}
// Adding elements into queue
void enqueue(struct queue* q, int value) {
if (q->rear == SIZE - 1)
printf("\nQueue is Full!!");
else {
if (q->front == -1)
q->front = 0;
q->rear++;
q->items[q->rear] = value;
}
}
// Removing elements from queue
int dequeue(struct queue* q) {
int item;
if (isEmpty(q)) {
printf("Queue is empty");
item = -1;
} else {
item = q->items[q->front];
q->front++;
if (q->front > q->rear) {
printf("Resetting queue ");
q->front = q->rear = -1;
}
}
return item;
}
// Print the queue
void printQueue(struct queue* q) {
int i = q->front;
if (isEmpty(q)) {
printf("Queue is empty");
} else {
printf("\nQueue contains \n");
for (i = q->front; i < q->rear + 1; i++) {
printf("%d ", q->items[i]);
}
}
}
int main() {
struct Graph* graph = createGraph(6);
addEdge(graph, 0, 1);
addEdge(graph, 0, 2);
addEdge(graph, 1, 2);
addEdge(graph, 1, 4);
addEdge(graph, 1, 3);
addEdge(graph, 2, 4);
addEdge(graph, 3, 4);
bfs(graph, 0);
return 0;
}
Output:
Queue contains
0 Resetting queue Visited 0
Queue contains
2 1 Visited 2
Queue contains
1 4 Visited 1
Queue contains
4 3 Visited 4
Queue contains
3 Resetting queue Visited 3
12 a)Write a C program for finding the transitive closure of a digraph Using warshall’s
Algorithm
#include<stdio.h>
#include<conio.h>
#include<math.h>
int max(int,int);
void warshal(int p[10][10],int n) {
int i,j,k;
for (k=1;k<=n;k++)
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
p[i][j]=max(p[i][j],p[i][k]&&p[k][j]);
}
int max(int a,int b) {
;
if(a>b)
return(a); else
return(b);
}
void main() {
int p[10][10]= {
0
}
,n,e,u,v,i,j;
clrscr();
printf("\n Enter the number of vertices:");
scanf("%d",&n);
printf("\n Enter the number of edges:");
scanf("%d",&e);
for (i=1;i<=e;i++) {
printf("\n Enter the end vertices of edge %d:",i);
scanf("%d%d",&u,&v);
p[u][v]=1;
}
printf("\n Matrix of input data: \n");
for (i=1;i<=n;i++) {
for (j=1;j<=n;j++)
printf("%d\t",p[i][j]);
printf("\n");
}
warshal(p,n);
printf("\n Transitive closure: \n");
for (i=1;i<=n;i++) {
for (j=1;j<=n;j++)
printf("%d\t",p[i][j]);
printf("\n");
}
getch();
}
Output:
12 B)Write a C program for finding the shortest path from a given source to any vertex
in a digraph using Dijkstra’s algorithm.
We are given a graph with a source vertex in the graph. And we have to find the shortest path
from the source vertex to all other vertices of the graph.
The Dijikstra’s algorithm is a greedy algorithm to find the shortest path from the source
vertex of the graph to the root node of the graph.
Algorithm
Step 1 : Create a set shortPath to store vertices that come in the way of the shortest path tree.
Step 2 : Initialize all distance values as INFINITE and assign distance values as 0 for source vertex
so that it is picked first.
Step 3 : Loop until all vertices of the graph are in the shortPath.
Step 3.1 : Take a new vertex that is not visited and is nearest.
Step 3.2 : Add this vertex to shortPath.
Step 3.3 : For all adjacent vertices of this vertex update distances. Now check every adjacent
vertex of V, if sum of distance of u and weight of edge is else the update it.
#include <limits.h>
#include <stdio.h>
#define V 9
int minDistance(int dist[], bool sptSet[]) {
int min = INT_MAX, min_index;
for (int v = 0; v < V; v++)
if (sptSet[v] == false && dist[v] <= min)
min = dist[v], min_index = v;
return min_index;
}
int printSolution(int dist[], int n) {
printf("Vertex Distance from Source\n");
for (int i = 0; i < V; i++)
printf("%d \t %d\n", i, dist[i]);
}
void dijkstra(int graph[V][V], int src) {
int dist[V];
bool sptSet[V];
for (int i = 0; i < V; i++)
dist[i] = INT_MAX, sptSet[i] = false;
dist[src] = 0;
for (int count = 0; count < V - 1; count++) {
int u = minDistance(dist, sptSet);
sptSet[u] = true;
for (int v = 0; v < V; 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];
}
printSolution(dist, V);
}
int main() {
int graph[V][V] = { { 0, 6, 0, 0, 0, 0, 0, 8, 0 },
{ 6, 0, 8, 0, 0, 0, 0, 13, 0 },
{ 0, 8, 0, 7, 0, 6, 0, 0, 2 },
{ 0, 0, 7, 0, 9, 14, 0, 0, 0 },
{ 0, 0, 0, 9, 0, 10, 0, 0, 0 },
{ 0, 0, 6, 14, 10, 0, 2, 0, 0 },
{ 0, 0, 0, 0, 0, 2, 0, 1, 6 },
{ 8, 13, 0, 0, 0, 0, 1, 0, 7 },
{ 0, 0, 2, 0, 0, 0, 6, 7, 0 }
};
dijkstra(graph, 0);
return 0;
}
Output
Vertex Distance from Source
00
16
2 14
3 21
4 21
5 11
69
78
8 15
10)Write a C program for the representation of polynomials using circular linked list
and for the addition of two such polynomials
#include <stdio.h>
#include <stdlib.h>
// Structure of a node
// in a circular linked list
struct Node {
// Stores coefficient
// of a node
int coeff;
// Stores power of
// variable y of a node
int powy;
// Stores pointer
// to next node
struct Node* next;
};
// If z is null
if (z == NULL) {
// Update temp node
(*temp) = r;
// Update next pointer of temp node
(*temp)->next = (*temp);
}
else {
// Update next pointer of z
r->next = z->next;
// Update next pointer of z
z->next = r;
// Update temp Node
(*temp) = r;
}
}
// If z is null
if (z == NULL) {
// Update temp
(*temp) = r;
// Update next pointer of temp
(*temp)->next = (*temp);
}
else {
// Update next pointer of r
r->next = z->next;
// Update next pointer of z
z->next = r;
// Update temp
(*temp) = r;
}
}
// If z is null
if (z == NULL) {
// Update temp
(*temp) = r;
// Update pointer to next node
(*temp)->next = (*temp);
}
else {
// Update next pointer of r
r->next = z->next;
// Update next pointer of z
z->next = r;
// Update temp
(*temp) = r;
}
}
// If z is null
if (z == NULL) {
// Update temp
(*temp) = r;
// Update next pointer of temp
(*temp)->next = (*temp);
}
else {
// Update next pointer of r
r->next = z->next;
// Update next pointer of z
z->next = r;
// Update temp
(*temp) = r;
}
}
}
// If z is null
if (z == NULL) {
// Update temp
(*temp) = r;
// Update next pointer of temp
(*temp)->next = (*temp);
}
else {
// Update next pointer of r
r->next = z->next;
// Update next pointer of z
z->next = r;
// Update temp
(*temp) = r;
}
}
// Update node
node = node->next;
}
printf("\n\n");
}
// Driver Code
int main() {
// Stores node of first polynomial
struct Node *poly1 = NULL;
// Stores node of second polynomial
struct Node *poly2 = NULL;
// Stores node of resultant polynomial
struct Node *store = NULL;
// Display polynomial 1
printf("Polynomial 1\n");
display(poly1);
// Display polynomial 2
printf("Polynomial 2\n");
display(poly2);
return 0;
}
Explanation :
Given two polynomial numbers represented by a circular linked list, the task is to add
these two polynomials by adding the coefficients of the powers of the same variable.
Note: In given polynomials, the term containing the higher power of x will come first.
Examples:
Input:
1st Number = 5x^2 * y^1 + 4x^1 * y^2 + 3x^1 * y^1 + 2x^1
2nd Number = 3x^1 * y^2 + 4x^1
Output:
5x^2 * y^1 + 7x^1 * y^2 + 3x^1 * y^1 + 6x^1
Explanation:
The coefficient of x^2 * y^1 in 1st numbers is 5 and 0 in the 2nd number. Therefore,
sum of the coefficient of x^2 * Y^1 is 5.
The coefficient of x^1 * y^2 in 1st numbers is 4 and 3 in the 2nd number. Therefore,
sum of the coefficient of x^1 * Y^2 is 7.
The coefficient of x^1 * y^1 in 1st numbers is 3 and 0 in the 2nd number. Therefore,
sum of the coefficient of x^1 * Y^1 is 2.
The coefficient of x^1 * Y^0 in 1st numbers is 2 and 4 in the 2nd number. Therefore,
sum of the coefficient of x^1 * Y^0 is 6.
Input:
1st Number = 3x^3 * y^2 + 2x^2 + 5x^1 * y^1 + 9y^1 + 2
2nd Number = 4x^3 * y^3 + 2x^3 * y^2 + 1y^2 + 3
Output:
4x^3 * y^3 + 5x^3 * y^2 + 2x^2 + 5x^1 * y^1 + 1y^2 + 9y^1 + 5
Approach: Follow the below steps to solve the problem:
1. Create two circular linked lists , where each node will consist of the
coefficient, power of x, power of y and pointer to the next node.
2. Traverse both the polynomials and check the following conditions:
If power of x of 1st polynomial is greater than power of x of
second polynomial then store node of first polynomial in resultant
polynomial and increase counter of polynomial 1.
If power of x of 1st polynomial is less than power of x of second
polynomial then store the node of second polynomial in resultant
polynomial and increase counter of polynomial 2.
If power of x of 1st polynomial is equal to power of x of second
polynomial and power of y of 1st polynomial is greater than
power of y of 2nd polynomial then store the node of first
polynomial in resultant polynomial and increase counter of
polynomial 1.
If power of x of 1st polynomial is equal to power of x of second
polynomial and power of y of 1st polynomial is equal to power
of y of 2nd polynomial then store the sum of coefficient of both
polynomial in resultant polynomial and increase counter of both
polynomial 1 and polynomial 2.
3. If there are nodes left to be traversed in 1st polynomial or in 2nd polynomial
then append them in resultant polynomial.
4. Finally, print the resultant polynomial.