SVKM'S Nmims Mukesh Patel School of Technology Management & Engineering Computer Engineering Department
SVKM'S Nmims Mukesh Patel School of Technology Management & Engineering Computer Engineering Department
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:
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
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.
The Minimum Spanning Tree for a given graph is the Spanning Tree of minimum cost for that 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.
**********************
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.)
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()
scanf("%d",&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;
while(ne < n)
{
for(i=1,min=999;i<=n;i++)
min=cost[i][j];
a=u=i;
b=v=j;
u=find(u);
v=find(v);
if(uni(u,v))
mincost +=min;
cost[a][b]=cost[b][a]=999;
getch();
int find(int i)
{
while(parent[i])
i=parent[i];
return i;
if(i!=j)
parent[j]=i;
return 1;
return 0;
OUTPUT:
TASK 2:
Answer)
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.
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.
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.
************************