Sei sulla pagina 1di 64

Analysis and Design of Algorithm Lab

File
(CSE303)

Submitted to: Submitted by:

Dr. Vasudha Vashisht Name: Parth Gupta

Associate Professor (CSE) Enrolment No: A2305216354


ASET Section: 6CSE-3X (6th Sem)

Department of Computer Science &


Engineering AMITY SCHOOL OF ENGINEERING &
TECHNOLOGY AMITY UNIVERSITY, UTTAR
PRADESH
INDEX

S. Name of programs to be carried out Date


N.
Write programs in ‘C’ language to implement following programs:
1 Tower of Hanoi Jan 16, 2019

2 Linked List and its operations Jan 23, 2019

3 Merge Sort Jan 30, 2019

4 Quick Sort Feb 6, 2019

5 Binary Search using recursion Feb 13, 2019

6 Fractional Knapsack Problem using Greedy Method Feb 20, 2019

7 Single Source Shortest Path Problem (Dijkstra’s Algorithm) Feb 27, 2019

8 0/1 Knapsack Problem using Dynamic programming March 6, 2019

9 Traveling Salesman Problem using Dynamic programming March 6, 2019

10 All Pair Shortest Path Problem (Floyed Warshall’s Algorithm) March 13, 2019

11 Longest Common Subsequence Problem March 13, 2019

12 Matrix Chain Multiplication March 20, 2019

13 N- Queens Problem using Backtracking approach March 27, 2019

14 Travelling Salesman Problem using Branch & Bound approach March 27, 2019

15 Depth First Search (DFS) Traversal April 3, 2019

16 Breadth First Search (BFS) Traversal April 3, 2019

(Faculty Signature)
EXPERIMENT 1
Q.1) WAP to find an element in an array using recursive binary search.

Sol:
#include<stdio.h>

#include<stdlib.h>

#define size 10

int binsearch(int[], int, int, int);

int main() {

int num, i, key, position;

int low, high, list[size];

printf("\nEnter the total number of elements:");

scanf("%d", &num);

printf("Enter the array:\n");

for (i = 0; i < num; i++) {

scanf("%d", &list[i]);

low = 0;

high = num - 1;

printf("Enter element to be searched: ");

scanf("%d", &key);

position = binsearch(list, key, low, high);

if (position != -1) {

printf("Number present at %d", (position + 1));

} else

printf("The number is not present in the array");

return (0);
}

int binsearch(int a[], int x, int low, int high) {

int mid;

if (low > high)

return -1;

mid = (low + high) / 2;

if (x == a[mid]) {

return (mid);

} else if (x < a[mid]) {

binsearch(a, x, low, mid - 1);

} else {

binsearch(a, x, mid + 1, high);

OUTPUT
Internal Assessment (Mandatory Experiment) Sheet for Lab Experiment
Department of Computer Science & Engineering
Amity University, Noida (UP)

Analysis and design of


Programme B.Tech-CSE Course Name
algorithms Lab

Course Code CSE303 Semester 6th

Student Name Parth Gupta Enrolment No. A2305216354

Marking Criteria

Criteria Total Marks Marks Obtained Comments

Concept (A) 2

Implementation (B) 2

Performance (C) 2

Total 6
EXPERIMENT 2
Q.2) WAP to sort a given array using quick sort.

Sol:
#include<stdio.h>

#include<conio.h>

void quicksort(int [10],int,int);

int main(){

int x[20],size,i;

clrscr();

printf("Enter size of the array:");

scanf("%d",&size);

printf("Enter elements of array:\n");

for(i=0;i<size;i++)

scanf("%d",&x[i]);

quicksort(x,0,size-1);

printf("Sorted array:\n");

for(i=0;i<size;i++)

printf(" %d",x[i]);

return 0;

void quicksort(int x[10],int first,int last){

int pivot,j,temp,i;

if(first<last){

pivot=first;

i=first;
j=last;

while(i<j){

while(x[i]<=x[pivot]&&i<last)

i++;

while(x[j]>x[pivot])

j--;

if(i<j){

temp=x[i];

x[i]=x[j];

x[j]=temp;

temp=x[pivot];

x[pivot]=x[j];

x[j]=temp;

quicksort(x,first,j-1);

quicksort(x,j+1,last);

OUTPUT
Internal Assessment (Mandatory Experiment) Sheet for Lab Experiment
Department of Computer Science & Engineering
Amity University, Noida (UP)

Analysis and design of


Programme B.Tech-CSE Course Name
algorithms Lab

Course Code CSE303 Semester 6th

Student Name Parth Gupta Enrolment No. A2305216354

Marking Criteria

Criteria Total Marks Marks Obtained Comments

Concept (A) 2

Implementation (B) 2

Performance (C) 2

Total 6
EXPERIMENT 3
Q.3) WAP to sort a given array using merge sort.

Sol:
#include<iostream.h>

#include<conio.h>

void mergesort(int *,int,int);

void merge(int *,int,int,int,int);

int main()

clrscr();

int arr[100],n,i;

cout<<"enter the number of elements in an array:";

cin>>n;

cout<<"enter the array:\n";

for(i=0;i<n;i++)

cin>>arr[i];

cout<<"array entered is:\n";

for(i=0;i<n;i++)

cout<<arr[i];

cout<<endl;

mergesort(arr,0,n-1);

cout<<"array after sorting:\n";


for(i=0;i<n;i++)

cout<<arr[i];

cout<<endl;

getch();

void mergesort(int *A,int l,int h)

int mid;

if(l<h)

mid=(l+h)/2;

mergesort(A,l,mid);

mergesort(A,mid+1,h);

merge(A,l,mid,mid+1,h);

void merge(int *a,int l1,int mid1,int mid2,int h1)

int temp[100],k=0,i,j,x=l1,y=h1;

while(l1<=mid1 && mid2<=h1)

if(a[l1]<a[mid2])

temp[k++]=a[l1++];

}
else

temp[k++]=a[mid2++];

for(;l1<=mid1;l1++)

temp[k++]=a[l1];

for(;mid2<=h1;mid2++)

temp[k++]=a[mid2];

for(i=x,j=0;i<=y;i++,j++)

a[i]=temp[j];

OUTPUT
Internal Assessment (Mandatory Experiment) Sheet for Lab Experiment
Department of Computer Science & Engineering
Amity University, Noida (UP)

Analysis and design of


Programme B.Tech-CSE Course Name
algorithms Lab

Course Code CSE303 Semester 6th

Student Name Parth Gupta Enrolment No. A2305216354

Marking Criteria

Criteria Total Marks Marks Obtained Comments

Concept (A) 2

Implementation (B) 2

Performance (C) 2

Total 6
EXPERIMENT 4
Q.4) WAP to count number of nodes in a given list.

Sol:
#include<stdio.h>

#include<stdlib.h>

struct node{

int data;

struct node* next;

};

struct node* head;

void insert(int data){

struct node* temp = (struct node*)malloc(sizeof(struct node));

temp->data = data;

temp->next = head;

head = temp;

void print(){

struct node* temp = head;

int count=0;

while(temp != NULL){

temp = temp->next;

count++;

printf("\n Total no. of nodes is %d",count);

}
int main(){

head = NULL;

insert(2);

insert(4);

insert(6);

insert(5);

insert(8);

print();

return 0;

OUTPUT
Internal Assessment (Mandatory Experiment) Sheet for Lab Experiment
Department of Computer Science & Engineering
Amity University, Noida (UP)

Analysis and design of


Programme B.Tech-CSE Course Name
algorithms Lab

Course Code CSE303 Semester 6th

Student Name Parth Gupta Enrolment No. A2305216354

Marking Criteria

Criteria Total Marks Marks Obtained Comments

Concept (A) 2

Implementation (B) 2

Performance (C) 2

Total 6
EXPERIMENT 5
Q.5) WAP to implement tower of hanoi.

Sol:
#include <stdio.h>

void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod)

if (n == 1)

printf("\n Move disk 1 from rod %c to rod %c", from_rod, to_rod);

return;

towerOfHanoi(n-1, from_rod, aux_rod, to_rod);

printf("\n Move disk %d from rod %c to rod %c", n, from_rod, to_rod);

towerOfHanoi(n-1, aux_rod, to_rod, from_rod);

int main()

int n;

printf("enter the number of disks:");

scanf("%d",&n);

towerOfHanoi(n, 'A', 'C', 'B');

return 0;

}
OUTPUT
Internal Assessment (Mandatory Experiment) Sheet for Lab Experiment
Department of Computer Science & Engineering
Amity University, Noida (UP)

Analysis and design of


Programme B.Tech-CSE Course Name
algorithms Lab

Course Code CSE303 Semester 6th

Student Name Parth Gupta Enrolment No. A2305216354

Marking Criteria

Criteria Total Marks Marks Obtained Comments

Concept (A) 2

Implementation (B) 2

Performance (C) 2

Total 6
EXPERIMENT 6
Q.6) WAP to implement fractional knapsack problem.

Sol:
#include<iostream>

using namespace std;

int main()

int n,i,j,m;

float p[100],w[100],ratio[100],temp,profit=0.0;

cout<<"enter the max weight of the bag:";

cin>>m;

cout<<"enter no of states:";

cin>>n;

cout<<"enter profit for each state:\n";

for(i=1;i<=n;i++)

cin>>p[i];

cout<<"enter weight for each state:\n";

for(i=1;i<=n;i++)

cin>>w[i];

for(i=1;i<=n;i++)

ratio[i]=p[i]/w[i];
}

for(i=1;i<=n;i++)

for(j=i+1;j<=n;j++)

if(ratio[i]<ratio[j])

temp=ratio[i];

ratio[i]=ratio[j];

ratio[j]=temp;

temp=p[i];

p[i]=p[j];

p[j]=temp;

temp=w[i];

w[i]=w[j];

w[j]=temp;

cout<<"data you entered in sorted manner is:\n";

cout<<"states:\t\t";

for(i=1;i<=n;i++)

cout<<i<<"\t";

}
cout<<"\nprofits:\t";

for(i=1;i<=n;i++)

cout<<p[i]<<"\t";

cout<<"\nweights:\t";

for(i=1;i<=n;i++)

cout<<w[i]<<"\t";

cout<<"\nthe ratio's are:";

cout<<"\nratio's:\t";

for(i=1;i<=n;i++)

cout<<ratio[i]<<"\t";

while(m>0)

for(i=1;i<=n;i++)

if(m>w[i])

profit=profit+p[i];

m=m-w[i];

else

{
profit=profit+((m/w[i]))*p[i];

m=0;

cout<<"\nmaximum profit is:"<<profit;

return 0;

OUTPUT
Internal Assessment (Mandatory Experiment) Sheet for Lab Experiment
Department of Computer Science & Engineering
Amity University, Noida (UP)

Analysis and design of


Programme B.Tech-CSE Course Name
algorithms Lab

Course Code CSE303 Semester 6th

Student Name Parth Gupta Enrolment No. A2305216354

Marking Criteria

Criteria Total Marks Marks Obtained Comments

Concept (A) 2

Implementation (B) 2

Performance (C) 2

Total 6
EXPERIMENT 7
Q.7) WAP to implement single source shortest path problem (Dijkstra’s
Algorithm).

Sol:
#include<stdio.h>

#include<conio.h>

#include<process.h>

#include<string.h>

#include<math.h>

#define IN 99

#define N 6

int dijkstra(int cost[][N], int source, int target);

int main()

int cost[N][N],i,j,w,ch,co;

int source, target,x,y;

for(i=1;i< N;i++)

for(j=1;j< N;j++)

cost[i][j] = IN;

for(x=1;x< N;x++)

for(y=x+1;y< N;y++)

printf("Enter the weight of the path between nodes %d and %d:",x,y);

scanf("%d",&w);

cost [x][y] = cost[y][x] = w;

}
printf("\n");

