You are on page 1of 44

Exercise 1:

Write C program that implements Rehashing methods or collision resolution


techniques.
#include<stdio.h>
#include<conio.h>
void main()
{
int e[20],h[20],fill[20]={0},b[20];
int k=0,i,j,m,n,t,ch,r,p,q=0;
clrscr();
printf("Enter Hash Table size: ");
scanf("%d",&m);
printf("Enter No. of elements: ");
scanf("%d",&n);
printf("Enter elements: ");
for(i=0;i<n;i++)
scanf("%d",&e[i]);
printf("\n 1.Linear Probing \n 2.Quadratic Probing \n 3.Double Hashing \n");
printf("\nEnter Choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: for(i=0;i<n;i++)
{
k=e[i]%m;
t=k;
if(fill[k]==0)
{
h[k]=e[i];
fill[k]=1;
}
else
{
for(j=1;j<=m;j++)
{
k=(t+j)%m;
if(fill[k]==0)
{
h[k]=e[i];
fill[k]=1;
break;
}
}
if(m==j+1)
{
b[q++]=e[i];
1

}
}
}
break;
case 2: for(i=0;i<n;i++)
{
k=e[i]%m;
t=k;
if(fill[k]==0)
{
h[k]=e[i];
fill[k]=1;
}
else
{
for(j=1;j<=m;j++)
{
k=(t+j*j)%m;
if(fill[k]==0)
{
h[k]=e[i];
fill[k]=1;
break;
}
}
if(j==m+1)
{
b[q++]=e[i];
}
}
}
break;
case 3: printf("Enter Prime number < %d: ",m);
scanf("%d",&r);
for(i=0;i<n;i++)
{
k=e[i]%m;
t=k;
if(fill[k]==0)
{
h[k]=e[i];
fill[k]=1;
}
else
{
for(j=1;j<=m;j++)
{
p=r-e[i]%r;
k=(t+j*p)%m;
2

if(fill[k]==0)
{
h[k]=e[i];
fill[k]=1;
break;
}
}
if(j==m+1)
{
b[q++]=e[i];
}
}
}
break;
default: printf("\n Enter correct choice...");
}
printf(" Elements in Hash Table are: ");
for(i=0;i<m;i++)
{
if(fill[i]==1)
printf("\n%d-->%d",i,h[i]);
else
printf("\n%d-->",i);
}
if(q!=0)
{
printf("\n Elements in Overflow bucket are: ");
for(i=0;i<q;i++)
printf("%2d",b[i]);
}
getch();
}
Output
Enter No. of elements: 8
Enter elements: 13
16
7
21
25
34
43
50
1.Linear Probing
2.Quadratic Probing
3.Double Hashing
3

Enter Choice: 2
Elements in Hash Table are:
0-->16
1-->25
2-->34
3-->43
4-->
5-->13
6-->21
7-->7
Elements in Overflow bucket are: 50

Exercise 1:
Write a program to implement functions of Dictionary using Hashing (division
method, Multiplication method)
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int e[20],h[20],fill[20]={0},k=0,i,j,m,n,ch;
float A;
clrscr();
printf("Enter Hash Table size: ");
scanf("%d",&m);
printf("Enter No. of elements: ");
scanf("%d",&n);
printf("Enter elements: ");
for(i=0;i<n;i++)
scanf("%d",&e[i]);
4

printf("\n 1.Division Method \n 2.Multiplication Method \n");


printf("\nEnter Choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: for(i=0;i<n;i++)
{
k=e[i]%m;
if(fill[k]==0)
{
h[k]=e[i];
fill[k]=1;
}
else
{
while(fill[k]==1)
{
k++;
k=k%m;
}
h[k]=e[i];
fill[k]=1;
}
}
break;
case 2: printf("Enter A (0<A<1): ");
scanf("%f",&A);
for(i=0;i<n;i++)
{
k=floor(m*(A*e[i]-floor(A*e[i])));
if(fill[k]==0)
{
h[k]=e[i];
fill[k]=1;
}
else
{
while(fill[k]==1)
{
k++;
k=k%m;
}
h[k]=e[i];
fill[k]=1;
}
}
break;
default: printf("\n Enter correct choice...");
5

}
printf("Elements in Hash Table are: ");
for(i=0;i<m;i++)
{
if(fill[i]==1)
printf("\n%d-->%d",i,h[i]);
else
printf("\n%d-->",i);
}
getch();
}

