• No results found

Lecture 00 Array Stack Queue

N/A
N/A
Protected

Academic year: 2020

Share "Lecture 00 Array Stack Queue"

Copied!
65
0
0

Loading.... (view fulltext now)

Full text

(1)

Subhash Suri UC Santa Barbara

Divide and Conquer

A general paradigm for algorithm design; inspired

by emperors and colonizers.

Three-step process:

1.

Divide

the problem into smaller problems.

2.

Conquer

by solving these problems.

3.

Combine

these results together.

Examples: Binary Search, Merge sort, Quicksort

(2)

Subhash Suri UC Santa Barbara

Merge Sort: Illustration

2 5 4 6 1 3 2 6

6 3 2 1 6 5 4 2

1 2 2 3 4 5 6 6 5 2 4 6 1 3 2 6

5 2 4 6 1 3 2 6

5 2 4 6 1 3 2 6

(3)

Subhash Suri UC Santa Barbara

Merge Sort

Sort an

unordered

array of numbers

A

.

Merge-Sort

(A, p, q)

1.

if

p

q

return

A

;

2.

r

=

b

(p

+

q)/2

c

3.

Merge-Sort

(

A, p, r

)

4.

Merge-Sort

(

A, r

+ 1

, q

)

5.

MERGE

(A, p, q, r)

(4)

Merge sort:

• Divide: Split array of size n into two subarrays of size n/2

Conquer: Two recursive calls on subarrays of size n/2

• Combine: Merge two sorted subarrays to create sorted array of size n

Let T(n) = running time of merge sort on array of size n

T(n) = Tdivide(n) + Tconquer(n) + Tcombine(n)

T(n) = θ(1) + [T(n/2) + T(n/2)] + θ(n) T(n) = 2 T(n/2) + θ(n)

• Number of recursive subproblems = 2

Size of each subproblem = n/2

Time for all the non-recursive steps = θ(n)

Later we’ll see how to solve these kinds of recurrence equations to determine T(n)

(5)

Subhash Suri UC Santa Barbara

Binary Search

Search for

x

in a

sorted

array

A

.

Binary-Search

(A, p, q, x)

1.

if

p > q

return -1;

2.

r

=

b

(p

+

q)/2

c

3.

if

x

=

A

[

r

]

return

r

4.

else if

x < A[r]

Binary-Search

(A, p, r, x)

5.

else Binary-Search

(A, r

+ 1, q, x)

(6)

Binary search: Find(27)

0 1 2 3 4 5 6 7 8 9 10 13 18 24 27 34 41 49 55 60 66 72

27 < 41 0 1 2 3 4

13 18 24 27 34 27 > 24

3 4 27 34 found

Let T(n) = running time of binary search on array of size n T(n) = Tdivide(n) + Tconquer(n) + Tcombine(n)

T(n) = θ(1) + T(n/2) + 0 T(n) = T(n/2) + θ(1)

• Number of recursive subproblems = 1

Size of each subproblem = n/2

Time for all the non-recursive steps = θ(1)

Later we’ll see how to solve these kinds of recurrence equations to determine T(n)

(7)

Subhash Suri UC Santa Barbara

Multiplying Numbers

We want to multiply two

n

-bit numbers. Cost is

number of elementary bit steps.

Grade school method has

Θ(

n

2

)

cost.:

xxxxxxxx xxxxxxxx xxxxxxxxx xxxxxxxxx

xxxxxxxxx

xxxxxxxxxxxxxxxx ..

.

(8)

Polynomial multiplication and Large integer multiplication:

Given two polynomials:

P = 5x3 + 7x2 + 6x + 2 [5, 7, 6, 2] = [p3, p2, p1, p0]

Q = x3 – 8x2 + 9x – 1 [1, –8, 9, –1] = [q3, q2, q1, q0]

P*Q = (5x3 + 7x2 + 6x + 2)(x3 – 8x2 + 9x – 1)

Alternatively, given two large integers:

P = 9863 = 9x3 + 8x2 + 6x + 3 where x = 10

Q = 7245 = 7x3 + 2x2 + 4x + 5 where x = 10

P*Q = (9863)(7245) = (9x3 + 8x2 + 6x + 3)(7x3 + 2x2 + 4x + 5)

where x = 10

(9)

