Ds Da-2 Lab 18bca0045
Ds Da-2 Lab 18bca0045
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;
}
int i;
for (i = 0; i < vertices; i++)
graph->adjLists[i] = NULL;
return graph;
}
#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;
};
int i;
for (i = 0; i < V; ++i)
graph->array[i].head = NULL;
return graph;
}
newNode = newAdjListNode(src);
newNode->next = graph->array[dest].head;
graph->array[dest].head = newNode;
}
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;
}
int i;
for (i = 0; i < vertices; i++)
graph->adjLists[i] = NULL;
return graph;
}
#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;
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);
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.
#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;
};
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);
}
return(tNode);
}
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;
};
return node;
}
return current;
}
root->key = temp->key;
int main()
{
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;
}