printf("\nEnter the source:");

scanf("%d", &source);

printf("\nEnter the target:");

scanf("%d", &target);

co = dijsktra(cost,source,target);

printf("\nThe Shortest Path:%d",co);

return 0;

int dijsktra(int cost[][N],int source,int target)

int dist[N],prev[N],selected[N]={0},i,m,min,start,d,j;

char path[N];

for(i=1;i< N;i++)

dist[i] = IN;

prev[i] = -1;

start = source;

selected[start]=1;

dist[start] = 0;

while(selected[target]==0)

min = IN;

m = 0;

for(i=1;i< N;i++)
{

d = dist[start] +cost[start][i];

if(d< dist[i]&&selected[i]==0)

dist[i] = d;

prev[i] = start;

if(min>dist[i] && selected[i]==0)

min = dist[i];

m = i;

start = m;

selected[start] = 1;

start = target;

j = 0;

while(start != -1)

path[j++] = start+64;

start = prev[start];

path[j]='\0';

strrev(path);

printf("%s", path);

return dist[target];
}

OUTPUT
Internal Assessment (Mandatory Experiment) Sheet for Lab Experiment
Department of Computer Science & Engineering
Amity University, Noida (UP)

Analysis and design of


Programme B.Tech-CSE Course Name
algorithms Lab

