0% found this document useful (0 votes)
43 views35 pages

L11 Brute Force and Greedy Approach

The document discusses the Brute Force and Greedy approaches in algorithm design, highlighting their characteristics, advantages, and challenges. The Brute Force method checks all possible solutions but is inefficient for larger problems, while the Greedy algorithm makes locally optimal choices at each step, which may not always lead to the global optimum. Examples of applications and optimization models are provided, along with a recap of key differences between the two approaches.

Uploaded by

Tejas Dahiya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views35 pages

L11 Brute Force and Greedy Approach

The document discusses the Brute Force and Greedy approaches in algorithm design, highlighting their characteristics, advantages, and challenges. The Brute Force method checks all possible solutions but is inefficient for larger problems, while the Greedy algorithm makes locally optimal choices at each step, which may not always lead to the global optimum. Examples of applications and optimization models are provided, along with a recap of key differences between the two approaches.

Uploaded by

Tejas Dahiya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Analysis and Design Of

Algorithm

ENCS202

Dr. Aarti
Faculty
School of Engineering & Technology
K.R. Mangalam University

Unit 2 : Brute Force and Greedy Approach


Brute Force and Greedy Approach
Session 11
 Brute Force Method
 Optimization Model
 Greedy Algorithm Characteristics
 Greedy Advantages
 Greedy Challenges
 Need of Greedy Approach
Brute Force Method
• The brute force method is a straightforward approach to problem-solving that
involves checking all possible solutions to find the correct one.
• Does not rely on any complex algorithms, shortcuts, or clever tricks to solve a
problem.
• Just tries every possibility until it finds the right solution.
• The term "brute force" has two components:
 The first part "brute", suggests that the method is basic and doesn't involve
any cleverness, advanced strategies, complex algorithms or shortcuts.
 The second part "force", implies that the method uses sheer computational
power to try every possible solution until it finds the correct one. It means
that the method uses the computer's power to find the answer.

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

• A greedy algorithm is an approach for solving a problem by selecting the


best option available at the moment. It doesn't worry whether the current
best result will bring the overall optimal result.
• The algorithm never reverses the earlier decision even if the choice is
wrong. It works in a top-down approach.
• This algorithm may not produce the best result for all the problems. It's
because it always goes for the local best choice to produce the global best
result.

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

1. To begin with, the solution set (containing answers) is empty.


2. At each step, an item is added to the solution set until a solution is
reached.
3. If the solution set is feasible, the current item is kept.
4. Else, the item is rejected and never considered again.

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

• The frog begins at position 0 in the river.


Its goal is to get to position n.
• There are lily pads at various positions.
There is always a lily pad at position 0
and position n.
• The frog can jump at most r units at a
time.

Goal: Find the path the frog should take to


minimize jumps, assuming a solution exists.

13
The Approach

Always jump as far forward as possible.

14
The Algorithm

1. Let J be an empty series of jumps.


2. Let our current position x = 0.
3. While x < n:
Find the furthest lily pad l reachable from x that is not after
position n.
Add a jump to J from x to l's location.
Set x to l's location.
4. Return J.

15
The Solution

16
What is an Optimization Model?

• An objective function that is to be maximized or minimized, e.g.


o Minimize time spent traveling from New York to Boston

• A set of constraints (possibly empty) that must be honored, e.g.,


o Cannot spend more than $100
o Must be in Boston before 5:00 PM

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.

$5 bill + $1 bill + 25¢ coin + 10¢ coin + 1¢ coin

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

• It is quite easy to come up with a greedy algorithm for a problem.

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

• Which algorithm is NOT based on the greedy approach?


a) Dijkstra’s Algorithm
b) Kruskal’s Algorithm
c) Merge Sort
d) Prim’s Algorithm
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

• Which algorithm is NOT based on the greedy approach?


a) Dijkstra’s Algorithm
b) Kruskal’s Algorithm
c) Merge Sort
d) Prim’s Algorithm
References

 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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy