Monday, August 18, 2014

Write an algorithm such that if an element in an M*N matrix is 0, its entire row and column set to zero.



Program:


Connect n ropes with minimum cost: JAVA

Following is complete algorithm for finding the minimum cost for connecting n ropes.
Let there be n ropes of lengths stored in an array len[0..n-1]
1) Create a min heap and insert all lengths into the min heap.
2) Do following while number of elements in min heap is not one.
……a) Extract the minimum and second minimum from min heap
……b) Add the above two extracted values and insert the added value to the min-heap.
3) Return the value of only left item in min heap.

 For 5,6,7,8 is 11+15+26=52 and not 55.




References:
http://www.geeksforgeeks.org/connect-n-ropes-minimum-cost/