3. Merge Sort Algorithm Divide: Divide the n-element sequence into two subsequences of n/2 elements each. Conquer: Sort the two subsequences recursively using merge sort. Combine: Merge the two sorted sequences.
20. Analyzing Divide-and-Conquer Algorithm When an algorithm contains a recursive call to itself, its running time can be described by a recurrence equation or recurrence which describes the running time
21. Recurrence If the problem size is small enough, say n<=c for some constant c, the straightforward solution takes constant time, can be written as θ (1).
22.
23.
24. Recurrence Divide: The divide step computes the middle of the subarray which takes constant time, D(n)= θ (1)
25. Recurrence Conquer: We recursively solve two subproblems, each of size n/2, which contributes 2T(n/2) to the running time.
26. Recurrence Combine: Merge procedure takes θ (n) time on an n-element subarray. C(n)= θ (n) The recurrence T(n)= θ (1) if n=1 2T(n/2) + θ (n) if n>1
27. Recurrence Let us rewrite the recurrence T(n)= C represents the time required to solve problems of size 1 C if n=1 2T(n/2) + cn if n>1
29. A Recursion Tree for the Recurrence Cn Cn/2 Cn/2 T(n/4) T(n/4) T(n/4) T(n/4)
30. A Recursion Tree for the Recurrence C(n) Cn/2 Cn/2 Cn/4 Cn/4 Cn/4 Cn/4 C C C C C C C cn cn cn cn lg n
31. Total Running Time The fully expanded tree has lg n +1 levels and each level contributes a total cost of cn. Therefore T(n)= cn lg n + cn = θ (nlg n)
32. Growth of Functions We look at input sizes large enough to make only the order of growth of the running time relevant.
33. Asymptotic Notation Used to describe running time of an algorithm and defined in terms of functions whose domains are the set of natural numbers N={0,1,2--------------}
34. θ - Notation θ (g(n)) = { f(n) : there exist positive constants C 1 , C 2 , and n 0 such that 0 <= C 1 g(n)<=f(n) <=C 2 g(n) for all n>=n 0 }
35. θ - Notation n 0 n C 2 g(n) C 1 g(n) f(n) f(n)= θ (g(n))
36. θ - Notation For all n>=n 0 , the function f(n) is equal to g(n) to within a constant factor. So g(n) is asymptotically tight bound for f(n).
37. θ - Notation Let us show that 1/2n 2 - 3n= θ (n 2 ) To do so, we must determine C 1 , C 2 , and n 0 such that C 1 n 2 <=1/2n 2 -3n<=C 2 n 2 for all n>=n 0
38. θ - Notation Diving by n 2 C 1 <=1/2 – 3/n <= C 2 By choosing C 1 =1/14 , C 2 =1/2, and n 0 =7, we can verify that 1/2n 2 - 3n= θ (n 2 )
39. O - Notation O (g(n)) = { f(n) : there exist positive constants C and n 0 such that 0 <= f(n) <=Cg(n) for all n>=n 0 }
41. O - Notation O(n 2 ) bound on worst-case running time of insertion sort also applies to its running time on every input.
42. O - Notation θ (n 2 ) bound on worst-case running time of insertion sort, however, does not imply a θ (n 2 ) bound on the running time of insertion sort on every input.
43. Ω - Notation Ω (g(n)) = { f(n) : there exist positive constants C and n 0 such that 0 <= Cg(n)<= f(n) for all n>=n 0 }
45. Ω - Notation Since Ω-notation describes a lower bound, we use it to bound the best-case running time of an algorithm, we also bound the running time of algorithm on arbitrary inputs
46. o - Notation We use o- notation to denote a upper bound that is not asymptotically tight. The bound 2n 2 = O (n 2 ) is asymptotically tight, but the bound 2n= O (n 2 ) is not.
47. ω - Notation We use ω - notation to denote a lower bound that is not asymptotically tight.