0% found this document useful (0 votes)
25 views10 pages

Daalc

The document contains the lab assignments for the course IT351 Design & Analysis of Algorithms at R.V.R. & J.C. College of Engineering, Guntur. There are 11 programming problems covering topics like divide and conquer, greedy algorithms, dynamic programming, and backtracking. The problems include finding the minimum and maximum elements in an array, finding the kth smallest element, solving the knapsack problem, finding minimum spanning trees, shortest paths, string matching and more.

Uploaded by

shanmukn21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views10 pages

Daalc

The document contains the lab assignments for the course IT351 Design & Analysis of Algorithms at R.V.R. & J.C. College of Engineering, Guntur. There are 11 programming problems covering topics like divide and conquer, greedy algorithms, dynamic programming, and backtracking. The problems include finding the minimum and maximum elements in an array, finding the kth smallest element, solving the knapsack problem, finding minimum spanning trees, shortest paths, string matching and more.

Uploaded by

shanmukn21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

R.V.R. & J.C.

College of Engineering, Guntur – 522019


(Autonomous)
B. Tech. Semester-V [Third Year]
IT351 (R20) - Design & Analysis of Algorithms Lab

1. Write a program to find min-max in a given array using Divide and


Conquer.

#include <stdio.h>

void mimx(int a[], int l, int u, int *mi, int *mx)


{
int m, mi1, mx1;
if (l == u)
{
*mi = a[l];
*mx = a[l];
}
else if (l + 1 == u)
{
if (a[l] > a[u])
{
*mi = a[u];
*mx = a[l];
}
else
{
*mi = a[l];
*mx = a[u];
}
}
else
{
m = (l + u) / 2;
mimx(a, l, m, mi, mx);
mimx(a, m + 1, u, &mi1, &mx1);
if (mi1 < *mi)
*mi = mi1;
if (mx1 > *mx)
*mx = mx1;
}
}

int main()
R.V.R. & J.C. College of Engineering, Guntur – 522019
(Autonomous)
{
int i, n, a[10], mi = 0, mx = 0;
printf("enter list size:");
scanf("%d", &n);
printf("enter list:");
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
mimx(a, 0, n - 1, &mi, &mx);
printf("min=%d max=%d\n", mi, mx);
}

2. Write a program to find the kth smallest element in a given array using
Divide and Conquer.

#include <stdio.h>
int partition(int a[], int l, int u)
{
int i = l, j = u, k = a[l], t;
while (i < j)
{
while (a[i] <= k && i <= u)
i++;
while (a[j] > k)
j--;
if (i < j)
{
t = a[i];
a[i] = a[j];
a[j] = t;
}
}
a[l] = a[j];
a[j] = k;
return j;
}

int KthSmall(int a[], int l, int u, int k)


{
int p;
if (l < u)
{
p = partition(a, l, u);
if (p < k)
R.V.R. & J.C. College of Engineering, Guntur – 522019
(Autonomous)
return KthSmall(a, p + 1, u, k);
else if (p > k)
return KthSmall(a, l, p - 1, k);
else
return a[p];
}
}
int main()
{
int k, i, n, a[10];
printf("enter n value:");
scanf("%d", &n);
printf("enter array elements:");
for (i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
}
printf("enter k value:");
scanf("%d", &k);
printf("%dth minimum is %d\n", k, KthSmall(a, 1, n, k));
}
3. Write a program to find the optimal profit of a Knapsack using Greedy
method.

