Module2 2024
Module2 2024
Exhaustive Search
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
• 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
• Example:
2
a b
5 3
8 4
c 7 d
TSP by Exhaustive Search
Tour Cost
a→b→c→d→a 2+3+7+5 = 17
a→b→d→c→a 2+4+7+8 = 21
a→c→b→d→a 8+3+4+5 = 20
a→c→d→b→a 8+7+4+2 = 21
a→d→b→c→a 5+4+3+8 = 20
a→d→c→b→a 5+7+3+2 = 17
More tours?
Less tours?
Efficiency: Θ((n-1)!)
Example 2: Knapsack Problem
Given n items:
– weights: w1 w2 … wn
– values: v1 v2 … vn
– a knapsack of capacity W
Find most valuable subset of the items that fit into the knapsack
8
Final Comments on Exhaustive Search
• In many cases, exhaustive search or its variation is the only known way to get exact
solution
Divide-and-Conquer
a problem of size
n (instance)
subproblem 1 subproblem 2
of size n/2 of size n/2
a solution to a solution to
subproblem 1 subproblem 2
It generally leads to a
recursive algorithm!
a solution to
the original problem
Divide-and-Conquer Examples
• Binary search
where f (n) is a function that accounts for the time spent on dividing an instance
of size n into instances of size n/b and combining their solutions
General Divide-and-Conquer Recurrence
T(n) = aT(n/b) + f (n)
If f(n) ∈ Θ(nd), d ≥ 0
A[i]≤p A[i]≥p
• Exchange the pivot with the last element in the first (i.e., ≤) subarray — the pivot is now
in its final position
• Sort the two subarrays recursively
Partitioning Algorithm
<
Quicksort Example
0 1 2 3 4 5 6 7
--------------------------------------------------------------------------------------------------------------------------------------
i j
5 3 1 9 8 2 4 7
i j
5 3 1 9 8 2 4 7
i j
5 3 1 4 8 2 9 7
i j
5 3 1 4 8 2 9 7
i j
5 3 1 4 2 8 9 7
j i
5 3 1 4 2 8 9 7
2 3 1 4 5 8 9 7
****************************************** *******************************
• The number of key comparisons made before a partition is achieved is n + 1 if
the scanning indices cross over and n if they coincide
• If all the splits happen in the middle of corresponding subarrays, we will have
the best case. The number of key comparisons in the best case satisfies the
recurrence
• In the worst case, all the splits will be skewed to the extreme: one of the two
subarrays will be empty, and the size of the other will be just 1 less than the size of
the subarray being partitioned.
• This unfortunate situation will happen, in particular, for increasing arrays, i.e., for
inputs for which the problem is already solved
• Some applications, notably modern cryptography, require manipulation of integers that are over 100 decimal digits
long. Since such integers are too long to fit in a single word of a modern computer, they require special treatment.
• This practical need supports investigations of algorithms for efficient manipulation of large integers.
• Obviously, if we use the conventional pen-and-pencil algorithm for multiplying two n-digit integers, each of the n
digits of the first number is multiplied by each of the n digits of the second number for the total of n2 digit
multiplications.
• (If one of the numbers has fewer digits than the other, we can pad the shorter number with leading zeros to
equalize their lengths.)
• Though it might appear that it would be impossible to design an algorithm with fewer than n2 digit multiplications,
this turns out not to be the case.
• The miracle of divide-and-conquer comes to the rescue to accomplish this feat.
• To demonstrate the basic idea of the algorithm, let us start with a case of two-digit
integers, say, 23 and 14. These numbers can be represented as follows:
Conventional Matrix Multiplication
• Brute-force algorithm
c00 c01 a00 a01 b00 b01
= *
c10 c11 a10 a11 b10 b11
4 additions
7 multiplications
18 additions
Strassen’s Matrix Multiplication
M 1 + M4 - M5 + M7 M 3 + M5
=
M 2 + M4 M1 + M 3 - M 2 + M6
Formulas for Strassen’s Algorithm
M1 = (A00 + A11) * (B00 + B11)
Number of multiplications:
M(n) = 7M(n/2), M(1) = 1
Solution: M(n) = 7log 2n = nlog 27 ≈ n2.807 vs. n3 of brute-force alg.
Algorithms with better asymptotic efficiency are known but they are even
more complex and not used in practice.
Decrease and Conquer
• The DFS based algorithm is a simple application of depth-first search: perform a DFS
traversal and note the order in which vertices become dead-ends (i.e., popped off
the traversal stack).
• Reversing this order yields a solution to the topological sorting problem, provided, of
course, no back edge has been encountered during the traversal.
• If a back edge has been encountered, the digraph is not a DAG, and topological
sorting of its vertices is impossible