Output
Enter Hash Table size: 10
Enter No. of elements: 5
Enter elements: 89
18
48
58
69
1.Division Method
2.Multiplication Method
Enter Choice: 2
Enter A (0<A<1): 0.65
Elements in Hash Table are:
0-->
1-->48
2-->
3-->
4-->
5-->
6-->18
7-->58
8-->89
9-->69

Exercise 2:
Write a program to perform various operations of insertions and deletions on
AVL trees
#include<stdio.h>
#include<conio.h>
struct node
{
int key;
struct node *left;
struct node *right;
int height;
};
typedef struct node avlnode;
int height(avlnode *N)
{
if (N == NULL)
return 0;
return N->height;
}
int max(int a, int b)
{
return (a > b)? a : b;
}
avlnode* newNode(int key)
{
avlnode* node = (avlnode*)malloc(sizeof(avlnode));
node->key = key;
node->left = NULL;
node->right = NULL;
node->height = 1; // new node is initially added at leaf
return(node);
7

}
avlnode *rightRotate(avlnode *y)
{
avlnode *x = y->left;
avlnode *T2 = x->right;
x->right = y;
y->left = T2;
y->height = max(height(y->left), height(y->right))+1;
x->height = max(height(x->left), height(x->right))+1;
return x;
}
avlnode *leftRotate(avlnode *x)
{
avlnode *y = x->right;
avlnode *T2 = y->left;
y->left = x; //Perform Rotation
x->right = T2;
x->height = max(height(x->left), height(x->right))+1; // Update heights
y->height = max(height(y->left), height(y->right))+1;
return y; // Return new root
}
int getBalance(avlnode *N) // Return new root
{
if (N == NULL)
return 0;
return height(N->left) - height(N->right);
}
avlnode* insert(avlnode* node, int key)
{
int balance;
if (node == NULL)
return(newNode(key));
if (key < node->key)
node->left = insert(node->left, key);
else
node->right = insert(node->right, key);
node->height = max(height(node->left), height(node->right)) + 1;
balance = getBalance(node);
if (balance > 1 && key < node->left->key)
return rightRotate(node);
if (balance < -1 && key > node->right->key)
return leftRotate(node);
if (balance > 1 && key > node->left->key)
{
node->left = leftRotate(node->left);
return rightRotate(node);
}
8

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


{
node->right = rightRotate(node->right);
return leftRotate(node);
}
return node;
}
avlnode * minValueNode(avlnode* node)
{
avlnode* current = node;
/* loop down to find the leftmost leaf */
while (current->left != NULL)
current = current->left;
return current;
}
avlnode* deleteNode(avlnode* root, int key)
{
int balance;
if (root == NULL)
return root;
if ( key < root->key )
root->left = deleteNode(root->left, key);
else if( key > root->key )
root->right = deleteNode(root->right, key);
else
{
if( (root->left == NULL) || (root->right == NULL) )
{
avlnode *temp = root->left ? root->left : root->right;
if(temp == NULL)
{
temp = root;
root = NULL;
}
else
*root = *temp; // Copy the contents of the non-empty child
free(temp);
}
else
{
avlnode* temp = minValueNode(root->right);
root->key = temp->key;
root->right = deleteNode(root->right, temp->key);
}
}
if (root == NULL)
return root;
root->height = max(height(root->left), height(root->right)) + 1;
9

balance = getBalance(root);
if (balance > 1 && getBalance(root->left) >= 0)
return rightRotate(root);
if (balance > 1 && getBalance(root->left) < 0)
{
root->left = leftRotate(root->left);
return rightRotate(root);
}
if (balance < -1 && getBalance(root->right) <= 0)
return leftRotate(root);
if (balance < -1 && getBalance(root->right) > 0)
{
root->right = rightRotate(root->right);
return leftRotate(root);
}
return root;
}
void preorder(avlnode *root)
{
if(root != NULL)
{
printf(" %d", root->key);
preorder(root->left);
preorder(root->right);
}
}
void main()
{
avlnode *root = NULL;
int i,n,x,ch;
clrscr();
printf("\n 1. Insert \n 2. Delete \n 3. Display \n 4. Exit \n");
while(1)
{
printf(" Enter choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf(" Enter Node to insert: ");
scanf("%d",&x);
root=insert(root,x);
break;
case 2: printf(" Enter node to delete: ");
scanf("%d",&x);
root=deleteNode(root,x);
break;
case 3: printf(" Preorder traversal of AVL Tree: \n");
preorder(root);
10

printf("\n");
break;
case 4: exit(0);
default:printf(" Enter correct choice...");
}
}
getch();
}

