M2 Chapter 3
M2 Chapter 3
• The subject of this class is brute force and its important special case, exhaustive search.
Brute force can be described as follows:
➢ Brute force is a straightforward approach to solving a problem, usually directly
based on the problem statement and definitions of the concepts involved.
➢ “Just do it!” would be another way to describe the prescription of the brute-force
approach. And often, the brute-force strategy is indeed the one that is easiest to
apply.
Ex: compute 𝑎𝑛 for a nonzero number ‘a’ and a nonnegative integer ‘n’.
Ex: 25 = 2 ∗ 2 ∗ 2 ∗ 2 ∗ 2 = 32
1. Selection Sort :
Note: its time complexity depends on the size of ‘P’ & ‘T’. If both P & T are of same size ‘n’, then the worst-
case complexity is O(n*n)
Partition problem using brute-force technique
• The partition problem is to determine whether a given Algorithm:
set can be partitioned into two subsets such that the
sum of elements in both subsets is the same.
1. For each recursion of the method, divide the
Examples: problem into two sub problems such that:
1. Input: arr[] = {1, 5, 11, 5} a. Create a new subset of the
array including the last element of the array if
Output: true its value does not exceed S/2 and repeat the
The array can be partitioned as {1, 5, 5} and {11} recursive step 1 again for the new subarray
2. Input: arr[] = {1, 5, 3} b. Create a new subset of the
Output: false array excluding the last element of the array
and repeat the recursive step 2 again for the
The array cannot be partitioned into equal sum new subarray
sets.
c. If the sum of any of the above subsets is
• Brute force approach: This is a recursive method in equal to S/2, return true otherwise return false
which we consider each possible subset of the array
2. If any of the above sub problems return true, then
and check if its sum is equal to total sum S/2 or not, by return true otherwise return false
eliminating the last element in the array in each turn.
L10: Exhaustive Search
• Exhaustive search is simply a brute-force approach to combinatorial
problems. It suggests generating each and every element of the problem
domain, selecting those of them that satisfy all the constraints, and then
finding a desired element (e.g., the one that optimizes some objective
function).
• We illustrate exhaustive search by applying it to three important
problems:
➢Traveling Salesman Problem
➢Knapsack Problem
➢Assignment Problem
1. Traveling Salesman Problem
Problem Statement:
• A traveler needs to visit all the cities from a list, where distances between all the cities are known and each
city should be visited just once. What is the shortest possible route that he visits each city exactly once and
returns to the origin city?
• The problem can be conveniently modeled by a weighted graph, with the graph’s vertices representing the
cities and the edge weights specifying the distances.
• Then the problem can be stated as the problem of finding the shortest Hamiltonian circuit of the graph. (A
Hamiltonian circuit is defined as a cycle that passes through all the vertices of the graph exactly once.)
Solution:
• It is easy to see that a Hamiltonian circuit can also be defined as a sequence of n + 1 adjacent vertices
vi0 , vi1 ,...,vin−1 , vi0 …………………….>(Ex: a-b-c-d-a)
where the first vertex of the sequence is the same as the last one and all the other n − 1 vertices are distinct.
• Thus, we can get all the tours by generating all the permutations of n − 1 intermediate cities i.e, (n-1)!
feasible solutions (one that satisfies all defined constraints and requirements), compute the tour lengths,
and find the shortest among them (optimal).
• Consider a source node(a) and form a path
by its adjacent nodes (a-b, a-c, & a-d).
𝟐𝒏
FIGURE : (a) Instance of the knapsack problem. (b) Its solution by exhaustive search. The information about the optimal selection is in bold
• Since the number of subsets of an n-element set is
𝟐𝒏 (𝑖𝑛𝑐𝑙𝑢𝑑𝑖𝑛𝑔 𝑒𝑚𝑝𝑙𝑡𝑦 𝑠𝑒𝑡), the exhaustive search leads to a Ω(2𝑛 )
algorithm, no matter how efficiently individual subsets are generated.
• Both the traveling salesman and knapsack problems are the best-
known examples of so-called NP-hard (non-deterministic
polynomial-time hard) problems.
• No polynomial-time algorithm is known for any NP-hard problem.
3. Assignment Problem
• There are ‘n’ people who need to be assigned to execute ‘n’ jobs, one
person per job. (That is, each person is assigned to exactly one job and
each job is assigned to exactly one person.)
• The cost that would result if the ith person is assigned to the jth job is a
known quantity C[i, j ] for each pair i, j = 1, 2,...,n.
• The problem is to find an assignment with the minimum total cost.
• We can describe feasible solutions to the assignment problem as n-tuples
j1,........,jn in which the ith component, i = 1,......,n, indicates the column
of the element selected in the ith row (i.e., the job number assigned to
the ith person).
For example, for the cost matrix below,
• The Hungarian method is defined as a combinatorial optimization technique that solves the assignment
problems in polynomial time.
L11: Depth-First Search (DFS) and Breadth-First Search
(BFS).
By:
Dr. Geeta S Hukkeri