0% found this document useful (0 votes)
58 views

Brute Force and Greedy Algorithms (Pattern Matching, Fractional Knapsack, TSP)

A brute force algorithm exhaustively evaluates all possible solutions to a problem and reports the best solution found. The document describes a brute force algorithm as the most basic approach that iterates through every possibility to solve a problem. It provides examples of using brute force to find the shortest path to a market by exploring all paths, and cracking a 4-digit PIN by trying all 10,000 combinations. The document notes that brute force guarantees finding an optimal solution but can be very inefficient for large problems, with runtimes of O(N!) or worse.

Uploaded by

Shivansh Goel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

Brute Force and Greedy Algorithms (Pattern Matching, Fractional Knapsack, TSP)

A brute force algorithm exhaustively evaluates all possible solutions to a problem and reports the best solution found. The document describes a brute force algorithm as the most basic approach that iterates through every possibility to solve a problem. It provides examples of using brute force to find the shortest path to a market by exploring all paths, and cracking a 4-digit PIN by trying all 10,000 combinations. The document notes that brute force guarantees finding an optimal solution but can be very inefficient for large problems, with runtimes of O(N!) or worse.

Uploaded by

Shivansh Goel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Brute Force Algorithm

This is the most basic and simplest type of algorithm. A Brute Force Algorithm is the straightforward
approach to a problem i.e., the first approach that comes to our mind on seeing the problem. More technically
it is just like iterating every possibility available to solve that problem.

It is an intuitive, direct, and straightforward technique of problem-solving in which all the possible ways or all
the possible solutions to a given problem are enumerated.

Many problems solved in day-to-day life using the brute force strategy, for example exploring all the paths to
a nearby market to find the minimum shortest path.

Arranging the books in a rack using all the possibilities to optimize the rack spaces, etc.

For Example: If there is a lock of 4-digit PIN. The digits to be chosen from 0-9 then the brute force will be
trying all possible combinations one by one like 0001, 0002, 0003, 0004, and so on until we get the right PIN.
In the worst case, it will take 10,000 tries to find the right combination.
A brute force algorithm simply tries all possibilities until a satisfactory solution is found. Such an
algorithm can be:

Optimizing: Find the best solution. This may require finding all solutions, or if a value for the best
solution is known, it may stop when any best solution is found.
Example: Finding the best path for a traveling salesman

Satisficing: Stop as soon as a solution is found that is good enough.


Example: Finding a traveling salesman path that is within 10% of optimal.
Pattern Matching Algorithm
Main String (S) 8 5 14 9 6 12 7 1 13 11

Pattern String (P) 7 1 13 11

7 1 13 11

Length (S) = 10 (LS)


7 1 13 11
Length (P) = 4 (LP)
Max no of string comparison = 10-4+1 = 7
(LS-LP+1)
7 1 13 11
For j = 1 to LP

i/j 1 2 3 4 • Here j is moving from 1 to Length of pattern string


• i is moving from 1 to length of Main string
1 1 2 3 4 j+0 j+i-1
For i = 1 to LS – LP +1

2 2 3 4 5 j+1 j+i-1

3 3 4 5 6 j+2 j+i-1

4 4 5 6 7 j+3 j+i-1

5 5 6 7 8 j+4 j+i-1

6 6 7 8 9 j+i-1
j+5

7 7 8 9 10 j+6 j+i-1

These are the character places