Output
1.
2.
3.
4.

Insert
Delete
Display
Exit
11

Enter choice: 1
Enter Node to insert: 9
Enter choice: 1
Enter Node to insert: 5
Enter choice: 1
Enter Node to insert: 10
Enter choice: 1
Enter Node to insert: 0
Enter choice: 1
Enter Node to insert: 6
Enter choice: 1
Enter Node to insert: 11
Enter choice: 1
Enter Node to insert: -1
Enter choice: 1
Enter Node to insert: 1
Enter choice: 1
Enter Node to insert: 2
Enter choice: 3
Preorder traversal of AVL Tree:
9 1 0 -1 5 2 6 10 11
Enter choice: 2
Enter node to delete: 10
Enter choice: 3
Preorder traversal of AVL Tree:
1 0 -1 9 5 2 6 11
Enter choice: 4

Exercise 3:
Write a Program to implement operations on graphs
i) vertex insertion
ii) Vertex deletion
iii) finding vertex
iv) Edge addition and deletion
#include<stdio.h>
#include<conio.h>
12

void main()
{
int a[10][10],i,j,n=0,ch,p,q,v;
clrscr();
printf("\n 1.Vertex Insertion \n 2.Vertex Deletion \n 3.Edge insertion ");
printf("\n 4.Edge deletion \n 5.Display \n 6.Exit \n");
while(1)
{
printf(" Enter choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: n=n+1;
for(i=1;i<=n;i++)
a[i][n]=0;
for(j=1;j<=n;j++)
a[n][j]=0;
break;
case 2: if(n>0)
{
printf(" Which vertex you want to delete: ");
scanf("%d",&v);
if(v<=n)
{
if(v!=n)
{
for(i=1;i<=n;i++)
{
for(j=v;j<n;j++)
a[i][j]=a[i][j+1];
}
for(j=1;j<n;j++)
{
for(i=v;i<n;i++)
a[i][j]=a[i+1][j];
}
}
n--;
}
else
printf("\n No such vertex found ");
}
else
printf("\n Enter correct vertex ");
break;
case 3: printf(" where u want to insert the edge: ");
scanf("%d%d",&i,&j);
if(i<=n && j<=n)
{
13

if(a[i][j]==0)
a[i][j]=a[j][i]=1;
else
printf("\n Edge already exist ");
}
else
printf("\n Inserting edge is not possible ");
break;
case 4: printf(" where u want to delete the edge: ");
scanf("%d%d",&i,&j);
if(i<=n && j<=n)
{
if(a[i][j]==1)
a[i][j]=a[j][i]=0;
else
printf("\n There is no Edge ");
}
else
printf("\n No such edge exists ");
break;
case 5: printf(" Resultant Graph: ");
for(i=1;i<=n;i++)
{
printf("\n");
for(j=1;j<=n;j++)
printf("%4d",a[i][j]);
}
printf("\n");
break;
case 6: getch();
exit(0);
default:printf("\n Enter correct choice... ");
}
}
}

14

Output
1.Vertex Insertion
2.Vertex Deletion
3.Edge insertion
4.Edge deletion
5.Display
6.Exit
Enter choice: 1
Enter choice: 1
Enter choice: 1
Enter choice: 1
Enter choice: 5
Resultant Graph:
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
Enter choice: 3
where u want to insert the edge: 1 2
Enter choice: 3
where u want to insert the edge: 1 4
Enter choice: 3
where u want to insert the edge: 2 3
Enter choice: 3
15

where u want to insert the edge: 2 4


