divide and conquer algorithms - virginia techcs4104/murali/spring2016/... · 2016. 3. 1. · divide...

34
Divide and Conquer Algorithms T. M. Murali March 1, 2016 T. M. Murali March 1, 2016 Divide and Conquer Algorithms

Upload: others

Post on 02-Mar-2021

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Divide and Conquer Algorithms - Virginia Techcs4104/murali/spring2016/... · 2016. 3. 1. · Divide and Conquer I Break up a problem into several parts. I Solve each part recursively

Divide and Conquer Algorithms

T. M. Murali

March 1, 2016

T. M. Murali March 1, 2016 Divide and Conquer Algorithms

Page 2: Divide and Conquer Algorithms - Virginia Techcs4104/murali/spring2016/... · 2016. 3. 1. · Divide and Conquer I Break up a problem into several parts. I Solve each part recursively

Divide and Conquer

I Break up a problem into several parts.

I Solve each part recursively.

I Solve base cases by brute force.

I Efficiently combine solutions for sub-problems into final solution.

I Common use:I Partition problem into two equal sub-problems of size n/2.I Solve each part recursively.I Combine the two solutions in O(n) time.I Resulting running time is O(n log n).

T. M. Murali March 1, 2016 Divide and Conquer Algorithms

Page 3: Divide and Conquer Algorithms - Virginia Techcs4104/murali/spring2016/... · 2016. 3. 1. · Divide and Conquer I Break up a problem into several parts. I Solve each part recursively

Divide and Conquer

I Break up a problem into several parts.

I Solve each part recursively.

I Solve base cases by brute force.

I Efficiently combine solutions for sub-problems into final solution.

I Common use:I Partition problem into two equal sub-problems of size n/2.I Solve each part recursively.I Combine the two solutions in O(n) time.I Resulting running time is O(n log n).

T. M. Murali March 1, 2016 Divide and Conquer Algorithms

Page 4: Divide and Conquer Algorithms - Virginia Techcs4104/murali/spring2016/... · 2016. 3. 1. · Divide and Conquer I Break up a problem into several parts. I Solve each part recursively

Mergesort

Sort

INSTANCE: Nonempty list L = x1, x2, . . . , xn of integers.

SOLUTION: A permutation y1, y2, . . . , yn of x1, x2, . . . , xn such thatyi ≤ yi+1, for all 1 ≤ i < n.

I Mergesort is a divide-and-conquer algorithm for sorting.

1. Partition L into two lists A and B of size bn/2c and dn/2e respectively.2. Recursively sort A.3. Recursively sort B.4. Merge the sorted lists A and B into a single sorted list.

T. M. Murali March 1, 2016 Divide and Conquer Algorithms

Page 5: Divide and Conquer Algorithms - Virginia Techcs4104/murali/spring2016/... · 2016. 3. 1. · Divide and Conquer I Break up a problem into several parts. I Solve each part recursively

Merging Two Sorted Lists

I Merge two sorted lists A = a1, a2, . . . , ak and B = b1, b2, . . . bl .

Maintain a current pointer for each list.Initialise each pointer to the front of the list.While both lists are nonempty:

Let ai and bj be the elements pointed to by the current pointers.Append the smaller of the two to the output list.Advance the current pointer in the list that the smaller element belonged to.

EndWhileAppend the rest of the non-empty list to the output.

I Running time of this algorithm is O(k + l).

T. M. Murali March 1, 2016 Divide and Conquer Algorithms

Page 6: Divide and Conquer Algorithms - Virginia Techcs4104/murali/spring2016/... · 2016. 3. 1. · Divide and Conquer I Break up a problem into several parts. I Solve each part recursively

Merging Two Sorted Lists

I Merge two sorted lists A = a1, a2, . . . , ak and B = b1, b2, . . . bl .

Maintain a current pointer for each list.Initialise each pointer to the front of the list.While both lists are nonempty:

Let ai and bj be the elements pointed to by the current pointers.Append the smaller of the two to the output list.Advance the current pointer in the list that the smaller element belonged to.

EndWhileAppend the rest of the non-empty list to the output.

I Running time of this algorithm is O(k + l).

T. M. Murali March 1, 2016 Divide and Conquer Algorithms

Page 7: Divide and Conquer Algorithms - Virginia Techcs4104/murali/spring2016/... · 2016. 3. 1. · Divide and Conquer I Break up a problem into several parts. I Solve each part recursively

Analysing Mergesort1. Partition L into two lists A and B of size bn/2c and dn/2e respectively.2. Recursively sort A.3. Recursively sort B.4. Merge the sorted lists A and B into a single sorted list.

Worst-case running time for n elements ≤Worst-case running time for bn/2c elements +Worst-case running time for dn/2e elements +Time to split the input into two lists +Time to merge two sorted lists.

I Assume n is a power of 2.I Define T (n) ≡ Worst-case running time for n elements, for every n ≥ 1.

T (n) ≤ 2T (n/2) + cn, n > 2

T (2) ≤ c

I Three basic ways of solving this recurrence relation:1. “Unroll” the recurrence (somewhat informal method).2. Guess a solution and substitute into recurrence to check.3. Guess solution in O() form and substitute into recurrence to determine the

constants.

T. M. Murali March 1, 2016 Divide and Conquer Algorithms

Page 8: Divide and Conquer Algorithms - Virginia Techcs4104/murali/spring2016/... · 2016. 3. 1. · Divide and Conquer I Break up a problem into several parts. I Solve each part recursively

Analysing Mergesort1. Partition L into two lists A and B of size bn/2c and dn/2e respectively.2. Recursively sort A.3. Recursively sort B.4. Merge the sorted lists A and B into a single sorted list.

Worst-case running time for n elements ≤Worst-case running time for bn/2c elements +Worst-case running time for dn/2e elements +Time to split the input into two lists +Time to merge two sorted lists.

I Assume n is a power of 2.I Define T (n) ≡ Worst-case running time for n elements, for every n ≥ 1.

T (n) ≤ 2T (n/2) + cn, n > 2

T (2) ≤ c

I Three basic ways of solving this recurrence relation:1. “Unroll” the recurrence (somewhat informal method).2. Guess a solution and substitute into recurrence to check.3. Guess solution in O() form and substitute into recurrence to determine the

constants.

T. M. Murali March 1, 2016 Divide and Conquer Algorithms

Page 9: Divide and Conquer Algorithms - Virginia Techcs4104/murali/spring2016/... · 2016. 3. 1. · Divide and Conquer I Break up a problem into several parts. I Solve each part recursively

Analysing Mergesort1. Partition L into two lists A and B of size bn/2c and dn/2e respectively.2. Recursively sort A.3. Recursively sort B.4. Merge the sorted lists A and B into a single sorted list.

Worst-case running time for n elements ≤Worst-case running time for bn/2c elements +Worst-case running time for dn/2e elements +Time to split the input into two lists +Time to merge two sorted lists.

I Assume n is a power of 2.I Define T (n) ≡ Worst-case running time for n elements, for every n ≥ 1.

T (n) ≤ 2T (n/2) + cn, n > 2

T (2) ≤ c

I Three basic ways of solving this recurrence relation:1. “Unroll” the recurrence (somewhat informal method).2. Guess a solution and substitute into recurrence to check.3. Guess solution in O() form and substitute into recurrence to determine the

constants.

T. M. Murali March 1, 2016 Divide and Conquer Algorithms

Page 10: Divide and Conquer Algorithms - Virginia Techcs4104/murali/spring2016/... · 2016. 3. 1. · Divide and Conquer I Break up a problem into several parts. I Solve each part recursively

Analysing Mergesort1. Partition L into two lists A and B of size bn/2c and dn/2e respectively.2. Recursively sort A.3. Recursively sort B.4. Merge the sorted lists A and B into a single sorted list.

