09d transform & conquer spring2015

13
Transform & Conquer

Upload: hira-gul

Post on 11-Aug-2015

66 views

Category:

Software


4 download

TRANSCRIPT

Page 1: 09d transform & conquer spring2015

Transform & Conquer

Page 2: 09d transform & conquer spring2015

Algorithms based on the idea of transformation

Transformation stage

Problem instance is modified to be more amenable to solution

Conquering stage

Transformed problem is solved

Major variations are for the transform to perform:

Instance simplification

Different representation

Problem reduction

Page 3: 09d transform & conquer spring2015

Instance Simplification

Reducing the Problem to a Simpler One

Techniques:

Presorting

Gaussian Elimination

Search Trees

Page 4: 09d transform & conquer spring2015

Pre Sorting

Presorting is an old idea, you sort the data

and that allows you to more easily compute some answer

Some simple presorting examples

Element Uniqueness

Computing the mode of n numbers

Page 5: 09d transform & conquer spring2015

Element Uniqueness

Given a list A of n orderable elements, determine if there are any duplicates of anyelement

Brute Force: for each x є A for each y є {A – x} if x = y return not unique return unique

Presorting: Sort A for i ¬ 1 to n-1 if A[i] = A[i+1] return not unique return unique

Runtime?

Page 6: 09d transform & conquer spring2015

Computing a mode

A mode is a value that occurs most often in a list of numbers

e.g. the mode of [5, 1, 5, 7, 6, 5, 7] is 5 If several different values occur most often any of them can

be

max = max(A)

freq[1..max] = 0

for each x in A

freq[x] +=1

mode =freq[1]

for i = 2 to max

if freq[i] > freq[mode] mode =i

return mode

Page 7: 09d transform & conquer spring2015

Presort Computing Mode

Sort A

i = 0

modefrequency= 0

while i <= n-1

runlength = 1; runvalue = A[i]

while i+runlength <= n-1 and A[i+runlength] = runvalue

runlength += 1

if runlength > modefrequency

modefrequency=runlength

modevalue = runvalue

i+= runlength

return modevalue

Page 8: 09d transform & conquer spring2015

Representation Change

Page 9: 09d transform & conquer spring2015

Horner’s Rule For Polynomial Evaluation

Given a polynomial of degree n

p(x) = anxn + an-1xn-1 + … + a1x + a0

and a specific value of x, find the value of p at that point.

Page 10: 09d transform & conquer spring2015

Example: p(x) = 2x4 - x3 + 3x2 + x - 5 =

= x(2x3 - x2 + 3x + 1) - 5 =

= x(x(2x2 - x + 3) + 1) - 5 =

= x(x(x(2x - 1) + 3) + 1) - 5

Substitution into the last formula leads to a faster algorithm Same sequence of computations are obtained by simply arranging the coefficient in a table and proceeding as follows

coefficients 2 -1 3 1 -5

x=3 2*3 6-1 15+3 54+1 165-5

6 5*3 18*3 55*3

Page 11: 09d transform & conquer spring2015

Efficiency of Horner’s Rule: # multiplications = # additions = n

Page 12: 09d transform & conquer spring2015

Domain Change

Page 13: 09d transform & conquer spring2015

Finding LCM

LCM(m,n)=m.n/(gcd(m,n))