Enter choice: 3
where u want to insert the edge: 3 4
Enter choice: 5
Resultant Graph:
0 1 0 1
1 0 1 1
0 1 0 1
1 1 1 0
Enter choice: 2
Which vertex you want to delete: 2
Enter choice: 5
Resultant Graph:
0 0 1
0 0 1
1 1 0
Enter choice: 6
Exercise 4:
Write a program to implement Breadth First Search for a graph
nonrecursively.
#include<stdio.h>
#include<conio.h>
int a[20][20],q[20],vis[20],front=-1,rear=-1;
void add(int item);
int remov();
int empty();
void bfs(int s,int n);
void main()
{
int i,j,n,s;
clrscr();
printf(" Enter Number of Vertices: ");
scanf( "%d", &n );
printf("\n Note: Enter 1 if edge exists otherwise 0 \n");
printf("\n Adjacency Matrix: \n" );
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=1;i<=n;i++)
16

vis[i]=0;
printf("\n Enter the Source Vertex: ");
scanf("%d",&s);
printf("\n BFS: ");
bfs(s,n);
getch();
}
void bfs(int s,int n)
{
int p,i;
add(s);
vis[s]=1;
while(!empty())
{
p=remov();
if(p!=0)
printf("%4d",p);
for(i=1;i<=n;i++)
{
if((a[p][i]!=0)&&(vis[i]==0))
{
add(i);
vis[i]=1;
}
}
}
}
void add(int item)
{
if(rear==19)
printf("QUEUE FULL");
else
{
if(rear==-1)
{
q[++rear]=item;
front++;
}
else
q[++rear]=item;
}
}
int remov()
{
int k;
if((front>rear)||(front==-1))
17

return 0;
else
{
k=q[front++];
return k;
}
}

int empty()
{
if(front>rear)
return 1;
else
return 0;
}
Output
Enter Number of Vertices: 6
Note: Enter 1 if edge exists otherwise 0
Adjacency Matrix:
0
1
1
0
0
1

1
0
1
0
0
0

1
1
0
1
1
0

0
0
1
0
0
0

0
0
1
0
0
1

1
0
0
0
1
0

Enter the Source Vertex: 5


BFS:

5 3 6 1 2 4

18

Exercise 5:
Write a program to implement Depth First Search for a graph nonrecursively.
#include<stdio.h>
#include<conio.h>
int top=-1,a[20][20],vis[20],stack[20];
void dfs(int s,int n);
void push(int item);
int pop();
int empty();
void main()
{
int n,i,s,ch,j;
clrscr();
printf(" Enter Number of Vertices: ");
scanf("%d",&n);
printf("\n Note: Enter 1 if edge exists otherwise 0 \n");
printf("\n Adjacency Matrix \n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf( "%d", &a[i][j]);
}
}
for(i=1;i<=n;i++)
vis[i]=0;
printf("\n Enter the Source Vertex: ");
scanf( "%d",&s);
printf("\n DFS: ");
dfs(s,n);
getch();
}
void dfs(int s,int n)
{
int i,p,count[20]={0};
push(s);
vis[s]=1;
19

while(!empty())
{
p=pop();
for(i=1;i<=n;i++)
{
if((a[p][i]!=0)&&(vis[i]==0))
{
push(i);
}
vis[p]=1;
if(p!=0&&count[p]==0)
{
printf("%4d",p);
count[p]=1;
}
}
}
}
void push( int item )
{
if(top==19)
printf( "Stack overflow " );
else
stack[++top]=item;
}
int pop()
{
int k;
if(top==-1)
return 0;
else
{
k=stack[top--];
return k;
}
}
int empty()
{
if(top==-1)
return 1;
else
return 0;
}
Output
Enter Number of Vertices: 6
Note: Enter 1 if edge exists otherwise 0
20

Adjacency Matrix:
011000
100110
100100
011001
010000
000100
Enter the Source Vertex: 4
DFS:

4 6 3 1 2 5

Exercise 6:
Write a program to implement operations on binary heap.
#include<stdio.h>
#include<conio.h>
void insert(int x);
void percolatedown(int hole);
int h[20],n,size=0;
21