Worst-case running time for n elements ≤Worst-case running time for bn/2c elements +Worst-case running time for dn/2e elements +Time to split the input into two lists +Time to merge two sorted lists.

I Assume n is a power of 2.I Define T (n) ≡ Worst-case running time for n elements, for every n ≥ 1.

T (n) ≤ 2T (n/2) + cn, n > 2

T (2) ≤ c

I Three basic ways of solving this recurrence relation:1. “Unroll” the recurrence (somewhat informal method).2. Guess a solution and substitute into recurrence to check.3. Guess solution in O() form and substitute into recurrence to determine the

constants.

T. M. Murali March 1, 2016 Divide and Conquer Algorithms

Page 11: Divide and Conquer Algorithms - Virginia Techcs4104/murali/spring2016/... · 2016. 3. 1. · Divide and Conquer I Break up a problem into several parts. I Solve each part recursively

Analysing Mergesort1. Partition L into two lists A and B of size bn/2c and dn/2e respectively.2. Recursively sort A.3. Recursively sort B.4. Merge the sorted lists A and B into a single sorted list.

Worst-case running time for n elements ≤Worst-case running time for bn/2c elements +Worst-case running time for dn/2e elements +Time to split the input into two lists +Time to merge two sorted lists.

I Assume n is a power of 2.I Define T (n) ≡ Worst-case running time for n elements, for every n ≥ 1.

T (n) ≤ 2T (n/2) + cn, n > 2

T (2) ≤ c

I Three basic ways of solving this recurrence relation:1. “Unroll” the recurrence (somewhat informal method).2. Guess a solution and substitute into recurrence to check.3. Guess solution in O() form and substitute into recurrence to determine the

constants.T. M. Murali March 1, 2016 Divide and Conquer Algorithms

Page 12: Divide and Conquer Algorithms - Virginia Techcs4104/murali/spring2016/... · 2016. 3. 1. · Divide and Conquer I Break up a problem into several parts. I Solve each part recursively

Unrolling the recurrence

I Recursion tree has log n levels.

I Total work done at each level is cn.

I Running time of the algorithm is cn log n.

I Use this method only to get an idea of the solution.

T. M. Murali March 1, 2016 Divide and Conquer Algorithms

Page 13: Divide and Conquer Algorithms - Virginia Techcs4104/murali/spring2016/... · 2016. 3. 1. · Divide and Conquer I Break up a problem into several parts. I Solve each part recursively

Unrolling the recurrence

I Recursion tree has log n levels.

I Total work done at each level is cn.

I Running time of the algorithm is cn log n.

I Use this method only to get an idea of the solution.

T. M. Murali March 1, 2016 Divide and Conquer Algorithms

Page 14: Divide and Conquer Algorithms - Virginia Techcs4104/murali/spring2016/... · 2016. 3. 1. · Divide and Conquer I Break up a problem into several parts. I Solve each part recursively

Substituting a Solution into the Recurrence

I Guess that the solution is T (n) ≤ cn log n (logarithm to the base 2).I Use induction to check if the solution satisfies the recurrence relation.

I Base case: n = 2. Is T (2) = c ≤ 2c log 2? Yes.I (Strong) Inductive hypothesis: assume T (m) ≤ cm log2 m for all m < n.

Therefore, T (n/2) ≤ (cn/2) log(n/2).

I Inductive step: Prove T (n) ≤ cn log n.

T (n) ≤ 2T(n

2

)+ cn

≤ 2

(cn

2log(n

2

))+ cn, by the inductive hypothesis

= cn log(n

2

)+ cn

= cn log n − cn + cn

= cn log n.

I Why is T (n) ≤ kn2 a “loose” bound?I Why doesn’t an attempt to prove T (n) ≤ kn, for some k > 0 work?

T. M. Murali March 1, 2016 Divide and Conquer Algorithms