Course Code CSE303 Semester 6th

Student Name Parth Gupta Enrolment No. A2305216354

Marking Criteria

Criteria Total Marks Marks Obtained Comments

Concept (A) 2

Implementation (B) 2

Performance (C) 2

Total 6
EXPERIMENT 8
Q.8) WAP to implement 0/1 knapsack problem using dynamic programming.

Sol:
#include<iostream>

using namespace std;

int knapsack_dp(int n, int M, int w[], int p[])

int i,j;

int knapsack[n+1][M+1];

for(j=0;j<=M;j++)

knapsack[0][j]=0;

for(i=0;i<=n;i++)

knapsack[i][0]=0;

for(i=1;i<=n;i++)

for(j=1;j<=M;j++)

if(w[i-1]<=j)

knapsack[i][j]=max(knapsack[i-1][j],p[i-1]+knapsack[i-1][j-w[i-1]]);

else

knapsack[i][j]=knapsack[i-1][j];

}
}

return knapsack[n][M];

int main()

int i,j;

int n;

int M;

cout<<"Enter the no. of items:";

cin>>n;

int w[n];

int p[n];

cout<<"Enter the weight of all items:\n";

for(i=0;i<n;i++)

cin>>w[i];

cout<<endl;

cout<<"Enter the cost of all items:\n";

for(i=0;i<n;i++)

cin>>p[i];

cout<<endl;

cout<<"enter the capacity of knapsack:";

cin>>M;
int result=knapsack_dp(n,M,w,p);

cout<<"The maximum value of items that can be put into knapsack is:"<<result;

return 0;

OUTPUT
Internal Assessment (Mandatory Experiment) Sheet for Lab Experiment
Department of Computer Science & Engineering
Amity University, Noida (UP)

Analysis and design of


Programme B.Tech-CSE Course Name
algorithms Lab

Course Code CSE303 Semester 6th

Student Name Parth Gupta Enrolment No. A2305216354

Marking Criteria

Criteria Total Marks Marks Obtained Comments

Concept (A) 2

Implementation (B) 2

Performance (C) 2

Total 6
EXPERIMENT 9
Q.9) WAP to implement Travelling salesman problem using dynamic
programming.

Sol:
#include<stdio.h>

int ary[10][10],completed[10],n,cost=0;

void takeInput()

int i,j;

printf("Enter the number of villages:");

scanf("%d",&n);

printf("\nEnter the Cost Matrix:\n");

for(i=0;i < n;i++)

printf("\nEnter Elements of Row:%d\n",i+1);

for( j=0;j < n;j++)

scanf("%d",&ary[i][j]);

completed[i]=0;

printf("\n\nThe cost list is:");

for( i=0;i < n;i++)

printf("\n");

for(j=0;j < n;j++)

printf("\t%d",ary[i][j]);

}
void mincost(int city)

int i,ncity;

completed[city]=1;

printf("%d--->",city+1);

ncity=least(city);

if(ncity==999)

ncity=0;

printf("%d",ncity+1);

cost+=ary[city][ncity];

return;

mincost(ncity);

int least(int c)

int i,nc=999;

int min=999,kmin;

for(i=0;i < n;i++)

if((ary[c][i]!=0)&&(completed[i]==0))

if(ary[c][i]+ary[i][c] < min)

min=ary[i][0]+ary[c][i];

kmin=ary[c][i];

nc=i;
}

if(min!=999)

cost+=kmin;

return nc;

int main()

takeInput();

printf("\n\nThe Path is:\n");

mincost(0);

printf("\n\nMinimum cost is:%d\n",cost);

return 0;

OUTPUT
Internal Assessment (Mandatory Experiment) Sheet for Lab Experiment
Department of Computer Science & Engineering
Amity University, Noida (UP)

Analysis and design of


