L11 Brute Force and Greedy Approach
L11 Brute Force and Greedy Approach
Algorithm
ENCS202
Dr. Aarti
Faculty
School of Engineering & Technology
K.R. Mangalam University
3
Brute Force Method
Advantages Disadvantages
• Applicable for wide variety of • Rarely yields efficient algorithms.
problems. • Not as creative as other design
• It is very useful for solving small techniques.
size instances of a problem, even • Brute force algorithms are
tough it is inefficient. unacceptably slow. e.g. Bubble
sort.
4
Greedy Algorithm
5
1. Greedy Choice Property
If an optimal solution to the problem can be found by choosing the best
choice at each step without reconsidering the previous steps once chosen,
the problem can be solved using a greedy approach. This property is called
greedy choice property.
2. 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. This property is called optimal substructure.
6
Greedy Approach
1. Let's start with the root node 20. The weight of the right child is 3 and the weight of the left
child is 2.
2. Our problem is to find the largest path. And, the optimal solution at the moment is 3. So,
the greedy algorithm will choose 3.
3. Finally the weight of an only child of 3 is 1. This gives us our final result 20 + 3 + 1 = 24.
However, it is not the optimal solution. There is another path that carries more weight (20
+ 2 + 10 = 32)
7
8
Greedy Algorithm
9
The Coin Problem
Problem: You have to make a change of an amount using the smallest possible
number of coins
Amount: $18
Available coins are:
• $5 coin
• $2 coin
• $1 coin
There is no limit to the number of each coin you can use.
Solution
1. Create an empty solution-set= { }. Available coins are {5, 2, 1}.
2. We are supposed to find the sum=18. Let’s start with sum=0.
3. Always select the coin with the largest value (i.e. 5) until the sum>18.
(When we select the largest value at each step, we hope to reach the destination faster.
This concept is called greedy choice property.)
4. In the first iteration, solution-set= {5} and sum=5.
5. In the second iteration, solution-set= {5, 5} and sum=10.
6. In the third iteration, solution-set= {5, 5, 5} and sum=15.
7. In the fourth iteration, solution-set= {5, 5, 5, 2} and sum=17.
(We cannot select 5 here because if we do so, sum=20 which is greater than 18. So, we
select the 2nd largest item which is 2.)
8. Similarly, in the fifth iteration, select 1. Now sum=18 and solution-set= {5, 5, 5, 2, 1}.
11
Greedy Algorithm
• Follows local optimal choice at each stage with intend of finding global
optimum.
• Feasible Solution (Selection)
• Optimal Solution
Minimum Cost
Maximum Profit
Minimum Risk
12
The Jumping Frog
13
The Approach
14
The Algorithm
15
The Solution
16
What is an Optimization Model?
17
Optimisation Problem
An optimization problem is one in which you want to find, not just a solution, but the best
solution.
Example: Suppose you want to count out a certain amount of money, using the fewest
possible bills and coins. How will you approach it?
At each step, take the largest possible bill or coin that does not overshoot.
18
Greedy Algorithms
• A greedy algorithm is an algorithm that constructs an object X one step at a time, at each
step choosing the locally best option.
• At each phase:
• You take the best you can get right now, without regard for future consequences.
• You hope that by choosing a local optimum at each step, you will end up at a global
optimum.
19
Greedy Algorithms
• Characteristics
• Greedy Algorithms are short- sighted.
• Greedy Algorithms are most efficient if it works.
• For every instance of input Greedy Algorithms make a decision and continues to
process further set of input.
• The other input values at the instance of decision are lost and not used in further
processing.
20
Greedy Advantages
Simplicity
Easy to Analyse
• Analyzing the run time for greedy algorithms will generally be much easier than for
other techniques
21
Greedy Challenges
Hard to verify : Proving that a greedy algorithm is correct is more of an
art than a science.
22
Where to use Greedy?
Optimal
Sub-structure Greedy Choice
Property
23
OPTIMAL SUB-STRUCTURE: A problem exhibits optimal substructure if
an optimal solution to the problem contains within its optimal solutions to subproblems.
Ex: That is, if the shortest route from Seattle to Los Angeles passes through Portland and
then Sacramento, then the shortest route from Portland to Los Angeles must pass
through Sacramento too.
GREEDY CHOICE PROPERTY: A globally optimal solution can be arrived at by making a
locally optimal (greedy) choice.
24
Greedy Method Applications
1. CPU
Scheduling
Algorithm
2. Minimum
8. Bin Packing
Spanning
Problem
Trees
Applications 3. Dijkstra’s
7. Egyptian
Fraction of Greedy Shortest Path
Algorithm
Method
4. Fit
6. Fractional
Algorithm in
Knapsack
Memory
Problem
5. Travelling Management
Salesman
Problem
25
Recap
Brute Force Method:
• A straightforward approach that checks all possible solutions.
• Applicable for small problem instances, though inefficient for large datasets.
• Examples: Bubble Sort, Exhaustive Search.
Greedy Algorithm:
• Solves problems by selecting the locally optimal choice at each step.
• Characteristics: Greedy Choice Property and Optimal Substructure.
• May not always lead to the global optimum but efficient for specific problems.
• Examples: Coin Change Problem, Activity Selection, Fractional Knapsack.
Recap
Optimization Models:
• Aim to maximize or minimize an objective function under certain constraints.
Key Differences:
• Brute Force: Tries all possibilities (guarantees correctness but inefficient).
• Greedy: Makes decisions step-by-step (efficient but not always optimal).
Test Your Knowledge
• Which of the following is a characteristic of the brute force approach?
a) Always efficient
b) Uses problem-specific strategies
c) Checks all possible solutions
d) Guarantees minimum time complexity
• What is the time complexity of the brute force approach in the worst case?
a) O(1)
b) O(log n)
c) O(n)
d) Exponential or factorial time
• Which of the following problems can be efficiently solved using a greedy algorithm?
a) Traveling Salesman Problem
b) Fractional Knapsack Problem
c) Sudoku Solver
d) N-Queens Problem
Test Your Knowledge
• Which of the following is a characteristic of the brute force approach?
a) Always efficient
b) Uses problem-specific strategies
c) Checks all possible solutions
d) Guarantees minimum time complexity
• What is the time complexity of the brute force approach in the worst case?
a) O(1)
b) O(log n)
c) O(n)
d) Exponential or factorial time
• Which of the following problems can be efficiently solved using a greedy algorithm?
a) Traveling Salesman Problem
b) Fractional Knapsack Problem
c) Sudoku Solver
d) N-Queens Problem
Test Your Knowledge
• The Greedy Choice Property means:
a) Making choices randomly
b) Always considering future consequences
c) Making locally optimal choices at each step
d) Choosing the most complex option
• Which condition must hold true for a greedy algorithm to produce an optimal solution?
a) Linear recursion
b) Greedy Choice Property and Optimal Substructure
c) Divide and Conquer approach
d) Dynamic Programming principles
Test Your Knowledge
• The Greedy Choice Property means:
a) Making choices randomly
b) Always considering future consequences
c) Making locally optimal choices at each step
d) Choosing the most complex option
• Which condition must hold true for a greedy algorithm to produce an optimal solution?
a) Linear recursion
b) Greedy Choice Property and Optimal Substructure
c) Divide and Conquer approach
d) Dynamic Programming principles
Test Your Knowledge
• Which of the following is NOT an advantage of the greedy algorithm?
a) Simpler implementation
b) Efficient for large datasets
c) Always guarantees the global optimal solution
d) Fast execution for certain problems
• In the Activity Selection Problem, the greedy algorithm selects activities based on:
a) Start time
b) Duration
c) Latest finish time
d) Earliest finish time
• In the Activity Selection Problem, the greedy algorithm selects activities based on:
a) Start time
b) Duration
c) Latest finish time
d) Earliest finish time
Cormen, T. H., Leiserson, C. E., Rivest, R. L., & Stein, C. Introduction to Algorithms (3rd Edition)
https://www.geeksforgeeks.org/
Kleinberg, J., & Tardos, É. Algorithm Design
THANK YOU