Docsity

Analog and digital C - Brute force, Study notes of Digital & Analog Electronics

Partial preview of the text

Download Analog and digital C - Brute force and more Study notes Digital & Analog Electronics in PDF only on Docsity!

Brute Force

Brute Force

Brute Force

A straightforward approach, usually

based directly on the problem’s

statement and definitions of the

concepts involved

Analysis of Selection Sort

Time efficiency:

Space efficiency:

Stability:

Θ

Θ(( n^

n^ )

)

Θ

Θ(( 1

1 ), so in place

), so in place

yes

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

Pseudocode and Efficiency

Time efficiency:

Θ

Θ(mn)(mn) comparisons (in the worst case)comparisons (in the worst case)

Brute-Force Polynomial

Evaluation

Problem: Find the value of polynomial
p ( x ) = a

n

x

n

+ a

n -

x

n -

+… + a

1

x

1

+ a

0

at a point x = x

0

Brute-force algorithm
Efficiency:

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

Find the two closest points in a set of n

points (in the two-dimensional

Cartesian plane).

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:

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!

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

  • 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

  • Example:

a b

c d

8

2

7

5 3

4

How do we represent a solution (Hamiltonian circuit)?

Example 2: Knapsack

Problem

Given n items:

  • weights: w

1

w

2

… w

n

  • values: v

1

v

2

… v

n

  • a knapsack of capacity W

Find most valuable subset of the items that

fit into the knapsack

Example: Knapsack capacity W=

item weight value

Knapsack Problem by

Exhaustive Search

Subset Total weight Total value

{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)