Subhash Suri UC Santa Barbara

Why Bother?

Doesn’t hardware provide multiply? It is fast,

optimized, and free. So, why bother?

True for numbers that fit in one computer word.

But what if numbers are very large.

Cryptography (encryption, digital signatures)

uses big number “keys.” Typically 256 to 1024

bits long!

n

2

multiplication too slow for such large numbers.

Karatsuba’s (1962) divide-and-conquer scheme

(10)

Divide-and-conquer Algorithm 1 for Polynomial multiplication:

P = 5x3 + 7x2 + 6x + 2 = (5x + 7)x2 + (6x + 2) = Ax2 + B

Q = x3 – 8x2 + 9x – 1 = (x – 8)x2 + (9x – 1) = Cx2 + D

A = 5x + 7 ⇒ [5, 7] B = 6x + 2 ⇒ [6, 2]

C = x – 8 ⇒ [1, –8]

D = 9x – 1 ⇒ [9, –1]

Let n = number of terms in P and Q P = Axn/2 + B

Q = Cxn/2 + D

A, B, C, D are polynomials with n/2 terms

P*Q = (Axn/2 + B)(Cxn/2 + D) = (AC)xn + (BC + AD)xn/2 + (BD)

Four recursive subproblems: A*C

B*C

A*D

B*D

(11)

Let T(n) = running time for the preceding Algorithm 1

T(n) = 4 T(n/2) + θ(n)

• Number of recursive subproblems = 4

Size of each subproblem = n/2

Time for all the non-recursive steps = θ(n):

o adding n-term polynomials

o xn and xn/2 which are just shifts (not products) o also note the final product P*Q has < 2n terms

Again, later we’ll see how to solve these kinds of recurrence equations to determine T(n)

(12)

Subhash Suri UC Santa Barbara

Karatsuba’s Algorithm

Let

X

and

Y

be two

n

-bit numbers. Write

X

=

a b

Y

=

c d

a, b, c, d

are

n/

2

bit numbers. (Assume

n

= 2

k

.)

XY

= (

a

2

n/2

+

b

)(

c

2

n/2

+

d

)

(13)

Subhash Suri UC Santa Barbara

An Example

X

= 4729

Y

= 1326

.

a

= 47;

b

= 29

c

= 13;

d

= 26

.

ac

= 47

13 = 611

ad

= 47

26 = 1222

bc

= 29

13 = 377

bd

= 29

26 = 754

XY

= 6110000 + 159900 + 754

(14)

Subhash Suri UC Santa Barbara

Karatsuba’s Algorithm

This is D&C: Solve 4 problems, each of size

n/2

;

then perform

O(n)

shifts to multiply the terms by

2

n

and

2

n/2

.

We can write the recurrence as

T

(n) = 4T

(n/2) +

O

(n)

(15)

Divide-and-conquer Algorithm 2 for Polynomial multiplication

(Karatsuba’s algorithm)

:

As before,

P*Q = (Axn/2 + B)(Cxn/2 + D) = (AC)xn + (BC + AD)xn/2 + (BD)

This time we write (BC + AD) = (A+B)(C+D) – (AC) – (BD)

So only three recursive calls: A*C

B*D

(A+B)*(C+D)

Again stop the recursion when n = 1

Let T(n) = running time for the preceding Algorithm 2

T(n) = 3 T(n/2) + θ(n)

• Number of recursive subproblems = 3

• Size of each subproblem = n/2

Time for all the non-recursive steps = θ(n)

Again, later we will see how to solve these kinds of recurrence equations to determine T(n)

(16)

Majority element problem:

Given array A of size n, does there exist any value M that appears more than n/2 times in array A?

Majority element algorithm:

• Phase 1: Use divide-and-conquer to find candidate value M

• Phase 2: Check if M really is a majority element, θ(n) time, simple loop

Phase 1 details:

• Divide:

o Group the elements of A into n/2 pairs

o If n is odd, there is one unpaired element, x

 Check if this x is majority element of A

 If so, then return x, but otherwise discard x

o Compare each pair (y, z)

 If (y==z) keep y and discard z  If (y!=z) discard both y and z

o So we keep ≤ n/2 elements

• Conquer: One recursive call on subarray of size ≤ n/2

(17)

Example:

A = [7, 7, 5, 2, 5, 5, 4, 5, 5, 5, 7]

