# Correctness of Greedy Algorithms

A greedy algorithm selects a candidate greedily (local optimum) and adds it to the current solution provided that it doesn’t corrupt the feasibility. If the solution obtained by above step is not final, repeat till global optimum or the final solution is obtained.

Although there are several mathematical strategies available to proof the correctness of Greedy Algorithms, we will try to proof it intuitively and use method of contradiction.

Greedy Algorithm usually involves a sequence of choices.Greedy algorithms can’t backtrack,hence once they make a choice, they’re committed to it. So it’s critical that they never make a bad choice.

Suppose **S** be the solution obtained by applying greedy algorithm to a problem and **O** be the optimum solution to the problem. If both **S** and **O** are same then our algorithm is by default correct. If **S** and **O** are different then clearly while stacking up various local solutions for the problem we made a mistake and chose a less efficient solution which resulted in **S** rather than **O** as a solution. But according to the definition of greedy algorithms we always choose the local optimum solution.

Hence using proof by contradiction it can said that greedy algorithm gives the correct solution.

The above proof can be understood better with help of Krushkal’s Algorithm.

**Kruskal’s Algorithm:**

This is a greedy algorithm used to find the minimum spanning tree of a graph.

Kruskal’s algorithm can be stated as follows:

*0. Create a minimum spanning tree T that initially contains no edges,
1. Choose an edge e in G, where
(a) e is not in T and …
(b) e is of minimum weight and …
(c) e does not create a cycle in T
2. If T does not contain all the vertices of G go to step 1.*

Let **T** be a the tree obtained and **S** be the desired tree such that **W(T) > W(S)**.

Clearly for this to happen, at some point of time we chose an edge which is not having a minimum weight, but according to the above algorithm we always choose the minimum weight edges. Hence Krushkal’s Algorithm will always give the correct result. Note that a Minimum Spanning Tree of V vertices must have at least V-1 edges and should not contain cycle. So we did not pick any extra edge in above step.

This article is contributed **Vineet Joshi**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.