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

SVKM'S Nmims Mukesh Patel School of Technology Management & Engineering Computer Engineering Department

1. The document describes an experiment on implementing greedy algorithms to solve optimization problems. 2. It provides examples of using Prim's and Kruskal's algorithms to find the minimum spanning tree for supplying water to buildings to minimize pipe costs. 3. The student implemented Kruskal's algorithm to solve the problem, outlining the steps and providing code and output.

Uploaded by

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

SVKM'S Nmims Mukesh Patel School of Technology Management & Engineering Computer Engineering Department

1. The document describes an experiment on implementing greedy algorithms to solve optimization problems. 2. It provides examples of using Prim's and Kruskal's algorithms to find the minimum spanning tree for supplying water to buildings to minimize pipe costs. 3. The student implemented Kruskal's algorithm to solve the problem, outlining the steps and providing code and output.

Uploaded by

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

SVKM’s NMIMS

Mukesh Patel School of Technology Management & Engineering


Computer Engineering Department
Program: B.Tech / MBA Tech Sem V

Course: Design and Analysis of Algorithms


w.e.f. 1st July 2020

Faculty: Manisha Tiwari

LAB Manual

PART A
(PART A : TO BE REFFERED BY STUDENTS)

Experiment No.04
A.1 Aim: Implement Greedy Approach Design paradigm.
Task 1:

A construction company wants to supply water to all the buildings in the society. Can you help
them to minimize the cost of pipes with the help of greedy approach and knowledge of minimum
spanning tree.

Task 2:

Differentiate between approach of Prim’s and Kruskal algorithm.

A.2 Prerequisite:
 Knowledge of greedy techniques,
 Arrays and handling of arrays.

A.3 Outcome:
After successful completion of this experiment students will be able to

1. Design & implement a solution using Greedy Technique.


2. Identify different problems that can be solved by using Greedy Technique.
3. Identify applications of Knapsack Problem.

A.4 Theory:
General Concept of Greedy Technique of Algorithm Design

 An optimization problem is one in which you want to find, not just a solution, but the best solution
 A “greedy algorithm” sometimes works well for optimization problems
 A greedy algorithm works in phases. At each phase:
 You take the best you can get right now, without regard for future consequences
 You hope that by choosing a local optimum at each step, you will end up at a global optimum
A spanning tree of a graph is just a subgraph that contains all the vertices and is a tree.
A graph may have many spanning trees. Different possible spanning trees for a given graph are as shown in
Fig. 1.

Figure 1: Graph & Its Spanning Trees

Minimum Spanning Trees

The Minimum Spanning Tree for a given graph is the Spanning Tree of minimum cost for that graph.

Figure 2: A graph & Its spanning tree


A.5 Procedure/Algorithm:
1. Initialize a tree with a single vertex, chosen arbitrarily from the graph.

2. Grow the tree by one edge: of the edges that connect the tree to vertices not yet in the tree, find the
minimum-weight edge, and transfer it to the tree.

3.Repeat step 2 (until all vertices are in the tree).

**********************

PART B
(PART B : TO BE COMPLETED BY STUDENTS)

(Students must submit the soft copy as per following segments within two hours of the practical.
The soft copy must be uploaded on the Portal.)

Program: MBA.Tech (CS) Sem: 5th


Roll No. N047 Name: Hemit Shah
Division: B Batch: B3
Date of Experiment: 11/08/2021 Date of Submission: 25/08/2021
Grade:

TASK 1:
Question) A construction company wants to supply water to all the buildings in the society. Can you
help
them to minimize the cost of pipes with the help of greedy approach and knowledge of minimum
spanning tree.

Answer)
 A, B, D, E, F, G are buildings in a complex and C is the central tank from which all the
buildings receive water.
 Building G receives water from E and F, it is not connected to main central tank (C)
 Weights are mentioned on the edges and all the steps followed to make the MST are also
shown in the given 2 pages
 We used Kruskal’s Algorithm to make the Minimum Spanning Tree (MST)
 Total weight of MST is also mentioned on page 2
Page 1:
Page 2:
CODE:

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

int i,j,k,a,b,u,v,n,ne=1;

int min,mincost=0,cost[9][9],parent[9];

int find(int);

int uni(int,int);

void main()

printf("\n\tImplementation of Kruskal's algorithm\n");

printf("\nEnter the no. of vertices:");

scanf("%d",&n);

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

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

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

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

if(cost[i][j]==0)

cost[i][j]=999;

printf("The edges of Minimum Cost Spanning Tree are\n");

while(ne < n)
{

for(i=1,min=999;i<=n;i++)

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

if(cost[i][j] < min)

min=cost[i][j];

a=u=i;

b=v=j;

u=find(u);

v=find(v);

if(uni(u,v))

printf("%d edge (%d,%d) =%d\n",ne++,a,b,min);

mincost +=min;

cost[a][b]=cost[b][a]=999;

printf("\n\tMinimum cost = %d\n",mincost);

getch();

int find(int i)
{

while(parent[i])

i=parent[i];

return i;

int uni(int i,int j)

if(i!=j)

parent[j]=i;

return 1;

return 0;

OUTPUT:
TASK 2:

Question) Differentiate between approach of Prim’s and Kruskal algorithm.

Answer)

Prim’s Algorithm Kruskal’s Algorithm

The tree that we are making or growing The tree that we are making or growing usually
always remains connected. remains disconnected.

It starts to build the MST from any It starts to build the MST from the vertex
vertex in graph carrying the minimum weight
Prim’s Algorithm grows a solution from Kruskal’s Algorithm grows a solution from the
a random vertex by adding the next cheapest edge by adding the next cheapest
cheapest vertex to the existing tree edge to the existing tree / forest.

Traverses one node more than one


Traverses only one node once
time to get minimum distance

Kruskal’s algorithm can generate forest


Prims algorithm gives connected
(disconnected components) at any instant as
component as well as it works only on
well as it can work on disconnected
connected graph
components

Prim’s Algorithm is faster for dense


Kruskal’s Algorithm is faster for sparse graphs.
graphs.

 
B.3 Observations and learning:
We learned about Prims and Kruskal’s algorithm to create minimum spanning tree (MST) and also understood
how costly the MST will be depending on the weights of the edges.

B.4 Conclusion:
We designed & implemented a solution using Kruskal’s Greedy Technique. We identified different problems
that can be solved by using Greedy Technique.

B.5 Question of Curiosity

Q) Why greedy approach cannot be applied on 0/1 knapsack problem?

A) In this item cannot be broken which means thief should take the item as a whole or should leave
it. That's why it is called 0/1 knapsack Problem.

o Each item is taken or not taken.


o Cannot take a fractional amount of an item taken or take an item more than once.
o It cannot be solved by the Greedy Approach because it is enabled to fill the knapsack to capacity.
o Greedy Approach doesn't ensure an Optimal Solution.

************************

You might also like