Page 15: Divide and Conquer Algorithms - Virginia Techcs4104/murali/spring2016/... · 2016. 3. 1. · Divide and Conquer I Break up a problem into several parts. I Solve each part recursively

Substituting a Solution into the Recurrence

I Guess that the solution is T (n) ≤ cn log n (logarithm to the base 2).I Use induction to check if the solution satisfies the recurrence relation.I Base case: n = 2. Is T (2) = c ≤ 2c log 2? Yes.

I (Strong) Inductive hypothesis: assume T (m) ≤ cm log2 m for all m < n.

Therefore, T (n/2) ≤ (cn/2) log(n/2).

I Inductive step: Prove T (n) ≤ cn log n.

T (n) ≤ 2T(n

2

)+ cn

≤ 2

(cn

2log(n

2

))+ cn, by the inductive hypothesis

= cn log(n

2

)+ cn

= cn log n − cn + cn

= cn log n.

I Why is T (n) ≤ kn2 a “loose” bound?I Why doesn’t an attempt to prove T (n) ≤ kn, for some k > 0 work?

T. M. Murali March 1, 2016 Divide and Conquer Algorithms

Page 16: Divide and Conquer Algorithms - Virginia Techcs4104/murali/spring2016/... · 2016. 3. 1. · Divide and Conquer I Break up a problem into several parts. I Solve each part recursively

Substituting a Solution into the Recurrence

I Guess that the solution is T (n) ≤ cn log n (logarithm to the base 2).I Use induction to check if the solution satisfies the recurrence relation.I Base case: n = 2. Is T (2) = c ≤ 2c log 2? Yes.I (Strong) Inductive hypothesis: assume T (m) ≤ cm log2 m for all m < n.

Therefore, T (n/2) ≤ (cn/2) log(n/2).I Inductive step: Prove T (n) ≤ cn log n.

T (n) ≤ 2T(n

2

)+ cn

≤ 2

(cn

2log(n

2

))+ cn, by the inductive hypothesis

= cn log(n

2

)+ cn

= cn log n − cn + cn

= cn log n.

I Why is T (n) ≤ kn2 a “loose” bound?I Why doesn’t an attempt to prove T (n) ≤ kn, for some k > 0 work?

T. M. Murali March 1, 2016 Divide and Conquer Algorithms

Page 17: Divide and Conquer Algorithms - Virginia Techcs4104/murali/spring2016/... · 2016. 3. 1. · Divide and Conquer I Break up a problem into several parts. I Solve each part recursively

Substituting a Solution into the Recurrence

I Guess that the solution is T (n) ≤ cn log n (logarithm to the base 2).I Use induction to check if the solution satisfies the recurrence relation.I Base case: n = 2. Is T (2) = c ≤ 2c log 2? Yes.I (Strong) Inductive hypothesis: assume T (m) ≤ cm log2 m for all m < n.

Therefore, T (n/2) ≤ (cn/2) log(n/2).

I Inductive step: Prove T (n) ≤ cn log n.

T (n) ≤ 2T(n

2

)+ cn

≤ 2

(cn

2log(n

2

))+ cn, by the inductive hypothesis

= cn log(n

2

)+ cn

= cn log n − cn + cn

= cn log n.

I Why is T (n) ≤ kn2 a “loose” bound?I Why doesn’t an attempt to prove T (n) ≤ kn, for some k > 0 work?

T. M. Murali March 1, 2016 Divide and Conquer Algorithms

Page 18: Divide and Conquer Algorithms - Virginia Techcs4104/murali/spring2016/... · 2016. 3. 1. · Divide and Conquer I Break up a problem into several parts. I Solve each part recursively

Substituting a Solution into the Recurrence

I Guess that the solution is T (n) ≤ cn log n (logarithm to the base 2).I Use induction to check if the solution satisfies the recurrence relation.I Base case: n = 2. Is T (2) = c ≤ 2c log 2? Yes.I (Strong) Inductive hypothesis: assume T (m) ≤ cm log2 m for all m < n.

