T(n) = 4 T(n/3) +  (n)

1 / 50

# T(n) = 4 T(n/3) +  (n) - PowerPoint PPT Presentation

T(n) = 4 T(n/3) +  (n) T(n) = 2 T(n/2) +  (n) E[X i ] = n/(n-i) X=X 0 + … + X n/2-1 E[X]=  n/(n-i) =  (n) 1  n/(n-i)  2 for i  n/2 n/2-1 i=1 compute the median m of A check if m occurs in A more than n/2 times  n/2  m  n/2  -1 n/2  n/2   n/2

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## T(n) = 4 T(n/3) +  (n)

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

E[Xi] = n/(n-i)

X=X0 + … + Xn/2-1

E[X]= n/(n-i) = (n)

1 n/(n-i)2

for in/2

n/2-1

i=1

compute the median m of A

• check if m occurs in A more than n/2 times

n/2

m

n/2 -1 n/2

n/2  n/2

If element occurs in A more then n/2 times

then it must be a median.

compute the n/3-rd and 2n/3-rd smallest

• elements x and y
• 2. check if x or y occurs in A more than n/3 times

y

x

n/3

n/3

n/3

sort array A

• for each b  B, using binary search
• check whether X-b is in A

Step 1 – O(n log n)

Step 2 – n * O(log n) = O(n log n)

1. Find MIN and MAX of A

2. divide the interval [MIN,MAX] into n+1 buckets

3. for each non-empty bucket b, compute

minb and maxb in the bucket

4. for each pair of neighboring non-empty

buckets b,c compare the interval

[maxb,minc] to the longest interval so far.

Optimization problems

Input:

Output:

Objective:

Activity selection problem

Input:list of time-intervals L

Output:a non-overlapping subset S of the intervals

Objective:maximize |S|

3,7

2,4

5,8

6,9

1,11

10,12

0,3

Activity selection problem

Input:list of time-intervals L

Output:a non-overlapping subset S of the intervals

Objective:maximize |S|

3,7

2,4

5,8

6,9

1,11

10,12

0,3

Activity selection problem

Algorithm 1:

1. sort the activities by the starting time

2. pick the first activity a

3. remove all activities conflicting with a

4. repeat

Activity selection problem

Algorithm 1:

1. sort the activities by the starting time

2. pick the first activity a

3. remove all activities conflicting with a

4. repeat

Activity selection problem

Algorithm 1:

1. sort the activities by the starting time

2. pick the first activity a

3. remove all activities conflicting with a

4. repeat

Activity selection problem

Algorithm 2:

1. sort the activities by length

2. pick the shortest activity a

3. remove all activities conflicting with a

4. repeat

Activity selection problem

Algorithm 2:

1. sort the activities by length

2. pick the shortest activity a

3. remove all activities conflicting with a

4. repeat

Activity selection problem

Algorithm 2:

1. sort the activities by length

2. pick the shortest activity a

3. remove all activities conflicting with a

4. repeat

Activity selection problem

Algorithm 3:

1. sort the activities by ending time

2. pick the activity which ends first

3. remove all activities conflicting with a

4. repeat

Activity selection problem

Algorithm 3:

1. sort the activities by ending time

2. pick the activity which ends first

3. remove all activities conflicting with a

4. repeat

Activity selection problem

Algorithm 3:

1. sort the activities by ending time

2. pick the activity which ends first

3. remove all activities conflicting with a

4. repeat

Activity selection problem

Algorithm 3:

1. sort the activities by ending time

2. pick the activity which ends first

3. remove all activities conflicting with a

4. repeat

Activity selection problem

Algorithm 3:

1. sort the activities by ending time

2. pick the activity a which ends first

3. remove all activities conflicting with a

4. repeat

Theorem:

Algorithm 3 gives an optimal solution to

the activity selection problem.

Activity selection problem

Theorem:

Algorithm 3 gives an optimal solution to

the activity selection problem.

Proof idea:

Any solution can be “morphed” to the

solution given by the Algorithm 3.

Activity selection problem

Proof idea:

Any solution can be “morphed” to the

solution given by the Algorithm 3.

Proof:

W.l.o.g., all solutions below are sorted by the ending time.

Let A=[a_1,b_1],…,[a_k,b_k] be the solution output by the

Algorithm 3. Let O=[c_1,d_1],…,[c_l,d_l] be an optimal

solution, which maximizes the length of the prefix common

with A.

A

O

Activity selection problem

Proof:

W.l.o.g., all solutions below are sorted by the ending time.

Let A=[a_1,b_1],…,[a_k,b_k] be the solution output by the

Algorithm 3. Let O=[c_1,d_1],…,[c_l,d_l] be an optimal

solution, which maximizes the length of the prefix common

with A.

A

O

Let m-1 be the length of the common prefix of A and O.

Assume m  k. Then [am,bm] does not intersect [ci,di] for i<m

(since it does not intersect [ai,bi] for i<m). Moreover [am,bm]

does not intersect [ci,di] for i>m (since bm dm and dm<ci

for i>m). Hence we can replace [cm,dm] by [am,.bm], and obtain

another optimal solution which contradicts our choice of O.

Case m>k cannot happen, since Algorithm 3 would output

more activities

Coin change problem

Input:a number P (price)

