Sei sulla pagina 1di 31

ALGORITHMS LABORATORY

Subject Code: 06CSL58 I.A. Marks : 25


Hours/Week : 03 Exam Hours: 03
Total Hours : 42 Exam Marks: 50

Implement the following using C/C++ language.

1. Implement Recursive Binary search and Linear search and determine the time
required to search an element. Repeat the experiment for different values of n,
the number of elements in the list to be searched and plot a graph of the time
taken versus n.

2. Sort a given set of elements using the Heapsort method and determine the time
required 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.

3. Sort a given set of elements using Merge sort method and determine the time
required 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.

4. Sort a given set of elements using Selection sort and determine 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.

5. a. Obtain the Topological ordering of vertices in a given digraph.


b. Implement All Pair Shortest paths problem using Floyd's algorithm.

6. Implement 0/1 Knapsack problem using dynamic programming.

7. From a given vertex in a weighted connected graph, find shortest paths to other
vertices using Dijkstra's algorithm.

8. Sort a given set of elements using Quick sort method and determine the time
required 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.

9. Find Minimum Cost Spanning Tree of a given undirected graph using


Kruskal's algorithm.
10. a. Print all the nodes reachable from a given starting node in a digraph using BFS
method.
b. Check whether a given graph is connected or not using DFS method.

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. For example, 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.

12. a. Implement Horspool algorithm for String Matching.


b. Find the Binomial Co-efficient using Dynamic Programming.

13. Find Minimum Cost Spanning Tree of a given undirected graph using Prim’s
algorithm.

14. a. Implement Floyd’s algorithm for the All-Pairs- Shortest-Paths problem.


b. Compute the transitive closure of a given directed graph using Warshall's
algorithm.

15. Implement N Queen's problem using Back Tracking.

Note: In the examination questions must be given based on ots.


/* Program1.PERFORM RECURSIVE BINARY SEARCH AND LINEAR SEARCH AND HENCE
FIND THE TIME REQUIRED TO SEARCH AN ELEMENT */

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

int binsearch(int a[],int key,int first,int last)


{
int mid;
if(first > last)
return -1;
else
{
mid=(first+last)/2;
if(key < a[mid])
return binsearch(a,key,first,mid-1);
else if(key > a[mid])
return binsearch(a,key,mid+1,last);
else
return mid;
}
}

int linsearch(int a[10],int i,int n,int key)


{
if(i<=n)
{
if(a[i]==key) return 1;
else return linsearch(a,i+1,n,key);
}
else
return -1;
}

void main()
{
int a[20],n,key,i,pos,choice;
clock_t start,end;
float duration;
for(;;)
{
clrscr();
printf("1.Binary search\n 2.Linear search\n 3.exit\n");
printf("\n Enter your choice :");
scanf("%d",&choice);
if(choice >= 3)
exit(0);
else
{
printf("\n Enter the number of elements : ");
scanf("%d",&n);
printf("\n Enter the elements \n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n Enter the key to be searched : ");
scanf("%d",&key);
switch(choice)
{
case 1:
start=clock();
pos=binsearch(a,key,0,n-1);
delay(100);
end=clock();
duration=(end-start)/CLK_TCK;
printf("\n Time taken is %f\n",duration);
if(pos==-1)
printf("\n key is not found\n");
else
printf("\n key is found\n");
break;
case 2:
start=clock();
pos=linsearch(a,0,n,key);
delay(100);
end=clock();
duration=(end-start)/CLK_TCK;
printf("\n Time taken is %f",duration);
if(pos==-1)
printf("\n key not found\n");
else
printf("\n Key is found\n");
break;

}
getch();
}
}
}

Output :

1.Binary search
2.Linear search
3.exit

Enter your choice :1

Enter the number of elements : 5

Enter the elements


11
22
33
44
55

Enter the key to be searched : 33

Time taken is 0.109890

key is found
1.Binary search
2.Linear search
3.exit

Enter your choice :2

