record print 2
TRANSCRIPT
7/27/2019 Record Print 2
http://slidepdf.com/reader/full/record-print-2 1/12
10. IMPLEMENTATION OF TOPLOLOGICAL SORT ON A
DIRECTED GRAPH TO DECIDE IF IT IS CYCLIC
AIM
Write a C program to implement topological sort algorithm in a graph to decide if
it is cyclic.
ALGORITHM
1. Let L be the empty list that will contain the sorted elements2. Let S be the set of all nodes with no incoming edges
3. while S is non-empty do
a. remove a node n from S
b. insert n into L
4. for each node m with an edge e from n to m do
a. remove edge e from the graph
b. if m has no other incoming edges then
i. insert m into S
5. if graph has edges then
i. return error (graph has at least one cycle)
b. elsei. return L (a topologically sorted order)
SOURCE CODE
#include<stdio.h>
#include<conio.h>
void main(){
int i,j,k,a[10][10],indeg[10],flag[10],count=0,check=0,n;
clrscr();
printf("enter the maximum no. of elements:");scanf("%d",&n);printf("Enter the adjacency matrix:\n");for(i=0;i<n;i++)
for(j=0;j<n;j++){
printf("v%d->v%d:",i+1,j+1);scanf("%d",&a[i][j]);
}for(i=0;i<n;i++)
7/27/2019 Record Print 2
http://slidepdf.com/reader/full/record-print-2 2/12
{
indeg[i]=0;flag[i]=0;
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
indeg[i]+=a[j][i];
printf("Topological sort is:\n");while(count<n)
{for(j=0;j<n;j++)
{if(indeg[j]==0&&flag[j]==0)
{ printf("%d->",j+1);
flag[j]=1;for(i=0;i<n;i++)
{
if(a[j][i]==1)indeg[i]--;
}check++;
}} count++;
}
if(check==n)
printf("\ngraph is acyclic");else
printf("\nNo topological sort\nGraph is cyclic");getch();
}
OUTPUT
enter the maximum no. of elements:4
Enter the adjacency matrix:
v1->v1:0
v1->v2:1
v1->v3:0v1->v4:1
v2->v1:0
v2->v2:0
v2->v3:1
v2->v4:1
v3->v1:0
v3->v2:0
7/27/2019 Record Print 2
http://slidepdf.com/reader/full/record-print-2 3/12
v3->v3:0
v3->v4:0
v4->v1:0v4->v2:0
v4->v3:1
v4->v4:0
Topological sort is:
1->2->4->3->
graph is acyclic
enter the maximum no. of elements:4
Enter the adjacency matrix:
v1->v1:0
v1->v2:1v1->v3:0
v1->v4:1
v2->v1:0
v2->v2:0
v2->v3:1
v2->v4:0
v3->v1:0
v3->v2:0
v3->v3:0
v3->v4:1
v4->v1:0v4->v2:1
v4->v3:0
v4->v4:0
Topological sort is:
1->
No topological sort
Graph is cyclic
RESULT
Thus the C program to implement topological sort algorithm in a graph to decide if itis cyclic is written, executed and verified.
7/27/2019 Record Print 2
http://slidepdf.com/reader/full/record-print-2 4/12
14. IMPLEMENTATION OF BRANCH AND BOUND ALGORITHM
FOR TRAVELING SALESMAN PROBLEM
AIM
Write a C program to implement branch and bound algorithm for travelling
sales man problem.
ALGORITHM
1. Get the number of cities
2. Get the cost of the edges
3. Let 0 be the starting city
4. Compute all possible paths from starting city and returning back to starting city
5. Find the shortest path(path with minimum cost) among the possible paths and
display
SOURCE CODE
#include<stdio.h>
int x[15],used[15];int adj[15][15]={0};
int path [15][15],wght[15];int c,min;
int path_ok(int k,int n)
{if(used[x[k]])
return 0;if(k<n-1)
return(adj[x[k-1]][x[k]]);else
return(adj[x[k-1]][x[k]])&& (adj[x[k]][x[0]]);}
void tsp(int k,int n){
int i,sum;
for(x[k]=1;x[k]<n;x[k]++){
if(path_ok(k,n)){
used[x[k]]=1;
7/27/2019 Record Print 2
http://slidepdf.com/reader/full/record-print-2 5/12
if(k==n-1){
sum=0;printf("\n\n\t possible path %d: ",c+1);
for(i=0;i<n;i++){
printf("%d\t",x[i]);path[c][i]=x[i];sum+=adj[x[i]][x[i+1]];
}
printf(":%d",sum);wght[c]=sum;
if(c==0||sum<min)min=sum;
c++;used[x[k]]=0;
getch();}
else
tsp(k+1,n);used[x[k]]=0;}
}}
void findmin(int n)
{int i,j;
for(i=0;i<c;i++)if(wght[i]==min)
{
printf("\n\n\t minimum path :");for(j=0;j<n;j++)
printf("%d\t",path[i][j]);}
}
void main()
{int i,n,j;
int edg;clrscr();
printf("\n\n\t\t travelling salesman problem\n\n");printf("\n\t enter the no. of cities:");
scanf("%d",&n);printf("\n\n enter the cost if path exist between cities:{c1,c2}.else enter 0\n\n");
printf("\n\t cities\t\tcost\n\n");for(i=0;i<n;i++)
for(j=i+1;j<n;j++){
printf("\n\t %d------ %d \t:,i,j);scanf("%d",&edg);
if(edg)
7/27/2019 Record Print 2
http://slidepdf.com/reader/full/record-print-2 6/12
adj[i][j]=adj[j][i]=edg;}
used[0]=1;tsp(1,n);
if(!c)printf("\n\n\t\t no path found to cover all the cities\n\n);
else{printf("\n\n\t\t minimum cost is %d and the paths are \n\n,min);
findmin(n);
}getch();
}
OUTPUT
travelling salesman problem
enter the no. of cities:4
enter the cost if path exist between cities:{c1,c2}.else enter 0
cities cost
0------ 1 2
0------ 2 4
0------ 3 2
1------ 2 3
1------ 3 5
2------ 3 1
possible path 1: 0 1 2 3 :8
possible path 2: 0 1 3 2 :12
possible path 3: 0 2 1 3 :14
possible path 4: 0 2 3 1 :12
possible path 5: 0 3 1 2 :14
possible path 6: 0 3 2 1 :8
7/27/2019 Record Print 2
http://slidepdf.com/reader/full/record-print-2 7/12
minimum cost is 8 and the paths are
minimum path :0 1 2 3
minimum path :0 3 2 1
RESULT
Thus the C program to implement branch and bound algorithm for travelling sales manproblem is written, executed and verified.
7/27/2019 Record Print 2
http://slidepdf.com/reader/full/record-print-2 8/12
15. IMPLEMENTATION OF RANDOMIZED ALOGORITHM-
RANDOM NUMBER GENERATOR
AIM
Write a C program to implement the randomized algorithm for random number
generator using c.
ALGORITHM
1. Initialize the random number generator using srand() with current time as seed.
2. Use the random function to get random numbers.
3. Display the random numbers.
SOURCE CODE
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(void)
{
int i;
time_t t;
srand((unsigned) time(&t));
printf(“ten random numbers from 0 to 99\n\n”);
for(i=0; i<10;i++)printf(“%d\n”,rand()%100);
getch();
}
7/27/2019 Record Print 2
http://slidepdf.com/reader/full/record-print-2 9/12
OUTPUT
ten random numbers from 0 to 99
94
90
24
7
71
81
28
5
14
33
RESULT
Thus the C program to implement the randomized algorithm for random number
generator is written, executed and verified.
7/27/2019 Record Print 2
http://slidepdf.com/reader/full/record-print-2 10/12
16. IMPLEMENTATION OF BINARY SEARCH ALGORITHM
AIM
Write a C program to implement binary search algorithm.
ALGORITHM
1. Get the element(search key) to be searched
2. Compare the search key with the element at the middle
2.1 If the value matches, return the value
2.2 If the search key < the middle element, search the elements between
the first element to the element before the middle element (MIDDLE
-1)
2.3 If the search key > the middle element, search the elements between
the MIDDLE+ 1 elements to the last element
3 Search is repeated until the searched key is found or the last element in the
subset is traversed
SOURCE CODE
#include<stdio.h>#include<conio.h>
void main()
{int n,i,search,f=0,low,high,mid,a[20];
clrscr();printf("Enter the n value:");
scanf("%d",&n);for(i=1;i<=n;i++)
{printf("Enter the number in ascending order a[%d]=",i);
scanf("%d",&a[i]);}
printf("Enter the search element:");scanf("%d",&search);
low=1;high=n;
while(low<=high){
7/27/2019 Record Print 2
http://slidepdf.com/reader/full/record-print-2 11/12
if(search<a[mid]){high=mid-1;
}else if(search>a[mid])
low=mid+1;
}else
{
f=1;printf("obtainedin the position %d:",mid);getch();
exit();}
}if(f==0)
{printf("not present");
}getch();
}
OUTPUT
Enter the n value:5
Enter the number in ascending order a[1]=3Enter the number in ascending order a[2]=7
Enter the number in ascending order a[3]=11
Enter the number in ascending order a[4]=16Enter the number in ascending order a[5]=20
Enter the search element:11obtainedin the position 3:
Enter the n value:5Enter the number in ascending order a[1]=2
Enter the number in ascending order a[2]=8Enter the number in ascending order a[3]=12
Enter the number in ascending order a[4]=17Enter the number in ascending order a[5]=19
Enter the search element:7not present