Therefore, T (n/2) ≤ (cn/2) log(n/2).I Inductive step: Prove T (n) ≤ cn log n.

T (n) ≤ 2T(n

2

)+ cn

≤ 2

(cn

2log(n

2

))+ cn, by the inductive hypothesis

= cn log(n

2

)+ cn

= cn log n − cn + cn

= cn log n.

I Why is T (n) ≤ kn2 a “loose” bound?I Why doesn’t an attempt to prove T (n) ≤ kn, for some k > 0 work?

T. M. Murali March 1, 2016 Divide and Conquer Algorithms

Page 19: Divide and Conquer Algorithms - Virginia Techcs4104/murali/spring2016/... · 2016. 3. 1. · Divide and Conquer I Break up a problem into several parts. I Solve each part recursively

Substituting a Solution into the Recurrence

I Guess that the solution is T (n) ≤ cn log n (logarithm to the base 2).I Use induction to check if the solution satisfies the recurrence relation.I Base case: n = 2. Is T (2) = c ≤ 2c log 2? Yes.I (Strong) Inductive hypothesis: assume T (m) ≤ cm log2 m for all m < n.

Therefore, T (n/2) ≤ (cn/2) log(n/2).I Inductive step: Prove T (n) ≤ cn log n.

T (n) ≤ 2T(n

2

)+ cn

≤ 2

(cn

2log(n

2

))+ cn, by the inductive hypothesis

= cn log(n

2

)+ cn

= cn log n − cn + cn

= cn log n.

I Why is T (n) ≤ kn2 a “loose” bound?I Why doesn’t an attempt to prove T (n) ≤ kn, for some k > 0 work?

T. M. Murali March 1, 2016 Divide and Conquer Algorithms

Page 20: Divide and Conquer Algorithms - Virginia Techcs4104/murali/spring2016/... · 2016. 3. 1. · Divide and Conquer I Break up a problem into several parts. I Solve each part recursively

Substituting a Solution into the Recurrence

I Guess that the solution is T (n) ≤ cn log n (logarithm to the base 2).I Use induction to check if the solution satisfies the recurrence relation.I Base case: n = 2. Is T (2) = c ≤ 2c log 2? Yes.I (Strong) Inductive hypothesis: assume T (m) ≤ cm log2 m for all m < n.

Therefore, T (n/2) ≤ (cn/2) log(n/2).I Inductive step: Prove T (n) ≤ cn log n.

T (n) ≤ 2T(n

2

)+ cn

≤ 2

(cn

2log(n

2

))+ cn, by the inductive hypothesis

= cn log(n

2

)+ cn

= cn log n − cn + cn

= cn log n.

I Why is T (n) ≤ kn2 a “loose” bound?

I Why doesn’t an attempt to prove T (n) ≤ kn, for some k > 0 work?

T. M. Murali March 1, 2016 Divide and Conquer Algorithms

Page 21: Divide and Conquer Algorithms - Virginia Techcs4104/murali/spring2016/... · 2016. 3. 1. · Divide and Conquer I Break up a problem into several parts. I Solve each part recursively

Substituting a Solution into the Recurrence

I Guess that the solution is T (n) ≤ cn log n (logarithm to the base 2).I Use induction to check if the solution satisfies the recurrence relation.I Base case: n = 2. Is T (2) = c ≤ 2c log 2? Yes.I (Strong) Inductive hypothesis: assume T (m) ≤ cm log2 m for all m < n.

Therefore, T (n/2) ≤ (cn/2) log(n/2).I Inductive step: Prove T (n) ≤ cn log n.

T (n) ≤ 2T(n

2

)+ cn

≤ 2

(cn

2log(n

2

))+ cn, by the inductive hypothesis

= cn log(n

2

)+ cn

= cn log n − cn + cn

= cn log n.