(7, 7) (5, 2) (5, 5) (4, 5) (5, 5) (7) A = [7, 5, 5]

(7, 5) (5) ⇒ return 5 (candidate, also majority)

Example:

A = [1, 2, 3, 1, 2, 3, 1, 2, 9, 9]

(1, 2) (3, 1) (2, 3) (1, 2) (9, 9)

A = [9] ⇒ return 9 (candidate, but not majority)

Let T(n) = running time of Phase 1 on array of size n

T(n) = T(n/2) + θ(n)

• Number of recursive subproblems = 1

Size of each subproblem = n/2 [worst-case] Time for all the non-recursive steps = θ(n)

Later we’ll see how to solve these kinds of recurrence equations to determine T(n)

(18)

Subhash Suri UC Santa Barbara

Recurrence Solving: Review

T

(

n

) = 2

T

(

n/

2) +

cn

, with

T

(1) = 1.

By term expansion.

T

(

n

) = 2

T

(

n/

2) +

cn

= 2

¡

2

T

(

n/

2

2

) +

cn/

2

¢

+

cn

= 2

2

T

(

n/

2

2

) + 2

cn

= 2

2

¡

2

T

(

n/

2

3

) +

cn/

2

2

¢

+ 2

cn

= 2

3

T

(

n/

2

3

) + 3

cn

...

= 2

i

T

(

n/

2

i

) +

icn

Set

i

= log

2

n

. Use

T

(1) = 1.

(19)

Subhash Suri UC Santa Barbara

The Tree View

T

(

n

) = 2

T

(

n/

2) +

cn

, with

T

(1) = 1.

T(n/4) T(n/4) cn/8 cn/8 cn/8 cn/4 cn/4 T(n/8) cn/2 cn/2 T(n/2) T(n/2) T(n) cn T(n/4)

cn/4 T(n/4)cn/4

cn/8 cn/8 cn/8 cn/8 cn/8 cn Total Cost cn

2(cn/2) = cn

4(cn/4) = cn

8(cn/8) =

#

leaves

=

n

;

#

levels

= log

n

.

(20)

Subhash Suri UC Santa Barbara

Solving By Induction

Recurrence:

T

(

n

) = 2

T

(

n/

2) +

cn

.

Base case:

T

(1) = 1

.

Claim:

T

(

n

) =

cn

log

n

+

cn

.

T

(

n

) = 2

T

(

n/

2) +

cn

= 2 (

c

(

n/

2) log(

n/

2) +

cn/

2) +

cn

=

cn

(log

n

1 + 1) +

cn

(21)

Subhash Suri UC Santa Barbara

More Examples

T

(

n

) = 4

T

(

n/

2) +

cn

,

T

(1) = 1

.

Level Work

0

1

2

3

i

cn

4i cn /2i

= 2 cni 4cn/2 = 2cn

16cn/4 = 4cn n/4

n/2

n

(22)

Subhash Suri UC Santa Barbara

More Examples

Level Work

0

1

2

3

i

cn

4i cn /2i

= 2 cni 4cn/2 = 2cn

16cn/4 = 4cn n/4

n/2

n

n/8

Stops when

n/

2

i

= 1, and

i

= log

n

.

(23)

Subhash Suri UC Santa Barbara

By Term Expansion

T

(

n

) = 4

T

(

n/

2) +

cn

= 4

2

T

(

n/

2

2

) + 2

cn

+

cn

= 4

3

T

(

n/

2

3

) + 2

2

cn

+ 2

cn

+

cn

...

= 4

i

T

(

n/

2

i

) +

cn

¡

2

i−1

+ 2

i−2

+

. . .

+ 2 + 1

¢

= 4

i

T

(

n/

2

i

) + 2

i

cn

Terminates when

2

i

=

n

, or

i

= log

n

.

4

i

= 2

i

×

2

i

=

n

×

n

=

n

2

.

(24)

Subhash Suri UC Santa Barbara

More Examples

T

(

n

) = 2

T

(

n/

4) +

n,

T

(1) = 1

.

T

(n) = 2T

(n/4) +

n

= 2

³

2T

(n/4

2

) +

p

n/4

´

+

n

= 2

2

T

(n/4

2

) + 2

n

= 2

2

³

2T