Programme B.Tech-CSE Course Name
algorithms Lab

Course Code CSE303 Semester 6th

Student Name Parth Gupta Enrolment No. A2305216354

Marking Criteria

Criteria Total Marks Marks Obtained Comments

Concept (A) 2

Implementation (B) 2

Performance (C) 2

Total 6
EXPERIMENT 10
Q.10) WAP to implement all pair shortest path problem (Floyed Warshall’s
Algorith).

Sol:
#include<iostream>

using namespace std;

void floyds(int b[][4])

int i, j, k;

for (k = 0; k < 4; k++)

for (i = 0; i < 4; i++)

for (j = 0; j < 4; j++)

if ((b[i][k] * b[k][j] != 0) && (i != j))

if ((b[i][k] + b[k][j] < b[i][j]) || (b[i][j] == 0))

b[i][j] = b[i][k] + b[k][j];

for (i = 0; i < 4; i++)

{
cout<<"\nMinimum Cost With Respect to Node:"<<i<<endl;

for (j = 0; j < 4; j++)

cout<<b[i][j]<<"\t";

int main()

int b[4][4];

cout<<"ENTER VALUES OF ADJACENCY MATRIX\n\n";

for (int i = 0; i < 4; i++)

cout<<"enter values for "<<(i+1)<<" row"<<endl;

for (int j = 0; j < 4; j++)

cin>>b[i][j];

floyds(b);

return 0;

}
OUTPUT
Internal Assessment (Mandatory Experiment) Sheet for Lab Experiment
Department of Computer Science & Engineering
Amity University, Noida (UP)

Analysis and design of


Programme B.Tech-CSE Course Name
algorithms Lab

Course Code CSE303 Semester 6th

Student Name Parth Gupta Enrolment No. A2305216354

Marking Criteria

Criteria Total Marks Marks Obtained Comments

Concept (A) 2

Implementation (B) 2

Performance (C) 2

Total 6
EXPERIMENT 11
Q.11) WAP to implement longest common subsequence problem.

Sol:
#include<iostream>

#include<string.h>

using namespace std;

int longestCommonSubsequece(string str1, string str2, int len1, int len2)

int i, j;

int LCS[len1+1][len2+1];

for(i=0;i<=len1;i++)

LCS[i][0]=0;

for(j=0;j<=len2;j++)

LCS[0][j]=0;

for(i=1;i<=len1;i++)

for(j=1;j<=len2;j++)

if(str1[i-1]==str2[j-1])

LCS[i][j]=1+LCS[i-1][j-1];

else

LCS[i][j]=max(LCS[i-1][j],LCS[i][j-1]);

}
}

return LCS[len1][len2];

int main()

string str1,str2;

cout<<"Enter first string:";

getline(cin, str1);

cout<<"Enter second string:";

getline(cin, str2);

int len1=str1.length();

int len2=str2.length();

cout<<"Length of longest common subsequence


is:"<<longestCommonSubsequece(str1,str2,len1,len2);

cout<<endl;

return 0;

OUTPUT
Internal Assessment (Mandatory Experiment) Sheet for Lab Experiment
Department of Computer Science & Engineering
Amity University, Noida (UP)

Analysis and design of


Programme B.Tech-CSE Course Name
algorithms Lab

Course Code CSE303 Semester 6th

Student Name Parth Gupta Enrolment No. A2305216354

Marking Criteria

Criteria Total Marks Marks Obtained Comments

Concept (A) 2

Implementation (B) 2

Performance (C) 2

Total 6
EXPERIMENT 12
Q.12) WAP to implement matrix chain multiplication.

Sol:
#include<stdio.h>

#include<stdlib.h>

int minimum_cost(int matrix[20], int t)

int x, small;

if(t == 1)

return matrix[0];

else

small = matrix[0];

for(x = 1; x < t; x++)

if(matrix[x] < small)

small = matrix[x];

return small;

