LU5-Exhaustive Search
Mrs. G. Priyanka, AP(Sl.Gr)/CSE
Mepco Schlenk Engineering College, Sivakasi
Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 3
3-1
Exhaustive Search
• Exhaustive search is simply a brute-force approach to
combinatorial problems.
• A brute force solution to a problem involving search for an
element with a special property, usually among
combinatorial objects such as permutations, combinations,
or subsets of a set.
Method:
• generate a list of all potential solutions to the problem in a
systematic manner
• evaluate potential solutions one by one, disqualifying infeasible
ones and, for an optimization problem, keeping track of the best
one found so far
• when search ends, announce the solution(s) found
Example 1: Traveling Salesman
Problem
• Given n cities with known distances between each pair, find the
shortest tour that passes through all the cities exactly once
before returning to the starting city
• Alternatively: Find shortest Hamiltonian circuit in a weighted
connected graph
2
• Example: 1
a
b
5
3
8
c
7
4
d
Example 1: Traveling Salesman
Problem
How do we represent a solution (Hamiltonian circuit)?
• Hamiltonian circuit can also be defined as a sequence of n + 1
adjacent vertices v0, v1, . . . , vn−1, v0,
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, compute the tour
lengths, and find the shortest among them.
TSP by Exhaustive Search
Tour
a→b→c→d→a
a→b→d→c→a
a→c→b→d→a
a→c→d→b→a
a→d→b→c→a
a→d→c→b→a
Cost
2+3+7+5 = 17
2+4+7+8 = 21
8+3+4+5 = 20
8+7+4+2 = 21
5+4+3+8 = 20
5+7+3+2 = 17
Efficiency:
Θ((n-1)!)
• Improvement: 3 pairs of tour (same cost) that differ
only by directions so the total number of permutations
needed is still 1/2 *(n − 1)!
Example-2 – Find TSP starting with city a
Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 3
3-6
Example-2 – Find TSP starting with city a
(Contd.,)
Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 3
3-7
TSP by Exhaustive Search
Efficiency: Θ((n-1)!)
• Improvement: The number of vertex permutations
can be cut by half.
• For example, choose any two intermediate vertices,
say, b and c, and then consider only permutations in
which b precedes c and thus ignores c precedes b .
𝟏
• Improved Efficiency: Θ(𝟐(n-1)!)
Example 2: Knapsack Problem
• Problem: Given n items of known weights w1, w2, . . . ,
wn and values v1, v2, . . . , vn and a knapsack of capacity
W, find the most valuable subset of the items that fit into
the knapsack.
Knapsack Problem by Exhaustive Search
The exhaustive-search approach to this problem leads to
• generating all the subsets of the set of n items given
• computing the total weight of each subset in order to
identify feasible subsets (i.e., the ones with the total
weight not exceeding the knapsack capacity)
• finding a subset of the largest value among them
Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 3
3-10
Knapsack problem: Example
Example: Knapsack capacity W=10
item weight
value
1
7
$42
2
3
$12
3
4
$40
4
5
$25
Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 3
3-11
Solution by exhaustive search
Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 3
3-12
Example 2
Example: Knapsack capacity W=16
item weight
value
1
2
$20
2
5
$30
3
10
$50
4
5
$10
Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 3
3-13
Knapsack Problem by Exhaustive Search – Example 2
(Contd.,)
Subset Total weight
{1}
{2}
{3}
{4}
{1,2}
{1,3}
{1,4}
{2,3}
{2,4}
{3,4}
{1,2,3}
{1,2,4}
{1,3,4}
{2,3,4}
{1,2,3,4}
2
5
10
5
7
12
7
15
10
15
17
12
17
20
22
Total value
$20
$30
$50
$10
$50
$70
$30
$80
$40
$60
not feasible
$60
not feasible
not feasible
not feasible
Efficiency:
Ω(2n)
Since the number of subsets of an n-element set is
2n, the exhaustive search leads to Ω(2n) algorithm.
TSP and Knapsack problems are NP-hard
problems. No polynomial-time algorithm is
known for any NP hard problem.