Design and Analysis of Algorithms Lab-1
Design and Analysis of Algorithms Lab-1
1. Use divide and conquer method to recursively find the maximum and minimum
elements in a given list of n elements.
#include<stdio.h>
struct temp{
int max,min;
};
struct temp maxmin(int a[],int l,int r){
struct temp mm,mml,mmr;
if(l==r){
mm.max=mm.min=a[l];
return mm;
}
else if(r-l==1){
mm.max= (a[l]<a[r])?a[r]:a[l];
mm.min= (a[l]<a[r])?a[l]:a[r];
return mm;
}
else {
int mid=(r+l)/2;
mml=maxmin(a,l,mid);
mmr=maxmin(a,mid+1,r);
mm.max= (mml.max>mmr.max)?mml.max:mmr.max;
mm.min= (mml.min<mmr.min)?mml.min:mmr.min;
return mm;
}
}
int main(){
int n;
printf("Enter size of arry:");
scanf("%d",&n);
int a[n];
printf("Enter those Elements:");
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
// Global variables
int t = 0;
int n;
// Function prototypes
void divarr(int arry[], int low, int high);
void mergesort(int a[], int l, int m, int h);
int main() {
int ch;
while(1){
printf("\n1.Merge Sort\n2.Graph\n3.Exit\nEnter your choice:");
scanf("%d", &ch);
switch(ch){
case 1:{
printf("Enter the size of array: ");
scanf("%d", &n);
int a[n];
printf("Enter the elements of array: ");
for(int i = 0; i < n; i++)
scanf("%d", &a[i]);
divarr(a, 0, n - 1);
}
break;
case 2: {
FILE *fp = fopen("mergesort.txt", "w");
for (int i = 100; i < 10000; i += 100) {
int a[i];
srand(time(NULL));
for (int j = 0; j < i; j++) {
a[j] = rand() % 1000;
}
clock_t s = clock();
divarr(a, 0, n - 1);
clock_t e = clock();
double tk = ((double)(e - s)) / CLOCKS_PER_SEC;
fprintf(fp, "%d\t%lf\n", i, tk);
}
printf("\nData File generated and stored in file <mergesort.txt >.\n Use a
plotting utility\n");
fclose(fp);
break;
case 3:exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}
}
return 0;
}
3. Sort a given set of elements using Quick 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 me taken versus n. The elements can be read from a file or can
be generated using the random number generator.
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
}
} */
int partision(int a[], int l, int r) {
int p = a[l];
int i = l+1;
int j = r;
while (i<j) {
while (a[i] <= p && i < r)
i++;
while (a[j] >= p && j > l)
j--;
if (i < j)
swap(&a[i], &a[j]);
else
swap(&a[l], &a[j]);
return j;
}
}
void quicksort(int a[],int l,int r){
if(l<r){
int s=partision(a,l,r);
quicksort(a,l,s-1);
quicksort(a,s+1,r);
}
}
int main(){
while(1){
int ch;
int a[10000];
printf("1.quick sort \n2.Graph\n3.Exit\nEnter your choice:");
scanf("%d",&ch);
fflush(stdin);
switch(ch){
case 1:
int n;
printf("Enter the Size of arry:");
scanf("%d",&n);
printf("Enter those Elementes:");
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
clock_t s=clock();
quicksort(a,0,n-1);
clock_t e=clock();
double t=((double)(e-s))/CLOCKS_PER_SEC;
printf("time taken is %lf\n",t);
printf("Elementes in sorted order are:");
for(int i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n");
break;
case 2:
FILE *fp=fopen("quicksort_graph.txt","w");
srand(time(0));
for(int i=100;i<=10000;i+=100){
for(int j=0;j<i;j++)
a[j]=rand()%1000;
clock_t s=clock();
quicksort(a,0,i-1);
clock_t e=clock();
double t=((double) (e-s))/CLOCKS_PER_SEC;
fprintf(fp,"%d\t%lf\n",i,t);
}
int main() {
int n;
printf("Enter number of vertices\n"); /* READ NUMBER OF VERTICES */
scanf("%d", &n);
int g[max][max];
printf("Enter the adjacency matrix :\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &g[i][j]);
}
}
char vertices[max];
printf("Enter the vertices:\n");
for (int i = 0; i < n; i++) {
scanf(" %c", &vertices[i]); // Add space before %c to consume any newline
characters
}
printf("\nTopological order:\n");
topological_order(n, g, vertices);
Source Removal:
#include <stdio.h>
#include <stdlib.h>
#define max 100
int id[max];
int findsource(int n){
for(int i=0;i<n;i++)
if(id[i]==0)
return i;
int main() {
int n;
printf("Enter number of vertices\n"); /* READ NUMBER OF VERTICES */
scanf("%d", &n);
int g[max][max];
printf("Enter the adjacency matrix :\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &g[i][j]);
}
}
char vertices[max];
printf("Enter the vertices:\n");
for (int i = 0; i < n; i++) {
scanf(" %c", &vertices[i]); // Add space before %c to consume any newline
characters
}
for(int i=0;i<n;i++){
int count=0;
for(int j=0;j<n;j++){
if(g[j][i]==1)
count++;
}
id[i]=count;
}
}
printf("\n");
return 0;
}
5. Print all the nodes reachable from a given starng node in a given digraph using Depth First
Search method
#include <stdio.h>
int v[100];
void dfs(int n,int s, int v[n], int g[n][n]) {
v[s] = 1;
for (int i = 1; i <= n; i++) {
if (g[s][i] && !v[i]) {
dfs(n,i, v, g);
}
}
}
int main() {
int n;
int g[n][n];
printf("Enter the adjacency matrix :\n");
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
scanf("%d", &g[i][j]);
int s;
printf("Enter the source vertex : ");
scanf("%d", &s);
dfs(n,s, v, g);
return 0;
}
6. Print all the nodes reachable from a given starng node in a given digraph using breadth First
Search method.
#include <stdio.h>
#define max 100
int n,v[max], q[max], f=1, r= 0;
}
if (f <= r)
{
v[q[f]] = 1;
bfs(q[f++],g);
}
}
int main()
{
7. Sort a given set of elements using Heap Sort method and determine the me 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 time taken versus n. The elements can be read from a file or can
be generated using the random number generator.
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
// Function prototypes
void heapfy(int a[], int n);
void heapsort(int a[], int n);
int main(){
int ch;
while(1){
printf("\n1.Heap Sort\n2.Graph\n3.Exit\nEnter your choice:");
scanf("%d", &ch);
switch(ch){
case 1:{
int n;
printf("Enter the value of n:");
scanf("%d", &n);
int a[n];
heapfy(a, n);
heapsort(a, n);
printf("Sorted elements are:\n");
for(int i = 1; i <= n; i++)
printf("%d ", a[i]);
printf("\n");
break;
}
case 2:
FILE *fp = fopen("heapsort.txt", "w");
for (int i = 100; i < 10000; i += 100) {
int a[i];
srand(time(NULL));
for (int j = 1; j <= i; j++) {
a[j] = rand() % 1000;
}
clock_t s = clock();
heapfy(a, i);
heapsort(a, i);
clock_t e = clock();
double tk = ((double)(e - s)) / CLOCKS_PER_SEC;
fprintf(fp, "%d\t%lf\n", i, tk);
}
printf("\nData File generated and stored in file <heapsort.txt >.\n Use a
plotting utility\n");
fclose(fp);
break;
case 3:
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
if(v>=a[j])
flag=1;
else{
a[k]=a[j];
k=j;
}
}
a[k]=v;
}
}