









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










Analysis of Selection Sort
Θ
Θ(( n^
n^ )
)
Θ
Θ(( 1
1 ), so in place
), so in place
yes
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
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
Pseudocode and Efficiency
Θ
Θ(mn)(mn) comparisons (in the worst case)comparisons (in the worst case)
n
n
n -
n -
1
1
0
0
p
p
for
for
i
i
n
n
downto
downto
0
0
do
do
power
power
1
1
for
for j
j
1
1 to
to i
i do
do //compute
//compute x
x
ii
power
power
power
power
x
x
p
p
p
p
a
a [
[ i
i ]
]
power
power
return
return p
p
0
0
i
i
n
n i
i = Θ
Θ (n^2) multiplications
(n^2) multiplications
Closest-Pair Problem
Efficiency:
Efficiency:
How to make it faster?
How to make it faster?
Θ
Θ (n^2) multiplications (or sqrt)
(n^2) multiplications (or sqrt)
Using divide-and-conquer!
in a systematic manner
infeasible ones and, for an optimization problem,
keeping track of the best one found so far
Example 1: Traveling Salesman
Problem
between each pair, find the shortest
tour that passes through all the cities
exactly once before returning to the
starting city
Hamiltonian circuit in a weighted
connected graph
a b
c d
8
2
7
5 3
4
How do we represent a solution (Hamiltonian circuit)?
1
w
2
… w
n
1
v
2
… v
n
Knapsack Problem by
Exhaustive Search
{1} 2 $
{2} 5 $
{3} 10 $
{4} 5 $
{1,2} 7 $
{1,3} 12 $
{1,4} 7 $
{2,3} 15 $
{2,4} 10 $
{3,4} 15 $
{1,2,3} 17 not feasible
{1,2,4} 12 $
{1,3,4} 17 not feasible
{2,3,4} 20 not feasible
{1,2,3,4} 22 not feasible
Efficiency:
Efficiency: Θ
Θ (2^n)
(2^n)