100% found this document useful (1 vote)
504 views

Analysis & Design of Algorithms Lab (BCSL404)

VTU Lab Manual

Uploaded by

Vishal More
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
100% found this document useful (1 vote)
504 views

Analysis & Design of Algorithms Lab (BCSL404)

VTU Lab Manual

Uploaded by

Vishal More
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 30
S. V.E. Soclety’s nore institute of 7, Noy, °%, ~¢ Affiliated to Visvesvaraya Technological University Belagavi % = Approved by Govt. of Karnataka & AICTE, New Delhi & BACHELOR OF ENGINEERING IN COMPUTER SCIENCE AND ENGINEERING As Per Choice Based Credit System (CBCS) | (Effective from the academic year-2022) PROFESSIONAL CORE COURSE LABORATORY (PCCL) ANALYSIS & DESIGN OF ALGORITHMS LAB (BCSL404) Estd : 1982 BHEEMANNA KHANDRE INSTITUTE OF TECHNOLOGY COMPUTER SCIENCE AND ENGINEERING Bhalki -585328 INDEX SI. NO EXPERIMENTS P.NO Design and implement C/C++ Program to find Minimum Cost Spanning Tree of a given connected undirected graph using Kruskal's algorithm, o1 Desiga and implement C/C-+ Program to find Minimum Cost Spanning Tree of a given connected undirected graph using Prim's algorithm, 04 ‘A) Design and unplement C/C++ Program to solve All-Pairs Shortest Paths problem using Floyd's algorithm. B) Design and implement C/C++ Program to find the transitive closure using ‘Warshal's algorithm. 07 09 Design and implement C/C+¥ Program to find shortest paths from a given vertex in a weighted connected graph to other vertices using Dijkstra's algorithm, W Design and implement C/C++ Program to obtain the Topological ordering of vertices in a given digraph Design and implement C/C+= Program to solve 0/1 Knapsack problem using Dynamic Programming method. Design and implement C/C++ Program to solve discrete Kuapsack and continuous Knapsack problems using greedy approximation method. Design and implement C/C++ Program to find a subset of a given set S= 51, $2.08} Of n positive integers whose sum is equal to a given positive integer d, 19 Design and implement C/C++ Program fo sont a given set of n integer elements using Selection Sort method and compute its time complexity. Run the program for varied values of n> $000 and record the time taken to sort Plot a graph of the time taken versus n, The elements can be read from a file or can be generated using the random number generator. au 10 Design and umplement C/C++ Program to sort a given set of n integer elements using Quick Sort method and compute its time complexity. Run the program for varied values of n> 5000 and record the time taken to sort. Plot a graph of the time taken versus n. The elements can be read from a file or can be generated using the random number generator. lL Design and implement C/C++ Program to sort a given set of n integer elements using Merge Sort method and compute its time complexity. Run the program for varied values of n> 5000, and record the time taken to sort. Plot a graph of the time taken versus n. The elements can be read from a file or can be generated using the random number generator 25 Design and implement C/C+* Program for N Queen's problem using Backtwacking. Prof. V. R. More Analysis & Design of Algorithms Lab (BCSL404) Program 1: Design and implement C/C++ Program to find Minimum Cost Spanning Tree of a given connected undirected graph using Kruskal’s algorithm. #include itdefine INF 999 itdefine MAX 100 int p[MAX], c[MAX] [MAX], t[MAX] [2]; int find(int v) while(p[v]) v=p[v]5 return v; } void union1(int i,int j) { pli]=is void kruskal(int n) { int i, j, k, u, v, min, rest, res2, sum=0; for(k=1;k %d\n",t[i][1],t[i][2])5 main() int i,j,n; clrser()5 printf("\nEnter the n value:"); scan#("%d" ,&n) ; p[i]=9; printf("\nEnter the graph data:\n"); for(is1;i<=nji++) for (j=13 j idefine INF 999 int prim(int c[10][10], int n, int s) int v[10], i, j, sum=®, ver[10], d[10], min, u; for(isl; i #include #define INF 99 int min(int a,int b) £ return(acb) ?a:b; void floyd(int p[][10],int n) j ) pli}(j]=-min(pli](3], plik] +plk][5])5 } void main() { int a[10][10],myi,j5 clrser(); printf("\nEnter théln value:"); scanf("%d" ,&n)3 printf("\nEnter the graph data: \n"); scanf("%d" ali] [5])3 floyd(a,n); printf("\nShortest path matrix\n"); for(i=1;ic=n3i++) { for (j= printf("%d ",afi][3])5 printf("\n"); } getch(); } Dept. of CSE, BKIT Bhalki Page 7 Prof. V. R. More Analysis & Design of Algorithms Lab (BCSL404) Input Graph 8NoO8 orgs 8o8w 0 2 Adjacency matrix for the given graph | 6 Enter the n value:4 Enter the graph da 099 3 99 2099 99 99701 99 99 0 shortest path matrix 6 10 Dept. of CSE, BIT Bhalki Page 8 Prof. V. R. More Analysis & Design of Algorithms Lab (BCSL404) B) Design and implement C/C++ Program to find the transitive closure using Warshal's algorithm. #include void warsh(int p[][10],int n) { int i,j,k; void main() int a[10][10],n,i,45 clrser(); printf("\nEnter the n value:"); scanf("%d" ,&n) 5 printf("\nEnter the graph data: \n"); for(i=1 +4) 3 J<=n3 j++) scanf("%d" ,&ali][5])s warsh(a,n)5 printf("\nResultant path matrix\n"); for(isl;i¢snji++) for (j=1; J itdefine INF 999 void dijkstra(int c[10][10],int n,int s,int d[10]) int v[10],min,u,i,j3 for(i=1;i<=n;i++) d[il=c[s] [ils v[i]=0; n; j++) && d[j] #tinclude int temp[10],k=0; void sort(int a[][10],int id[],int n) if(id[i]== { id[i temp[++k]=4; for (j=15 j<=n; j++) if(a[i][j]==1 @& id[j]!=-1) id[5]--5 i=0; } + void main() int a[10]{1],id[10])n,i,3; clrser(); printf("\nEnter)the fn value:"); scanf("%d" ,&n) 5 sort(a,id,n)5 if(k!sn) printf("\nTopological ordering not possible"); else { Dept. of CSE, BKIT Bhalki Page 13 Prof. V. R. More Analysis & Design of Algorithms Lab (BCSL404) printf("\nTopological ordering is:"); for(i=1;i<=k;it+) print#("%d ",temp[i]); + getch(); Input Graph 00110 () ©) 10010 DN () Adjacency matrix! 0 0 0 0 1 G) () 00101 00000 Output Enter the n value Enter the graph data: 00110 100190 00001 00101 CMCC CMEC) oe ering is Dept. of CSE, BIT Bhalki Page 14 Prof. V. R. More Analysis & Design of Algorithms Lab (BCSL404) Program 6 Design and implement C/C++ Program to solve 0/1 Knapsack problem using Dynamic Programming method. #include #tdefine MAX 50 int p[MAX], w[MAX], x[MAX]; double maxprofit; int n, m, i, j, temp2, currentWeight; void Knapsack(int n, int w[], int p[], int m) { double ratio[MAX]; // Calculate the ratio of profit to weight for each item for (i = 0; i < nj i++) { ratio[i] = (double)p[i] / wli]s } // Sort items based on the ratio in non>increasing order for (i = @; i int w[10],p[10],n; int max(int a,int b) { } int knap(int i,int m) return a>b?a:b; if(is=n) return wli]>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; clrser() printf("\nEnter the no. of objects: scanf("%d" ,&n); printf("\nEnter the knapsack capacity: scanf("%d" ,&m) ; printf("\nEnter profit followed’ by weight:\n"); for(i=1;i<=n;itt) scanf("%d %d",&p[i],8w[i]); max_profit=knap(1,m)} printf("\nMax profit=%d",max_profit); getch(); 3 5 } Output Enter the no. of ob je Enter the knapsack capaci ORC SG emer ste Max prof it=?! Dept. of CSE, BIT Bhalki Page 18 Prof. V. R. More Analysis & Design of Algorithms Lab (BCSL404) Program 8 Design and implement C/C++ Program to find a subset of a given set S = {5 S3,....$n} of n positive integers whose sum is equal to a given positive integer d. #include #include itdefine MAX 10 int s[MAX], x[MAX],d; void sumofsub(int p,int k,int r) int 4; x[k]=1; perl’) k3it+) ) printf("%d ",s[i])5 printf("\n"); } else if(pts[k]+s[k+1]<=d) sumofsub(p+s[k], k+1,r-s[k]) 5 if((ptr-s[k]>=d)/@& (p+s[k+1]<=d)) x[k]=0; sumofsub(p,k+15r-s[k}); } void main() { clrser(); printf("\nEnter the n value:"); scanf("%d" ,&n); printf("\nEnter the set in increasing order:"); for(i=1;i<=n;i++) scanf("%d" ,&s[i])5 printf("\nEnter the max subset value:"); scanf("%d" ,&d); for(i =n;it+) sum=sum+s[ i]; if(sumed || s[1]>4) printf("\nNo subset possible"); Dept. of CSE, BKIT Bhalki Page 19 Prof. V. R. More Analysis & Design of Algorithms Lab (BCSL404) else sumofsub(@,1, sum); getch(); Output PA na ew CUCL) Enter the se reas ing rder $ 13 15 18 a eae tka Ted 5 10 15 5 12 13 Dept. of CSE, BIT Bhalki Page 20 Prof. V. R. More Analysis & Design of Algorithms Lab (BCSL404) Program 9 Design and implement C/C++ Program to sort a given set of n integer elements using Selection Sort method and compute its time complexity. Run the program for varied values of n> 5000 and record the time taken to sort. Plot a graph of the time taken versus n. The elements can be read from a file or can be generated using the random number generator. #include #include #include #include void selsort(int a[],int n) { int i,j,small,pos,temp; For (j=; j 5000 and record the time taken to sort. Plot a graph of the time taken versus n. The elements can be read from a file or can be generated using the random number generator. Quicksort # include # include # include void Exch(int *p, int *q) int temp = *p; *p = *q; *q = temp; void QuickSort(int a[], int low, int high) int i, j, key, k; if (low>=high) return; key=low; i=low+1; j-high; while(i<=3) while ( a[i] <= a[key] ) isi+1; while ( a[j] > a[key] ) $ if(ixj) Exch(&ali], 8a[5])5 } Exch(&a[j], &a[key]); QuickSort(a, low, j-1); QuickSort(a, j+1, high); void main() int n, a[500],k; clock_t st,et; double ts; clrser()5 printf("\n Enter How many Numbers: "); scanf("%d", &n)5 printf("\nThe Random Numbers are:\n"); for(ke1; ké=n; k++) a[k]=rand(); printf("%d\t",a[k])5 Dept. of CSE, BKIT Bhalki Page 23 Prof. V. R. More Analysis & Design of Algorithms Lab (BCSL404) st=clock() 5 QuickSort(a, 1, n); et=clock(); ts=(double) (et-st) /CLOCKS_PER_SEC; printf("\nSorted Numbers are: \n for(ke1; ké=n; k++) printf("%d\t", a[k])5 printf("\nThe time taken is %e",ts); getch(); } Output Pen tc etn) See Ct cena CL (Mt Cor) Ser ce nc Ce CC ME) BSC ccc) Dept. of CSE, BIT Bhalki Page 24 Prof. V. R. More Analysis & Design of Algorithms Lab (BCSL404) Program 11 Design and implement C/C++ Program to sort a given set of n integer elements using Merge Sort method and compute its time complexity. Run the program for varied values of n> 5000, and record the time taken to sort. Plot a graph of the time taken versus n. The elements can be read from a file or can be generated using the random number generator. Mergesort # include # include #include void Merge(int a[], int low, int mid, int high) int i, j, k, b[500]; islow; j=mid+1; k=low; while ( ic=mid && j= high) return; mid = (low+high)/2 ; MergeSort(a, low, mid); MergeSort(a, mid+1, high); Merge(a, low, mid, high); } void main() { int n, a[500],k; clock_t st,et; double ts; clrser(); printf("\n Enter How many Numbers scanf("%d", &n); Dept. of CSE, BKIT Bhalki Page 25 Prof. V. R. More Analysis & Design of Algorithms Lab (BCSL404) printf("\nThe Random Numbers are:\n"); for(k=1; k<=n; k++) a[k]=rand(); printf("%d\t", a[k])5 st=clock() ; MergeSort(a, 1, n); lock() 5 double) (et-st) /CLOCKS_PER_SEC; printf("\n Sorted Numbers are : \n "); for(k=1; k<=n; k++) print#("%d\t", a[k]); printf("\nThe time taken is %e",ts); getch(); } Output Enter How many Numbers:8 em ee 346 FEO) 10982 1690 11656 ahs Sorted Numbers are sic} aie 1090 6415 7117 10982 11656 SS ECR Oooo Dept. of CSE, BIT Bhalki Page 26 Prof. V. R. More Analysis & Design of Algorithms Lab (BCSL404) Program 12 Design and implement C/C++ Program for N Queen's problem using Backtracking #include #include #include #define MAX 50 int can_place(int c[],int r) { void int i; for(i if(c[i] return @; return 1; sicrsitt) [r] |] (abs(c[i]-c[r])==abs(i-r))) display(int c[],int n) int i,3; char cb[10][10]; =0) { c[r]+5 while(c[r]

You might also like