Enter the number of elements : 4

Enter the elements


1
6
3
8

Enter the key to be searched : 8

Time taken is 0.109890


Key is found

1.Binary search
2.Linear search
3.exit

Enter your choice :1

Enter the number of elements : 4

Enter the elements


23
45
67
78

Enter the key to be searched : 11

Time taken is 0.109890


key is not found

1.Binary search
2.Linear search
3.exit

Enter your choice :3

/* Program2. PROGRAM TO SORT A GIVEN SET OF ELEMENTS USING THE HEAP


SORT*/
#include<stdio.h>
#include<conio.h>
void heapcon(int a[],int n)
{
int i,j,k,item;
for(i=2;i<=n;i++)
{
item=a[i];
j=i;
k=j/2;
while(k!=0 && item > a[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<n)
{
if((i+1)<n)
{ if(a[i]<a[i+1])
i++;
}
if(item<a[i])
{
a[j]=a[i];
j=i;
i=2*j;
}
else
break;
}
a[j]=item;
}
void heapsort(int a[],int n)
{
int i,temp;
heapcon(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];
clrscr();
printf("\n Enter the number of elements : ");
scanf("%d",&n);
printf("\n Enter the elements \n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
heapsort(a,n);
printf("\n The sorted array is \n");
for(i=1;i<=n;i++)
printf("%d\n",a[i]);
getch();
}

Output :

Enter the number of elements : 8


Enter the elements
13
86
43
38
54
23
8
63
The sorted array is
8
13
23
38
43
54
63
86

/* Program3. PROGRAM TO SORT A GIVEN SET OF ELEMENTS USING MERGE SORT


METHOD */

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

void merge(int a[],int low,int mid,int high)


{
int i,j,k,b[15];
i=low;
j=mid+1;
k=low;
while((i<=mid) && (j<=high))
{
if(a[i]<a[j])
{
b[k]=a[i];
k=k+1;
i=i+1;
}
else
{
b[k]=a[j];
k=k+1;
j=j+1;
}
}
while(i<=mid)
{
b[k]=a[i];
k=k+1;
i=i+1;
}
while(j<=high)
{
b[k]=a[j];
k=k+1;
j=j+1;
}
for(i=low;i<=k-1;i++)
a[i]=b[i];
}

void mergesort(int a[],int low,int high)


{
int mid;
if(low<high)
{
mid=(low+high)/2;
mergesort(a,low,mid);
mergesort(a,mid+1,high);
merge(a,low,mid,high);
}
}

void main()
{
int i,n,a[15];
clrscr();
printf("\n Enter the number of elements : ");
scanf("%d",&n);
printf("\n Enter the elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
mergesort(a,0,n-1);
printf("\n The sorted elements are\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
getch();
}

Output :

Enter the number of elements : 7


Enter the elements
35
10
15
45
25
20
40
The sorted elements are
10
15
20
25
35
40
45

/* Program3b. CHECK WHETHER A GIVEN GRAPH IS CONNECTED OR NOT


USING DFS METHOD */

#include<stdio.h>
#include<conio.h>
int visited[10];

void dfs(int n,int a[10][10],int source)


{
int i;
visited[source]=1;
for(i=1;i<=n;i++)
if(a[source][i]==1 && visited[i]==0)
dfs(n,a,i);
}

void main()
{
int i,j,source;
int n,a[10][10];
int count=0;
clrscr();
printf("\n Enter the number of nodes : ");
scanf("%d",&n);
printf("\n Enter the paths (0 if edge is not present,1 if present\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
printf("\nEnter the source vertex :");
scanf("%d",&source);
for(i=1;i<=n;i++)
visited[i]=0;
dfs(n,a,source);
for(i=1;i<=n;i++)
if(visited[i]) count=count+1;
if(count==n)
printf("\n The graph is connected \n");
else
printf("\n The graph is not connected \n");
getch();
}
Output :

Enter the number of nodes : 4

Enter the paths (0 if edge is not present,1 if present


0 1 1 0
1 0 0 0
1 0 0 1
0 0 1 0

Enter the source vertex :2

The graph is connected

Enter the number of nodes : 4

Enter the paths (0 if edge is not present,1 if present


0 1 1 0
1 0 0 0
1 0 0 0
0 0 0 0

Enter the source vertex :1

The graph is not connected

Enter the number of nodes : 3

Enter the paths (0 if edge is not present,1 if present


0 1 1
0 0 0
0 0 0

Enter the source vertex :3

The graph is not connected

/* Program4. PROGRAM TO SORT A GIVEN SET OF ELEMENTS USING SELECTION


SORT AND HENCE FIND THE TIME REQUIRED TO SORT ELEMENTS */
#include<stdio.h>
#include<conio.h>
#include<time.h>
int min(int a[],int k,int n)
{
int loc,j,min;
min=a[k];
loc=k;
for(j=k+1;j<=n-1;j++)
{
if(min > a[j])
{
min=a[j];
loc=j;
}
}
return loc;
}
void main()
{
int i,a[20],n,k,pos=0,temp;
clock_t start,end;
float duration;
clrscr();
printf("\n Enter the number of elemnts : ");
scanf("%d",&n);
printf("\n Enter the elements of the array :\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
start=clock();
for(k=0;k<n;k++)
{
pos=min(a,k,n);
temp=a[k];
a[k]=a[pos];
a[pos]=temp;
}
delay(100);
end=clock();
duration=(end-start)/CLK_TCK;
printf("\n The sorted array is \n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
printf("\n \n The time required to sort is %f\n",duration);
getch();
}

Output :

Enter the number of elemnts : 5


Enter the elements of the array :
38
47
24
42
17

The sorted array is


17
24
38
42
47

The time required to sort is 0.109890


/* program5a.OBTAIN THE TOPOLOGICAL ORDERING OF VERTICES IN A GIVEN
DIAGRAPH */
#include<stdio.h>
#include<conio.h>
int temp[10],k=0;
void topo(int n,int indegree[10],int a[10][10])
{ int i,j;
for(i=1;i<=n;i++)
{
if(indegree[i]==0)
{
indegree[i]=-1;
temp[++k]=i;
for(j=1;j<=n;j++)
{
if(a[i][j]==1 && indegree[j]!=-1)
indegree[j]--;
}
i=0;
}
}
}
void main()
{
int i,j,n,indegree[10],a[10][10];
clrscr();
printf("Enter the number of vertices :");
scanf("%d",&n);
for(i=1;i<=n;i++)
indegree[i]=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]);
if(a[i][j]==1)
indegree[j]++;
}
topo(n,indegree,a);
if(k!=n)
printf("\n Topological ordering is not posssible\n");
else
{
printf("\n The topoligical ordering is :\n");
for(i=1;i<=k;i++)
printf("%d\t",temp[i]);
}
getch();
}

Output :

Enter the number of vertices : 3


Enter the adjacency matrix
0 1 1
0 0 1
0 0 0
The topoligical ordering is :
1 2 3

/* Program5b. SORT A GIVEN SET OF ELEMENTS USING INSERTION SORT */

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

void insertsort(int a[],int n)


{
int i,j,item;
for(i=1; i<n; i++)
{
item=a[i];
for(j=i-1;j>=0 && item < a[j];j--)
{
a[j+1]=a[j];
}
a[j+1]=item;
}
}

void main()
{
int i,a[20],n;
clrscr();
printf("\n\n Enter the number of elements : ");
scanf("%d",&n);
printf("\n\n Enter the elments : \n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
insertsort(a,n);
printf("\n The sorted elements are \n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
getch();
}

Output :

Enter the number of elements : 5


Enter the elments :
38
47
34
42
89
The sorted elements are
34 38 42 47 89
/* Program6.IMPLEMENT 0/1 KNAPSACK PROBLEM USING DYNAMIC PROGRAMMING */
#include<stdio.h>
#include<conio.h>
int w[10],p[10],n;

int max(int a,int b)


{
return a>b?a:b;
}

int knap(int i,int m)


{
if (i==n) return w[i]>m?0:p[i];
if (w[i]>m) return knap(i+1,m);
return
max(knap(i+1,m),knap(i+1,m-w[i])+p[i]);
}

void main()
{
int m,i,max_profit;
clrscr();
printf ("\n Enter the number of objects : ");
scanf ("%d",&n);
printf ("\n Enter the knapsack capacity:");
scanf("%d",&m);
printf ("\n Enter profit followed by weight:\n");
for (i=1;i<=n;i++)
scanf("%d%d",&p[i],&w[i]);
max_profit=knap(1,m);
printf("\n Max profit = %d",max_profit);
getch();
}

Output :

Enter the number of objects : 3


Enter the knapsack capacity: 116
Enter profit followed by weight:
20 100
18 14
15 10
Max profit = 38

/* program7. FROM A GIVEN VERTEX IN A WEIGHTED CONNECTED GRAPH FIND


SHORTEST PATHS TO OTHER VERTICES USING DIJKSTRA'S ALGORITHM */

#include<stdio.h>
#include<conio.h>
#define INFINITY 999

void dijikstra(int cost[10][10],int n,int source,int distance[10])


{
int visited[10],min,u;
int i,j;
for(i=1;i<=n;i++)
{
distance[i]=cost[source][i];
visited[i]=0;
}
visited[source]=1;
for(i=1;i<=n;i++)
{
min=INFINITY;
for(j=1;j<=n;j++)
if(visited[j]==0 && distance[j]<min)
{
min=distance[j];
u=j;
}
visited[u]=1;
for(j=1;j<=n;j++)
if(visited[j]==0 && (distance[u]+cost[u][j])<distance[j])
{
distance[j]=distance[u]+cost[u][j];
}
}
}

void main()
{
int n,cost[10][10],distance[10];
int i,j,source,sum;
clrscr();
printf("\n Enter how many nodes :");
scanf("%d",&n);
printf("\n Cost Matrix\n Enter 999 for no edge\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&cost[i][j]);
printf("Enter the source node :");
scanf("%d",&source);
dijikstra(cost,n,source,distance);
for(i=1;i<=n;i++)
printf("\n SHORTEST DISTANCE FROM %d TO %d IS %d\n",source,i,
distance[i]);
getch();
}

Output :

Enter how many nodes : 5


Cost Matrix
Enter 999 for no edge
0 5 12 17 999
999 0 999 8 7
999 999 0 6 999
999 999 999 0 9
999 999 999 999 0

Enter the source node : 1

SHORTEST DISTANCE FROM 1 TO 1 IS 0

SHORTEST DISTANCE FROM 1 TO 2 IS 5

SHORTEST DISTANCE FROM 1 TO 3 IS 12

SHORTEST DISTANCE FROM 1 TO 4 IS 13

SHORTEST DISTANCE FROM 1 TO 5 IS 12

/* Program8. PROGRAM TO SORT A GIVEN SET OF ELEMENTS USING QUICK SORT


*/
#include<stdio.h>
#include<conio.h>
int partition(int a[],int low,int high)
{
int i,j,temp,key;
key=a[low];
i=low+1;
j=high;
while(1)
{
while(i<high && key >=a[i])
i++;
while(key<a[j])
j--;
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
else
{
temp=a[low];
a[low]=a[j];
a[j]=temp;
return j;
}
}
}

void quicksort(int a[],int low,int high)


{
int j;
if(low<high)
{
j=partition(a,low,high);
quicksort(a,low,j-1);
quicksort(a,j+1,high);
}
}

void main()
{
int i,n,a[20];
clrscr();
printf("\n Enter the number of elements : ");
scanf("%d",&n);
printf("\n Enter the elements \n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
quicksort(a,0,n-1);
printf("\n The sorted elements are \n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
getch();
}
Output :

Enter the number of elements : 6


Enter the elements
45
36
15
92
35
71
The sorted elements are
15
35
36
45
71
92

/* program9. FIND MINIMUM COST SPANNING TREE OF A GIVEN UNDIRECTED


GRAPH
USING KRUSKAL'S ALGORITHM */

#include<stdio.h>
#include<conio.h>
#define INFINITY 999
#define MAX 100
int parent[MAX],cost[MAX][MAX],t[MAX][2];

int find(int v)
{
while(parent[v])
{
v=parent[v];
}
return v;
}

void union1(int i,int j)


{
parent[j]=i;
}

void kruskal(int n)
{
int i,j,k,u,v,mincost,res1,res2,sum=0;
for(k=1;k<n;k++)
{
mincost=INFINITY;
for(i=1;i<n-1;i++)
{
for(j=1;j<=n;j++)
{
if(i==j) continue;
if(cost[i][j] < mincost)
{
u=find(i);
v=find(j);
if(u!=v)
{
res1=i;
res2=j;
mincost=cost[i][j];
}
}
}
}
union1(res1,find(res2));
t[k][1]=res1;
t[k][2]=res2;
sum=sum+mincost;
}
printf("\n cost of spanning tree is %d\n",sum);
printf("\n Edges of spanning tree are \n");
for(i=1;i<n;i++)
printf(" %d->%d\n",t[i][1],t[i][2]);
}
void main()
{
int i,j,n;
clrscr();
printf("\n Enter the number of vertices : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
parent[i]=0;
printf("\n Enter the cost adjaceny matrix 0- for self edge and 999-if
no edge \n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&cost[i][j]);
kruskal(n);
getch();
}

Output :

Enter the number of vertices : 4

Enter the cost adjaceny matrix 0- for self edge and 999-if no edge
0 20 2 999
20 0 15 5
2 15 0 25
999 5 25 999

cost of spanning tree is 22

Edges of spanning tree are


1->3
2->4
2->3

Enter the number of vertices : 7

Enter the cost adjaceny matrix 0 - for self egde and 999 – if no edge
0 28 999 999 999 10 999
28 0 16 999 999 999 14
999 16 999 12 999 999 999
999 999 12 0 22 999 18
999 999 999 22 0 25 24
10 999 999 999 25 0 999
999 14 999 18 24 999 0

cost of spanning tree is 99


Edges of spanning tree are
1->6
3->4
2->7
2->3
4->5
5->6
/* Program10a. PRINT ALL THE NODES REACHABLE FROM A GIVEN STARTING
NODE IN A DIAGRAPH USING BREADTH FIRST SEARCH METHOD */

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

int visited[10];

void bfs(int n,int a[10][10],int source)


{
int i,q[10],u;
int front=1,rear=1;
visited[source]=1;
q[rear]=source;
while(front<=rear)
{

u=q[front];
front=front+1;
for(i=1;i<=n;i++)
if(a[u][i]==1 && visited[i]==0)
{
rear=rear+1;
q[rear]=i;
visited[i]=1;
}
}
}

void main()
{
int n,a[10][10],i,j,source;
clrscr();
printf("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",&a[i][j]);
printf("\n Enter the source :");
scanf("%d",&source);
for(i=1;i<=n;i++)
visited[i]=0;
bfs(n,a,source);
for(i=1;i<=n;i++)
{
if(visited[i]==0)
printf("\n The node %d is not reacable",i);
else
printf("\n The node %d is reachable",i);
}
getch();
}

Output :
Enter the number of nodes : 4

Enter the adjacency matrix


0 0 1 1
1 0 0 0
0 0 0 1
0 1 0 0

Enter the source :1

The node 1 is reachable


The node 2 is reachable
The node 3 is reachable
The node 4 is reachable

Enter the number of nodes : 4

Enter the adjacency matrix


0 1 1 0
0 0 0 0
0 0 0 0
0 1 0 0

Enter the source :1

The node 1 is reachable


The node 2 is reachable
The node 3 is reachable
The node 4 is not reacable
/* Program10b.IMPLEMENT ALL PAIR SHORTEST PATHS PROBLEM USING FLOYD'S
ALGORITHM */
#include<stdio.h>
#include<conio.h>
#define INFINITY 999
int min(int a,int b)
{
return a<b?a:b;
}
void floyd(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]=min(p[i][j], p[i][k]+p[k][j]);
}

void main()
{
int a[10][10],n,i,j;
clrscr();
printf("\n Enter the number of vertices: ");
scanf("%d",&n);
printf("\n Enter the cost matrix 0 -self loop & 999 –for no
edge\n");
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
scanf("%d",&a[i][j]);
floyd(a,n);
printf("\n Shortest path matrix\n");
for (i=1;i<=n;i++)
{
for (j=1;j<=n;j++)
printf("%d\t",a[i][j]);
printf("\n");
}
getch();
}

output :
Enter the number of vertices: 4

Enter the cost matrix 0 - self loop and 999 – for no edge
0 999 3 999
2 0 999 999
999 7 0 1
6 999 999 0

Shortest path matrix


0 10 3 4
2 0 5 6
7 7 0 1
6 16 9 0
/* program11. FIND A SUBSET OF A GIVEN SET S={s1,S2,…Sn} OF N POSITIVE
INTEGERS WHOSE SUM IS EQUAL TO A GIVEN POSITIVE INTEGER d.FOR EXAMPLE
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. */
#include<stdio.h>
#include<conio.h>
#define MAX 10
int s[MAX],x[MAX];
int d;
void sumofsub(int p, int k, int r)
{
int i;
x[k]=1;
if ((p + s[k]) == d)
{
for(i=1; i<=k; i++)
if (x[i] == 1)
printf("%d ",s[i]);
printf("\n");
}
else
if (p+ s[k] + s[k+1] <= d)
sumofsub(p + s[k],k+1, r-s[k]);
if ((p + r - s[k] >= d) && (p + s[k+1] <= d))
{
x[k]=0;
sumofsub(p,k+1,r-s[k]);
}
}
void main()
{
int i,n,sum=0;
clrscr();
printf("\n Enter max. number : ");
scanf("%d",&n);
printf("\n Enter the set in increasing order :\n");
for(i=1;i<=n;i++)
scanf("%d",&s[i]);
printf("\n Enter the max. subset value : ");
scanf("%d",&d);
for(i=1;i<=n;i++)
sum=sum+s[i];
if (sum < d || s[1] > d)
printf("\n No subset possible");
else
sumofsub(0,1,sum);
getch();
}

Output :

Enter max. number : 5


Enter the set in increasing order :
1 2 5 6 8
Enter the max. subset value : 9
1 2 6
1 8

/* program12a. IMPLEMENT HORSPOOL ALGORITHM FOR STRING MATCHING */


#include<stdio.h>
#include<conio.h>
#include<string.h>
#define MAX 256
int t[MAX];
void shifttable(char pat[])
{
int i,j,m;
m=strlen(pat);
for(i=0;i<MAX;i++)
t[i]=m;
for(j=0;j<m-1;j++)
t[pat[j]]=m-1-j;
}
int horspool(char src[],char pat[])
{
int i,j,k,m,n;
n=strlen(src);
m=strlen(pat);
i=m-1;
while(i<n)
{ k=0;
while((k<m) && (pat[m-1-k]==src[i-k]))
k++;
if(k==m)
return (i-m+1);
else
i=i+t[src[i]];
}
return -1;
}
void main()
{
char src[100],pat[10];
int pos;
clrscr();
printf("\n Enter the main source string\n");
gets(src);
printf("\n Enter the pattern to be searched\n");
gets(pat);
shifttable(pat);
pos=horspool(src,pat);
if(pos>=0)
printf("\n Found at %d position ",pos+1);
else
printf("\n String match failed");
getch();
}
Output :

Enter the main source string


This is ada lab program
Enter the pattern to be searched
lab
Found at 13 position

/* Program12b.FIND THE BINOMIAL COEFFICIENT USING DYNAMIC PROGRAMMING


*/

#include<stdio.h>
#include<conio.h>
int binomial(int n,int k)
{
int i,j,a[10][10];
for(i=0;i<=n;i++)
{
a[i][0]=1;
a[i][i]=1;
}
for(i=2;i<=n;i++)
{
for(j=1;j<=i-1;j++)
{
a[i][j] = a[i-1][j] + a[i-1][j-1];
}
}
return a[n][k];
}

void main()
{
int n,k,res;
clrscr();
printf("\n Enter the value for n : ");
scanf("%d",&n);
printf("\n Enter the value for k : ");
scanf("%d",&k);
res=binomial(n,k);
printf("\n The binomial co-efficient is %d\n",res);
getch();
}

output :

Enter the value for n : 4


Enter the value for k : 2
The binomial co-efficient is 6
/* program13. FIND MINIMUM COST SPANNING TREE OF A GIVEN UNDIRECTED
GRAPH USING PRIMS ALGORITHM */
#include<stdio.h>
#include<conio.h>
#define INFINITY 999

int prim (int cost[10][10],int source,int n)


{
int i,j,sum=0,visited[10],cmp[10],vertex[10];
int min,u,v;
for (i=1;i<=n;i++)
{
vertex[i]=source;
visited[i]=0;
cmp[i]=cost[source][i];
}
visited[source]=1;
for (i=1;i<=n-1;i++)
{
min=INFINITY;
for (j=1;j<=n;j++)
if (!visited[j] && cmp[j]<min)
{
min=cmp[j];
u=j;
}
visited[u]=1;
sum=sum+cmp[u];
printf("\n%d - > %d sum = %d",vertex[u],u,sum);
for (v=1;v<=n;v++)
if (!visited[v] && cost[u][v]<cmp[v])
{
cmp[v]=cost[u][v];
vertex[v]=u;
}
}
return sum;
}

void main()
{
int a[10][10],n,i,j,m,source;
clrscr();
printf("\nEnter the number of vertices :");
scanf("%d",&n);
printf("\nEnter the cost matrix: 0 –self loop & 999 - no
edge\n");
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
scanf("%d",&a[i][j]);
printf("\n Enter the source :");
scanf("%d",&source);
m=prim(a,source,n);
printf("\n\n Cost = %d",m);
getch();
}
Output :

Enter the number of vertices :4


Enter the cost matrix: 0 –self loop & 999 - no edge
0 20 10 50
20 0 60 999
10 60 0 40
50 999 40 0

Enter the source :1

1 - > 3 sum = 10
1 - > 2 sum = 30
3 - > 4 sum = 70

Cost = 70

Enter the number of vertices : 5


Enter the cost matrix: 0 –self loop & 999 - no edge
0 11 9 7 8
11 0 15 14 13
9 15 0 12 14
7 14 12 0 6
8 13 14 6 0

Enter the source : 1

1 - > 4 sum=7
4 - > 5 sum=13
1 - > 3 sum=22
1 - > 2 sum=33

Cost=33

/* Program14a.PRINT ALL THE NODES REACHABLE FROM A GIVEN STARTING NODE


IN A GIVEN DIAGRAPH USING DFS METHOD */
#include<stdio.h>
#include<conio.h>
int visited[10];

void dfs(int n,int a[10][10],int source)


{
int i;
visited[source]=1;
for(i=1;i<=n;i++)
if(a[source][i]==1 && visited[i]==0)
dfs(n,a,i);
}

void main()
{
int i,j,source;
int n,a[10][10];
clrscr();
printf("\n Enter the number of nodes : ");
scanf("%d",&n);
printf("\n Enter the paths (0 - no edge and 1 - if present \n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
printf("\n Enter the source vertex :");
scanf("%d",&source);
for(i=1;i<=n;i++)
visited[i]=0;
dfs(n,a,source);
for(i=1;i<=n;i++)
{
if(visited[i]==0)
printf("\n The node %d is not reacable",i);
else
printf("\n The node %d is reachable",i);
}
getch();
}

output :

Enter the number of nodes : 4


Enter the paths (0 if edge is not present,1 if present
0 1 0 0
0 0 0 1
1 0 0 1
0 0 0 0
Enter the source vertex : 1

The node 1 is reachable


The node 2 is reachable
The node 3 is not reachable
The node 4 is reachable

Enter the number of nodes : 4


Enter the paths (0 - no edge and 1 - if present
0 1 0 0
0 0 0 1
1 0 0 1
0 0 0 0

Enter the source vertex :2

The node 1 is not reacable


The node 2 is reachable
The node 3 is not reacable
The node 4 is reachable
/* Program14b. COMPUTE THE TRANSITIVE CLOSURE OF A GIVEN DIRECTED GRAPH
USING WARSHALL'S ALGORITHM */

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

void warsh(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]=p[i][j] || (p[i][k] && p[k][j]);
}

void main()
{
int a[10][10],i,j,n;
clrscr();
printf("\n Enter the number of vertices : ");
scanf("%d",&n);
printf("\n Enter the adjacency matrix \n");
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
scanf("%d",&a[i][j]);
warsh(a,n);
printf("\n The resultant Path matrix is \n");
for (i=1;i<=n;i++)
{
for (j=1;j<=n;j++)
printf("%d ",a[i][j]);
printf("\n");
}
getch();
}

output :

Enter the number of vertices : 4


Enter the adjacency matrix
0 1 0 0
0 0 1 0
1 0 0 1
0 0 0 0

The resultant Path matrix is


1 1 1 1
1 1 1 1
1 1 1 1
0 0 0 0
/* Program15. IMPLEMENT N QUENN'S PROBLEM USING BACK TRACKING */

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define MAX 50

int can_place (int c[], int r)


{
int i;

/* Two queens attack if: they are in the same row/column, or


* if they are diagonally opposite to each other. */
for (i=0; i<r; i++)
if (c[i] == c[r] ||
(abs (c[i] - c[r]) == abs (i - r)))
return 0;

return 1;
}

void display (int c[], int n)


{
int i, j;
char cb[10][10];
for (i=0; i<n; i++)
for (j=0; j<n; j++)
cb[i][j]= ‘-‘;
for(i=0; i<n; i++) /* We are in the correct column. */
cb[i][c[i]]= ‘Q’;
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
printf(“%c “,cb[i][j]);
printf(“\n”);
}
}

void n_queens (int n)


{
int r; /* Current row. */
int c[MAX]; /* Stores the queen's column position for each row. */

c[0] = -1; /* Initially no column is found. */


r = 0; /* Start from row 1. */

/* If row < 0, no solution possible. */


while (r >= 0)
{
c[r]++; /* Start from the col. before which we stopped. */

/* Move ahead until we get a non-attacking column. */


while (c[r] < n && !can_place (c, r))
c[r]++;
/* Did we get a column at all? */
if (c[r] < n)
{
/* Was this the column for the last row? Then we're done. */
if (r == n - 1)
{
display (c, n);
printf(“\n\n”);
}
/* No? Good, now find the column for the next row. */
else
{
r++;
c[r] = -1;
}
}
/* No such column found. Backtrack and find another column. */
else
r--;
}
}

void main()
{
int n; /* Number of queens. */
clrscr();
printf ("\n Enter the number of queens :");
scanf("%d",&n);
n_queens(n);
getch();
}

output :

Enter the number of queens : 4

- Q - -
- - - Q
Q - - -
- - Q -

- - Q -
Q - - -
- - - Q
- Q - -

Potrebbero piacerti anche