(n/4

3

) +

p

n/4

2

´

+ 2

n

= 2

3

T

(n/4

3

) + 3

n

...

(25)

Subhash Suri UC Santa Barbara

More Examples

Terminates when

4

i

=

n

, or when

i

= log

4

n

=

log2 n

log2 4

=

12

log

n

.

T

(

n

) = 2

12 logn

+

n

log

4

n

(26)

Subhash Suri UC Santa Barbara

Master Method

T

(n) =

aT

³

n

b

´

+

f

(n)

n n/b n/b2 a children a a a a a a

a f(n/b )2 2

a f(n/b ) af(n/b)

i i

f(n)

(27)

Subhash Suri UC Santa Barbara

Master Method

n

n/b

n/b2

a children

a

a a

a

a a

a f(n/b )2 2

a f(n/b ) af(n/b)

i i

f(n)

Total Cost

#

children multiply by factor

a

at each level.

Number of leaves is

a

logbn

=

n

logb a

. Verify by

(28)

Subhash Suri UC Santa Barbara

Master Method

By recursion tree, we get

T

(

n

) = Θ(

n

logb a

) +

log

X

b n−1

i=0

a

i

f

³

n

b

i

´

Let

f

(n) = Θ(n

p

log

k

n)

, where

p, k

0

.

Important:

a

1

and

b >

1

are constants.

Case I:

p <

log

b

a.

n

logb a

grows faster than

f

(

n

).

(29)

Subhash Suri UC Santa Barbara

Master Method

By recursion tree, we get

T

(

n

) = Θ(

n

logb a

) +

log

X

b n−1

i=0

a

i

f

³

n

b

i

´

Let

f

(n) = Θ(n

p

log

k

n)

, where

p, k

0

.

Case II:

p

= log

b

a.

Both terms have same growth rates.

(30)

Subhash Suri UC Santa Barbara

Master Method

By recursion tree, we get

T

(

n

) = Θ(

n

logb a

) +

log

X

b n−1

i=0

a

i

f

³

n

b

i

´

Let

f

(n) = Θ(n

p

log

k

n)

, where

p, k

0

.

Case III:

p >

log

b

a.

n

logb a

is slower than

f

(

n

).

(31)

Subhash Suri UC Santa Barbara

Applying Master Method

Merge Sort:

T

(

n

) = 2

T

(

n/

2) + Θ(

n

).

a

=

b

= 2,

p

= 1, and

k

= 0. So

log

b

a

= 1, and

p

= log

b

a

. Case II applies, giving us

T

(n) = Θ(n

log

n)

Binary Search:

T

(

n

) =

T

(

n/

2) + Θ(1).

a

= 1

, b

= 2

, p

= 0, and

k

= 0. So

log

b

a

= 0, and

p

= log

b

a

. Case II applies, giving us

(32)

Subhash Suri UC Santa Barbara

Applying Master Method

T

(

n

) = 2

T

(

n/

2) + Θ(

n

log

n

).

a

=

b

= 2,

p

= 1, and

k

= 1.

p

= 1 = log

b

a

, and Case

II applies.

T

(n) = Θ(n

log

2

n)

T

(

n

) = 7

T

(

n/

2) + Θ(

n

2

).

a

= 7

, b

= 2,

p

= 2, and

log

b

2 = log 7

>

2. Case I

applied, and we get

(33)

Subhash Suri UC Santa Barbara

Applying Master Method

T

(

n

) = 4

T

(

n/

2) + Θ(

n

2

n

).

a

= 4

, b

= 2,

p

= 2

.

5, and

k

= 0. So

log

b

a

= 2, and

p >

log

b

a

. Case III applies, giving us

T

(n) = Θ(n

2

n)

T

(

n

) = 2

T

(

n/

2) + Θ

³

n

logn

´

.

(34)

Subhash Suri UC Santa Barbara

Matrix Multiplication

Multiply two

n

×

n

matrices:

C

=

A

×

B

.

Standard method:

C

ij

=

P

nk=1

A

ik

×

B

kj

.

This takes

O(n)

time per element of

C

, for the

total cost of

O(n

3

)

to compute

C

.

This method, known since Gauss’s time, seems

hard to improve.

A very surprising discovery by Strassen (1969)

broke the

n

3

asymptotic barrier.