void main()
{
int ch,i=1,x;
clrscr();
printf("\n 1.Insert \n 2.deleteMin \n 3.Display \n 4.Exit \n");
while(1)
{
printf(" Enter choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf(" Enter element to insert: ");
scanf("%d",&h[i]);
insert(h[i]);
n=i++;
break;
case 2: x=h[1];
h[1]=h[size--];
n--;
if(n>=0)
{
printf(" Deleted element: %d \n",x);
percolatedown(1);
}
else
printf("\n Deletion not possible... \n");
break;
case 3: printf(" The Elements are: \n");
for(i=1;i<=n;i++)
printf("%4d",h[i]);
printf("\n");
break;
case 4: getch();
exit(0);
default:printf(" Enter correct choice...");
}
}
}
void insert(int x)
{
int i;
for(i=++size;i>1&&h[i/2]>x;i=i/2)
h[i]=h[i/2];
h[i]=x;
}
void percolatedown(int hole)
{
int child;
22

int temp=h[hole];
for(;hole*2<=n;hole=child)
{
child=hole*2;
if(child!=n&&h[child+1]<h[child])
child++;
if(h[child]<temp)
h[hole]=h[child];
else
break;
}
h[hole]=temp;
}

Output
1.Insert
2.deleteMin
3.Display
4.Exit
Enter choice: 1
Enter element to
Enter choice: 1
Enter element to
Enter choice: 1
Enter element to
Enter choice: 1
Enter element to
Enter choice: 1
Enter element to
Enter choice: 1
Enter element to

insert: 9
insert: 8
insert: 7
insert: 6
insert: 5
insert: 4
23

Enter choice: 1
Enter element to insert: 3
Enter choice: 1
Enter element to insert: 2
Enter choice: 1
Enter element to insert: 1
Enter choice: 1
Enter element to insert: 0
Enter choice: 3
The Elements are:
0 1 4 3 2 8 5 9 6 7
Enter choice: 2
Deleted element: 0
Enter choice: 2
Deleted element: 1
Enter choice: 2
Deleted element: 2
Enter choice: 3
The Elements are:
3 6 4 9 7 8 5
Enter choice: 4

Exercise 7:
Write a program to create binary heap using buildheap()
#include<stdio.h>
#include<conio.h>
void buildheap();
void percolatedown(int hole);
int h[20],i,n;
void main()
{
clrscr();
printf("\n Enter n: ");
scanf("%d",&n);
printf("\n Enter Elements: ");
for(i=1;i<=n;i++)
scanf("%d",&h[i]);
buildheap();
printf(\n Elements in Binary heap is \n);
for(i=1;i<=n;i++)
printf(" %d ",h[i]);
getch();
24

}
void buildheap()
{
int j;
for(j=n/2;j>0;j--)
percolatedown(j);
}
void percolatedown(int hole)
{
int child;
int temp=h[hole];
for(;hole*2<=n;hole=child)
{
child=hole*2;
if(child!=n&&h[child+1]<h[child])
child++;
if(h[child]<temp)
h[hole]=h[child];
else
break;
}
h[hole]=temp;
}
Output
Enter n: 10
Enter Elements: 9
8
7
6
5
4
3
2
1
0
Elements in Binary heap is
0 1 3 2 5 4 7 9 6 8

25

Exercise 8:
Write a program to implement Prims algorithm to generate a min-cost
spanning tree.
#include<stdio.h>
#include<conio.h>
int a,b,u,v,n,i,j,ne=1;
int visited[10]={0},min,mincost=0,cost[10][10];
void main()
{
clrscr();
printf("\n Enter the number of nodes: ");
scanf("%d",&n);
printf("\n Enter the adjacency matrix: \n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=99;
}
}
visited[1]=1;
printf("\n");
printf("\n The Edges of Minimum Cost Spanning Tree are: \n");
while(ne<n)
{
for(i=1,min=99;i<=n;i++)
{
26

for(j=1;j<=n;j++)
{
if(cost[i][j]<min)
{
if(visited[i]!=0)
{
min=cost[i][j];
a=u=i;
b=v=j;
}
}
}
}
if(visited[u]==0 || visited[v]==0)
{
printf("\n edge %d: (%d %d) and cost= %d ",ne++,a,b,min);
mincost +=min;
visited[b]=1;
}
cost[a][b]=cost[b][a]=99;
} //end while loop
printf("\n\n Minimum cost=%d",mincost);
getch();
}
Output
Enter the number of nodes: 4
Enter the adjacency matrix:
0132
1050
3501
2010
The Edges of Minimum Cost Spanning Tree are:
edge 1: (1 2) and cost= 1
edge 2: (1 4) and cost= 2
edge 3: (4 3) and cost= 1
Minimum cost=4