int main()
{

int t, i, l, j, k, limit;

int matrix[30], multiplier[10][15], columns[15], rows[15], temp[15];

printf("\nEnter Total Number of Matrices:\t");

scanf("%d", &limit);

for(i = 0; i < limit; i++)

printf("\nEnter Number of Columns of Matrix %d:\t", i + 1);

scanf("%d", &columns[i]);

printf("Enter Number of Rows of Matrix %d:\t", i + 1);

scanf("%d", &rows[i]);

for(i = 0; i < limit; i++)

temp[i] = columns[i];

temp[i] = rows[i - 1];

printf("\n");

for(k = 1; k <= limit; k++)

for(j = k, i = 1; j <= limit; j++, i++)

multiplier[i][j] = 0;

for(l = 2; l <= limit; l++)

{
for(j = l, i = 1; j <= limit; j++, i++)

t = 0;

for(k = i; k < j; k++)

matrix[t++] = (multiplier[i][k] + multiplier[k + 1][j]) + (temp[i - 1] * temp[k]


* temp[j]);

multiplier[i][j] = minimum_cost(matrix, t);

printf("\nMinimum Scalar Multiplications:\t%d\n", multiplier[1][limit]);

return 0;

OUTPUT
Internal Assessment (Mandatory Experiment) Sheet for Lab Experiment
Department of Computer Science & Engineering
Amity University, Noida (UP)

Analysis and design of


Programme B.Tech-CSE Course Name
algorithms Lab

Course Code CSE303 Semester 6th

Student Name Parth Gupta Enrolment No. A2305216354

Marking Criteria

Criteria Total Marks Marks Obtained Comments

Concept (A) 2

Implementation (B) 2

Performance (C) 2

Total 6
EXPERIMENT 13
Q.13) WAP to implement N-queens problem using backtracking approach.

Sol:
#include<stdio.h>

#include<math.h>

int board[20],count;

int main()

int n,i,j;

void queen(int row,int n);

printf("\n\nEnter number of Queens:");

scanf("%d",&n);

queen(1,n);

return 0;

void print(int n)

int i,j;

printf("\n\nSolution %d:\n\n",++count);

for(i=1;i<=n;++i)

printf("\t%d",i);

for(i=1;i<=n;++i)

printf("\n\n%d",i);

for(j=1;j<=n;++j)

{
if(board[i]==j)

printf("\tQ");

else

printf("\t-");

int place(int row,int column)

int i;

for(i=1;i<=row-1;++i)

if(board[i]==column)

return 0;

else

if(abs(board[i]-column)==abs(i-row))

return 0;

return 1;

void queen(int row,int n)

int column;

for(column=1;column<=n;++column)

if(place(row,column))

{
board[row]=column;

if(row==n)

print(n);

else

queen(row+1,n);

OUTPUT
Internal Assessment (Mandatory Experiment) Sheet for Lab Experiment
Department of Computer Science & Engineering
Amity University, Noida (UP)

Analysis and design of


Programme B.Tech-CSE Course Name
algorithms Lab

Course Code CSE303 Semester 6th

Student Name Parth Gupta Enrolment No. A2305216354

Marking Criteria

Criteria Total Marks Marks Obtained Comments

Concept (A) 2

Implementation (B) 2

Performance (C) 2

Total 6
EXPERIMENT 14
Q.14) WAP to implement Travelling salesman problem using branch and bound
approach.

Sol:
#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

int a[10][10],visited[10],n,cost=0;

void get()

int i,j;

printf("\n\nEnter Number of Cities:");

scanf("%d",&n);

printf("\nEnter Cost Matrix:\n");

for( i=0;i<n;i++)

printf("\n Enter Elements of Row %d:\n",i+1);

for( j=0;j<n;j++)

scanf("%d",&a[i][j]);

visited[i]=0;

printf("\n\nThe Cost Matrix is:\n");

for( i=0;i<n;i++)

printf("\n\n");

for( j=0;j<n;j++)

printf("\t%d",a[i][j]);
}

void mincost(int city)

int i,ncity;

visited[city]=1;

printf("%d ===> ",city+1);

ncity=least(city);

if(ncity==999)

ncity=0;

printf("%d",ncity+1);

cost+=a[city][ncity];

return;

mincost(ncity);

int least(int c)

int i,nc=999;

int min=999,kmin;

for(i=0;i<n;i++)

if((a[c][i]!=0)&&(visited[i]==0))

if(a[c][i]<min)

min=a[i][0]+a[c][i];
kmin=a[c][i];

nc=i;

if(min!=999)

cost+=kmin;

return nc;

void put()

printf("\n\nMinimum cost:");

printf("%d",cost);

int main()

get();

printf("\n\nThe Path is:\n\n");

mincost(0);

put();

getch();

}
OUTPUT
Internal Assessment (Mandatory Experiment) Sheet for Lab Experiment
Department of Computer Science & Engineering
Amity University, Noida (UP)