(35)

Subhash Suri UC Santa Barbara

Divide and Conquer

Let

A, B

be two

n

×

n

matrices. We want to

compute the

n

×

n

matrix

C

=

AB

.

A

=

µ

a

11

a

12

a

21

a

22

B

=

µ

b

11

b

12

b

21

b

22

C

=

µ

c

11

c

12

c

21

c

22

(36)

Subhash Suri UC Santa Barbara

Divide and Conquer

The product matrix can be written as:

c

11

=

a

11

b

11

+

a

12

b

21

c

12

=

a

11

b

12

+

a

12

b

22

c

21

=

a

21

b

11

+

a

22

b

21

c

22

=

a

21

b

12

+

a

22

b

22

Recurrence for this D&C algorithm is

T

(

n

) = 8

T

(

n/

2) +

O

(

n

2

).

(37)

Subhash Suri UC Santa Barbara

Strassen’s Algorithm

Strassen chose these submatrices to multiply:

P

1

= (

a

11

+

a

22

)(

b

11

+

b

22

)

P

2

= (

a

21

+

a

22

)

b

11

P

3

=

a

11

(

b

12

b

22

)

P

4

=

a

22

(

b

21

b

11

)

P

5

= (

a

11

+

a

12

)

b

22

P

6

= (

a

21

a

11

)(

b

11

+

b

12

)

(38)

Subhash Suri UC Santa Barbara

Strassen’s Algorithm

Then,

c

11

=

P

1

+

P

4

P

5

+

P

7

c

12

=

P

3

+

P

5

c

21

=

P

2

+

P

4

c

22

=

P

1

+

P

3

P

2

+

P

6

Recurrence for this algorithm is

(39)

Subhash Suri UC Santa Barbara

Strassen’s Algorithm

The recurrence

T

(

n

) = 7

T

(

n/

2) +

O

(

n

2

)

.

solves to

T

(

n

) =

O

(

n

log2 7

) =

O

(

n

2.81

).

Ever since other researchers have tried other

products to beat this bound.

E.g. Victor Pan discovered a way to multiply two

70

×

70

matrices using

143

,

640

multiplications.

Using more advanced methods, the current best

algorithm for multiplying two

n

×

n

matrices runs

(40)

Subhash Suri UC Santa Barbara

Quick Sort Algorithm

Simple, fast, widely used in practice.

Can be done “in place;” no extra space.

General Form:

1.

Partition:

Divide into two subarrays,

L

and

R

;

elements in

L

are all

smaller

than those in

R

.

2.

Recurse:

Sort

L

and

R

recursively.

3.

Combine:

Append

R

to the end of

L

.

(41)

Subhash Suri UC Santa Barbara

Partition

Partition returns the index of the cell containing

the pivot in the reorganized array.

11 4 9 7 3 10 2 6 13 21 8

(42)

Subhash Suri UC Santa Barbara

Quick Sort Algorithm

QuickSort

(

A, p, q

)

sorts the subarray

A

[

p

· · ·

q

]

.

Initial call with

p

= 0

and

q

=

n

1.

QuickSort(A, p, q

)

if

p

q

then return

i

random(

p, q

)

(43)

Subhash Suri UC Santa Barbara

Analysis of QuickSort

Lucky Case:

Each Partition splits array in halves.

We get

T

(n) = 2T

(n/2) + Θ(n) = Θ(n

log

n)

.

Unlucky Case:

Each partition gives unbalanced

split. We get

T

(

n

) =

T

(

n

1) + Θ(

n

) = Θ(

n

2

).

In worst case, Quick Sort as bad as BubbleSort.

The worst-case occurs when the list is already

sorted, and the last element chosen as pivot.

But, while BubbleSort

always

performs poorly on

certain inputs, because of

random pivot,

(44)

Subhash Suri UC Santa Barbara

Analyzing QuickSort

T

(

n

)

: runtime of randomized QuickSort.

Assume all elements are distinct.

Recurrence for

T

(n)

depends on two subproblem

sizes, which depend on random partition element.

If pivot is

i

smallest

element, then exactly

(

i

1)

items in

L

and

(

n

i

)

in

R

. Call it an

i

-split.

What’s the probability of

i

-split?

(45)

Subhash Suri UC Santa Barbara

Solving the Recurrence

