DAA LAB MANNUAL - C Latest Updated Program
DAA LAB MANNUAL - C Latest Updated Program
EXPERIMENT DETAILS
1.Design and implement a 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.
………………………………………………………………………………………………………..
PROGRAM:
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
void selectionSort(int arr[], int n)
{
int i, j, minIndex, temp;
// Traverse through all array elements
for (i = 0; i < n-1; i++)
{
// Find the minimum element in unsorted array
minIndex = i;
for (j = i+1; j < n; j++)
{
if (arr[j] < arr[minIndex])
minIndex = j;
}
// Swap the found minimum element with the first element
temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
// Function to print an array
void printArray_beforesort(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
void printArray_aftersort(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main()
{
int arr[10000],n,i;
double time_taken;
clock_t start,end;
printf("Enter the number of elements \n");
scanf("%d",&n);
1
Department of Computer Science & Engineering, SCEM, Mangaluru.
CS422L5C:DESIGN AND ANALYSIS OF ALGORITHMS LABORATORY
for(i=0;i<n;i++)
arr[i]=rand()%10000;
printf("elements before sorting");
printArray_beforesort( arr, n);
start=clock();
selectionSort(arr, n );
end=clock();
printf("elements after sorting\n");
printArray_aftersort( arr, n);
time_taken=((double)(end-start))/CLOCKS_PER_SEC*1000;
printf("time taken to sort elements=%lf",time_taken);
return 0;
}
OUTPUT:
Enter the number of elements
100
elements before sorting9383 886 2777 6915 7793 8335 5386 492 6649 1421 2362 27 8690 59
7763 3926 540 3426 9172 5736 5211 5368 2567 6429 5782 1530 2862 5123 4067 3135 3929
9802 4022 3058 3069 8167 1393 8456 5011 8042 6229 7373 4421 4919 3784 8537 5198 4324
8315 4370 6413 3526 6091 8980 9956 1873 6862 9170 6996 7281 2305 925 7084 6327 336 6505
846 1729 1313 5857 6124 3895 9582 545 8814 3367 5434 364 4043 3750 1087 6808 7276 7178
5788 3584 5403 2651 2754 2399 9932 5060 9676 3368 7739 12 6226 8586 8094 7539
elements after sorting
12 27 59 336 364 492 540 545 846 886 925 1087 1313 1393 1421 1530 1729 1873 2305 2362
2399 2567 2651 2754 2777 2862 3058 3069 3135 3367 3368 3426 3526 3584 3750 3784 3895
3926 3929 4022 4043 4067 4324 4370 4421 4919 5011 5060 5123 5198 5211 5368 5386 5403
5434 5736 5782 5788 5857 6091 6124 6226 6229 6327 6413 6429 6505 6649 6808 6862 6915
6996 7084 7178 7276 7281 7373 7539 7739 7763 7793 8042 8094 8167 8315 8335 8456 8537
8586 8690 8814 8980 9170 9172 9383 9582 9676 9802 9932 9956
time taken to sort elements=0.022000
……………………………………………………………………………………………………….
2
Department of Computer Science & Engineering, SCEM, Mangaluru.
CS422L5C:DESIGN AND ANALYSIS OF ALGORITHMS LABORATORY
2.Design and implement a 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.
………………………………………………………………………………………………………..
PROGRAM :
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX 10000
void merge(int array[], int low, int mid, int high)
{
int i = low;
int j = mid + 1; int k = low;
int resarray[MAX];
while (i <= mid && j <= high)
{
if (array[i] < array[j])
{
resarray[k++] = array[i++];
}
else
{
resarray[k++] = array[j++];
}
}
while (i <= mid)
{
resarray[k++] = array[i++];
}
while (j <= high)
{
resarray[k++] = array[j++];
}
for (int m = low; m <= high; m++)
{
array[m] = resarray[m];
}
}
void sort(int array[], int low, int high)
{
if (low < high)
{
int mid = (low + high) / 2;
sort(array, low, mid);
sort(array, mid + 1, high);
merge(array, low, mid, high);
}
}
int main()
{
3
Department of Computer Science & Engineering, SCEM, Mangaluru.
CS422L5C:DESIGN AND ANALYSIS OF ALGORITHMS LABORATORY
int array[MAX];
int i;
printf("Enter the array size: ");
int n;
scanf("%d", &n);
srand(time(NULL));
for (i = 0; i < n; i++)
{
array[i] = rand() % 10000;
}
printf("Array before sorting:\n");
for (i = 0; i < n; i++)
{
printf("%d ", array[i]);
}
printf("\n");
clock_t start = clock();
sort(array, 0, n - 1);
clock_t end = clock();
printf("Sorted array:\n");
for (i = 0; i < n; i++)
{
printf("%d ", array[i]);
}
printf("\n");
double elapsedTime = (double)(end - start) / CLOCKS_PER_SEC * 1000;
printf("Time taken to sort array is: %lf miliseconds\n", elapsedTime);
return 0;
}
4
Department of Computer Science & Engineering, SCEM, Mangaluru.
CS422L5C:DESIGN AND ANALYSIS OF ALGORITHMS LABORATORY
3.Design and implement a Program to solve discrete Knapsack and continuous Knapsack problems using
greedy approximation method.
…………………………………………………………………………………………………………………….
PROGRAM:
#include <stdio.h>
#define MAX 50
int p[MAX], w[MAX], x[MAX];
double maxprofit;
int n, m, i;
void greedyKnapsack(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 < n; i++)
{
ratio[i] = (double)p[i] / w[i];
}
// Sort items based on the ratio in non-increasing order
for (i = 0; i < n - 1; i++)
{
for (int j = i + 1; j < n; j++)
{
if (ratio[i] < ratio[j])
{
double temp = ratio[i];
ratio[i] = ratio[j];
ratio[j] = temp;
int temp2 = w[i];
w[i] = w[j];
w[j] = temp2;
temp2 = p[i];
p[i] = p[j];
p[j] = temp2;
}
}
}
int currentWeight = 0;
maxprofit = 0.0;
// Fill the knapsack with items
for (i = 0; i < n; i++)
{
if (currentWeight + w[i] <= m)
{
x[i] = 1; // Item i is selected
currentWeight += w[i];
maxprofit += p[i];
}
else
{
// Fractional part of item i is selected
5
Department of Computer Science & Engineering, SCEM, Mangaluru.
CS422L5C:DESIGN AND ANALYSIS OF ALGORITHMS LABORATORY
6
Department of Computer Science & Engineering, SCEM, Mangaluru.
CS422L5C:DESIGN AND ANALYSIS OF ALGORITHMS LABORATORY
4. Design an application for a thermal power station and electrical lines that are connected among
various power stations. The costs of electrification involved appear as weights on the edges. Obtain the
minimum possible connection among the thermal stations so that any two thermal stations can be linked
with the minimum cost involved.
………………………………………………………………………………………………………………………
PROGRAM :
#include<stdio.h>
int a,b,u,n,i,j,ne=1,v;
int visited[10],min,mincost=0,cost[10][10];
int main()
{
printf("Enter the number of vertices and graph data\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
printf("(%d,%d):",i,j);
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=999;
}
for(i=1;i<=n;i++)
visited[i]=0;
printf("The edges of spanning tree are\n");
visited[1]=1;
while(ne<n)
{
for(i=1,min=999;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(cost[i][j] <min)
{
if(visited[i]==0)
continue;
else
{
min=cost[i][j];
a=u=i;
b=v=j;
}
}
}
}
if(visited[u]==0||visited[v]==0)
{
printf("%d:edge(%d,%d)=%d\t",ne++,a,b,min);
mincost=mincost+min;
visited[b]=1;
7
Department of Computer Science & Engineering, SCEM, Mangaluru.
CS422L5C:DESIGN AND ANALYSIS OF ALGORITHMS LABORATORY
}
cost[a][b]=cost[b][a]=999;
}
printf("\nMinimum cost is %d\n",mincost);
return 0;
}
OUTPUT:
Enter the number of vertices and graph data
4
(1,1):999
(1,2):1
(1,3):5
(1,4):2
(2,1):1
(2,2):999
(2,3):999
(2,4):999
(3,1):5
(3,2):999
(3,3):999
(3,4):3
(4,1):2
(4,2):999
(4,3):3
(4,4):999
The edges of spanning tree are
1:edge(1,2)=1 2:edge(1,4)=2 3:edge(4,3)=3
Minimum cost is 6
……………………………………………………………………………………………………...
8
Department of Computer Science & Engineering, SCEM, Mangaluru.
CS422L5C:DESIGN AND ANALYSIS OF ALGORITHMS LABORATORY
5.Develop an optimal route for a scenario where a person wants to buy a ticket to a baseball game. Along
the way from house to reaching the destination, the person using it is a toll road, and has to pay a certain
amount of money.
……………………………………………………………………………………………………….
PROGRAM :
#include<stdio.h>
int n,cost[10][10],dist[10];
void readmatrix(int);
int min(int,int);
void shortest_path(int,int);
int main()
{
int i,s;
printf("Enter the number of vertices\n");
scanf("%d",&n);
printf("Enter the cost adjecency matrix(999 for infinite)\n");
readmatrix(n);
printf("Enter the source vertex\n");
scanf("%d",&s);
shortest_path(n,s);
for(i=1;i<=n;i++)
printf("The shortest path between vertex %d to %d is %d\n",s,i,dist[i]);
return 0;
}
int min(int a,int b)
{
return a>b?b:a;
}
void readmatrix(int n)
{
int i,j;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
printf("(%d,%d):",i,j);
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=999;
}
}
void shortest_path(int n,int s)
{
int vis[10],c,u,i,k;
for(i=1;i<=n;i++)
{
vis[i]=0;
dist[i]=cost[s][i];
}
dist[s]=0;
vis[s]=1;
9
Department of Computer Science & Engineering, SCEM, Mangaluru.
CS422L5C:DESIGN AND ANALYSIS OF ALGORITHMS LABORATORY
for(k=1;k<=n;k++)
{
c=999;
for(i=1;i<=n;i++)
if(vis[i]==0)
{
if(dist[i]<=c)
{
c=dist[i];
u=i;
}
}
vis[u]=1;
for (i=1; i<=n;i++)
dist[i]=min(dist[i],dist[u]+cost[u][i]);
}
}
OUTPUT :Enter the number of vertices
5
Enter the cost adjecency matrix(999 for infinite)
(1,1):999
(1,2):1
(1,3):2
(1,4):999
(1,5):999
(2,1):1
(2,2):999
(2,3):3
(2,4):4
(2,5):999
(3,1):2
(3,2):3
(3,3):999
(3,4):5
(3,5):6
(4,1):999
(4,2):4
(4,3):5
(4,4):999
(4,5):6
(5,1):999
(5,2):999
(5,3):6
(5,4):6
(5,5):999
Enter the source vertex
1
The shortest path between vertex 1 to 1 is 0
The shortest path between vertex 1 to 2 is 1
The shortest path between vertex 1 to 3 is 2
The shortest path between vertex 1 to 4 is 5
10
Department of Computer Science & Engineering, SCEM, Mangaluru.
CS422L5C:DESIGN AND ANALYSIS OF ALGORITHMS LABORATORY
6a. Design and implement a Program to solve All-Pairs Shortest Paths problem using Floyd's algorithm.
b. Design and implement a Program to find the transitive closure using Warshal's algorithm.
……………………………………………………………………………………………………………………..
PROGRAM :
#include <stdio.h>
#include<omp.h>
#include<time.h>
int min(int,int);
void floyd(int [][10],int);
int main()
{
int a[10][10],n,i,j;
clock_t start,end;
printf("\nEnter the number of vertices\n");
scanf("%d",&n);
printf("Enter the cost matrix\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
start=clock();
floyd(a,n);
end=clock();
printf("Shortest path matrix is \n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d\t",a[i][j]);
printf("\n");
}
printf("Time taken is %lf\n",(double)(end-start));
return 0;
}
int min(int a,int b)
{
return (a<b?a:b);
}
void floyd(int w[10][10],int n)
{
int i,j,k;
#pragma omp parallel for private(i,j,k) shared(w)
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
w[i][j]=min(w[i][j],w[i][k]+w[k][j]);
}
11
Department of Computer Science & Engineering, SCEM, Mangaluru.
CS422L5C:DESIGN AND ANALYSIS OF ALGORITHMS LABORATORY
OUTPUT :
Enter the number of vertices
4
Enter the cost matrix
0136
1023
3201
6310
Shortest path matrix is
0 1 3 4
1 0 2 3
3 2 0 1
4 3 1 0
Time taken is 4.000000
………………………………………………………………………………………………………
12
Department of Computer Science & Engineering, SCEM, Mangaluru.
CS422L5C:DESIGN AND ANALYSIS OF ALGORITHMS LABORATORY
6b.Design and implement a Program to find the transitive closure using Warshal's algorithm.
…………………………………………………………………………………………………………………………
#include<stdio.h>
void warshall(int a[10][10],int);
int main()
{
int i,j,n,a[10][10];
printf("Enter the number of vertices\n");
scanf("%d",&n);
printf("Enter the adjacency matrix\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
printf("(%d,%d):",i,j);
scanf("%d",&a[i][j]);
}
warshall(a,n);
printf("All pair distance matrix\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d\t",a[i][j]);
printf("\n");
}
return 0;
}
void warshall(int a[10][10],int n)
{
int i,j,k;
for(k=1;k<=n;k++)
for(j=1;j<=n;j++)
for(i=1;i<=n;i++)
a[i][j]=a[i][j] || (a[i][k] && a[k][j]);
}
OUTPUT :
Enter the number of vertices
4
Enter the adjacency matrix
(1,1):0
(1,2):1
(1,3):0
(1,4):0
(2,1):0
13
Department of Computer Science & Engineering, SCEM, Mangaluru.
CS422L5C:DESIGN AND ANALYSIS OF ALGORITHMS LABORATORY
(2,2):0
(2,3):0
(2,4):1
(3,1):1
(3,2):0
(3,3):1
(3,4):0
(4,1):0
(4,2):0
(4,3):0
(4,4):0
All pair distance matrix
0 1 0 1
0 0 0 1
1 1 1 1
0 0 0 0
………………………………………………………………………………………………………..
14
Department of Computer Science & Engineering, SCEM, Mangaluru.
CS422L5C:DESIGN AND ANALYSIS OF ALGORITHMS LABORATORY
7. The owner of a gourmet coffee shop wishes to mix a 10-pound bag of coffee using various types of coffee
beans in such a way to produce the coffee blend at the maximum cost. The weights of the objects in the
problem correspond to the quantity in pounds available of each type of coffee bean. The value of each
quantity of coffee beans is the total cost of that quantity in rupees. Apply the Knapsack algorithm to
maximize the profit.
…………………………………………………………………………………………………………………………
PROGRAM :
#include<stdio.h>
int v[20][20];
int max_value(int,int);
int main()
{
int i,j,p[20],w[20],n,max;
printf("\nEnter the number of items\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter the weight and profit of the item %d:",i);
scanf("%d %d",&w[i],&p[i]);
}
printf("\nEnter the capacity of knapsack\n");
scanf("%d",&max);
for(i=0;i<=n;i++)
v[i][0]=0;
for(i=0;i<=max;i++)
v[0][i]=0;
for(i=1;i<=max;i++)
for(j=1;j<=max;j++)
{
if(w[i]>j)
v[i][j]=v[i-1][j];
else
v[i][j]=max_value(v[i-1][j],v[i-1][j-w[i]]+p[i]);
}
printf("The table is \n");
for(i=0;i<=n;i++)
{
for(j=0;j<=max;j++)
printf("%d\t",v[i][j]);
printf("\n");
}
printf("The max profit is %d",v[n][max]);
printf("most profitable subset is :{");
15
Department of Computer Science & Engineering, SCEM, Mangaluru.
CS422L5C:DESIGN AND ANALYSIS OF ALGORITHMS LABORATORY
j=max;
for(i=n;i>=1;i--)
if(v[i][j]!=v[i-1][j])
{
printf("item%d:1\t",i);
j=j-w[i];
}
else
printf("item%d:0\t",i);
printf("}\n");
return 0;
}
int max_value(int a,int b)
{
return (a>b?a:b);
}
OUTPUT:
Enter the number of items
4
Enter the weight and profit of the item 1:2 12
Enter the weight and profit of the item 2:1 10
Enter the weight and profit of the item 3:3 20
Enter the weight and profit of the item 4:2 15
16
Department of Computer Science & Engineering, SCEM, Mangaluru.
CS422L5C:DESIGN AND ANALYSIS OF ALGORITHMS LABORATORY
8. Design an application for drilling an optimal printed circuit board. To drill two holes of different
diameters consecutively, the head of the machine has to move to a toolbox and change the drilling
equipment. This is quite time consuming. Thus, it is clear that one has to choose some diameter, drill all
holes of the same diameter, change the drill, drill the holes of the next diameter, etc. Thus, this drilling
problem has to minimize the travel time for the machine head. Find the optimal time to drill the circuit
board.
…………………………………………………………………………………………………………………………
PROGRAM:
#include<stdio.h>
int a[10][10],visited[10],n,cost=0;
int least(int c);
void mincost(int city)
{
int i,ncity;
visited[city]=1;
printf("%d-->",city);
ncity=least(city);
if(ncity==999)
{
ncity=1;
printf("%d",ncity);
cost+=a[city][ncity];
return;
}
mincost(ncity);
}
int least(int c)
{
int i,nc=999;
int min=999,kmin;
for(i=1;i<=n;i++)
{
if((a[c][i]!=0)&&(visited[i]==0))
if(a[c][i]<min)
{
min=a[i][1]+a[c][i];
kmin=a[c][i];
nc=i;
}
}
if(min!=999)
cost+=kmin;
return nc;
}
17
Department of Computer Science & Engineering, SCEM, Mangaluru.
CS422L5C:DESIGN AND ANALYSIS OF ALGORITHMS LABORATORY
void main()
{
int i,j;
printf("enter No. of cities:\n");
scanf("%d",&n);
printf("enter the cost matrix\n");
for(i=1;i<=n;i++)
{
printf("enter elements of row:%d\n",i );
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
visited[i]=0;
}
printf("the cost list is\n");
for(i=1;i<=n;i++)
{
printf("\n\n");
for(j=1;j<=n;j++)
printf("\t%d",a[i][j]);
}
printf("\n\n the path is :\n\n");
mincost(1);
printf("\n\n minimum cost:");
printf("%d",cost);
}
OUTPUT :
enter No. of cities:
4
enter the cost matrix
enter elements of row:1
0136
enter elements of row:2
1023
enter elements of row:3
3201
enter elements of row:4
6310
the cost list is
0136
1023
3201
6310
the path is :
1-->2-->4-->3-->1
minimum cost:8
………………………………………………………………………………………………………….
18
Department of Computer Science & Engineering, SCEM, Mangaluru.
CS422L5C:DESIGN AND ANALYSIS OF ALGORITHMS LABORATORY
9. Design and implement for a given chess board having N×N cells, place N queens on the board in such a
way that no queen attacks any other queen. If it is possible to place all the N queens in such a way that no
queen attacks another queen, then print N lines having N Queens. If there is more than one solution of
placing the queens, print all of them. If it is not possible to place all N queens in the desired way, then print
"Not possible".
…………………………………………………………………………………………………………………………
PROGRAM :
#include<stdio.h>
#include<stdlib.h>
int count=0,x[5];
void n_queen(int,int);
int place(int,int);
int main()
{
int n;
printf("Enter the number of queens\n");
scanf("%d",&n);
n_queen(1,n);
if(count==0)
printf("No solutions are found\n");
else
19
Department of Computer Science & Engineering, SCEM, Mangaluru.
CS422L5C:DESIGN AND ANALYSIS OF ALGORITHMS LABORATORY
OUTPUT:
Enter the number of queens
4
solution 1
0 $ 0 0
0 0 0 $
$ 0 0 0
0 0 $ 0
solution 2
0 0 $ 0
$ 0 0 0
20
Department of Computer Science & Engineering, SCEM, Mangaluru.
CS422L5C:DESIGN AND ANALYSIS OF ALGORITHMS LABORATORY
0 0 0 $
0 $ 0 0
………………………………………………………………………………………………………………………
VIVA QUESTIONS
1. What is an algorithm?
An algorithm is a sequence of unambiguous instructions for solving a problem. i.e., for obtaining a
required output for any legitimate input in a finite amount of time.
2. What are important problem types? (or) Enumerate some important types of problems.
1. Sorting 2. Searching 3. Numerical problems 4. Geometric problems 5. Combinatorial Problem
6. Graph Problems 7. String processing Problems
21
Department of Computer Science & Engineering, SCEM, Mangaluru.
CS422L5C:DESIGN AND ANALYSIS OF ALGORITHMS LABORATORY
Time efficiency indicates how fast the algorithm runs. An algorithm’s time efficiency is measured
as a function of its input size by counting the number of times its basic operation(running time) is
executed. Basic operation is the most time consuming operation in the algorithm’s innermost loop.
22
Department of Computer Science & Engineering, SCEM, Mangaluru.
CS422L5C:DESIGN AND ANALYSIS OF ALGORITHMS LABORATORY
23
Department of Computer Science & Engineering, SCEM, Mangaluru.
CS422L5C:DESIGN AND ANALYSIS OF ALGORITHMS LABORATORY
1 The tree’s shape requirement – The binary tree is essentially complete ( or simply complete),
that is, all its levels are full except possibly the last level, where only some rightmost leaves may
be missing.
2. The parental dominance requirement – The key at each node is greater than or equal to the
keys at its children
24
Department of Computer Science & Engineering, SCEM, Mangaluru.
CS422L5C:DESIGN AND ANALYSIS OF ALGORITHMS LABORATORY
A weighted graph is a graph with numbers assigned to its edges. These numbers are called weights
or costs.
48) When do you say that a Quick sort having best case complexity?
When the pivot exactly divides the array into equal half
49) When do you say that Quick sort having worst case complexity?
When any one of the subarray is empty
50) How many more comparisons are needed for average case compared to best case?
38% more
25
Department of Computer Science & Engineering, SCEM, Mangaluru.
CS422L5C:DESIGN AND ANALYSIS OF ALGORITHMS LABORATORY
51) when do you say that the pivot element is in its final position?
When all the elements towards the left of pivot are <= pivot and all the elements towards the right
of pivot are >= pivot.
57) What are the various applications of BFS & DFS tree traversal technique?
Application of BFS
Checking connectivity and checking acyclicity of a graph.
Check whether there is only one root in the BFS forest or not.
If there is only one root in the BFS forest, then it is connected graph otherwise disconnected
graph.
For checking cycle presence, we can take advantage of the graph's representation in the form of a
BFS forest, if the latter vertex does not have cross edge, then the graph is acyclic.
Application of DFS
To check whether given graph is connected or not.
To check whether the graph is cyclic or not.
To find the spanning tree.
Topological sorting.
58) Which data structures are used in BFS & DFS tree traversal technique?
We use Stack data structure to traverse the graph in DFS traversal. We use queue data structure to
traverse the graph in BFS traversal.
26
Department of Computer Science & Engineering, SCEM, Mangaluru.
CS422L5C:DESIGN AND ANALYSIS OF ALGORITHMS LABORATORY
Both solve the problems by dividing the problem into sub problems. Using the solutions of sub
problems, the solutions for larger instance of the problem can be obtained.
27
Department of Computer Science & Engineering, SCEM, Mangaluru.
CS422L5C:DESIGN AND ANALYSIS OF ALGORITHMS LABORATORY
77) What are the requirements that are needed for performing Backtracking?
Complex set of constraints. They are:
i. Explicit constraints.
ii. Implicit constraints.
GREEDY METHOD
1. Explain the greedy method.
Greedy method is the most important design technique, which makes a choice that looks best
atthat moment. A given ‘n’ inputs are required us to obtain a subset that satisfies some constraints
that is the feasible solution. A greedy method suggests that one candevice an algorithm that works
in stages considering one input at a time.
4. Specify the algorithms used for constructing Minimum cost spanning tree.
a) Prim’s Algorithm
b) Kruskal’s Algorithm
28
Department of Computer Science & Engineering, SCEM, Mangaluru.
CS422L5C:DESIGN AND ANALYSIS OF ALGORITHMS LABORATORY
DYNAMIC PROGRAMMING
1. Define multistage graph
A multistage graph G =(V,E) is a directed graph in which the vertices are partitioned into K>=2
disjoint sets Vi,1<=i<=k.The multi stage graph problem is to find a minimum cost paths from
s(source ) to t(sink)
Two approach(forward and backward)
29
Department of Computer Science & Engineering, SCEM, Mangaluru.