8/6/2019 Ada Lab 2009
1/41
8/6/2019 Ada Lab 2009
2/41
Algorithms Lab Manual
printf("\n Element %d is found at position %d",k,pos+1);printf("\n Time taken is %lf CPU1 cycles\n",(end-begin)/CLK_TCK);getch();break;
case 2:printf("\nEnter the number of elements\n");scanf("%d",&n);printf("\nEnter the elements of an array\n");for(i=0;ihigh)return -1;
if(k==a[mid])return(mid);
elseif(k
8/6/2019 Ada Lab 2009
3/41
Algorithms Lab Manual
return binsearch(n,a,k,low,mid-1);elsereturn binsearch(n,a,k,mid+1,high);
}
int linsearch(int n,int a[],int k){delay(1000);if(n
8/6/2019 Ada Lab 2009
4/41
Algorithms Lab Manual
Element 12 is found at position 2
Time taken is 1.978022 CPU1 cycles
Case 2
.....MENU.....1.Binary Search
2.Linear Search
3.Exit
Enter your choice
2
Enter the number of elements
4
Enter the elements of an array3
69
12
Enter the elements to be searched9
Element 9 is found at position 3Time taken is 3.021978 CPU cycles
2. Sort a given set of elements using the Heap sort method and determine the time taken to
sort the elements. Repeat the experiment for different values of n, the number of elements in
the list to be sorted and plot a graph of the time taken versus n.
Dept of MCA 2009 4
8/6/2019 Ada Lab 2009
5/41
Algorithms Lab Manual
#include#include#include
void heapcom(int a[],int n){
int i,j,k,item;for(i=1;ia[k]){
a[j]=a[k];j=k;k=j/2;
}a[j]=item;
}}void adjust(int a[],int n){
int item,i,j;j=1;
item=a[j];i=2*j;while(i
8/6/2019 Ada Lab 2009
6/41
Algorithms Lab Manual
void heapsort(int a[],int n){
int i,temp;delay(1000);heapcom(a,n);
for(i=n;i>=1;i--){temp=a[1];a[1]=a[i];a[i]=temp;adjust(a,i);
}}void main()
{int i,n,a[20],ch=1;clock_t start,end;clrscr();while(ch){printf("\n enter the number of elements to sort\n");scanf("%d",&n);printf("\n enter the elements to sort\n");for(i=1;i
8/6/2019 Ada Lab 2009
7/41
Algorithms Lab Manual
8
5
63
1
the sorted list of elemnts is
1
35
6
8
3. Sort a given set of elements using Merge sort method and determine the time taken to sort
the elements. Repeat the experiment for different values of n, the number of elements in the
list to be sorted and plot a graph of the time taken versus n.
Dept of MCA 2009 7
8/6/2019 Ada Lab 2009
8/41
Algorithms Lab Manual
#include#include#include#define max 20
void mergesort(int a[],int low,int high);void merge(int a[],int low,int mid,int high);void main(){
int n,i,a[max],ch=1;clock_t start,end;clrscr();while(ch){
printf("\n\t enter the number of elements\n");
scanf("%d",&n);printf("\n\t enter the elements\n");for(i=0;i
8/6/2019 Ada Lab 2009
9/41
8/6/2019 Ada Lab 2009
10/41
Algorithms Lab Manual
time taken=0.824176
Dept of MCA 2009 10
8/6/2019 Ada Lab 2009
11/41
Algorithms Lab Manual
4. Sort a given set of elements using Selection sort and hence find the time required to sort
elements. Repeat the experiment for different values of n, the number of elements in the list
to be sorted and plot a graph of the time taken versus n.
#include#include#includevoid main(){
int i,n,j,min,k,a[20],ch=1;clock_t begin,end;clrscr();while(ch){
printf("\n enter the number of elements\n");scanf("%d",&n);printf("\n enter the elements to be sorted\n");for(i=0;i
8/6/2019 Ada Lab 2009
12/41
Algorithms Lab Manual
OUTPUT
enter the number of elements5
enter the elements to be sorted8
3
51
9
the sorted list of elements are:1 3 5 8 9
time taken:0.824176
Dept of MCA 2009 12
8/6/2019 Ada Lab 2009
13/41
Algorithms Lab Manual
5. a. Obtain the Topological ordering of vertices in a given digraph.
#include
#include#define max 20
int a[max][max],n;void topological_sort();void main(){
int i,j;clrscr();printf("\n enter the number of vertices\n");scanf("%d",&n);printf("\n enter the adjacency matrix\n");for(i=1;i
8/6/2019 Ada Lab 2009
14/41
8/6/2019 Ada Lab 2009
15/41
Algorithms Lab Manual
5 b. Implement All Pair Shortest paths problem using Floyd's
algorithm.
#include#include
#includeint cost[10][10],a[10][10];void all_paths(int [10][10],int [10][10],int);int min1(int,int);
void main(){
int i,j,n;clrscr();printf("\n enter the number of vertices\n");scanf("%d",&n);printf("\n enter the adjacency matrix\n");for(i=1;i
8/6/2019 Ada Lab 2009
16/41
8/6/2019 Ada Lab 2009
17/41
Algorithms Lab Manual
6. Implement 0/1 Knapsack problem using dynamic programming.
#include#includeint v[20][20];
int max1(int a,int b){return(a>b)?a:b;
}void main(){
int i,j,p[20],w[20],n,max;clrscr();printf("\n enter the number of items\n");scanf("%d",&n);for(i=1;i
8/6/2019 Ada Lab 2009
18/41
Algorithms Lab Manual
for(i=n;i>=1;i--)if(v[i][j]!=v[i-1][j]){
printf("\t item %d:",i);j=j-w[i];}printf("}");getch();
}
OUTPUT
enter the number of items
4
enter the weight and profit of the item 1:2 12
enter the weight and profit of the item 2:1 10
enter the weight and profit of the item 3:3 20
enter the weight and profit of the item 4:2 15
enter the capacity of the knapsack5
The table is0 0 0 0 0 0
0 0 12 12 12 12
0 10 12 22 22 220 10 12 22 30 32
0 10 15 25 30 37
The maximum profit is 37
The most valuable subset is:{ item 4: item 2: item 1:}
Dept of MCA 2009 18
8/6/2019 Ada Lab 2009
19/41
Algorithms Lab Manual
7. From a given vertex in a weighted connected graph, find shortest
paths to other vertices using Dijkstra's algorithm.
#include
main (){int n, cost[15][15], i, j, s[15], v, u, w, dist[15],
num, min;clrscr();printf ("Enter the vertices please\n");scanf ("%d", &n);printf ("Enter the cost of the edges please\n");printf ("Enter 999 if the edge is not present or for the
self loop\n");for (i = 1; i
8/6/2019 Ada Lab 2009
20/41
Algorithms Lab Manual
dist[w] = (dist[u] + cost[u][w]);}
}}
printf ("VERTEX\tDESTINATION\tCOST\n");for (i = 1; i
8/6/2019 Ada Lab 2009
21/41
Algorithms Lab Manual
8. Sort a given set of elements using Quick sort method and determine the time taken to sort
the elements. Repeat the experiment for different values of n, the number of elements in the
list to be sorted and plot a graph of the time taken versus n.
#include#include#includevoid quicksort(int[],int,int);int partition(int[],int,int);void main(){
int i,n,a[20],ch=1;clock_t begin,end;clrscr();while(ch){
printf("\n enter the number of elements\n");scanf("%d",&n);printf("\n enter the array elements\n");for(i=0;i
8/6/2019 Ada Lab 2009
22/41
Algorithms Lab Manual
int key,i,j,temp,k;key=a[low];i=low+1;j=high;while(i
8/6/2019 Ada Lab 2009
23/41
Algorithms Lab Manual
9. Find Minimum Cost Spanning Tree of a given undirected graph
using Kruskal's algorithm-
#include#includeint root[10], flag = 0, count=0, temp, min;int a[20], cost[20][20], n, i, j, k, totalcost = 0, x, y;void find_min (), check_cycle (), update ();main (){clrscr();printf ("Enter the number of vertices please\n");scanf ("%d", &n);printf ("Enter the cost of the matrix please\n");for (i = 1; i
8/6/2019 Ada Lab 2009
24/41
8/6/2019 Ada Lab 2009
25/41
Algorithms Lab Manual
OUTPUT
Enter the number of vertices please
4Enter the cost of the matrix please999 1 5 2
1 999 999 999
5 999 999 32 999 3 999
1 ---> 2 = 1
1 ---> 4 = 23 ---> 4 = 3
The graph is connected & the min cost is 6
Dept of MCA 2009 25
8/6/2019 Ada Lab 2009
26/41
Algorithms Lab Manual
10. a. Print all the nodes reachable from a given starting node in a digraph using Breadth
First Search method.
#include
#include
void distance(int,int);int a[10][10];
void main()
{int i,j,n;
clrscr();
printf("\n Enter the number of vertices in the diagraph:");
scanf("%d",&n);printf("\n Enter the adjacency matrix\n");
for(i=1;i
8/6/2019 Ada Lab 2009
27/41
Algorithms Lab Manual
OUTPUT
Enter the number of vertices in the diagraph:4
Enter the adjacency matrix0 1 1 1
0 0 0 1
0 0 0 0
0 0 1 0
the starting vertex is 1
the vertex 1 to 2 is of distance=1
the vertex 1 to 3 is of distance=1
the vertex 1 to 4 is of distance=1
press enter for other source vertex
the starting vertex is 2
the vertex 2 to 4 is of distance=1
the vertex 2 to 3 is of distance=2
press enter for other source vertex
the starting vertex is 3
press enter for other source vertex
the starting vertex is 4
the vertex 4 to 3 is of distance=1
press enter for other source vertex
Dept of MCA 2009 27
8/6/2019 Ada Lab 2009
28/41
Algorithms Lab Manual
10 b. Check whether a given graph is connected or not using DFS method.
#include#includevoid dfs(int n,int cost[10][10],int u,int s[]){
int v;s[u]=1;for(v=0;v
8/6/2019 Ada Lab 2009
29/41
Algorithms Lab Manual
connected=1;}
if(connected==1)printf("graph is connected\n");elseprintf("graph is not connected\n");getch();
}
OUTPUT
Case 1:
enter the number of nodes
4
enter the adjacency matrix
0 0 0 10 0 0 0
0 0 1 0
0 0 0 1
graph is not connected
Case 2:
enter the number of nodes4
enter the adjacency matrix1 1 1 1
1 1 1 11 1 1 1
1 1 1 1graph is connected
Dept of MCA 2009 29
8/6/2019 Ada Lab 2009
30/41
Algorithms Lab Manual
11. Find a subset of a given set S == {sl,s2,......sn} of n positive
integers whose sum is equal to a given positive integer d. Forexample, if S== (1, 2, 5, 6, 8} and d = 9 there are two solutions
{1,2,6} and {1,8}. A suitable message is to be displayed if the given problem
instance doesn't have a solution.
#includeint s[10],d,n,set[10],count=0;void display(int);int flag = 0;
void main(){
int subset(int,int);int i;clrscr();printf("Enter the Number of elements in the set\n");scanf("%d",&n);printf("enter the set values\n");for(i=0;id || i>=n) return;else{
set[count]=s[i];count++;subset(sum+s[i],i+1);count--;
Dept of MCA 2009 30
8/6/2019 Ada Lab 2009
31/41
Algorithms Lab Manual
subset(sum,i+1);}
}
void display(int count){
int i;printf("{ ");for(i=0;i
8/6/2019 Ada Lab 2009
32/41
Algorithms Lab Manual
12. a. Implement Horspool algorithm for String Matching,
#include#includevoid main(){
int table[126];char t[100],p[25];int n,i,k,j,m,flag=0;clrscr();printf("Enter the Text\n");gets(t);n=strlen(t);printf("Enter the Pattern\n");gets(p);
m=strlen(p);for(i=0;i
8/6/2019 Ada Lab 2009
33/41
Algorithms Lab Manual
OUTPUT
Case 1:
Enter the Text
acharya_computer_science_engineering
Enter the Patternscience
The position of the pattern is 18
Case 2:
Enter the Textacharya_computer_sceince_engineering
Enter the Pattern
cjPattern is not found in the given text
Dept of MCA 2009 33
8/6/2019 Ada Lab 2009
34/41
Algorithms Lab Manual
12. b. Find the Binomial Co-efficient using Dynamic Programming.
#include#includevoid main()
{ int i,j,k,n,c[50][50];clrscr();printf("\n enter the value of n & k\n");scanf("%d%d",&n,&k);for(i=0;i
8/6/2019 Ada Lab 2009
35/41
Algorithms Lab Manual
OUTPUT
enter the value of n & k
6 3
the table for valuation is
11 1
1 2 1
1 3 3 1
1 4 6 41 5 10 10
1 6 15 20
the binomial coefficient of C(6,3) is 20
Dept of MCA 2009 35
8/6/2019 Ada Lab 2009
36/41
Algorithms Lab Manual
13. Find Minimum Cost Spanning Tree of a given undirected graph
using Prims algorithm.
#include
#includevoid main(){
int cost[20][20],t[20][20],near1[20],a[20];int i,j,n,min,minimum,k,l,mincost,c,b;clrscr();printf("\n enter the number of nodes\n");scanf("%d",&n);printf("\n enter the adjacency matrix\n");for(i=1;i
8/6/2019 Ada Lab 2009
37/41
Algorithms Lab Manual
a[j]=cost[j][near1[j]];if(min>a[j]){
min=a[j];
c=near1[j];b=j;printf("\n");
}}
}mincost=mincost+cost[b][c];near1[b]=0;for(k=1;kcost[k][b]))
near1[k]=b;}printf("\n\ the cost of minimum spanning tree is=
%d",mincost);getch();
}
OUTPUT
enter the number of nodes
4
enter the adjacency matrix
999 1 5 21 999 999 999
5 999 999 3
2 999 3 999
the cost of minimum spanning tree is=6
Dept of MCA 2009 37
8/6/2019 Ada Lab 2009
38/41
Algorithms Lab Manual
14. Compute the transitive closure of a given directed graph
using Warshall's algorithm.
#include
#include
int a[10][10];void main(){
int i,j,k,n;clrscr();printf("\n enter the number of vertices\n");scanf("%d",&n);printf("\n enter the adjacency matrix\n");for(i=1;i
8/6/2019 Ada Lab 2009
39/41
Algorithms Lab Manual
OUTPUT
enter the number of vertices4
enter the adjacency matrix0 1 0 0
0 0 0 1
1 0 1 00 0 0 0
the tranitive closure is
0 1 0 10 0 0 1
1 1 1 1
0 0 0 0
Dept of MCA 2009 39
8/6/2019 Ada Lab 2009
40/41
Algorithms Lab Manual
15. Implement N Queen's problem using Back Tracking
#include#include#include
int x[20],count=1;void queens(int,int);int place(int,int);
void main(){
int n,k=1;clrscr();printf("\n enter the number of queens to be placed\n");scanf("%d",&n);queens(k,n);
}void queens(int k,int n){
int i,j;for(j=1;j
8/6/2019 Ada Lab 2009
41/41
Algorithms Lab Manual
OUTPUT
enter the number of queens to be placed4
1 solution1 row 2 column
2 row 4 column
3 row 1 column4 row 3 column
2 solution
1 row 3 column
2 row 1 column3 row 4 column
4 row 2 column
Top Related