27

Exercise 9:
Write a program to implement Kruskals algorithm to generate a min-cost
spanning tree.
#include < stdio.h>
#include < conio.h>
void sortedges(edge a[],int n);
int checkcycle(int p[],int i,int j);
typedef struct
{
int node1,node2,wt;
}edge;
void main()
{
edge e[20];
int parent[20];
int n,i,j,m,k = 1,cost = 0;
clrscr();
printf(" Enter number of nodes: ");
scanf("%d",&n);
for(i=0;i<n;++i)
parent[i]=-1;
i=0;
printf(" Enter number of Edges: ");
scanf("%d",&m);
for(i=0;i<m;i++)
{
printf(" Enter Edge %d end vertices and weight: ",i+1);
scanf("%d %d %d", &e[i].node1,&e[i].node2,&e[i].wt);
}
sortedges(e,m);
printf("\n Edges of the tree are: \n");
i=0;
while(k<n)
{
28

if(checkcycle(parent,e[i].node1,e[i].node2))
{
k++;
cost=cost+e[i].wt;
i++;
}
}
printf("\n Minimum cost = %d",cost);
getch();
}
void sortedges(edge a[],int n)
{
int i,j;
edge temp;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i].wt>a[j].wt)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
}
int checkcycle(int p[],int i,int j)
{
int v1,v2;
v1=i;
v2=j;
while(p[i]>-1)
i=p[i];
while(p[j]>-1)
j=p[j];
if(i!=j)
{
p[j]=i;
printf("%d %d\n",v1,v2);
return 1;
}
return 0;
}

29

Output
Enter
Enter
Enter
Enter
Enter
Enter
Enter
Enter
Enter
Enter

number of nodes: 5
number of Edges: 8
Edge 1 end vertices
Edge 2 end vertices
Edge 3 end vertices
Edge 4 end vertices
Edge 5 end vertices
Edge 6 end vertices
Edge 7 end vertices
Edge 8 end vertices

and
and
and
and
and
and
and
and

weight:
weight:
weight:
weight:
weight:
weight:
weight:
weight:

1
1
1
2
2
3
4
5

2
4
5
3
5
4
5
3

2
1
2
1
2
3
3
3

Edges of the tree are:


1
2
1
1

4
3
5
2

Minimum cost = 6

Exercise 10:
30

Write a Program to implement Dijkstras algorithm to find shortest path in


the graph.
#include<stdio.h>
#include<conio.h>
void dij(int n,int v,int cost[10][10],int dist[]);
void main()
{
int n,v,i,j,cost[10][10],dist[10];
clrscr();
printf("\n Enter the number of nodes: ");
scanf("%d",&n);
printf("\n Enter the cost matrix: \n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=99;
}
}
printf("\n Enter the source node: ");
scanf("%d",&v);
dij(n,v,cost,dist);
printf("\n Shortest paths: \n");
for(i=1;i<=n;i++)
{
if(i!=v)
printf("\n %d-->%d and cost: %d ",v,i,dist[i]);
}
getch();
}
void dij(int n,int v,int cost[10][10],int dist[])
{
int i,u,count,w,flag[10],min;
for(i=1;i<=n;i++)
{
flag[i]=0;
dist[i]=cost[v][i];
}
count=2;
while(count<=n)
{
min=99;
for(w=1;w<=n;w++)
{
31

if(dist[w]<min && !flag[w])


min=dist[w],u=w;
}
flag[u]=1;
count++;
for(w=1;w<=n;w++)
if((dist[u]+cost[u][w]<dist[w]) && !flag[w])
dist[w]=dist[u]+cost[u][w];
}
}
Output
Enter the number of nodes: 4
Enter the cost matrix:
0132
1050
3501
2010
Enter the source node: 3
Shortest paths:
3-->1 and cost: 3
3-->2 and cost: 4
3-->4 and cost: 1

