0% found this document useful (0 votes)
72 views23 pages

Ds Da-2 Lab 18bca0045

The document contains code for implementing graph algorithms in C including: 1) Printing the adjacency list of graphs 2) Performing depth first search (DFS) and breadth first search (BFS) on graphs 3) Finding the minimum spanning tree (MST) of a graph using Prim's or Kruskal's algorithm The document provides code snippets to represent graphs as adjacency lists and matrices. It also includes functions for DFS, BFS traversal and the Kruskal's MST algorithm.

Uploaded by

Shamil Iqbal
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)
72 views23 pages

Ds Da-2 Lab 18bca0045

The document contains code for implementing graph algorithms in C including: 1) Printing the adjacency list of graphs 2) Performing depth first search (DFS) and breadth first search (BFS) on graphs 3) Finding the minimum spanning tree (MST) of a graph using Prim's or Kruskal's algorithm The document provides code snippets to represent graphs as adjacency lists and matrices. It also includes functions for DFS, BFS traversal and the Kruskal's MST algorithm.

Uploaded by

Shamil Iqbal
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/ 23

DIGITAL ASSIGNMENT-3

NAME: Shamil Iqbal


REG NO: 18BCA0045

1. Develop a simple program to perform the following operations on the graphs


given below and produce the results:

(i) To print the Adjacency List.

Graph-1

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

struct node
{
int vertex;
struct node* next;
};
struct node* createNode(int);
struct Graph
{
int numVertices;
struct node** adjLists;
};
struct Graph* createGraph(int vertices);
void addEdge(struct Graph* graph, int src, int dest);
void printGraph(struct Graph* graph);
int main()
{
struct Graph* graph = createGraph(9);
addEdge(graph, 1, 2);
addEdge(graph, 2, 3);
addEdge(graph, 2, 4);
addEdge(graph, 4, 3);
addEdge(graph, 4, 5);
addEdge(graph, 5, 3);
addEdge(graph, 3, 6);
addEdge(graph, 6, 7);
addEdge(graph, 7, 8);
addEdge(graph, 6, 8);
addEdge(graph, 8, 9);
printf("A=1\t X=2\t H=3\t G=4\t P=5\t E=6\t Y=7\t M=8\t J=9\t");
printGraph(graph);

return 0;
}

struct node* createNode(int v)


{
struct node* newNode = malloc(sizeof(struct node));
newNode->vertex = v;
newNode->next = NULL;
return newNode;
}

struct Graph* createGraph(int vertices)


{
struct Graph* graph = malloc(sizeof(struct Graph));
graph->numVertices = vertices;

graph->adjLists = malloc(vertices * sizeof(struct node*));

int i;
for (i = 0; i < vertices; i++)
graph->adjLists[i] = NULL;

return graph;
}

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

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");
}
getch();
}

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

struct AdjListNode
{
int dest;
struct AdjListNode* next;
};

struct AdjList
{
struct AdjListNode *head;
};

struct Graph
{
int V;
struct AdjList* array;
};

// A utility function to create a new adjacency list node


struct AdjListNode* newAdjListNode(int dest)
{
struct AdjListNode* newNode =
(struct AdjListNode*) malloc(sizeof(struct AdjListNode));
newNode->dest = dest;
newNode->next = NULL;
return newNode;
}

// A utility function that creates a graph of V vertices


struct Graph* createGraph(int V)
{
struct Graph* graph =
(struct Graph*) malloc(sizeof(struct Graph));
graph->V = V;
graph->array =
(struct AdjList*) malloc(V * sizeof(struct AdjList));

int i;
for (i = 0; i < V; ++i)
graph->array[i].head = NULL;

return graph;
}

void addEdge(struct Graph* graph, int src, int dest)


{

struct AdjListNode* newNode = newAdjListNode(dest);


newNode->next = graph->array[src].head;
graph->array[src].head = newNode;

newNode = newAdjListNode(src);
newNode->next = graph->array[dest].head;
graph->array[dest].head = newNode;
}

void printGraph(struct Graph* graph)


{
int v;
for (v = 0; v < graph->V; ++v)
{
struct AdjListNode* pCrawl = graph->array[v].head;
printf("\n Adjacency list of vertex %d\n head ", v);
while (pCrawl)
{
printf("-> %d", pCrawl->dest);
pCrawl = pCrawl->next;
}
printf("\n");
}
}

int main()
{

int V = 6;
struct Graph* graph = createGraph(V);
addEdge(graph, 5, 1);
addEdge(graph, 5, 4);
addEdge(graph, 4, 1);
addEdge(graph, 4, 3);
addEdge(graph, 4, 2);
addEdge(graph, 1, 2);
addEdge(graph, 3, 2);
addEdge(graph, 3, 6);
addEdge(graph, 2, 6);

printGraph(graph);

return 0;
}

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