I Why is T (n) ≤ kn2 a “loose” bound?I Why doesn’t an attempt to prove T (n) ≤ kn, for some k > 0 work?

T. M. Murali March 1, 2016 Divide and Conquer Algorithms

Page 22: Divide and Conquer Algorithms - Virginia Techcs4104/murali/spring2016/... · 2016. 3. 1. · Divide and Conquer I Break up a problem into several parts. I Solve each part recursively

Partial Substitution

I Guess that the solution is kn log n (logarithm to the base 2).

I Substitute guess into the recurrence relation to check what value of k willsatisfy the recurrence relation.

I k ≥ c will work.

T. M. Murali March 1, 2016 Divide and Conquer Algorithms

Page 23: Divide and Conquer Algorithms - Virginia Techcs4104/murali/spring2016/... · 2016. 3. 1. · Divide and Conquer I Break up a problem into several parts. I Solve each part recursively

Partial Substitution

I Guess that the solution is kn log n (logarithm to the base 2).

I Substitute guess into the recurrence relation to check what value of k willsatisfy the recurrence relation.

I k ≥ c will work.

T. M. Murali March 1, 2016 Divide and Conquer Algorithms

Page 24: Divide and Conquer Algorithms - Virginia Techcs4104/murali/spring2016/... · 2016. 3. 1. · Divide and Conquer I Break up a problem into several parts. I Solve each part recursively

Proof for All Values of n

I We assumed n is a power of 2.

I How do we generalise the proof?

I Basic axiom: T (n) ≤ T (n + 1), for all n: worst case running time increasesas input size increases.

I Let m be the smallest power of 2 larger than n.

I T (n) ≤ T (m) = O(m logm) = O(n log n), because m ≤ 2n.

T. M. Murali March 1, 2016 Divide and Conquer Algorithms

Page 25: Divide and Conquer Algorithms - Virginia Techcs4104/murali/spring2016/... · 2016. 3. 1. · Divide and Conquer I Break up a problem into several parts. I Solve each part recursively

Proof for All Values of n

I We assumed n is a power of 2.

I How do we generalise the proof?

I Basic axiom: T (n) ≤ T (n + 1), for all n: worst case running time increasesas input size increases.

I Let m be the smallest power of 2 larger than n.

I T (n) ≤ T (m) = O(m logm)

= O(n log n), because m ≤ 2n.

T. M. Murali March 1, 2016 Divide and Conquer Algorithms

Page 26: Divide and Conquer Algorithms - Virginia Techcs4104/murali/spring2016/... · 2016. 3. 1. · Divide and Conquer I Break up a problem into several parts. I Solve each part recursively

Proof for All Values of n

I We assumed n is a power of 2.

I How do we generalise the proof?

I Basic axiom: T (n) ≤ T (n + 1), for all n: worst case running time increasesas input size increases.

I Let m be the smallest power of 2 larger than n.

I T (n) ≤ T (m) = O(m logm) = O(n log n), because m ≤ 2n.

T. M. Murali March 1, 2016 Divide and Conquer Algorithms

Page 27: Divide and Conquer Algorithms - Virginia Techcs4104/murali/spring2016/... · 2016. 3. 1. · Divide and Conquer I Break up a problem into several parts. I Solve each part recursively

Other Recurrence Relations

I Divide into q sub-problems of size n/2 and merge in O(n) time. Two distinctcases: q = 1 and q > 2.

I Divide into two sub-problems of size n/2 and merge in O(n2) time.

T. M. Murali March 1, 2016 Divide and Conquer Algorithms

Page 28: Divide and Conquer Algorithms - Virginia Techcs4104/murali/spring2016/... · 2016. 3. 1. · Divide and Conquer I Break up a problem into several parts. I Solve each part recursively

T (n) = qT (n/2) + cn, q = 1

I Each invocation reduces the problem size by a factor of 2 ⇒ there are log nlevels in the recursion tree.

I At level i of the tree, the problem size is n/2i and the work done is cn/2i .I Therefore, the total work done is

