C program for optimal merge pattern using greedy method

broken image

There is another path that carries more weight ( 20 + 2 + 10 = 32) as shown in the image below. This gives us our final result 20 + 3 + 1 = 24. Finally the weight of an only child of 3 is 1. So, the greedy algorithm will choose 3.ģ. And, the optimal solution at the moment is 3. The weight of the right child is 3 and the weight of the left child is 2.Ģ. Apply greedy approach to this tree to find the longest routeġ.

broken image

This is the major disadvantage of the algorithmįor example, suppose we want to find the longest path in the graph below from root to leaf. This algorithm can perform better than other algorithms (but, not in all cases).Īs mentioned earlier, the greedy algorithm doesn't always produce the optimal solution.This property is called optimal substructure. If the optimal overall solution to the problem corresponds to the optimal solution to its subproblems, then the problem can be solved using a greedy approach. Decrease Key and Delete Node Operations on a Fibonacci Heap.

broken image