Output:a way of paying P using bills, coins

Objective:minimize the number of bills, coins used

8.37 = 5 + 1 + 1 + 1 + 0.25 + 0.10 + 0.01 + 0.01

8 bills/coins used

Coin change problem

For simplicity assume that the

coin values are: 10,5,1

Pay(N)

if N  10 then

output(10), Pay(N-10)

elif N  5 then

output(5), Pay(N-5)

elif N  1 then

output(1), Pay(N-1)

Coin change problem

coin values: 10,5,1

Pay(N)

if N  10 then

output(10), Pay(N-10)

elif N  5 then

output(5), Pay(N-5)

elif N  1 then

output(1), Pay(N-1)

Theorem:

For coin values 10,5,1 the Pay algorithm uses

the minimal number of coins.

Coin change problem

coin values: 10,5,1

Theorem:

For coin values 10,5,1 the Pay algorithm uses

the minimal number of coins.

Proof:

Let N be the amount to be paid. Let the optimal solution be

P=A*10 + B*5 + C. Clearly B1 (otherwise we can decrease

B by 2 and increase A by 1, improving the solution). Similarly

C4.

Let the solution given by Pay be P=a*10 + b*5 + c. Clearly

b1 (otherwise the algorithm would output 10 instead of

5). Similarly c4.

Coin change problem

coin values: 10,5,1

Proof:

Let N be the amount to be paid. Let the optimal solution be

P=A*10 + B*5 + C. Clearly B1 (otherwise we can decrease

B by 2 and increase A by 1, improving the solution). Similarly

C4.

Let the solution given by Pay be P=a*10 + b*5 + c. Clearly

b1 (otherwise the algorithm would output 10 instead of

5). Similarly c4.

From 0 C4 and P=(2A+B)*5+C we have C=P mod 5.

Similarly c=P mod 5, and hence c=C. Let Q=(P-C)/5.

From 0 B 1 and Q = 2A + B we have B=Q mod 2.

Similarly b=Q mod 2, and hence b=B.

Thus a=A, b=B, c=C, i.e., the solution given by Pay is optimal.

Coin change problem

For simplicity assume that the

coin values are: 4,3,1

Pay(N)

if N  4 then

output(4), Pay(N-4)

elif N  3 then

output(3), Pay(N-3)

elif N  1 then

output(1), Pay(N-1)

Coin change problem

coin values: 4,3,1

6 = 4 + 1 + 1

6 = 3 + 3

Theorem:

For coin values 4,3,1 the Pay algorithm uses

the minimal number of coins.

Huffman codes

binary character code =

assignment of binary strings to characters

e.g. ASCII code

A = 01000001

B = 01000010

C = 01000011

….

fixed-length code

01000001010000100100001101000001

Huffman codes

binary character code =

assignment of binary strings to characters

e.g. ASCII code

A = 01000001

B = 01000010

C = 01000011

….

fixed-length code

01000001 01000010 01000011 01000001

Huffman codes

binary character code =

assignment of binary strings to characters

E.g.

A = 0

B = 01

C = 11

….

variable-length code

011111111110…

Huffman codes

binary character code =

assignment of binary strings to characters

DEFINITION:

a binary character code is prefix-free, if

no code-word is a prefix

of another code-word

A = 01

B = 101

C = 1011

….

NOT prefix-free

(B is a prefix of C)

Optimal prefix-code problem

Input:alphabet, with frequencies

Output:prefix code

Objective:expected number of bits per character

Optimal prefix-code problem

A 60%

B 20%

C 10%

D 10%

Optimal prefix-code problem

A 60% - 00

B 20% - 01

C 10% - 10

D 10% - 11

2bits per character

Optimal prefix-code problem

A 60% - 0

B 20% - 10

C 10% - 110

D 10% - 111

1.6 bits per character

Optimal prefix-code problem

Huffman ( [a1,f1],[a2,f2],…,[an,fn])

if n=1 then

code[a1] 

else

let fi,fj be the 2 smallest f’s

Huffman ( [ai,fi+fj],[a1,f1],…,[an,fn] )

code[aj]  code[ai] 0

code[ai]  code[ai] 1

Optimal prefix-code problem

Lemma 1:

Let x,y be the symbols with frequencies

fx < fy. Then in an optimal prefix code

length(Cx)  length(Cy).

Optimal prefix-code problem

Lemma 2:

Let C = R 0 be a longest codeword in an

optimal code. Then R 1 is also a codeword.

Lemma 1:

Let x,y be the symbols with frequencies

fx < fy. Then in an optimal prefix code

length(Cx)  length(Cy).

Optimal prefix-code problem

Lemma 3:

Let x,y be the symbols with the smallest

frequencies. Then there exists an optimal

prefix code such that the codewords for

x and y differ only in the last bit.

Lemma 2:

Let C = R 0 be a longest codeword in an

optimal code. Then R 1 is also a codeword.

Lemma 1:

Let x,y be the symbols with frequencies

fx < fy. Then in an optimal prefix code

length(Cx)  length(Cy).

Optimal prefix-code problem

Theorem: the prefix code output by the

Huffman algorithm is optimal.

Lemma 3:

Let x,y be the symbols with the smallest

frequencies. Then there exists an optimal

prefix code such that the codewords for

x and y differ only in the last bit.