Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
15 views11 pages

Daa Exp 4,5,6

The document outlines several experiments focused on developing programs to estimate minimum-cost spanning trees using greedy methods, single source shortest paths with Dijkstra's algorithm, and optimal binary search trees with dynamic programming. Each section includes source code and expected outputs for algorithms like Prim's and Kruskal's for MST, Dijkstra's for shortest paths, and a dynamic programming approach for binary search trees. The aim is to measure the running time and efficiency of these algorithms.
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)
15 views11 pages

Daa Exp 4,5,6

The document outlines several experiments focused on developing programs to estimate minimum-cost spanning trees using greedy methods, single source shortest paths with Dijkstra's algorithm, and optimal binary search trees with dynamic programming. Each section includes source code and expected outputs for algorithms like Prim's and Kruskal's for MST, Dijkstra's for shortest paths, and a dynamic programming approach for binary search trees. The aim is to measure the running time and efficiency of these algorithms.
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/ 11

Experinment-4: Develop a program and measure the running time for estimating minimum-cost spanning

Trees with Greedy Method


Aim: To develop a program and measure the running time for estimating minimum-cost spanning Trees
with Greedy Method.
Description:
The greedy method for finding a minimum cost spanning tree (MST) uses algorithms like kruskals algorithm and
prims algorithm .minimum spanning tree is a subset of a graph's edges that connects all the vertices with the
minimum total edge weight

Source code:

Prim’s Algorithm Code:

#include <limits.h>

#include <stdbool.h>

#include <stdio.h>

#define V 5

int minKey(int key[], bool mstSet[])

int min = INT_MAX, min_index;

for (int v = 0; v < V; v++)

if (mstSet[v] == false && key[v] < min) min = key[v], min_index = v;

return min_index;

int printMST(int parent[], int graph[V][V])

printf("Edge \tWeight\n");

for (int i = 1; i < V; i++)

printf("%d - %d \t%d \n", parent[i], i, graph[i][parent[i]]);

void primMST(int graph[V][V])

int parent[V];

int key[V];

bool mstSet[V];

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

key[i] = INT_MAX, mstSet[i] = false;

key[0] = 0;

parent[0] = -1;
for (int count = 0; count < V - 1; count++)

int u = minKey(key, mstSet);

mstSet[u] = true;

for (int v = 0; v < V; v++)

if (graph[u][v] && mstSet[v] == false && graph[u][v] < key[v])

parent[v] = u, key[v] = graph[u][v];

printMST(parent, graph);

int main()

int graph[V][V] = { { 0, 2, 0, 6, 0 },

{ 2, 0, 3, 8, 5 },

{ 0, 3, 0, 0, 7 },

{ 6, 8, 0, 0, 9 },

{ 0, 5, 7, 9, 0 } };

primMST(graph);

return 0;}
Output:
Edge Weight
0-1 2
1-2 3
0-3 6
1-4 5
Running time:
Kruskal’s Algorithm code:

#include <stdio.h>

#include <stdlib.h>

int comparator(const void* p1, const void* p2);

const int(*x)[3] = p1;

const int(*y)[3] = p2;

return (*x)[2] - (*y)[2];

void makeSet(int parent[], int rank[], int n)

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

parent[i] = i; rank[i] = 0;

int findParent(int parent[], int component)

if (parent[component] == component)

return component;

return parent[component]= findParent(parent, parent[component]);

void unionSet(int u, int v, int parent[], int rank[], int n)

u = findParent(parent, u);

v = findParent(parent, v);

if (rank[u] < rank[v])

parent[u] = v;

else if (rank[u] > rank[v])

