# Introduction to Greedy Strategy in Algorithms

In this article, we are going to discuss about the **introduction of greedy strategy, algorithm for greedy strategy, some applications and the elements of greedy strategy in Analysis and Design of Algorithms**.

Submitted by Prerana Jain, on June 21, 2018

### Introduction

The solution is determined by a sequence of steps each step has given a particular solution and later a complete solution to given the problem can be achieved. In short, while making a choice there should be a greed for the optimum solution. **Some points about Greedy strategy**:

- Look for the optimal solution and assumes it as best.
- Solves the sub-problems in Top-down manner.
- This approach is less powerful programming techniques.
- It is not applicable to a wider area like dynamic programming approach.
- It is useful for solving optimization problems.
- It generates only one solution sequences.

**In greedy Strategy following activities are performed,**

- First, select some solutions from input domain.
- Then check whether the solution is feasible or not.
- In this, we find an optimum solution which satisfies the objective of the function and it can be obtained from a particular solution out of the set of feasible solution.
- As greedy method works in stages only one stage is considered at a time. Based on this input it is decided whether a particular input is given the optimal solution or not.

## Algorithm for Greedy Strategy

In greedy approach D is domain, from which solution is to be obtained of size n...

Initially assumeSolution ← 0 For i ← 1 to n do { S ← select(D) // section of solution from D If (Feasible (solution) then Solution ← Union (solution, s); } Return solution

### Elements of Greedy Strategy

**Greedy Choice property**

- A global optimal solution can be arrived by local optimal choice.
- For finding the solutions to the problem the subproblems are solved and best from these sub-problems is considered.
- This choice may depend upon the previously made choices but it does not depend on any future choice.
- Thus in the greedy method, greedy choices are made one after the another, reducing each given problem instances to smaller one.

**Optimal sub-structure**

- If an optimal solution to the problem containing the optimal solution to the subproblem then the problem shows an optimal substructure.
- A problem has optimal substructure if has been next choices always leads to an optimal solution.

### Applications of greedy method

Problems that can be solved by greedy approach,

- Knapsack problem
- Prim’s algorithm for minimum spanning tree.
- Kruskal’s algorithm for minimum spanning tree.
- Finding shortest job
- Job sequencing with deadlines
- Optimal storage on taps
- Huffman coding

**Feasible solution**

The set of values for the decision problem which satisfies all of the constraints of an optimization problem then the solution is called feasible solution. Feasible solution satisfy all linear and non-linear constraints. The set of all feasible solution defines the feasible region of the problem. To solve a problem first find anyone feasible solution and then try to find another feasible solution which satisfies the values of the objective functions. The whole process is repeated until when no further improvement is achieved or other criteria are met.

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions

**Ad:**
Are you a blogger? Join our Blogging forum.