Session 4
Session 4
College of Education
School of Continuing and Distance Education
2020/2021 – 2022/2023
Brute Force
A straightforward approach, usually based directly on the problem’s
statement and definitions of the concepts involved
Examples:
1. Computing an (a > 0, n a nonnegative integer)
2. Computing n!
Example: 7 3 2 5
Analysis of Selection Sort
Time efficiency:
Space efficiency:
Stability:
Brute-Force String Matching
• pattern: a string of m characters to search for
• text: a (longer) string of n characters to search in
• problem: find a substring in the text that matches the pattern
Brute-force algorithm
Step 1 Align pattern at beginning of text
Step 2 Moving from left to right, compare each character of
pattern to the corresponding character in text until
• all characters are found to match (successful search); or
• a mismatch is detected
Step 3 While pattern is not found and the text is not yet
exhausted, realign pattern one position to the right and
repeat Step 2
Examples of Brute-Force String Matching
Pattern: 001011
Text: 10010101101001100101111010
Pattern: happy
Text: It is never too late to have a happy childhood.
Pseudocode and Efficiency
Efficiency:
Brute-Force Polynomial Evaluation
Problem: Find the value of polynomial
p(x) = anxn + an-1xn-1 +… + a1x1 + a0
at a point x = x0
Brute-force algorithm
p 0.0
for i n downto 0 do
power 1
for j 1 to i do //compute xi
power power x
p p + a[i] power
return p
Efficiency:
Polynomial Evaluation: Improvement
We can do better by evaluating from right to left:
Brute-force algorithm
Compute the distance between every pair of distinct points
and return the indexes of the points for which the distance is
the smallest.
Closest-Pair Brute-Force Algorithm (cont.)
Efficiency:
• Weaknesses
– rarely yields efficient algorithms
– some brute-force algorithms are unacceptably slow
– not as constructive as some other design techniques
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 (see algorithms in Sec. 5.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:
Example 2: Knapsack Problem
Given n items:
– weights: w1 w2 … wn
– values: v1 v2 … v n
– a knapsack of capacity W
Find most valuable subset of the items that fit into the knapsack
• Uses a stack
– a vertex is pushed onto the stack when it’s reached for the first
time
– a vertex is popped off the stack when it becomes a dead end,
i.e., when there is no adjacent unvisited vertex
a b c d
e f g h
a b c d
e f g h
• Applications: same as DFS, but can also find paths from a vertex to
all other vertices with the smallest number of edges
Reference
Levitin, A. (2012). Introduction to the Design and
Analysis of Algorithms ( 3rd Edition). Harlow: Addison
Wesley.
Acknowledgement