parent[v] = u;

}
else {parent[v] = u;

rank[u]++;

void kruskalAlgo(int n, int edge[n][3])

qsort(edge, n, sizeof(edge[0]), comparator);

int parent[n];

int rank[n];

makeSet(parent, rank, n);

int minCost = 0;

printf("Following are the edges in the constructed MST\n");

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

int v1 = findParent(parent, edge[i][0]);

int v2 = findParent(parent, edge[i][1]);

int wt = edge[i][2];

if (v1 != v2)

unionSet(v1, v2, parent, rank, n);

minCost += wt;

printf("%d -- %d == %d\n", edge[i][0],edge[i][1], wt);

printf("Minimum Cost Spanning Tree: %d\n", minCost);

int main()

{
int edge[5][3] = { { 0, 1, 10},

{ 0, 2, 6 },

{ 0, 3, 5 },

{ 1, 3, 15},

{ 2, 3, 4 } };

kruskalAlgo(5, edge);
return 0;

Output:

Following are theedges in the constructed MST


2 -- 3 == 4
0 -- 3 == 5
0 -- 1 == 10
Minimum Cost Spanning Tree: 19
Running time:
Experinment-5: Develop a program and measure the running time for estimating Single Source Shortest Paths
with Greedy Method.

Aim: To Develop a program and measure the running time for estimating Single Source Shortest Paths with
Greedy Method.
Description:
Dijkstra's algorithm is a greedy algorithm that uses a greedy approach to find the shortest path from a source
node to all other nodes in a graph. It's also known as the single source shortest path algorithm.

Source Code:
#include<stdio.h>

#include<conio.h>

#define INFINITY 9999

#define MAX 10

void dijkstra(int G[MAX][MAX],int n,int startnode);

int main()

int G[MAX][MAX],i,j,n,u;

printf("Enter no. of vertices:");

scanf("%d",&n);

printf("\nEnter the adjacency matrix:\n");

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

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

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

printf("\nEnter the starting node:");

scanf("%d",&u);

dijkstra(G,n,u);

getch();

return 0;

}
void dijkstra(int G[MAX][MAX],int n,int startnode)

int cost[MAX][MAX],distance[MAX],pred[MAX];

int visited[MAX],count,mindistance,nextnode,i,j;

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

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

if(G[i][j]==0) cost[i][j]=INFINITY;

else cost[i][j]=G[i][j];

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

distance[i]=cost[startnode][i];

pred[i]=startnode;

visited[i]=0;

distance[startnode]=0;

visited[startnode]=1;

count=1;

while(count<n-1)

mindistance=INFINITY;

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

if(distance[i]<mindistance&&!visited[i])

mindistance=distance[i];

nextnode=i;

visited[nextnode]=1;

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

if(!visited[i])

if(mindistance+cost[nextnode][i]<distance[i])

distance[i]=mindistance+cost[nextnode][i];
pred[i]=nextnode;

count++;

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

if(i!=startnode)

printf("\nDistance of node%d=%d",i,distance[i]);

printf("\nPath=%d",i);

j=i;

do

j=pred[j];

printf("<-%d",j);

while(j!=startnode);

Output:
Experinment-6: Develop a program and measure the running time for optimal Binary search trees withDynamic
Programming

Aim: To develop a program and measure the running time for optimal Binary search trees with Dynamic
Programming
Description:

Source Code:

#include<stdio.h>
#include<conio.h>
#define MAX 10
void main()

{ char ele[MAX][MAX];
int w[MAX][MAX], c[MAX][MAX], r[MAX][MAX], p[MAX], q[MAX];

int temp=0, root, min, min1, n;


int i,j,k,b;

clrscr();

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


scanf("%d",&n);

printf("\n");
for(i=1; i <= n; i++) {

printf("Enter the Element of %d:" ,i);


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

printf("\n");
for(i=0; i <= n; i++){

printf("Enter the Probability of %d:" ,i);


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

printf("W\t\tC\t\tR\n");
for(i=0; i <= n; i++)

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

{if(i == j)

{w[i][j] = q[i];

c[i][j] = 0;

r[i][j] = 0;

printf("W[%d][%d]: %d\tC[%d][%d]: %d\tR[%d][%d]:


%d\n",i,j,w[i][j],i,j,c[i][j],i,j,r[i][j]); }}}
printf("\n");

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

{for(i=0,j=b+1; j < n+1 && i < n+1; j++,i++)

{if(i!=j && i < j)


{w[i][j] = p[j] + q[j] + w[i][j-1];
min = 30000;

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


{min1 = c[i][k-1] + c[k][j] + w[i][j];
if(min > min1)

{min = min1;

temp = k;} }
c[i][j] = min;

r[i][j] = temp;

printf("W[%d][%d]: %d\tC[%d][%d]: %d\tR[%d][%d]:


%d\n",i,j,w[i][j],i,j,c[i][j],i,j,r[i][j]);}
printf("\n"); }

printf("Minimum cost = %d\n",c[0][n]);


root = r[0][n];

printf("Root = %d \n",root);
getch();

Output:
Enter the number of elements:6

Enter the Element of 1:10


Enter the Element of 2:3
Enter the Element of 3:9
Enter the Element of 4:2
Enter the Element of 5:0
Enter the Element of 6:10

Enter the Probability of 0:5


Enter the Probability of 1:6
Enter the Probability of 2:4
Enter the Probability of 3:4
Enter the Probability of 4:3
Enter the Probability of 5:8
Enter the Probability of 6:0
W C R
W[0][0]: 5 C[0][0]: 0 R[0][0]: 0
W[1][1]: 6 C[1][1]: 0 R[1][1]: 0
W[2][2]: 4 C[2][2]: 0 R[2][2]: 0
W[3][3]: 4 C[3][3]: 0 R[3][3]: 0
W[4][4]: 3 C[4][4]: 0 R[4][4]: 0
W[5][5]: 8 C[5][5]: 0 R[5][5]: 0
W[6][6]: 0 C[6][6]: 0 R[6][6]: 0

W[0][1]: 21 C[0][1]: 21 R[0][1]: 1


W[1][2]: 13 C[1][2]: 13 R[1][2]: 2
W[2][3]: 17 C[2][3]: 17 R[2][3]: 3
W[3][4]: 9 C[3][4]: 9 R[3][4]: 4
W[4][5]: 11 C[4][5]: 11 R[4][5]: 5
W[5][6]: 18 C[5][6]: 18 R[5][6]: 6

W[0][2]: 28 C[0][2]: 41 R[0][2]: 1


W[1][3]: 26 C[1][3]: 39 R[1][3]: 3
W[2][4]: 22 C[2][4]: 31 R[2][4]: 3
W[3][5]: 17 C[3][5]: 26 R[3][5]: 5
W[4][6]: 21 C[4][6]: 32 R[4][6]: 6

W[0][3]: 41 C[0][3]: 79 R[0][3]: 2


W[1][4]: 31 C[1][4]: 53 R[1][4]: 3
W[2][5]: 30 C[2][5]: 56 R[2][5]: 3
W[3][6]: 27 C[3][6]: 53 R[3][6]: 6

W[0][4]: 46 C[0][4]: 96 R[0][4]: 3


W[1][5]: 39 C[1][5]: 78 R[1][5]: 3
W[2][6]: 40 C[2][6]: 89 R[2][6]: 4

W[0][5]: 54 C[0][5]: 121 R[0][5]: 3


W[1][6]: 49 C[1][6]: 115 R[1][6]: 3

W[0][6]: 64 C[0][6]: 158 R[0][6]: 3

Minimum cost = 158


Root = 3

You might also like