T

(

n

) =

n

X

i=1

1

n

(runtime with

i

-split) +

n

+ 1

=

1

n

n

X

i=1

(

T

(

i

1) +

T

(

n

i

)) +

n

+ 1

=

2

n

n

X

i=1

T

(

i

1) +

n

+ 1

=

2

n

n

X

−1

i=0

(46)

Subhash Suri UC Santa Barbara

Solving the Recurrence

Multiply both sides by

n

. Subtract the same

formula for

n

1

.

nT

(

n

) = 2

n

X

−1

i=0

T

(

i

) +

n

2

+

n

(

n

1)

T

(

n

1) = 2

n

X

−2

i=0

(47)

Subhash Suri UC Santa Barbara

Solving the Recurrence

nT

(

n

) = (

n

+ 1)

T

(

n

1) + 2

n

T

(

n

)

n

+ 1

=

T

(

n

1)

n

+

2

n

+ 1

=

T

(

n

2)

n

1

+

2

n

+

2

n

+ 1

...

=

T

(2)

3

+

n

X

i=3

2

i

= Θ(1) + 2 ln

n

(48)

Subhash Suri UC Santa Barbara

Median Finding

Median

of

n

items is the item with rank

n/

2

.

Rank of an item is its position in the list if the

items were sorted in ascending order.

Rank

i

item also called

ith statistic

.

Example:

{16

,

5

,

30

,

8

,

55}.

Popular statistics are

quantiles

: items of rank

n/4, n/2,

3n/4

.

(49)

Subhash Suri UC Santa Barbara

Median Finding

After spending

O

(

n

log

n

)

time on sorting, any

rank can be found in

O

(

n

)

time.

(50)

Subhash Suri UC Santa Barbara

Min and Max Finding

We can find items of rank 1 or

n

in

O

(

n

)

time.

minimum

(A)

min

A

[0]

for

i

= 1

to

n

1

do

if

min

> A

[

i

]

then

min

A

[

i

];

return

min

The algorithm

minimum

finds the smallest

(rank 1) item in

O

(

n

)

time.

(51)

Subhash Suri UC Santa Barbara

Both Min and Max

Find both min and max using

3

n/

2

comparisons.

MIN-MAX

(A)

if

|

A

|

= 1, then return

min = max =

A

[0]

Divide

A

into two equal subsets

A

1

, A

2

(min

1

,

max

1

) :=

MIN-MAX

(

A

1

)

(min

2

,

max

2

) :=

MIN-MAX

(

A

2

)

if

min

1

min

2

then return

min = min

1

else return

min = min

2

(52)

Subhash Suri UC Santa Barbara

Both Min and Max

The recurrence for this algorithm is

T

(

n

) = 2

T

(

n/

2) + 2

.

(53)

Subhash Suri UC Santa Barbara

Finding Item of Rank

k

Direct extension of min/max finding to rank

k

item will take

Θ(

kn

)

time.

In particular, finding the median will take

Ω(

n

2

)

time, which is worse than sorting.

Median can be used as a perfect pivot for

(deterministic) quick sort.

But only if found faster than sorting itself.

We present a linear time algorithm for selecting

(54)

Subhash Suri UC Santa Barbara

Linear Time Selection

SELECT

(k

)

1.

Divide items into

b

n/

5

c

groups of 5 each.

2.

Find the

median

of each group (using sorting).

3.

Recursively find

median

of

b

n/5

c

group medians.

4.

Partition using median-of-median as pivot.

5.

Let low side have

s

, and high side have

n

s

items.

6.

If

k

s

, call

select

(

k

)

on low side; otherwise, call

(55)

Subhash Suri UC Santa Barbara

Illustration

Divide items into bn/5c groups of 5 items each.

Find the median of each group (using sorting).

Use SELECT to recursively find the median of the bn/5c group medians.

Group 1 Group 2 Group 3 Group 4 Group 5 Group 6

medians

x = median of

(56)

Subhash Suri UC Santa Barbara

Illustration

Partition the input by using this median-of-median as pivot.

Suppose low side of the partition has s elements, and high side has n s elements.

If k s, recursively call SELECT(k) on low side; otherwise, recursively call SELECT(k s) on high side.

Items => x

Group 6

(57)

Subhash Suri UC Santa Barbara