#include <stdio.h>
void read(int a[], int n)
{
int i;
printf("enter elements: ");
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
}
void print(int a[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d, ", a[i]);
printf("\n");
}

float Knapsack(int p[], int w[], int n, float m)


{
R.V.R. & J.C. College of Engineering, Guntur – 522019
(Autonomous)
float pro = 0.0;
int i = 0;
while (w[i] < m)
{
m -= w[i];
pro += p[i];
i++;
printf("%f\n", pro);
}
pro += (p[i] * (m / w[i]));

return pro;
}

void main()
{
int n, i, p[10], w[10];
float m;
printf("enter size: ");
scanf("%d", &n);
printf("enter profits 'sorted order('p/w')':");
read(p, n);
printf("enter weights:");
read(w, n);
printf("bag-size:");
scanf("%f", &m);
printf("profit:%.2f\n", Knapsack(p, w, n, m));
}

4. Write a program to find the minimum cost spanning tree for the given
graph. (Kruskal’s algorithm)

#include <stdio.h>
#include <stdlib.h>
struct edge
{
int v1, v2, w;
};

typedef struct edge edge;

int p[20];
edge e[20];
R.V.R. & J.C. College of Engineering, Guntur – 522019
(Autonomous)

void init(int n)
{
for (int i = 1; i <= n; i++)
p[i] = -1;
}

int find(int i)
{
while (p[i] != -1)
i = p[i];
return i;
}

void union1(int i, int j)


{
p[i] = j;
}

void sort(int m)
{
edge t;
for (int i = 1; i <= m; i++)
for (int j = 1; j <= m; j++)
if (e[i].w < e[j].w)
{
t = e[i];
e[i] = e[j];
e[j] = t;
}
}

void readedges(int m)
{
int i;
for (i = 1; i <= m; i++)
{
printf("Enter %d Edge details\n", i);
scanf("%d", &e[i].v1);
scanf("%d", &e[i].v2);
scanf("%d", &e[i].w);
}
}
R.V.R. & J.C. College of Engineering, Guntur – 522019
(Autonomous)

void printtree(int t[][2], int n)


{
int i, j;
printf("MST is\n");
for (i = 1; i < n; i++)
printf("%d\t%d\n", t[i][1], t[i][2]);
}

void kruskal(int n, int m)


{
int v, u, t[20][2], i = 0, j = 1, mincost = 0;
while (i < n && j < m)
{
u = find(e[j].v1);
v = find(e[j].v2);
if (u == v)
{
j++;
continue;
}
i++;
t[i][1] = e[j].v1;
t[i][2] = e[j].v2;
mincost += e[j].w;
union1(u, v);
j++;
}
printf("mincost:%d\n", mincost);
printtree(t, n);
}

void main()
{
int n, m;
printf("enter no.of vertices:");
scanf("%d", &n);
printf("enter no.of edges:");
scanf("%d", &m);
readedges(m);
sort(m);
init(n);
kruskal(n, m);
R.V.R. & J.C. College of Engineering, Guntur – 522019
(Autonomous)
}

5. Write a program to determine the shortest path length from a source


vertex to all the other vertices in a given graph. (Dijkstra’s algorithm)

#include <stdio.h>

void shp(int v, int g[10][10], int dist[10], int n)


{
int i, s[10], min, u, j, w;

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


{
s[i] = 0;
dist[i] = g[v][i];
}
s[v] = 1;

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


{
min = 999;
u = 0;
for (j = 1; j <= n; j++)
{
if (s[j] == 0 && dist[j] < min)
{
min = dist[j];
u = j;
}
}
if (u == 0)
break;
s[u] = 1;
for (w = 1; w <= n; w++)
{
if (g[u][w] != 0 && s[w] == 0)
{
if ((dist[u] + g[u][w]) < dist[w])
{
dist[w] = dist[u] + g[u][w];
}
}
}
R.V.R. & J.C. College of Engineering, Guntur – 522019
(Autonomous)
}
}

int main()
{
int cost[10][10], n, dist[10], i, j, v;

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


scanf("%d", &n);
printf("Enter the source vertex: ");
scanf("%d", &v);

printf("Enter the cost matrix:\n");


for (i = 1; i <= n; i++)
{
for (j = 1; j <= n; j++)
{
scanf("%d", &cost[i][j]);
}
}

shp(v, cost, dist, n);

printf("Shortest distances from vertex %d to all other vertices:\n", v);


for (i = 1; i <= n; i++)
{
printf("Vertex %d: %d\n", i, dist[i]);
}

return 0;
}

6. Write a program to determine shortest path from a source vertex s to


destination vertex t in a multistage graph using dynamic programming
(forward and backward).

7. Write a program to find shortest path distance between each and every
pair of vertices in a given graph using dynamic programming.

#include <stdio.h>

void read(int a[][10], int n)


{
printf("enter the graph(adj matrix):\n");
R.V.R. & J.C. College of Engineering, Guntur – 522019
(Autonomous)
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
scanf("%d", &a[i][j]);
}

void print(int a[][10], int n)


{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
printf("%d ", a[i][j]);
printf("\n");
}
}

void main()
{
int i, j, k, min1, min2, a[10][10], cost[10][10], n;
printf("enter no.of vertices in the graph:");
scanf("%d", &n);
read(cost, n);
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
a[i][j] = cost[i][j];
for (k = 0; k < n; k++)
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
{
min1 = a[i][k] + a[k][j];
min2 = a[i][j];
a[i][j] = min1 <= min2 ? min1 : min2;
}
printf("all pairs shortest path:\n");
print(a, n);
}

8. Write a program to find the optimal profit of a 0/1 Knapsack using


dynamic programming.

9. Write a program to find the non-attacking positions of Queens in a given


chess board using backtracking.

10.Write a program to find Hamiltonian cycles in a given graph using


backtracking.
R.V.R. & J.C. College of Engineering, Guntur – 522019
(Autonomous)
11.Write a program for Robin Karp string matching algorithm.

You might also like