Exercise 11:
Write C programs that implement Quick sort, to sort a given list of integers in
ascending order
#include<stdio.h>
#include<conio.h>
void quicksort(int a[],int first,int last);
void swap(int a[],int i,int j);
void main()
32

{
int a[20],i,n;
clrscr();
printf("\n Enter n: ");
scanf("%d",&n);
printf("\n Enter Elements: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
quicksort(a,0,n-1);
printf("\n After Sorting: \n");
for(i=0;i<n;i++)
printf("%3d",a[i]);
getch();
}
void quicksort(int a[],int first,int last)
{
int pivot,i,j;
if(first<last)
{
pivot=a[first];
i=first;
j=last;
while(i<j)
{
while((a[i]<=pivot)&&(i<last))
i++;
while((a[j]>=pivot)&&(j>first))
j--;
if(i<j)
swap(a,i,j);
}
swap(a,first,j);
quicksort(a,first,j-1);
quicksort(a,j+1,last);
}
}
void swap(int a[],int i,int j)
{
int temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
Output
Enter n: 4
Enter Elements: 4
33

2
1
-7
After Sorting:
-7 1 2 4

Exercise 12:
Write C programs that implement merge sort, to sort a given list of integers
in ascending order
#include<stdio.h>
#include<conio.h>
#define MAX 20
void mergesort(int a[],int,int);
void merge(int a[],int,int,int);
void main()
{
int i,n,a[20];
clrscr();
printf("Enter the number of elements : ");
scanf("%d",&n);
printf("Enter %d elements: ",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
mergesort(a,0,n-1);
printf("\n Sorted list is: \n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
getch();
}
void mergesort(int a[],int low,int high)
34

{
int mid;
if(low!=high)
{
mid=(low+high)/2;
mergesort(a,low,mid);
mergesort(a,mid+1,high);
merge(a,low,mid,high);
}
}
void merge(int a[],int low,int mid,int high)
{
int b[20];
int i=low;
int j=mid+1;
int k=low;
while((i<=mid)&&(j<=high))
{
if(a[i]<=a[j])
b[k++]=a[i++];
else
b[k++]=a[j++];
}
while(i<=mid)
b[k++]=a[i++];
while(j<=high)
b[k++]=a[j++];
for(i=low;i<=high;i++)
a[i]=b[i];
}
Output
Enter the number of elements: 5
Enter elements:
-32 -116 13 4 2
Sorted list is:
-116 -32 2 4 13

35

Exercise 13:
Write C program that implement Heap sort, to sort a given list of integers in
ascending order
#include<stdio.h>
#include<conio.h>
void buildheap(int a[],int);
void heapsort(int a[],int,int);
void main()
{
int a[20],n,i,j,k;
clrscr();
printf("\n Enter n: ");
scanf("%d",&n);
printf("\n Enter the elements: ");
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
buildheap(a,i);
}
j=n;
for(i=1;i<=j;i++)
{
int temp;
temp=a[1];
a[1]=a[n];
a[n]=temp;
n--;
heapsort(a,1,n);
}
n=j;
printf("\n After Sorting \n");
for(i=1;i<=n;i++)
printf("%4d",a[i]);
36

getch();
}
void buildheap(int a[],int i)
{
int v=a[i];
while((i>1)&&(a[i/2]<v))
{
a[i]=a[i/2];
i=i/2;
}
a[i]=v;
}
void heapsort(int a[],int i,int n)
{
int v=a[i];
int j=i*2;
while(j<=n)
{
if((j<n)&&(a[j]<a[j+1]))
j++;
if(a[j]<a[j/2])
break;
a[j/2]=a[j];
j=j*2;
}
a[j/2]=v;
}
Output
Enter n: 5
Enter the elements: 3
1
0
-5
6
After Sorting
-5 0 1 3 6

37

Exercise 14:
Write C programs that implement Radix sort, to sort a given list of integers in
ascending order
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],i,n;
clrscr();
printf("Enter n: ");
scanf("%d", &n);
printf("Enter Elements: ");
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
radixsort(a,n);
printf("\n After Sorting: \n");
for(i=0;i<n;i++)
printf("\t %d",a[i]);
getch();
}
void radixsort(int a[], int n)
{
int i, b[20],m=0,exp=1;
for(i=0;i<n;i++)
{
if(a[i]>m)
m=a[i];
}
while(m/exp>0)
{
int bucket[10]={0};
for(i=0;i<n;i++)
bucket[a[i]/exp%10]++;
for(i=1;i<10;i++)
bucket[i]+=bucket[i-1];
for(i=n-1;i>=0;i--)
b[--bucket[a[i]/exp%10]]=a[i];
for(i=0;i<n;i++)
a[i]=b[i];
exp*= 10;
}
38

}
Output
Enter n: 5
Enter the elements: 31
110
56
3214
112
After Sorting
31 56 110 112 3214

Exercise 15:
Write a program to implement Knuth-Morris-Pratt algorithm for pattern
matching.
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define strsize 100
#define patsize 100
int pmatch();
39

void fail(pat);
int failure[patsize];
char str[strsize],pat[patsize];
void main()
{
int match;
clrscr();
printf(" Enter main String: ");
gets(str);
printf(" Enter Substring to search: ");
gets(pat);
match=pmatch(str,pat);
if(match==-1)
printf("\n Match not found ");
else
printf("\n Match found at %d Location ",match+1);
getch();
}
int pmatch(char *str,char *pat)
{
int i=0,j=0,lens,lenp;
lens=strlen(str);
lenp=strlen(pat);
fail(pat);
while(i<lens && j<lenp)
{
if(str[i]==pat[j])
{
i++;
j++;
}
else if(j==0)
i++;
else
j=failure[j-1]+1;
}
return ((j==lenp) ? (i-lenp) : -1);
}
void fail(char *pat)
{
int i,j,n;
n=strlen(pat);
failure[0]= -1;
for(j=1;j<n;j++)
{
i=failure[j-1];
40

while((pat[j]!=pat[i+1]) && (i>=0))


i=failure[i];
if(pat[j]==pat[i+1])
failure[j]=i+1;
else
failure[j]=-1;
}
}
Output
Enter main String: QIS College of Engg and Technology
Enter Substring to search: ge of Eng
Match found at 10 Location

Exercise 16:
Write a program to implement Boyer-Moore algorithm for pattern matching.
#include<stdio.h>
#include<conio.h>
char text[30],pat[20];
int last(char);
void main()
{
int i,j,m,n,k=-1;
clrscr();
printf("\n Enter Text : ");
gets(text);
printf("\n Enter Pattern: ");
gets(pat);
n=strlen(text);
m=strlen(pat);
i=m-1;
41

j=m-1;
do
{
if(text[i]==pat[j])
{
if(j==0)
{
k=i;
break;
}
else
i--,j--;
}
else
{
i=i+m-min(j,1+last(text[i]));
j=m-1;
}
}while(i<n);
if(k==-1)
printf("\n Match not found");
else
printf("\n Match found at %d position",k+1);
getch();
}
int last(char ch)
{
int i;
for(i=strlen(pat)-1;i>=0;i--)
{
if(ch==pat[i])
return i;
}
return -1;
}
int min(int a,int b)
{
if(a<b)
return a;
else
return b;
}
Output
Enter Text : Lord Jagannadh
Enter Pattern: d Jagan
Match found at 4 position
42

Exercise 17:
Write a program to implement concept of Files using command line
arguments.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main(int argc,char *argv[])
{
FILE *f1,*f2;
char ch;
clrscr();
f1=fopen(argv[1],"w");
printf("\n No. of arguments: %d",argc);
printf("\n Enter data into file: ");
while((ch=getchar())!=EOF)
putc(ch,f1);
fclose(f1);
f1=fopen(argv[1],"r");
f2=fopen(argv[2],"w");
fseek(f1,-1,2);
do
{
ch=getc(f1);
putc(ch,f2);
}while(!(fseek(f1,-2,1)));
fcloseall();
printf("\n Copied data in Reverse order is: ");
43

f2=fopen(argv[2],"r");
while((ch=getc(f2))!=EOF)
putchar(ch);
fclose(f2);
getch();
}
Output
C:\TURBOC\SRI>filerev input.txt output.txt
No. of arguments: 3
Enter data into file: Dept of CSE, QISCET, Ongole-523272^Z
Copied data in Reverse order is: 272325-elognO ,TECSIQ ,ESC fo tpeD

44

You might also like