Recurrence

For runtime analysis, we bound the number of

items

x

, the median of medians.

At least half the medians are

x

.

At least half of the

b

n/

5c

groups contribute at

least 3 items to the high side. (Only the last

group can contribute fewer.

Thus, items

x

are at least

3

³

n

10

2

´

3n

10

6.

(58)

Subhash Suri UC Santa Barbara

Recurrence

Recursive call to

select

is on size

7

n/

10 + 6

.

Let

T

(

n

) =

worst-case complexity of

select

.

Group medians, and partition take

O(n)

time.

Step 3 has a recursive call

T

(

n/

5), and Step 5 has

a recursive call

T

(7

n/

10 + 6).

Thus, we have the recurrence:

T

(n)

T

(

n

5

) +

T

(

7n

(59)

Subhash Suri UC Santa Barbara

Recurrence

T

(n)

T

(

n

5

) +

T

(

7n

10

+ 6) +

O(n)

Inductively verify that

T

(

n

)

cn

for some

constant

c

.

T

(

n

)

c

(

n/

5) +

c

(7

n/

10 + 6) +

O

(

n

)

9

cn/

10 + 6

c

+

O

(

n

)

cn

In above, choose

c

so that

c(n/10

6)

beats the

(60)

Subhash Suri UC Santa Barbara

Convex Hulls

1.

Convex hulls

are to CG what sorting is to

discrete algorithms.

2.

First order

shape approximation

. Invariant under

rotation and translation.

p

(61)

Subhash Suri UC Santa Barbara

Convex Hulls

Many aplications in

robotics, shape analysis, line

fitting

etc.

Example:

if

CH

(

P

1

)

CH

(

P

2

) =

∅, then objects

P

1

and

P

2

do not intersect.

Convex Hull Problem:

(62)

Subhash Suri UC Santa Barbara

Divide and Conquer

A B

CH(A) CH(B)

Upper Tangent

Sort points by

X

-coordinates.

Divide points into equal halves

A

and

B

.

Recursively

compute

CH

(

A

)

and

CH

(

B

)

.

(63)

Subhash Suri UC Santa Barbara

Merging Convex Hulls

Lower Tangent

a

=

rightmost point of

CH

(

A

)

.

b

=

leftmost point of

CH

(

B

).

while

ab

not lower tangent of

CH

(A)

and

CH

(B

)

do

1.

while

ab

not lower tangent to

CH

(

A

)

set

a

=

a

1

(move

a

CW);

2.

while

ab

not lower tangent to

CH

(

B

)

set

b

=

b

+ 1

(move

b

CCW);

(64)

Subhash Suri UC Santa Barbara

Tangent Finding

a

b

(65)

Subhash Suri UC Santa Barbara

Analysis of D&C

A B

CH(A) CH(B)

Upper Tangent

Initial sorting takes

O

(

N

log

N

)

time.

Recurrence

T

(

N

) = 2

T

(

N/

2) +

O

(

N

)

O(N

)

for merging (computing tangents)

.

References

Related documents

416. Similar analysis could apply to require reconciliation of courts, like the Fifth Circuit, which have a firm federal j.n.o.v. rule but some panels implying a

16.Effects of the rise and of the fall of the Roman Empire (2.2 IV) Similar question type: Chart showing a pattern, must recognize the underlying cause of pattern and/or the

Between March 2009 and January 2014, 29 newly diagnosed, non-metastatic, biopsy-proven adenocarcinoma localized prostate cancer patients with no prior therapy; referred to

Podľa spoločnosti Microsoft môže dôjsť k strate dôležitých údajov, a to v prípade nastavenia systému Windows na nesprávnej mechanike pevného disku, alebo v prípade

Downloaded from.. TABLE 2 Specificities of the Elecsys syphilis assay using lot A and specificities of comparator assays Result by sample type Elecsys syphilis assay lot A

It is established, that decapitalization of the financial sector in Ukraine in 2014-2016 ac- quired the following main forms: a decrease in equity due to depreciation

This study aimed at assessing the proportions and risk factors of poor immune recovery in adult HIV- infected patients on 48 months of HAART attending care and treatment center (CTC)

Despite overall positive perceptions of the referral system, caregivers in three villages reported that health facilities utilised the VHT as a gatekeeper, noting that if they