306 views
Farmer Raju has been elected mayor of his town!! One of his campaign promises was to bring internet connectivity to all farms in the area. He need someone help, of course.

Raju ordered a high speed connection for his farm and is going to share his connectivity with other farmers. To minimize cost, he wants to lay the minimum amount of optical fiber to connect his farm to all the other farms. Given a list f how much fiber it takes to connect each pair of farms, problem is to find out minimum amount of fiber needed to connect them all together. Each farm must connect to some other farm such that a packet can flow from any one farm to any other farm.

Consider the farms as the vertices of a graph.

Let us assume that to join the villages V1 and V2 by an optical fibre needs f fibres.

So, in our graph we join the vertices V1 and V2 by an edge and put the weight f on it.

Now,apply kruskal algorithm on the graph to find the minimum weight spanning tree.

The summation of the edge weights of the minimum spanning tree is the reqd minimum number of fibres needed to connect the village with optical fibre.

srestha  We need the overall cost to be minimal but Floyd Warshall will give minimum cost between each pair of vertices but when you add them all, the overall cost won't be minimized.
Check the above graph-
Cost of MST will be 8
But Floyd Warshall Algorithm will give 14.

why u use kruskal ? Why not dijkstra or any other single source?

Yes u can use anything the condition is it should compute mst.