Analysis and design of


Programme B.Tech-CSE Course Name
algorithms Lab

Course Code CSE303 Semester 6th

Student Name Parth Gupta Enrolment No. A2305216354

Marking Criteria

Criteria Total Marks Marks Obtained Comments

Concept (A) 2

Implementation (B) 2

Performance (C) 2

Total 6
EXPERIMENT 15
Q.15) WAP to implement Depth first search (DFS) traversal.

Sol:
#include<stdio.h>

#include<stdlib.h>

struct btnode {

int value;

struct btnode *l;

struct btnode *r;

};

typedef struct btnode bt;

bt *root;

bt *new, *list;

bt *create_node();

void display(bt *);

void construct_tree();

void dfs(bt *);

void main()

construct_tree();

display(root);

printf("\n");

printf("Depth first traversal\n ");

dfs(root);

bt * create_node()
{

new=(bt *)malloc(sizeof(bt));

new->l = NULL;

new->r = NULL;

void construct_tree()

root = create_node();

root->value = 50;

root->l = create_node();

root->l->value = 20;

root->r = create_node();

root->r->value = 30;

root->l->l = create_node();

root->l->l->value = 70;

root->l->r = create_node();

root->l->r->value = 80;

root->l->r->r = create_node();

root->l->r->r->value = 60;

root->l->l->l = create_node();

root->l->l->l->value = 10;

root->l->l->r = create_node();

root->l->l->r->value = 40;

void display(bt * list)

if (list == NULL)
{

return;

display(list->l);

printf("->%d", list->value);

display(list->r);

void dfs(bt * list)

if (list == NULL)

return;

dfs(list->l);

dfs(list->r);

printf("->%d", list->value);

OUTPUT
Internal Assessment (Mandatory Experiment) Sheet for Lab Experiment
Department of Computer Science & Engineering
Amity University, Noida (UP)

Analysis and design of


Programme B.Tech-CSE Course Name
algorithms Lab

Course Code CSE303 Semester 6th

Student Name Parth Gupta Enrolment No. A2305216354

Marking Criteria

Criteria Total Marks Marks Obtained Comments

Concept (A) 2

Implementation (B) 2

Performance (C) 2

Total 6
EXPERIMENT 16
Q.16) WAP to implement Breadth first search (BFS) traversal.

Sol:
#include<stdio.h>

#include<stdlib.h>

struct btnode {

int value;

struct btnode *l;

struct btnode *r;

};

typedef struct btnode bt;

bt *root;

bt *new, *list;

bt *create_node();

void display(bt *);

void construct_tree();

void bfs(bt *);

void main()

construct_tree();

display(root);

printf("\n");

printf("Breadth first traversal\n ");

bfs(root);

bt * create_node()
{

new=(bt *)malloc(sizeof(bt));

new->l = NULL;

new->r = NULL;

void construct_tree()

root = create_node();

root->value = 50;

root->l = create_node();

root->l->value = 20;

root->r = create_node();

root->r->value = 30;

root->l->l = create_node();

root->l->l->value = 70;

root->l->r = create_node();

root->l->r->value = 80;

root->l->r->r = create_node();

root->l->r->r->value = 60;

root->l->l->l = create_node();

root->l->l->l->value = 10;

root->l->l->r = create_node();

root->l->l->r->value = 40;

void display(bt * list)

if (list == NULL)
{

return;

display(list->l);

printf("->%d", list->value);

display(list->r);

void bfs(bt * list)

if (list == NULL)

return;

bfs(list->l);

printf("->%d", list->value);

bfs(list->r);

OUTPUT
Internal Assessment (Mandatory Experiment) Sheet for Lab Experiment
Department of Computer Science & Engineering
Amity University, Noida (UP)

Analysis and design of


Programme B.Tech-CSE Course Name
algorithms Lab

Course Code CSE303 Semester 6th

Student Name Parth Gupta Enrolment No. A2305216354

Marking Criteria

Criteria Total Marks Marks Obtained Comments

Concept (A) 2

Implementation (B) 2

Performance (C) 2

Total 6

Potrebbero piacerti anche