of main string and pattern string
Algorithm Pattern_Match_BF (S, P)
{
• If N is the total number of elements in the
LS = length (S)
main string
LP = length (P)
• M is the total number of elements in the
max = (LS – LP + 1)
Pattern string
for( i = 1; i < = max; i++)
{
➢ Total number of comparisons: M (N-M+1)
flag = true
for(j = 1; j<= LP && flag = true; j++)
➢ Worst case time complexity: Ο(MN)
{
if(P[j] != S[j+i-1]
flag = false
}
if(flag = = true)
{
return i
}
return 0
}
}
Pros and Cons of Brute Force
Algorithm:

Pros:
• The brute force approach is a guaranteed way to find the correct solution by listing all the possible
candidate solutions for the problem.

• It is a generic method and not limited to any specific domain of problems.

• The brute force method is ideal for solving small and simpler problems.

• It is known for its simplicity and can serve as a comparison benchmark.


Cons:
• The brute force approach is inefficient. For real-time problems, algorithm analysis often goes above the
O(N!) order of growth.

• This method relies more on compromising the power of a computer system for solving a problem than
on a good algorithm design.

• Brute force algorithms are slow.

• Brute force algorithms are not constructive or creative compared to algorithms that are constructed using
some other design paradigms.

Brute force algorithm is a technique that guarantees solutions for problems of any domain helps in solving
the simpler problems and also provides a solution that can serve as a benchmark for evaluating other design
techniques, but takes a lot of run time and inefficient.
Greedy Method

A greedy algorithm for an optimization problem always makes the choice that looks best at the moment and
adds it to the current sub-solution.

Greedy algorithms don’t always yield optimal solutions but, when they do, they’re usually the simplest and
most efficient algorithms available.

Greedy is a strategy that works well on optimization problems with the following characteristics:
1. Greedy-choice property: A global optimum can be arrived at by selecting a local optimum.
2. Optimal substructure: An optimal solution to the problem contains an optimal solution to subproblems.
How to decide which choice is optimal?

Assume there is an objective function that needs to be optimized. A Greedy algorithm makes greedy choices at
each step to ensure that the objective function is optimized. The Greedy algorithm has only one shot to
compute the optimal solution so that it never goes back and reverses the decision.

Greedy algorithms have some advantages and disadvantages

Advantages
1.It is quite easy to come up with a greedy algorithm for a problem.
2.Analyzing the run time for greedy algorithms is much easier than for other techniques cause there is no
branching or backtracking.

Disadvantages
1.It does not give the optimal solution.
2.Proving that a greedy algorithm is correct is difficult.
The greedy method is quite powerful and works well for a wide range of problems. Many algorithms can be
viewed as applications of Greedy algorithms, such as:

• Fractional Knapsack
• Travelling Salesperson Problem
• Minimum Spanning Tree
• Dijkstra’s algorithm for shortest paths from a single source
• Huffman codes (data-compression codes)
Knapsack Problem
You are given the following-

• A knapsack (kind of shoulder bag) with limited weight capacity.


• Few items each having some weight and value.

The problem states- “Which items should be placed into the knapsack such that-”

• The value or profit obtained by putting the items into the knapsack is maximum.
• And the weight limit of the knapsack does not exceed.
In Fractional Knapsack Problem,

• As the name suggests, items are divisible here.


• We can even put the fraction of any item into the knapsack if taking the complete item is not possible.
• It is solved using Greedy Method.

Fractional knapsack problem is solved using greedy method in the following steps-

Step-01: For each item, compute its value / weight ratio.

Step-02: Arrange all the items in decreasing order of their value / weight ratio.

Step-03: Start putting the items into the knapsack beginning from the item with the highest ratio.

Put as many items as you can into the knapsack.


Consider the following problem
Weight = 15
Objects: 1 2 3 4 5 6 7
Profit: 5 10 15 7 8 9 4
Weight: 1 3 5 4 1 3 2

These are the 7 objects, you need to place within the bag, but the capacity of bag is 15, and while placing objects
in the bag you need to earn the maximum profit.

Solution:

Now, we have two simple options. Put the objects with maximum weights in the bag, and check the capacity.

Or place the objects with less weights.


Maximum Profit

Objects: 1 2 3 4 5 6 7
Profit: 5 10 15 7 8 9 4
Weight: 1 3 5 4 1 3 2
Here, the remaining
Object Profit Weight Maximum weight capacity is 3 so we
can’t put the object
3 15 5 15-5 = 10
with weight 4.
2 10 3 10 – 3 = 7
6 9 3 7–3=4 But since it is
fractional knapsack,
5 8 1 4–1=3
objects are
4 7 4 3–3=0 divisible. So we will
1 5 1 select the fraction
of object.
7 4 2

Now we will select 3 out of 4 weight. But


Now the profit of object with now the profit should also be considered
weight 3 will be = 7*3/4 = 5.25 accordingly.
Total Profit = 15+10+9+8+5.25 = 47.25
Minimum Weight

Objects: 1 2 3 4 5 6 7
Profit: 5 10 15 7 8 9 4
Weight: 1 3 5 4 1 3 2

Object Profit Weight Maximum weight


1 5 1 15-1 = 14
5 8 1 14 – 1 = 13
7 4 2 13 – 2 = 11
2 10 3 11 – 3 = 8
6 9 3 8–3=5
4 7 4 5–4=1
3 15 5 1–1=0

15*1/5 = 3
Total Profit = 5+8+4+10+9+7+3 = 46
Maximum Profit/Weight
This approach is
Objects: 1 2 3 4 5 6 7 the best one.
Profit: 5 10 15 7 8 9 4
Weight: 1 3 5 4 1 3 2

Object Profit Weight Profit/Weight Maximum


Weight
1 5 1 5/1 = 5 14 – 1 = 13
2 10 3 10/3 = 3.3 13 – 3 = 10
3 15 5 15/5 = 3 10 – 5 = 5
4 7 4 7/4 = 1.75
5 8 1 8/1 = 8 (max) 15-1 = 14
6 9 3 9/3 = 3 5–3=2
7 4 2 4/2 = 2 2–2=0

Total Profit = 5+10+15+8+9+4 = 51


Time Complexity-

• The main time taking step is the sorting of all items in decreasing order of their Profit / weight ratio.
• If the items are already arranged in the required order, then while loop takes O(n) time.
• The average time complexity of Quick Sort is O(nlogn).
• Therefore, total time taken including the sort is O(nlogn).
Question 1: Consider 5 items along their respective weights and values: -

I = (I1,I2,I3,I4,I5)

w = (5, 10, 20, 30, 40)

v = (30, 20, 100, 90,160)

The capacity of knapsack W = 60

Question 2. Find the optimal solution for the fractional knapsack problem making use of greedy approach.
Consider:

n=4
m = 6 kg
(w1, w2, w3, w4) = (3,2,10,2)
(p1, p2, p3, p4) = (15,20,30,14)
Travelling Salesperson Problem
The TSP describes a scenario where a salesman is required to travel between n cities. He wishes to travel to
all locations exactly once and he must finish at his starting point. The order in which the cities are visited is
not important but he wishes to minimize the distance traveled. This problem can be describes as a network,
where the cities are represented by nodes which are connected by edges that carry a weight describing the
time or distance it takes to travel between cities.

This problem may sounds simple and using a brute force method in theory it is: calculate the time to
transverse all possible routes and select the shortest. However, this is extremely time consuming and as the
number of cities grows, brute force quickly becomes an infeasible method. A TSP with just 10 cities has 9! or
362,880 possible routes, far too many for any computer to handle in a reasonable time. The TSP is an NP-
hard problem and so there is no polynomial-time algorithm that is known to efficiently solve every travelling
salesman problem.
Greedy Algorithm for TSP
• This algorithm searches for the local optima and optimizes the local best solution to find the global optima.

• It begins by sorting all the edges and then selects the edge with the minimum cost.

• It continuously selects the best next choices given a condition that no loops are formed.

• The computational complexity of the greedy algorithm is O(N 2 log2(N)) and there is no guarantee that a
global optimum solution is found.
Given Graph for cities

Step-I
Step-II
Step-III
Step-IV

The Final answer is A -> B -> D -> C -> A = 2.4 + 5.1 + 5.9 + 6.8 = 20.2

You might also like