Greedy algorithm is used to solve a problem with a heuristic way, In many problems, a greedy strategy does not usually produce an optimal solution, but nonetheless a greedy heuristic may yield locally optimal solutions that approximate a globally optimal solution in a reasonable amount of time.
But sometimes it could yield a optimal solution when the problem could be fracted into some Subitem. For example Kruskal’s Minimum Spanning Tree Algorithm use the greedy
Kruskal Algorithm
the process of Kruskal Algorithm
1. Sort all the edges in non-decreasing order of their weight.
2. Pick the smallest edge. Check if it forms a cycle with the spanning tree formed so far. If cycle is not formed, include this edge. Else, discard it.
3. Repeat step#2 until there are (V-1) edges in the spanning tree.