i=log n∑i=0

cn

2i= O(n).

T. M. Murali March 1, 2016 Divide and Conquer Algorithms

Page 29: Divide and Conquer Algorithms - Virginia Techcs4104/murali/spring2016/... · 2016. 3. 1. · Divide and Conquer I Break up a problem into several parts. I Solve each part recursively

T (n) = qT (n/2) + cn, q = 1

I Each invocation reduces the problem size by a factor of 2 ⇒ there are log nlevels in the recursion tree.

I At level i of the tree, the problem size is n/2i and the work done is cn/2i .I Therefore, the total work done is

i=log n∑i=0

cn

2i= O(n).

T. M. Murali March 1, 2016 Divide and Conquer Algorithms

Page 30: Divide and Conquer Algorithms - Virginia Techcs4104/murali/spring2016/... · 2016. 3. 1. · Divide and Conquer I Break up a problem into several parts. I Solve each part recursively

T (n) = qT (n/2) + cn, q > 2

I There are log n levels in the recursion tree.I At level i of the tree, there are qi sub-problems, each of size n/2i .I The total work done at level i is qicn/2i . Therefore, the total work done is

T (n) ≤i=log2 n∑

i=0

qicn

2i≤ cn

i=log2 n∑i=0

(q2

)i= O

(cn(q

2

)log2 n )= O

(cn(q

2

)(logq/2 n)(log2 q/2))= O(cn nlog2 q/2) = O(nlog2 q).

T. M. Murali March 1, 2016 Divide and Conquer Algorithms

Page 31: Divide and Conquer Algorithms - Virginia Techcs4104/murali/spring2016/... · 2016. 3. 1. · Divide and Conquer I Break up a problem into several parts. I Solve each part recursively

T (n) = qT (n/2) + cn, q > 2

I There are log n levels in the recursion tree.I At level i of the tree, there are qi sub-problems, each of size n/2i .I The total work done at level i is qicn/2i . Therefore, the total work done is

T (n) ≤i=log2 n∑

i=0

qicn

2i≤

cn

i=log2 n∑i=0

(q2

)i= O

(cn(q

2

)log2 n )= O

(cn(q

2

)(logq/2 n)(log2 q/2))= O(cn nlog2 q/2) = O(nlog2 q).

T. M. Murali March 1, 2016 Divide and Conquer Algorithms

Page 32: Divide and Conquer Algorithms - Virginia Techcs4104/murali/spring2016/... · 2016. 3. 1. · Divide and Conquer I Break up a problem into several parts. I Solve each part recursively

T (n) = qT (n/2) + cn, q > 2

I There are log n levels in the recursion tree.I At level i of the tree, there are qi sub-problems, each of size n/2i .I The total work done at level i is qicn/2i . Therefore, the total work done is

T (n) ≤i=log2 n∑

i=0

qicn

2i≤ cn

i=log2 n∑i=0

(q2

)i= O

(cn(q

2

)log2 n )= O

(cn(q

2

)(logq/2 n)(log2 q/2))= O(cn nlog2 q/2) = O(nlog2 q).

T. M. Murali March 1, 2016 Divide and Conquer Algorithms

Page 33: Divide and Conquer Algorithms - Virginia Techcs4104/murali/spring2016/... · 2016. 3. 1. · Divide and Conquer I Break up a problem into several parts. I Solve each part recursively

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

I Total work done isi=log n∑i=0

2i(cn

2i

)2≤

O(n2).

T. M. Murali March 1, 2016 Divide and Conquer Algorithms

Page 34: Divide and Conquer Algorithms - Virginia Techcs4104/murali/spring2016/... · 2016. 3. 1. · Divide and Conquer I Break up a problem into several parts. I Solve each part recursively

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

I Total work done isi=log n∑i=0

2i(cn

2i

)2≤ O(n2).

T. M. Murali March 1, 2016 Divide and Conquer Algorithms