struct node
{
int vertex;
struct node* next;
};
struct node* createNode(int);
struct Graph
{
int numVertices;
struct node** adjLists;
};
struct Graph* createGraph(int vertices);
void addEdge(struct Graph* graph, int src, int dest);
void printGraph(struct Graph* graph);
int main()
{
struct Graph* graph = createGraph(5);
addEdge(graph, 1, 2);
addEdge(graph, 2, 3);
addEdge(graph, 2, 4);
addEdge(graph, 2, 5);
addEdge(graph, 3, 5);
addEdge(graph, 4, 5);

printGraph(graph);
return 0;
}

struct node* createNode(int v)


{
struct node* newNode = malloc(sizeof(struct node));
newNode->vertex = v;
newNode->next = NULL;
return newNode;
}

struct Graph* createGraph(int vertices)


{
struct Graph* graph = malloc(sizeof(struct Graph));
graph->numVertices = vertices;

graph->adjLists = malloc(vertices * sizeof(struct node*));

int i;
for (i = 0; i < vertices; i++)
graph->adjLists[i] = NULL;

return graph;
}

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

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");
}
getch();
}

(ii) To perform Depth First Search Traversal


GRAPH-1:

#include<stdio.h>
#include<conio.h>
int a[20][20],reach[20],n;
void dfs(int v) {
int i;
reach[v]=1;
for (i=1;i<=n;i++)
if(a[v][i] && !reach[i]) {
printf("\n %d->%d",v,i);
dfs(i);
}
}
int main() {
int i,j,count=0;

printf("\n Enter number of vertices:");


scanf("%d",&n);
for (i=1;i<=n;i++) {
reach[i]=0;
for (j=1;j<=n;j++)
a[i][j]=0;
}
printf("\n Enter the adjacency matrix:\n");
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
scanf("%d",&a[i][j]);
dfs(1);
printf("\n");
for (i=1;i<=n;i++) {
if(reach[i])
count++;
}
if(count==n)
printf("\n Graph is connected"); else
printf("\n Graph is not connected");
getch();
}
GRAPH-2:

GRAPH-3:
(iii) To perform Breadth First Search TraversaL;
#include<stdio.h>
#include<conio.h>
int a[20][20], q[20], visited[20], n, i, j, f = 0, r = -1;

void bfs(int v) {
for(i = 1; i <= n; i++)
if(a[v][i] && !visited[i])
q[++r] = i;
if(f <= r) {
visited[q[f]] = 1;
bfs(q[f++]);
}
}

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

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


q[i] = 0;
visited[i] = 0;
}

printf("\n Enter graph data in matrix form:\n");


for(i=1; i<=n; i++) {
for(j=1;j<=n;j++) {
scanf("%d", &a[i][j]);
}
}

printf("\n Enter the starting vertex:");


scanf("%d", &v);
bfs(v);
printf("\n The node which are reachable are:\n");

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


if(visited[i])
printf("%d\t", i);
else {
printf("\n Bfs is not possible. Not all nodes are reachable");
break;
}
}
getch();
}
GRAPH-1:

GRAPH-2:

GRAPH-3:
(iii) To find the Minimum Spanning Tree for Prim’s or Krsukal’s algorithm.

#include<stdio.h>
#include<conio.h>
#define MAX 30
typedef struct edge
{
int u,v,w;
}edge;
typedef struct edgelist
{
edge data[MAX];
int n;
}edgelist;
edgelist elist;
int G[MAX][MAX],n;
edgelist spanlist;
void kruskal();
int find(int belongs[],int vertexno);
void union1(int belongs[],int c1,int c2);
void sort();
void print();
void main()
{
int i,j,total_cost;
printf("\nEnter number of vertices:");
scanf("%d",&n);
printf("\nEnter the adjacency matrix:\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);
kruskal();
print();
}
void kruskal()
{
int belongs[MAX],i,j,cno1,cno2;
elist.n=0;
for(i=1;i<n;i++)
for(j=0;j<i;j++)
{
if(G[i][j]!=0)
{
elist.data[elist.n].u=i;
elist.data[elist.n].v=j;
elist.data[elist.n].w=G[i][j];
elist.n++;
}
}
sort();
for(i=0;i<n;i++)
belongs[i]=i;
spanlist.n=0;
for(i=0;i<elist.n;i++)
{
cno1=find(belongs,elist.data[i].u);
cno2=find(belongs,elist.data[i].v);
if(cno1!=cno2)
{
spanlist.data[spanlist.n]=elist.data[i];
spanlist.n=spanlist.n+1;
union1(belongs,cno1,cno2);
}
}
}
int find(int belongs[],int vertexno)
{
return(belongs[vertexno]);
}
void union1(int belongs[],int c1,int c2)
{
int i;
for(i=0;i<n;i++)
if(belongs[i]==c2)
belongs[i]=c1;
}
void sort()
{
int i,j;
edge temp;
for(i=1;i<elist.n;i++)
for(j=0;j<elist.n-1;j++)
if(elist.data[j].w>elist.data[j+1].w)
{
temp=elist.data[j];
elist.data[j]=elist.data[j+1];
elist.data[j+1]=temp;
}
}
void print()
{
int i,cost=0;
for(i=0;i<spanlist.n;i++)
{
printf("\n%d\t%d\t%d",spanlist.data[i].u,spanlist.data[i].v,spanlist.data[i].w);
cost=cost+spanlist.data[i].w;
}
printf("\n\nCost of the spanning tree=%d",cost);
getch();
}

GRAPH-1:
GRAPH-2:

GRAPH-3:
2. Implement a program to perform the traversals on Binary Tree without using
Recursion. Show the sample execution for your input Binary Tree with the height
of 4.

In-order with height 4.

#include<stdio.h>
#include<stdlib.h>
#define bool int

struct tNode
{
int data;
struct tNode* left;
struct tNode* right;
};

struct sNode
{
struct tNode *t;
struct sNode *next;
};

void push(struct sNode** top_ref, struct tNode *t);


struct tNode *pop(struct sNode** top_ref);
bool isEmpty(struct sNode *top);
void inOrder(struct tNode *root)
{

struct tNode *current = root;


struct sNode *s = NULL; /* Initialize stack s */
bool done = 0;

while (!done)
{

if(current != NULL)
{
push(&s, current);

current = current->left;
}

else

{
if (!isEmpty(s))
{
current = pop(&s);
printf("%d ", current->data);

current = current->right;
}
else
done = 1;
}
} /* end of while */
}

/* UTILITY FUNCTIONS */
/* Function to push an item to sNode*/
void push(struct sNode** top_ref, struct tNode *t)
{
/* allocate tNode */
struct sNode* new_tNode =
(struct sNode*) malloc(sizeof(struct sNode));

if(new_tNode == NULL)
{
printf("Stack Overflow \n");
getchar();
exit(0);
}

/* put in the data */


new_tNode->t = t;

/* link the old list off the new tNode */


new_tNode->next = (*top_ref);

/* move the head to point to the new tNode */


(*top_ref) = new_tNode;
}

/* The function returns true if stack is empty, otherwise false */


bool isEmpty(struct sNode *top)
{
return (top == NULL)? 1 : 0;
}

/* Function to pop an item from stack*/


struct tNode *pop(struct sNode** top_ref)
{
struct tNode *res;
struct sNode *top;

/*If sNode is empty then error */


if(isEmpty(*top_ref))
{
printf("Stack Underflow \n");
getchar();
exit(0);
}
else
{
top = *top_ref;
res = top->t;
*top_ref = top->next;
free(top);
return res;
}
}

struct tNode* newtNode(int data)


{
struct tNode* tNode = (struct tNode*)
malloc(sizeof(struct tNode));
tNode->data = data;
tNode->left = NULL;
tNode->right = NULL;

return(tNode);
}

/* Driver program to test above functions*/


int main()
{

struct tNode *root = newtNode(1);


root->left = newtNode(2);
root->right = newtNode(3);
root->left->left = newtNode(4);
root->left->right = newtNode(5);
root->left->right = newtNode(6);
inOrder(root);

getchar();
return 0;
}
3. Build a simple program to perform the insert, delete and search operations in
Binary Search Tree without using recursion. Show the sample execution for your
input Binary Search Tree with the height of 4.

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

/* loop down to find the leftmost leaf */


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

return current;
}

struct node* search(struct node* root, int key)


{
// Base Cases: root is null or key is present at root
if (root == NULL || root->key == key)
return root;

// Key is greater than root's key


if (root->key < key)
return search(root->right, key);

// Key is smaller than root's key


return search(root->left, key);
}

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


{

if (root == NULL) return root;

if (key < root->key)


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

// If the key to be deleted is greater than the root's key,


// then it lies in right subtree
else if (key > root->key)
root->right = deleteNode(root->right, key);

// if key is same as root's key, then This is the node


// to be deleted
else
{
// node with only one child or no child
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, 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);
printf("Inorder traversal of the given tree \n");
inorder(root);

printf("\nDelete 20\n");
root = deleteNode(root, 20);
printf("Inorder traversal of the modified tree \n");
inorder(root);

printf("\nDelete 30\n");
root = deleteNode(root, 30);
printf("Inorder traversal of the modified tree \n");
inorder(root);

printf("\nDelete 50\n");
root = deleteNode(root, 50);
printf("Inorder traversal of the modified tree \n");
inorder(root);

return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy