SlideShare a Scribd company logo
1 of 48
270-1/02-divide-and-conquer_handout.pdf
CS 270
Algorithms
Oliver
Kullmann
Growth of
Functions
Divide-and-
Conquer
Min-Max-
Problem
Tutorial
Week 2
Divide and Conquer
1 Growth of Functions
2 Divide-and-Conquer
Min-Max-Problem
3 Tutorial
CS 270
Algorithms
Oliver
Kullmann
Growth of
Functions
Divide-and-
Conquer
Min-Max-
Problem
Tutorial
General remarks
First we consider an important tool for the analysis of
algorithms: Big-Oh.
Then we introduce an important algorithmic paradigm:
Divide-and-Conquer.
We conclude by presenting and analysing a simple example.
Reading from CLRS for week 2
Chapter 2, Section 3
Chapter 3
CS 270
Algorithms
Oliver
Kullmann
Growth of
Functions
Divide-and-
Conquer
Min-Max-
Problem
Tutorial
Growth of Functions
A way to describe behaviour of functions in the limit. We
are studying asymptotic efficiency.
Describe growth of functions.
Focus on what’s important by abstracting away low-order
terms and constant factors.
How we indicate running times of algorithms.
A way to compare “sizes” of functions:
O corresponds to ≤
Ω corresponds to ≥
Θ corresponds to =
We consider only functions f , g : N → R≥0.
CS 270
Algorithms
Oliver
Kullmann
Growth of
Functions
Divide-and-
Conquer
Min-Max-
Problem
Tutorial
O-Notation
O
(
g(n)
)
is the set of all functions f (n) for which there are
positive constants c and n0 such that
f (n) ≤ cg(n) for all n ≥ n0.
cg(n)
f (n)
n
n0
g(n) is an asymptotic upper bound for f (n).
If f (n) ∈ O(g(n)), we write f (n) = O(g(n)) (we will precisely
explain this soon)
CS 270
Algorithms
Oliver
Kullmann
Growth of
Functions
Divide-and-
Conquer
Min-Max-
Problem
Tutorial
O-Notation Examples
2n2 = O(n3), with c = 1 and n0 = 2.
Example of functions in O(n2):
n2
n2 + n
n2 + 1000n
1000n2 + 1000n
Also
n
n/1000
n1.999999
n2/ lg lg lg n
CS 270
Algorithms
Oliver
Kullmann
Growth of
Functions
Divide-and-
Conquer
Min-Max-
Problem
Tutorial
Ω-Notation
Ω
(
g(n)
)
is the set of all functions f (n) for which there are
positive constants c and n0 such that
f (n) ≥ cg(n) for all n ≥ n0.
cg(n)
f (n)
n
n0
g(n) is an asymptotic lower bound for f (n).
CS 270
Algorithms
Oliver
Kullmann
Growth of
Functions
Divide-and-
Conquer
Min-Max-
Problem
Tutorial
Ω-Notation Examples
√
n = Ω(lg n), with c = 1 and n0 = 16.
Example of functions in Ω(n2):
n2
n2 + n
n2 − n
1000n2 + 1000n
1000n2 − 1000n
Also
n3
n2.0000001
n2 lg lg lg n
22
n
CS 270
Algorithms
Oliver
Kullmann
Growth of
Functions
Divide-and-
Conquer
Min-Max-
Problem
Tutorial
Θ-Notation
Θ
(
g(n)
)
is the set of all functions f (n) for which there are
positive constants c1, c2 and n0 such that
c1g(n) ≤ f (n) ≤ c2g(n) for all n ≥ n0.
c2g(n)
c1g(n)
f (n)
n
n0
g(n) is an asymptotic tight bound for f (n).
CS 270
Algorithms
Oliver
Kullmann
Growth of
Functions
Divide-and-
Conquer
Min-Max-
Problem
Tutorial
Θ-Notation (cont’d)
Examples 1
n2/2 − 2n = Θ(n2), with c1 = 14, c2 =
1
2
, and n0 = 8.
Theorem 2
f (n) = Θ(g(n)) if and only if f (n) = O(g(n)) and
f (n) = Ω(g(n)).
Leading constants and lower order terms do not matter.
CS 270
Algorithms
Oliver
Kullmann
Growth of
Functions
Divide-and-
Conquer
Min-Max-
Problem
Tutorial
Asymptotic notation in equations
When on right-hand side
Θ(n2) stands for some anonymous function in the set Θ(n2).
2n2 + 3n + 1 = 2n2 + Θ(n) means 2n2 + 3n + 1 = 2n2 + f (n)
for some f (n) ∈ Θ(n). In particular, f (n) = 3n + 1.
When on left-hand side
No matter how the anonymous functions are chosen on the
left-hand side, there is a way to choose the anonymous
functions
on the right-hand side to make the equation valid.
Interpret 2n2 + Θ(n) = Θ(n2) as meaning for all functions
f (n) ∈ Θ(n), there exists a function g(n) ∈ Θ(n2) such that
2n2 + f (n) = g(n).
CS 270
Algorithms
Oliver
Kullmann
Growth of
Functions
Divide-and-
Conquer
Min-Max-
Problem
Tutorial
Asymptotic notation chained together
2n2 + 3n + 1 = 2n2 + Θ(n) = Θ(n2)
Interpretation:
First equation: There exists f (n) ∈ Θ(n) such that
2n2 + 3n + 1 = 2n2 + f (n).
Second equation: For all g(n) ∈ Θ(n) (such as the f (n)
used to make the first equation hold), there exists
h(n) ∈ Θ(n2) such that 2n2 + g(n) = h(n).
Note
What has been said of “Θ” on this and the previous slide also
applies to “O” and “Ω”.
CS 270
Algorithms
Oliver
Kullmann
Growth of
Functions
Divide-and-
Conquer
Min-Max-
Problem
Tutorial
Example Analysis
Insertion-Sort(A)
1 for j = 2 to A.length
2 key = A[j]
3 // Insert A[j] into sorted sequence A[1 . . j−1].
4 i = j−1
5 while i > 0 and A[i] > key
6 A[i+1] = A[i]
7 i = i−1
8 A[i+1] = key
The for -loop on line 1 is executed O(n) times; and each
statement costs constant time, except for the while -loop on
lines 5-7 which costs O(n).
Thus overall runtime is: O(n) × O(n) = O(n2).
Note: In fact, as seen last week, worst-case runtime is Θ(n2).
CS 270
Algorithms
Oliver
Kullmann
Growth of
Functions
Divide-and-
Conquer
Min-Max-
Problem
Tutorial
Divide-and-Conquer Approach
There are many ways to design algorithms.
For example, insertion sort is incremental: having sorted
A[1 . . j−1], place A[j] correctly, so that A[1 . . j] is sorted.
Divide-and-Conquer is another common approach:
Divide the problem into a number of subproblems that are
smaller instances of the same problem.
Conquer the subproblems by solving them recursively.
Base case: If the subproblem are small enough, just solve
them by brute force.
Combine the subproblem solutions to give a solution to the
original problem.
CS 270
Algorithms
Oliver
Kullmann
Growth of
Functions
Divide-and-
Conquer
Min-Max-
Problem
Tutorial
Naive Min-Max
Find minimum and maximum of a list A of n>0 numbers.
Naive-Min-Max(A)
1 least = A[1]
2 for i = 2 to A.length
3 if A[i] < least
4 least = A[i]
5 greatest = A[1]
6 for i = 2 to A.length
7 if A[i] > greatest
8 greatest = A[i]
9 return (least, greatest)
The for-loop on line 2 makes n−1 comparisons, as does the
for-loop on line 6, making a total of 2n−2 comparisons.
Can we do better? Yes!
CS 270
Algorithms
Oliver
Kullmann
Growth of
Functions
Divide-and-
Conquer
Min-Max-
Problem
Tutorial
Divide-and-Conquer Min-Max
As we are dealing with subproblems, we state each subproblem
as computing minimum and maximum of a subarray A[p . . q].
Initially, p = 1 and q = A.length, but these values change as we
recurse through subproblems.
To compute minimum and maximum of A[p . . q]:
Divide by splitting into two subarrays A[p . . r] and A[r+1 . . q],
where r is the halfway point of A[p . . q].
Conquer by recursively computing minimum and maximum of
the two subarrays A[p . . r] and A[r+1 . . q].
Combine by computing the overall minimum as the min of the
two recursively computed minima, similar for the overall
maximum.
CS 270
Algorithms
Oliver
Kullmann
Growth of
Functions
Divide-and-
Conquer
Min-Max-
Problem
Tutorial
Divide-and-Conquer Min-Max Algorithm
Initially called with Min-Max(A, 1, A.length).
Min-Max(A, p, q)
1 if p = q
2 return (A[p], A[q])
3 if p = q−1
4 if A[p] < A[q]
5 return (A[p], A[q])
6 else return (A[q], A[p])
7 r = ⌊ (p+q)/2⌋
8 (min1, max1) = Min-Max(A, p, r)
9 (min2, max2) = Min-Max(A, r+1, q)
10 return
(
min(min1, min2), max(max1, max2)
)
Note
In line 7, r computes the halfway point of A[p . . q].
n = q − p + 1 is the number of elements from which we compute
the min and max.
CS 270
Algorithms
Oliver
Kullmann
Growth of
Functions
Divide-and-
Conquer
Min-Max-
Problem
Tutorial
Solving the Min-Max Recurrence
Let T(n) be the number of comparisons made by
Min-Max(A, p, q), where n = q−p+1 is the number of
elements from which we compute the min and max.
Then T(1) = 0, T(2) = 1, and for n > 2:
T(n) = T (⌈ n/2⌉ ) + T (⌊ n/2⌋ ) + 2.
Claim
T(n) = 3
2
n − 2 for n = 2k ≥ 2, i.e., powers of 2.
Proof.
The proof is by induction on k (using n = 2k).
Base case: true for k=1, as T(21) = 1 = 3
2
· 21 − 2.
Induction step: assuming T(2k) = 3
2
2k − 2, we get
T(2k+1) = 2T(2k)+2 = 2
(
3
2
2k−2
)
+2 = 3
2
2k+1−2
CS 270
Algorithms
Oliver
Kullmann
Growth of
Functions
Divide-and-
Conquer
Min-Max-
Problem
Tutorial
Solving the Min-Max Recurrence (cont’d)
Some remarks:
1 If we replace line 7 of the algorithm by r = p+1, then the
resulting runtime T ′(n) satisfies T ′(n) =
⌈
3n
2
⌉
−2 for all
n > 0.
2 For example, T ′(6) = 7 whereas T(6) = 8.
3 It can be shown that at least
⌈
3n
2
⌉
− 2 comparisons are
necessary in the worst case to find the maximum and
minimum of n numbers for any comparison-based
algorithm: this is thus a lower bound on the problem.
4 Hence this (last) algorithm is provably optimal.
CS 270
Algorithms
Oliver
Kullmann
Growth of
Functions
Divide-and-
Conquer
Min-Max-
Problem
Tutorial
Big-Oh, Omega, Theta by examples
1 5n + 111 = O(n) ? YES
2 5n + 111 = O(n2) ? YES
3 5n + 111 = Ω(n) ? YES
4 5n + 111 = Ω(n2) ? NO
5 5n + 111 = Θ(n) ? YES
6 5n + 111 = Θ(n2) ? NO
7 2n = O(3n) ? YES
8 2n = Ω(3n) ? NO
9 120n2 +
√
n + 99n = O(n2) ? YES
10 120n2 +
√
n + 99n = Θ(n2) ? YES
11 sin(n) = O(1) ? YES
CS 270
Algorithms
Oliver
Kullmann
Growth of
Functions
Divide-and-
Conquer
Min-Max-
Problem
Tutorial
Can we improve the Min-Max algorithm?
Determine T(6), the number of comparisons the Min-Max
algorithms performs for 6 elements.
As usual, the argumentation is important (why is this
correct?).
Perhaps best is that you run the algorithm (on paper) for 6
elements.
Notice that the basic parameters for a run do not depend on
the values of the elements, but only on their total number.
Once you found T(6), is this really optimal?
CS 270
Algorithms
Oliver
Kullmann
Growth of
Functions
Divide-and-
Conquer
Min-Max-
Problem
Tutorial
Unfolding the recursion for Min-Max
We have
T(n) =
0 if n = 1
1 if n = 2
T(
⌈
n
2
⌉
) + T(
⌊
n
2
⌋
) + 2 else
.
1 T(1) = 0
2 T(2) = 1
3 T(3) = T(2) + T(1) + 2 = 1 + 0 + 2 = 3
4 T(4) = T(2) + T(2) + 2 = 1 + 1 + 2 = 4
5 T(5) = T(3) + T(2) + 2 = 3 + 1 + 2 = 6
6 T(6) = T(3) + T(3) + 2 = 3 + 3 + 2 = 8
7 T(7) = T(4) + T(3) + 2 = 4 + 3 + 2 = 9
8 T(8) = T(4) + T(4) + 2 = 4 + 4 + 2 = 10
9 T(9) = T(5) + T(4) + 2 = 6 + 4 + 2 = 12
10 T(10) = T(5) + T(5) + 2 = 6 + 6 + 2 = 14.
We count 4 steps +1 and 5 steps +2 — we guess T(n) ≈ 3
2
n.
CS 270
Algorithms
Oliver
Kullmann
Growth of
Functions
Divide-and-
Conquer
Min-Max-
Problem
Tutorial
Finding the best min-max algorithm
1 As you can see in the section on the min-max problem, for
some input sizes we can validate the guess T(n) ≈ 3
2
n.
2 One can now try to find a precise general formula for T(n),
3 However we see that we have T(6) = 8, while we can
handle this case with 7 comparisons. So perhaps we can
find a better algorithm?
4 And that is the case:
1 If n is even, find the min-max for the first two elements
using 1 comparison; if n is odd, find the min-max for the
first element using 0 comparisons.
2 Now iteratively find the min-max of the next two elements
using 1 comparison, and compute the new current min-max
using 2 further comparisons. And so on ....
This yields an algorithm using precisely
⌈
3
2
n
⌉
− 2
comparisons. And this is precisely optimal for all n.
We learn: Here divide-and-conqueor provided a good stepping
stone to find a really good algorithm.
CS 270
Algorithms
Oliver
Kullmann
Growth of
Functions
Divide-and-
Conquer
Min-Max-
Problem
Tutorial
Designing an algorithm: Median
The median of a sequence of numbers is the “middle value” —
the value in the middle position of the list after sorting.
Can we do better than the obvious algorithm (by sorting),
using divide-and-conquer?
(Here some judgement is needed, that the precise details about
what actually is the “middle value” won’t make a fundamental
difference, and so it’s best to ignore them for the initial phase,
where developing the ideas is of utmost importance.)
CS 270
Algorithms
Oliver
Kullmann
Growth of
Functions
Divide-and-
Conquer
Min-Max-
Problem
Tutorial
First strategy: divide in half
1 Divide the array A of numbers into two parts, B and C, of
equal size (more precisely, nearly equal size, but such details
are best ignored in the beginning).
2 In principle B and C could be anything, but easiest is to let
them be the first half and the second half of A.
3 Compute (that is now the conquer-phase) the medians
mB, mC of the arrays B, C.
4 If mC < mB, then swap B and C.
5 Re-order B and C internally, so that the elements smaller
than the median are on the left, and the larger elements on
the right.
6 Now consider the array of elements from mB to mC (note
that this is again half of the size of A): The median of this
array (computed again in the conquer-phase, recursively) is
the median of A.
CS 270
Algorithms
Oliver
Kullmann
Growth of
Functions
Divide-and-
Conquer
Min-Max-
Problem
Tutorial
Example run
1, 20, 5, 18, 7, 10, 10 | 4, 20, 7, 7, 17, 14, 1, 12
of length 15, partitioned into 7 and 8 elements.
The left median is 10. The right median is 7 or 12; let’s take 7.
Swap, and partition the two parts according to their medians:
4, 7, 1, 7, 20, 17, 14, 12 | 1, 5, 7, 10, 20, 18, 10.
Compute the median of the new middle part
20, 17, 14, 12, 1, 5, 7, 10
which is 10 (the right answer) or 12.
So, it could work (or not).
CS 270
Algorithms
Oliver
Kullmann
Growth of
Functions
Divide-and-
Conquer
Min-Max-
Problem
Tutorial
The recurrence
We are in this phase only interested in the recurrence:
T(n) = 3 · T(n/2) + O(n).
We ignore here all issues about the precise partitioning (and
whether we can get it to work at all!) — all what counts here is
the insight whether we could get a good algorithm!
Next week we develop tools to see immediately
how good this approach could be.
Divide and ConquerGrowth of FunctionsDivide-and-
ConquerMin-Max-ProblemTutorial
270-1/03-solving-recurrences_handout.pdf
CS 270
Algorithms
Oliver
Kullmann
Divide-and-
Conquer
Merge Sort
Solving
Recurrences
Recursion
Trees
Master
Theorem
Divide-and-
Conquer
Matrix
multiplication
Tutorial
Week 3
Solving Recurrences
1 Divide-and-Conquer
Merge Sort
2 Solving Recurrences
Recursion Trees
Master Theorem
3 Divide-and-Conquer
Matrix multiplication
4 Tutorial
CS 270
Algorithms
Oliver
Kullmann
Divide-and-
Conquer
Merge Sort
Solving
Recurrences
Recursion
Trees
Master
Theorem
Divide-and-
Conquer
Matrix
multiplication
Tutorial
General remarks
First we continue with an important example for
Divide-and-Conquer, namely Merge Sort.
Then we present a basic tool for analysing algorithms by
Solving Recurrences.
We conclude by considering another example, namely
Matrix Multiplication.
Reading from CLRS for week 3
Chapter 4
CS 270
Algorithms
Oliver
Kullmann
Divide-and-
Conquer
Merge Sort
Solving
Recurrences
Recursion
Trees
Master
Theorem
Divide-and-
Conquer
Matrix
multiplication
Tutorial
Another example: Merge-Sort
A sorting algorithm based on divide and conquer. The worst-
case
running time has a lower order of growth than insertion sort.
Again we are dealing with subproblems of sorting subarrays
A[p . . q] Initially, p = 1 and q = A.length, but these values
change again as we recurse through subproblems.
To sort A[p . . q]:
Divide by splitting into two subarrays A[p . . . r] and
A[r+1 . . . q], where r is the halfway point of A[p . . . q].
Conquer by recursively sorting the two subarrays A[p . . . r] and
A[r+1 . . . q].
Combine by merging the two sorted subarrays A[p . . . r] and
A[r+1 . . . q] to produce a single sorted subarray A[p . . . q].
The recursion bottoms out when the subarray has just 1
element, so that it is trivially sorted.
CS 270
Algorithms
Oliver
Kullmann
Divide-and-
Conquer
Merge Sort
Solving
Recurrences
Recursion
Trees
Master
Theorem
Divide-and-
Conquer
Matrix
multiplication
Tutorial
Another example: Merge-Sort
Merge-Sort(A, p, q)
1 if p < q // check for base case
2 r = ⌊ (p+q)/2⌋ // divide
3 Merge-Sort(A, p, r) // conquer
4 Merge-Sort(A, r+1, q) // conquer
5 Merge(A, p, r, q) // combine
Initial call: Merge-Sort(A, 1, A.length)
CS 270
Algorithms
Oliver
Kullmann
Divide-and-
Conquer
Merge Sort
Solving
Recurrences
Recursion
Trees
Master
Theorem
Divide-and-
Conquer
Matrix
multiplication
Tutorial
Merge
Input: Array A and indices p, r, q such that
p ≤ r < q
Subarrays A[p . . r] and subarray A[r+1 . . q] are sorted. By
the restriction on p, r, q neither subarray is empty.
Output: The two subarrays are merged into a single sorted
subarray in A[p . . q].
We implement is so that it takes Θ(n) time, with
n = q − p + 1 = the number of elements being merged.
CS 270
Algorithms
Oliver
Kullmann
Divide-and-
Conquer
Merge Sort
Solving
Recurrences
Recursion
Trees
Master
Theorem
Divide-and-
Conquer
Matrix
multiplication
Tutorial
Merge(A, p, r, q)
1 n1 = r − p + 1
2 n2 = q − r
3 let L[1 . . n1+1] and R[1 . . n2+1] be new arrays
4 for i = 1 to n1
5 L[i] = A[p+i−1]
6 for j = 1 to n2
7 R[j] = A[r+j]
8 L[n1+1] = R[n2+1] = ∞
9 i = j = 1
10 for k = p to q
11 if L[i] ≤ R[j]
12 A[k] = L[i]
13 i = i+1
14 else A[k] = R[j]
15 j = j+1
CS 270
Algorithms
Oliver
Kullmann
Divide-and-
Conquer
Merge Sort
Solving
Recurrences
Recursion
Trees
Master
Theorem
Divide-and-
Conquer
Matrix
multiplication
Tutorial
Analysis of Merge-Sort
The runtime T(n), where n = q−p+1 > 1, satisfies:
T(n) = 2T(n/2) + Θ(n).
We will show that T(n) = Θ(n lg n).
It can be shown (see tutorial-section) that Ω(n lg n)
comparisons are necessary in the worst case to sort n
numbers for any comparison-based algorithm: this is thus
an (asymptotic) lower bound on the problem.
Hence Merge-Sort is provably (asymptotically) optimal.
CS 270
Algorithms
Oliver
Kullmann
Divide-and-
Conquer
Merge Sort
Solving
Recurrences
Recursion
Trees
Master
Theorem
Divide-and-
Conquer
Matrix
multiplication
Tutorial
Analysing divide-and-conquer algorithms
Recall the divide-and-conquer paradigm:
Divide the problem into a number of subproblems that are
smaller instances of the same problem.
Conquer the subproblems by solving them recursively.
Base case: If the subproblem are small enough, just solve
them by brute force.
Combine the subproblem solutions to give a solution to the
original problem.
We use recurrences to characterise the running time of a
divide-and-conquer algorithm. Solving the recurrence gives us
the asymptotic running time.
A recurrence is a function defined in terms of
one or more base cases, and
itself, with smaller arguments
CS 270
Algorithms
Oliver
Kullmann
Divide-and-
Conquer
Merge Sort
Solving
Recurrences
Recursion
Trees
Master
Theorem
Divide-and-
Conquer
Matrix
multiplication
Tutorial
Examples for recurrences
T(n) =
{
1 if n = 1
T(n − 1) + 1 if n > 1
Solution
: T(n) = n.
T(n) =
{
1 if n = 1
2T(n/2) + n if n > 1

More Related Content

Similar to 270-102-divide-and-conquer_handout.pdfCS 270Algorithm.docx

how to calclute time complexity of algortihm
how to calclute time complexity of algortihmhow to calclute time complexity of algortihm
how to calclute time complexity of algortihmSajid Marwat
 
Asymptotic Notation and Complexity
Asymptotic Notation and ComplexityAsymptotic Notation and Complexity
Asymptotic Notation and ComplexityRajandeep Gill
 
Introduction
IntroductionIntroduction
Introductionpilavare
 
Analysis Of Algorithms I
Analysis Of Algorithms IAnalysis Of Algorithms I
Analysis Of Algorithms ISri Prasanna
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquerVikas Sharma
 
Analysis Of Algorithms Ii
Analysis Of Algorithms IiAnalysis Of Algorithms Ii
Analysis Of Algorithms IiSri Prasanna
 
Dr hasany 2467_16649_1_lec-2-zabist
Dr hasany 2467_16649_1_lec-2-zabistDr hasany 2467_16649_1_lec-2-zabist
Dr hasany 2467_16649_1_lec-2-zabistGatewayggg Testeru
 
Algorithm Design and Analysis
Algorithm Design and AnalysisAlgorithm Design and Analysis
Algorithm Design and AnalysisReetesh Gupta
 
01 - DAA - PPT.pptx
01 - DAA - PPT.pptx01 - DAA - PPT.pptx
01 - DAA - PPT.pptxKokilaK25
 
04. Growth_Rate_AND_Asymptotic Notations_.pptx
04. Growth_Rate_AND_Asymptotic Notations_.pptx04. Growth_Rate_AND_Asymptotic Notations_.pptx
04. Growth_Rate_AND_Asymptotic Notations_.pptxarslanzaheer14
 
01 - Analysis of Efficiency.pptx
01 - Analysis of Efficiency.pptx01 - Analysis of Efficiency.pptx
01 - Analysis of Efficiency.pptxIshtiaq Rasool Khan
 
Solving recurrences
Solving recurrencesSolving recurrences
Solving recurrencesMegha V
 

Similar to 270-102-divide-and-conquer_handout.pdfCS 270Algorithm.docx (20)

Divide and Conquer
Divide and ConquerDivide and Conquer
Divide and Conquer
 
how to calclute time complexity of algortihm
how to calclute time complexity of algortihmhow to calclute time complexity of algortihm
how to calclute time complexity of algortihm
 
Time complexity.ppt
Time complexity.pptTime complexity.ppt
Time complexity.ppt
 
3.pdf
3.pdf3.pdf
3.pdf
 
Asymptotic Notation and Complexity
Asymptotic Notation and ComplexityAsymptotic Notation and Complexity
Asymptotic Notation and Complexity
 
Daa notes 2
Daa notes 2Daa notes 2
Daa notes 2
 
Introduction
IntroductionIntroduction
Introduction
 
Analysis Of Algorithms I
Analysis Of Algorithms IAnalysis Of Algorithms I
Analysis Of Algorithms I
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Analysis Of Algorithms Ii
Analysis Of Algorithms IiAnalysis Of Algorithms Ii
Analysis Of Algorithms Ii
 
Time complexity
Time complexityTime complexity
Time complexity
 
Dr hasany 2467_16649_1_lec-2-zabist
Dr hasany 2467_16649_1_lec-2-zabistDr hasany 2467_16649_1_lec-2-zabist
Dr hasany 2467_16649_1_lec-2-zabist
 
Algorithm Design and Analysis
Algorithm Design and AnalysisAlgorithm Design and Analysis
Algorithm Design and Analysis
 
Algorithm.ppt
Algorithm.pptAlgorithm.ppt
Algorithm.ppt
 
01 - DAA - PPT.pptx
01 - DAA - PPT.pptx01 - DAA - PPT.pptx
01 - DAA - PPT.pptx
 
dynamic-programming
dynamic-programmingdynamic-programming
dynamic-programming
 
04. Growth_Rate_AND_Asymptotic Notations_.pptx
04. Growth_Rate_AND_Asymptotic Notations_.pptx04. Growth_Rate_AND_Asymptotic Notations_.pptx
04. Growth_Rate_AND_Asymptotic Notations_.pptx
 
AsymptoticAnalysis.ppt
AsymptoticAnalysis.pptAsymptoticAnalysis.ppt
AsymptoticAnalysis.ppt
 
01 - Analysis of Efficiency.pptx
01 - Analysis of Efficiency.pptx01 - Analysis of Efficiency.pptx
01 - Analysis of Efficiency.pptx
 
Solving recurrences
Solving recurrencesSolving recurrences
Solving recurrences
 

More from eugeniadean34240

I need a 7 pg research essay on the following   Select a real o.docx
I need a 7 pg research essay on the following   Select a real o.docxI need a 7 pg research essay on the following   Select a real o.docx
I need a 7 pg research essay on the following   Select a real o.docxeugeniadean34240
 
I need a 4-5 APA formatted paper with references that is clearly wri.docx
I need a 4-5 APA formatted paper with references that is clearly wri.docxI need a 4-5 APA formatted paper with references that is clearly wri.docx
I need a 4-5 APA formatted paper with references that is clearly wri.docxeugeniadean34240
 
I need a 3 page research paper on Title  Addictive being youn.docx
I need a 3 page research paper on Title  Addictive being youn.docxI need a 3 page research paper on Title  Addictive being youn.docx
I need a 3 page research paper on Title  Addictive being youn.docxeugeniadean34240
 
I need a 3 page double-spaced 12-point paper on Immunotherapy. the i.docx
I need a 3 page double-spaced 12-point paper on Immunotherapy. the i.docxI need a 3 page double-spaced 12-point paper on Immunotherapy. the i.docx
I need a 3 page double-spaced 12-point paper on Immunotherapy. the i.docxeugeniadean34240
 
I need a 2500 word essay on the 1st Battalion 7th Cavalry Regiment. .docx
I need a 2500 word essay on the 1st Battalion 7th Cavalry Regiment. .docxI need a 2500 word essay on the 1st Battalion 7th Cavalry Regiment. .docx
I need a 2500 word essay on the 1st Battalion 7th Cavalry Regiment. .docxeugeniadean34240
 
I need a 200-word paper that answers the following questions:D.docx
I need a 200-word paper that answers the following questions:D.docxI need a 200-word paper that answers the following questions:D.docx
I need a 200-word paper that answers the following questions:D.docxeugeniadean34240
 
i need a 2 page essay on LA crimes as it pertains to Rape you will h.docx
i need a 2 page essay on LA crimes as it pertains to Rape you will h.docxi need a 2 page essay on LA crimes as it pertains to Rape you will h.docx
i need a 2 page essay on LA crimes as it pertains to Rape you will h.docxeugeniadean34240
 
I need a 1 page professional bio. My cover letter and resume i.docx
I need a 1 page professional bio. My cover letter and resume i.docxI need a 1 page professional bio. My cover letter and resume i.docx
I need a 1 page professional bio. My cover letter and resume i.docxeugeniadean34240
 
I need 100 words response for this two discussion forum1 discu.docx
I need 100 words response for this two discussion forum1 discu.docxI need 100 words response for this two discussion forum1 discu.docx
I need 100 words response for this two discussion forum1 discu.docxeugeniadean34240
 
I need 200 words response for each discussion post.Guided Respon.docx
I need 200 words response for each discussion post.Guided Respon.docxI need 200 words response for each discussion post.Guided Respon.docx
I need 200 words response for each discussion post.Guided Respon.docxeugeniadean34240
 
I need 3 pages discussion for an intersection (Attached image).docx
I need 3 pages discussion for an intersection (Attached image).docxI need 3 pages discussion for an intersection (Attached image).docx
I need 3 pages discussion for an intersection (Attached image).docxeugeniadean34240
 
I need 1page write up on Hypothesis & Methods Proposal,Due on .docx
I need 1page write up on Hypothesis & Methods Proposal,Due on .docxI need 1page write up on Hypothesis & Methods Proposal,Due on .docx
I need 1page write up on Hypothesis & Methods Proposal,Due on .docxeugeniadean34240
 
I need 2-3 pages written about the sieve of Eratosthenes. Starti.docx
I need 2-3 pages written about the sieve of Eratosthenes. Starti.docxI need 2-3 pages written about the sieve of Eratosthenes. Starti.docx
I need 2-3 pages written about the sieve of Eratosthenes. Starti.docxeugeniadean34240
 
I need 120 words for each question. Please ensure to post individual.docx
I need 120 words for each question. Please ensure to post individual.docxI need 120 words for each question. Please ensure to post individual.docx
I need 120 words for each question. Please ensure to post individual.docxeugeniadean34240
 
I need 10-12 slides Presentation with detailed speaker notes. Instru.docx
I need 10-12 slides Presentation with detailed speaker notes. Instru.docxI need 10-12 slides Presentation with detailed speaker notes. Instru.docx
I need 10-12 slides Presentation with detailed speaker notes. Instru.docxeugeniadean34240
 
I N N O V A T I O N N E T W O R K , I N C . www.innone.docx
I N N O V A T I O N  N E T W O R K ,  I N C .   www.innone.docxI N N O V A T I O N  N E T W O R K ,  I N C .   www.innone.docx
I N N O V A T I O N N E T W O R K , I N C . www.innone.docxeugeniadean34240
 
I like to tie my learning to Biblical Principles. On Virtuous Le.docx
I like to tie my learning to Biblical Principles. On Virtuous Le.docxI like to tie my learning to Biblical Principles. On Virtuous Le.docx
I like to tie my learning to Biblical Principles. On Virtuous Le.docxeugeniadean34240
 
I just want one paragraph.!!C.W.Mills described ‘sociological im.docx
I just want one paragraph.!!C.W.Mills described ‘sociological im.docxI just want one paragraph.!!C.W.Mills described ‘sociological im.docx
I just want one paragraph.!!C.W.Mills described ‘sociological im.docxeugeniadean34240
 
i just need serious help answering the question. I have answered mos.docx
i just need serious help answering the question. I have answered mos.docxi just need serious help answering the question. I have answered mos.docx
i just need serious help answering the question. I have answered mos.docxeugeniadean34240
 
I Headnotes and indexes are copyrighted and may not be duplica.docx
I Headnotes and indexes are copyrighted and may not be duplica.docxI Headnotes and indexes are copyrighted and may not be duplica.docx
I Headnotes and indexes are copyrighted and may not be duplica.docxeugeniadean34240
 

More from eugeniadean34240 (20)

I need a 7 pg research essay on the following   Select a real o.docx
I need a 7 pg research essay on the following   Select a real o.docxI need a 7 pg research essay on the following   Select a real o.docx
I need a 7 pg research essay on the following   Select a real o.docx
 
I need a 4-5 APA formatted paper with references that is clearly wri.docx
I need a 4-5 APA formatted paper with references that is clearly wri.docxI need a 4-5 APA formatted paper with references that is clearly wri.docx
I need a 4-5 APA formatted paper with references that is clearly wri.docx
 
I need a 3 page research paper on Title  Addictive being youn.docx
I need a 3 page research paper on Title  Addictive being youn.docxI need a 3 page research paper on Title  Addictive being youn.docx
I need a 3 page research paper on Title  Addictive being youn.docx
 
I need a 3 page double-spaced 12-point paper on Immunotherapy. the i.docx
I need a 3 page double-spaced 12-point paper on Immunotherapy. the i.docxI need a 3 page double-spaced 12-point paper on Immunotherapy. the i.docx
I need a 3 page double-spaced 12-point paper on Immunotherapy. the i.docx
 
I need a 2500 word essay on the 1st Battalion 7th Cavalry Regiment. .docx
I need a 2500 word essay on the 1st Battalion 7th Cavalry Regiment. .docxI need a 2500 word essay on the 1st Battalion 7th Cavalry Regiment. .docx
I need a 2500 word essay on the 1st Battalion 7th Cavalry Regiment. .docx
 
I need a 200-word paper that answers the following questions:D.docx
I need a 200-word paper that answers the following questions:D.docxI need a 200-word paper that answers the following questions:D.docx
I need a 200-word paper that answers the following questions:D.docx
 
i need a 2 page essay on LA crimes as it pertains to Rape you will h.docx
i need a 2 page essay on LA crimes as it pertains to Rape you will h.docxi need a 2 page essay on LA crimes as it pertains to Rape you will h.docx
i need a 2 page essay on LA crimes as it pertains to Rape you will h.docx
 
I need a 1 page professional bio. My cover letter and resume i.docx
I need a 1 page professional bio. My cover letter and resume i.docxI need a 1 page professional bio. My cover letter and resume i.docx
I need a 1 page professional bio. My cover letter and resume i.docx
 
I need 100 words response for this two discussion forum1 discu.docx
I need 100 words response for this two discussion forum1 discu.docxI need 100 words response for this two discussion forum1 discu.docx
I need 100 words response for this two discussion forum1 discu.docx
 
I need 200 words response for each discussion post.Guided Respon.docx
I need 200 words response for each discussion post.Guided Respon.docxI need 200 words response for each discussion post.Guided Respon.docx
I need 200 words response for each discussion post.Guided Respon.docx
 
I need 3 pages discussion for an intersection (Attached image).docx
I need 3 pages discussion for an intersection (Attached image).docxI need 3 pages discussion for an intersection (Attached image).docx
I need 3 pages discussion for an intersection (Attached image).docx
 
I need 1page write up on Hypothesis & Methods Proposal,Due on .docx
I need 1page write up on Hypothesis & Methods Proposal,Due on .docxI need 1page write up on Hypothesis & Methods Proposal,Due on .docx
I need 1page write up on Hypothesis & Methods Proposal,Due on .docx
 
I need 2-3 pages written about the sieve of Eratosthenes. Starti.docx
I need 2-3 pages written about the sieve of Eratosthenes. Starti.docxI need 2-3 pages written about the sieve of Eratosthenes. Starti.docx
I need 2-3 pages written about the sieve of Eratosthenes. Starti.docx
 
I need 120 words for each question. Please ensure to post individual.docx
I need 120 words for each question. Please ensure to post individual.docxI need 120 words for each question. Please ensure to post individual.docx
I need 120 words for each question. Please ensure to post individual.docx
 
I need 10-12 slides Presentation with detailed speaker notes. Instru.docx
I need 10-12 slides Presentation with detailed speaker notes. Instru.docxI need 10-12 slides Presentation with detailed speaker notes. Instru.docx
I need 10-12 slides Presentation with detailed speaker notes. Instru.docx
 
I N N O V A T I O N N E T W O R K , I N C . www.innone.docx
I N N O V A T I O N  N E T W O R K ,  I N C .   www.innone.docxI N N O V A T I O N  N E T W O R K ,  I N C .   www.innone.docx
I N N O V A T I O N N E T W O R K , I N C . www.innone.docx
 
I like to tie my learning to Biblical Principles. On Virtuous Le.docx
I like to tie my learning to Biblical Principles. On Virtuous Le.docxI like to tie my learning to Biblical Principles. On Virtuous Le.docx
I like to tie my learning to Biblical Principles. On Virtuous Le.docx
 
I just want one paragraph.!!C.W.Mills described ‘sociological im.docx
I just want one paragraph.!!C.W.Mills described ‘sociological im.docxI just want one paragraph.!!C.W.Mills described ‘sociological im.docx
I just want one paragraph.!!C.W.Mills described ‘sociological im.docx
 
i just need serious help answering the question. I have answered mos.docx
i just need serious help answering the question. I have answered mos.docxi just need serious help answering the question. I have answered mos.docx
i just need serious help answering the question. I have answered mos.docx
 
I Headnotes and indexes are copyrighted and may not be duplica.docx
I Headnotes and indexes are copyrighted and may not be duplica.docxI Headnotes and indexes are copyrighted and may not be duplica.docx
I Headnotes and indexes are copyrighted and may not be duplica.docx
 

Recently uploaded

POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 

Recently uploaded (20)

Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 

270-102-divide-and-conquer_handout.pdfCS 270Algorithm.docx

  • 2. 3 Tutorial CS 270 Algorithms Oliver Kullmann Growth of Functions Divide-and- Conquer Min-Max- Problem Tutorial General remarks First we consider an important tool for the analysis of algorithms: Big-Oh. Then we introduce an important algorithmic paradigm: Divide-and-Conquer. We conclude by presenting and analysing a simple example.
  • 3. Reading from CLRS for week 2 Chapter 2, Section 3 Chapter 3 CS 270 Algorithms Oliver Kullmann Growth of Functions Divide-and- Conquer Min-Max- Problem Tutorial Growth of Functions A way to describe behaviour of functions in the limit. We are studying asymptotic efficiency. Describe growth of functions.
  • 4. Focus on what’s important by abstracting away low-order terms and constant factors. How we indicate running times of algorithms. A way to compare “sizes” of functions: O corresponds to ≤ Ω corresponds to ≥ Θ corresponds to = We consider only functions f , g : N → R≥0. CS 270 Algorithms Oliver Kullmann Growth of Functions Divide-and- Conquer Min-Max- Problem Tutorial
  • 5. O-Notation O ( g(n) ) is the set of all functions f (n) for which there are positive constants c and n0 such that f (n) ≤ cg(n) for all n ≥ n0. cg(n) f (n) n n0 g(n) is an asymptotic upper bound for f (n). If f (n) ∈ O(g(n)), we write f (n) = O(g(n)) (we will precisely explain this soon) CS 270 Algorithms Oliver Kullmann Growth of
  • 6. Functions Divide-and- Conquer Min-Max- Problem Tutorial O-Notation Examples 2n2 = O(n3), with c = 1 and n0 = 2. Example of functions in O(n2): n2 n2 + n n2 + 1000n 1000n2 + 1000n Also n n/1000 n1.999999 n2/ lg lg lg n
  • 7. CS 270 Algorithms Oliver Kullmann Growth of Functions Divide-and- Conquer Min-Max- Problem Tutorial Ω-Notation Ω ( g(n) ) is the set of all functions f (n) for which there are positive constants c and n0 such that f (n) ≥ cg(n) for all n ≥ n0.
  • 8. cg(n) f (n) n n0 g(n) is an asymptotic lower bound for f (n). CS 270 Algorithms Oliver Kullmann Growth of Functions Divide-and- Conquer Min-Max- Problem Tutorial Ω-Notation Examples √ n = Ω(lg n), with c = 1 and n0 = 16.
  • 9. Example of functions in Ω(n2): n2 n2 + n n2 − n 1000n2 + 1000n 1000n2 − 1000n Also n3 n2.0000001 n2 lg lg lg n 22 n CS 270 Algorithms Oliver Kullmann Growth of Functions
  • 10. Divide-and- Conquer Min-Max- Problem Tutorial Θ-Notation Θ ( g(n) ) is the set of all functions f (n) for which there are positive constants c1, c2 and n0 such that c1g(n) ≤ f (n) ≤ c2g(n) for all n ≥ n0. c2g(n) c1g(n) f (n) n n0 g(n) is an asymptotic tight bound for f (n). CS 270
  • 11. Algorithms Oliver Kullmann Growth of Functions Divide-and- Conquer Min-Max- Problem Tutorial Θ-Notation (cont’d) Examples 1 n2/2 − 2n = Θ(n2), with c1 = 14, c2 = 1 2 , and n0 = 8. Theorem 2 f (n) = Θ(g(n)) if and only if f (n) = O(g(n)) and f (n) = Ω(g(n)). Leading constants and lower order terms do not matter.
  • 12. CS 270 Algorithms Oliver Kullmann Growth of Functions Divide-and- Conquer Min-Max- Problem Tutorial Asymptotic notation in equations When on right-hand side Θ(n2) stands for some anonymous function in the set Θ(n2). 2n2 + 3n + 1 = 2n2 + Θ(n) means 2n2 + 3n + 1 = 2n2 + f (n) for some f (n) ∈ Θ(n). In particular, f (n) = 3n + 1. When on left-hand side No matter how the anonymous functions are chosen on the left-hand side, there is a way to choose the anonymous functions
  • 13. on the right-hand side to make the equation valid. Interpret 2n2 + Θ(n) = Θ(n2) as meaning for all functions f (n) ∈ Θ(n), there exists a function g(n) ∈ Θ(n2) such that 2n2 + f (n) = g(n). CS 270 Algorithms Oliver Kullmann Growth of Functions Divide-and- Conquer Min-Max- Problem Tutorial Asymptotic notation chained together 2n2 + 3n + 1 = 2n2 + Θ(n) = Θ(n2) Interpretation: First equation: There exists f (n) ∈ Θ(n) such that 2n2 + 3n + 1 = 2n2 + f (n).
  • 14. Second equation: For all g(n) ∈ Θ(n) (such as the f (n) used to make the first equation hold), there exists h(n) ∈ Θ(n2) such that 2n2 + g(n) = h(n). Note What has been said of “Θ” on this and the previous slide also applies to “O” and “Ω”. CS 270 Algorithms Oliver Kullmann Growth of Functions Divide-and- Conquer Min-Max- Problem Tutorial Example Analysis Insertion-Sort(A)
  • 15. 1 for j = 2 to A.length 2 key = A[j] 3 // Insert A[j] into sorted sequence A[1 . . j−1]. 4 i = j−1 5 while i > 0 and A[i] > key 6 A[i+1] = A[i] 7 i = i−1 8 A[i+1] = key The for -loop on line 1 is executed O(n) times; and each statement costs constant time, except for the while -loop on lines 5-7 which costs O(n). Thus overall runtime is: O(n) × O(n) = O(n2). Note: In fact, as seen last week, worst-case runtime is Θ(n2). CS 270 Algorithms Oliver Kullmann Growth of Functions Divide-and- Conquer Min-Max-
  • 16. Problem Tutorial Divide-and-Conquer Approach There are many ways to design algorithms. For example, insertion sort is incremental: having sorted A[1 . . j−1], place A[j] correctly, so that A[1 . . j] is sorted. Divide-and-Conquer is another common approach: Divide the problem into a number of subproblems that are smaller instances of the same problem. Conquer the subproblems by solving them recursively. Base case: If the subproblem are small enough, just solve them by brute force. Combine the subproblem solutions to give a solution to the original problem. CS 270 Algorithms Oliver Kullmann Growth of Functions
  • 17. Divide-and- Conquer Min-Max- Problem Tutorial Naive Min-Max Find minimum and maximum of a list A of n>0 numbers. Naive-Min-Max(A) 1 least = A[1] 2 for i = 2 to A.length 3 if A[i] < least 4 least = A[i] 5 greatest = A[1] 6 for i = 2 to A.length 7 if A[i] > greatest 8 greatest = A[i] 9 return (least, greatest) The for-loop on line 2 makes n−1 comparisons, as does the for-loop on line 6, making a total of 2n−2 comparisons. Can we do better? Yes! CS 270 Algorithms
  • 18. Oliver Kullmann Growth of Functions Divide-and- Conquer Min-Max- Problem Tutorial Divide-and-Conquer Min-Max As we are dealing with subproblems, we state each subproblem as computing minimum and maximum of a subarray A[p . . q]. Initially, p = 1 and q = A.length, but these values change as we recurse through subproblems. To compute minimum and maximum of A[p . . q]: Divide by splitting into two subarrays A[p . . r] and A[r+1 . . q], where r is the halfway point of A[p . . q]. Conquer by recursively computing minimum and maximum of the two subarrays A[p . . r] and A[r+1 . . q]. Combine by computing the overall minimum as the min of the two recursively computed minima, similar for the overall maximum.
  • 19. CS 270 Algorithms Oliver Kullmann Growth of Functions Divide-and- Conquer Min-Max- Problem Tutorial Divide-and-Conquer Min-Max Algorithm Initially called with Min-Max(A, 1, A.length). Min-Max(A, p, q) 1 if p = q 2 return (A[p], A[q]) 3 if p = q−1 4 if A[p] < A[q] 5 return (A[p], A[q]) 6 else return (A[q], A[p]) 7 r = ⌊ (p+q)/2⌋ 8 (min1, max1) = Min-Max(A, p, r)
  • 20. 9 (min2, max2) = Min-Max(A, r+1, q) 10 return ( min(min1, min2), max(max1, max2) ) Note In line 7, r computes the halfway point of A[p . . q]. n = q − p + 1 is the number of elements from which we compute the min and max. CS 270 Algorithms Oliver Kullmann Growth of Functions Divide-and- Conquer Min-Max- Problem
  • 21. Tutorial Solving the Min-Max Recurrence Let T(n) be the number of comparisons made by Min-Max(A, p, q), where n = q−p+1 is the number of elements from which we compute the min and max. Then T(1) = 0, T(2) = 1, and for n > 2: T(n) = T (⌈ n/2⌉ ) + T (⌊ n/2⌋ ) + 2. Claim T(n) = 3 2 n − 2 for n = 2k ≥ 2, i.e., powers of 2. Proof. The proof is by induction on k (using n = 2k). Base case: true for k=1, as T(21) = 1 = 3 2 · 21 − 2. Induction step: assuming T(2k) = 3 2 2k − 2, we get T(2k+1) = 2T(2k)+2 = 2 ( 3 2 2k−2
  • 22. ) +2 = 3 2 2k+1−2 CS 270 Algorithms Oliver Kullmann Growth of Functions Divide-and- Conquer Min-Max- Problem Tutorial Solving the Min-Max Recurrence (cont’d) Some remarks: 1 If we replace line 7 of the algorithm by r = p+1, then the resulting runtime T ′(n) satisfies T ′(n) =
  • 23. ⌈ 3n 2 ⌉ −2 for all n > 0. 2 For example, T ′(6) = 7 whereas T(6) = 8. 3 It can be shown that at least ⌈ 3n 2 ⌉ − 2 comparisons are necessary in the worst case to find the maximum and minimum of n numbers for any comparison-based algorithm: this is thus a lower bound on the problem. 4 Hence this (last) algorithm is provably optimal. CS 270 Algorithms Oliver Kullmann
  • 24. Growth of Functions Divide-and- Conquer Min-Max- Problem Tutorial Big-Oh, Omega, Theta by examples 1 5n + 111 = O(n) ? YES 2 5n + 111 = O(n2) ? YES 3 5n + 111 = Ω(n) ? YES 4 5n + 111 = Ω(n2) ? NO 5 5n + 111 = Θ(n) ? YES 6 5n + 111 = Θ(n2) ? NO 7 2n = O(3n) ? YES 8 2n = Ω(3n) ? NO 9 120n2 + √ n + 99n = O(n2) ? YES
  • 25. 10 120n2 + √ n + 99n = Θ(n2) ? YES 11 sin(n) = O(1) ? YES CS 270 Algorithms Oliver Kullmann Growth of Functions Divide-and- Conquer Min-Max- Problem Tutorial Can we improve the Min-Max algorithm? Determine T(6), the number of comparisons the Min-Max algorithms performs for 6 elements. As usual, the argumentation is important (why is this correct?).
  • 26. Perhaps best is that you run the algorithm (on paper) for 6 elements. Notice that the basic parameters for a run do not depend on the values of the elements, but only on their total number. Once you found T(6), is this really optimal? CS 270 Algorithms Oliver Kullmann Growth of Functions Divide-and- Conquer Min-Max- Problem Tutorial Unfolding the recursion for Min-Max We have
  • 27. T(n) = 0 if n = 1 1 if n = 2 T( ⌈ n 2 ⌉ ) + T( ⌊ n 2 ⌋ ) + 2 else
  • 28. . 1 T(1) = 0 2 T(2) = 1 3 T(3) = T(2) + T(1) + 2 = 1 + 0 + 2 = 3 4 T(4) = T(2) + T(2) + 2 = 1 + 1 + 2 = 4 5 T(5) = T(3) + T(2) + 2 = 3 + 1 + 2 = 6 6 T(6) = T(3) + T(3) + 2 = 3 + 3 + 2 = 8 7 T(7) = T(4) + T(3) + 2 = 4 + 3 + 2 = 9 8 T(8) = T(4) + T(4) + 2 = 4 + 4 + 2 = 10 9 T(9) = T(5) + T(4) + 2 = 6 + 4 + 2 = 12 10 T(10) = T(5) + T(5) + 2 = 6 + 6 + 2 = 14. We count 4 steps +1 and 5 steps +2 — we guess T(n) ≈ 3 2 n. CS 270 Algorithms Oliver Kullmann
  • 29. Growth of Functions Divide-and- Conquer Min-Max- Problem Tutorial Finding the best min-max algorithm 1 As you can see in the section on the min-max problem, for some input sizes we can validate the guess T(n) ≈ 3 2 n. 2 One can now try to find a precise general formula for T(n), 3 However we see that we have T(6) = 8, while we can handle this case with 7 comparisons. So perhaps we can find a better algorithm? 4 And that is the case: 1 If n is even, find the min-max for the first two elements using 1 comparison; if n is odd, find the min-max for the first element using 0 comparisons. 2 Now iteratively find the min-max of the next two elements using 1 comparison, and compute the new current min-max using 2 further comparisons. And so on ....
  • 30. This yields an algorithm using precisely ⌈ 3 2 n ⌉ − 2 comparisons. And this is precisely optimal for all n. We learn: Here divide-and-conqueor provided a good stepping stone to find a really good algorithm. CS 270 Algorithms Oliver Kullmann Growth of Functions Divide-and- Conquer Min-Max- Problem
  • 31. Tutorial Designing an algorithm: Median The median of a sequence of numbers is the “middle value” — the value in the middle position of the list after sorting. Can we do better than the obvious algorithm (by sorting), using divide-and-conquer? (Here some judgement is needed, that the precise details about what actually is the “middle value” won’t make a fundamental difference, and so it’s best to ignore them for the initial phase, where developing the ideas is of utmost importance.) CS 270 Algorithms Oliver Kullmann Growth of Functions Divide-and- Conquer Min-Max- Problem
  • 32. Tutorial First strategy: divide in half 1 Divide the array A of numbers into two parts, B and C, of equal size (more precisely, nearly equal size, but such details are best ignored in the beginning). 2 In principle B and C could be anything, but easiest is to let them be the first half and the second half of A. 3 Compute (that is now the conquer-phase) the medians mB, mC of the arrays B, C. 4 If mC < mB, then swap B and C. 5 Re-order B and C internally, so that the elements smaller than the median are on the left, and the larger elements on the right. 6 Now consider the array of elements from mB to mC (note that this is again half of the size of A): The median of this array (computed again in the conquer-phase, recursively) is the median of A. CS 270 Algorithms Oliver Kullmann Growth of
  • 33. Functions Divide-and- Conquer Min-Max- Problem Tutorial Example run 1, 20, 5, 18, 7, 10, 10 | 4, 20, 7, 7, 17, 14, 1, 12 of length 15, partitioned into 7 and 8 elements. The left median is 10. The right median is 7 or 12; let’s take 7. Swap, and partition the two parts according to their medians: 4, 7, 1, 7, 20, 17, 14, 12 | 1, 5, 7, 10, 20, 18, 10. Compute the median of the new middle part 20, 17, 14, 12, 1, 5, 7, 10 which is 10 (the right answer) or 12. So, it could work (or not). CS 270 Algorithms
  • 34. Oliver Kullmann Growth of Functions Divide-and- Conquer Min-Max- Problem Tutorial The recurrence We are in this phase only interested in the recurrence: T(n) = 3 · T(n/2) + O(n). We ignore here all issues about the precise partitioning (and whether we can get it to work at all!) — all what counts here is the insight whether we could get a good algorithm! Next week we develop tools to see immediately how good this approach could be. Divide and ConquerGrowth of FunctionsDivide-and- ConquerMin-Max-ProblemTutorial 270-1/03-solving-recurrences_handout.pdf
  • 36. 1 Divide-and-Conquer Merge Sort 2 Solving Recurrences Recursion Trees Master Theorem 3 Divide-and-Conquer Matrix multiplication 4 Tutorial CS 270 Algorithms Oliver Kullmann Divide-and- Conquer Merge Sort Solving Recurrences
  • 37. Recursion Trees Master Theorem Divide-and- Conquer Matrix multiplication Tutorial General remarks First we continue with an important example for Divide-and-Conquer, namely Merge Sort. Then we present a basic tool for analysing algorithms by Solving Recurrences. We conclude by considering another example, namely Matrix Multiplication. Reading from CLRS for week 3 Chapter 4 CS 270 Algorithms
  • 38. Oliver Kullmann Divide-and- Conquer Merge Sort Solving Recurrences Recursion Trees Master Theorem Divide-and- Conquer Matrix multiplication Tutorial Another example: Merge-Sort A sorting algorithm based on divide and conquer. The worst- case running time has a lower order of growth than insertion sort. Again we are dealing with subproblems of sorting subarrays
  • 39. A[p . . q] Initially, p = 1 and q = A.length, but these values change again as we recurse through subproblems. To sort A[p . . q]: Divide by splitting into two subarrays A[p . . . r] and A[r+1 . . . q], where r is the halfway point of A[p . . . q]. Conquer by recursively sorting the two subarrays A[p . . . r] and A[r+1 . . . q]. Combine by merging the two sorted subarrays A[p . . . r] and A[r+1 . . . q] to produce a single sorted subarray A[p . . . q]. The recursion bottoms out when the subarray has just 1 element, so that it is trivially sorted. CS 270 Algorithms Oliver Kullmann Divide-and- Conquer Merge Sort Solving Recurrences
  • 40. Recursion Trees Master Theorem Divide-and- Conquer Matrix multiplication Tutorial Another example: Merge-Sort Merge-Sort(A, p, q) 1 if p < q // check for base case 2 r = ⌊ (p+q)/2⌋ // divide 3 Merge-Sort(A, p, r) // conquer 4 Merge-Sort(A, r+1, q) // conquer 5 Merge(A, p, r, q) // combine Initial call: Merge-Sort(A, 1, A.length) CS 270 Algorithms Oliver
  • 41. Kullmann Divide-and- Conquer Merge Sort Solving Recurrences Recursion Trees Master Theorem Divide-and- Conquer Matrix multiplication Tutorial Merge Input: Array A and indices p, r, q such that p ≤ r < q Subarrays A[p . . r] and subarray A[r+1 . . q] are sorted. By the restriction on p, r, q neither subarray is empty. Output: The two subarrays are merged into a single sorted
  • 42. subarray in A[p . . q]. We implement is so that it takes Θ(n) time, with n = q − p + 1 = the number of elements being merged. CS 270 Algorithms Oliver Kullmann Divide-and- Conquer Merge Sort Solving Recurrences Recursion Trees Master Theorem Divide-and- Conquer Matrix
  • 43. multiplication Tutorial Merge(A, p, r, q) 1 n1 = r − p + 1 2 n2 = q − r 3 let L[1 . . n1+1] and R[1 . . n2+1] be new arrays 4 for i = 1 to n1 5 L[i] = A[p+i−1] 6 for j = 1 to n2 7 R[j] = A[r+j] 8 L[n1+1] = R[n2+1] = ∞ 9 i = j = 1 10 for k = p to q 11 if L[i] ≤ R[j] 12 A[k] = L[i] 13 i = i+1 14 else A[k] = R[j] 15 j = j+1 CS 270 Algorithms Oliver Kullmann Divide-and- Conquer
  • 44. Merge Sort Solving Recurrences Recursion Trees Master Theorem Divide-and- Conquer Matrix multiplication Tutorial Analysis of Merge-Sort The runtime T(n), where n = q−p+1 > 1, satisfies: T(n) = 2T(n/2) + Θ(n). We will show that T(n) = Θ(n lg n). It can be shown (see tutorial-section) that Ω(n lg n) comparisons are necessary in the worst case to sort n numbers for any comparison-based algorithm: this is thus an (asymptotic) lower bound on the problem. Hence Merge-Sort is provably (asymptotically) optimal.
  • 46. Analysing divide-and-conquer algorithms Recall the divide-and-conquer paradigm: Divide the problem into a number of subproblems that are smaller instances of the same problem. Conquer the subproblems by solving them recursively. Base case: If the subproblem are small enough, just solve them by brute force. Combine the subproblem solutions to give a solution to the original problem. We use recurrences to characterise the running time of a divide-and-conquer algorithm. Solving the recurrence gives us the asymptotic running time. A recurrence is a function defined in terms of one or more base cases, and itself, with smaller arguments CS 270 Algorithms Oliver Kullmann Divide-and-
  • 48. Solution : T(n) = n. T(n) = { 1 if n = 1 2T(n/2) + n if n > 1