CSC323-Sp2017-Mod1-Algorithm-Efficiency
CSC323-Sp2017-Mod1-Algorithm-Efficiency
Algorithm
Visiting a vertex or
Typical graph problem #vertices and/or edges
traversing an edge
Orders of Growth
• We are more interested in the order of growth on the number of times
the basic operation is executed on the input size of an algorithm.
• Because, for smaller inputs, it is difficult to distinguish efficient
algorithms vs. inefficient ones.
• For example, if the number of basic operations of two algorithms to
solve a particular problem are n and n2 respectively, then
– if n = 3, then we may say there is not much difference between requiring
3 basic operations and 9 basic operations and the two algorithms have
about the same running time.
– On the other hand, if n = 10000, then it does makes a difference whether
the number of times the basic operation is executed is n or n2.
Exponential-growth
functions
• Worst-Case: Cworst(n) = n
• Best-Case: Cbest(n) = 1
Probability-based Average-Case
Analysis of Sequential Search
• If p is the probability of finding an element in the list, then (1-p) is the
probability of not finding an element in the list.
• Also, on an n-element list, the probability of finding the search key as
the ith element in the list is p/n for all values of 1 ≤i ≤ n
• If p = 1 (the element that we will search for always exists in the list),
then Cavg(n) = (n+1)/2. That is, on average, we visit half of the entries
in the list to search for any element in the list.
• If p = 0 (all the time, the element that we will search never exists),
then Cavg(n) = n. That is, we visit all the elements in the list.
YouTube Link: https://www.youtube.com/watch?v=8V-bHrPykrE
Asymptotic Notations: Intro
2n ≤ 0.05 n2
for n ≥ 40
2n = O(n2)
0.05n2 ≥ 2n
for n ≥ 40
0.05n2 = Ω(n)
Asymptotic Notations: Intro
2n ≤ 5n
5n for n ≥ 1
2n = O(n)
2n ≥ n
for n ≥ 1
2n 2n = Ω(n)
n As 2n = O(n)
and 2n = Ω(n),
we say
2n = Θ(n)
n
Asymptotic Notations: Formal Intro
Note: If t(n) = O(g(n)) g(n) = Ω(t(n)); also, if t(n) = Ω(g(n)) g(n) = O(t(n))
Asymptotic Notations: Formal Intro
t(n) = Θ(g(n))
c2*g(n) ≤ t(n) ≤ c1*g(n) for all n ≥ n0
c1 and c2 are positive constants (> 0)
and n0 is a non-negative integer
Asymptotic Notations: Examples
• Let t(n) and g(n) be any non-negative functions defined on a set of all
real numbers.
• We say t(n) = O(g(n)) for all functions t(n) that have a lower or the
same order of growth as g(n), within a constant multiple as n ∞.
– Examples:
• We say t(n) = Ω(g(n)) for all functions t(n) that have a higher or the
same order of growth as g(n), within a constant multiple as n ∞.
– Examples:
• We say t(n) = Θ(g(n)) for all functions t(n) that have the same order of
growth as g(n), within a constant multiple as n ∞.
– Examples: an2 + bn + c = Θ(n2);
n2 + logn = Θ(n2)
Useful Property of Asymptotic
Notations
• If t1(n) ∈ O(g1(n)) and t2(n) ∈ O(g2(n)) , then
t1(n) + t2(n) ∈ O(max{g1(n), g2(n)})
L’Hopital’s Rule
Note: t’(n) and g’(n) are first-order derivatives of t(n) and g(n)
Stirling’s Formula
Example 1: To Determine the
Order of Growth
Example 1: To Determine the
Order of Growth (continued…)
Example 2: To Determine the
Order of Growth
Example 2: To Determine the
Order of Growth (continued…)
Examples to Compare the Order of Growth
b)
c)
d)
Some More Examples: Order of Growth
∑1 = (u − l + 1)
i =l
Examples on Summation
• 1 + 3 + 5 + 7 + …. + 999
• 2 + 4 + 8 + 16 + … + 1024
Example 1: Finding Max. Element
Best-case situation:
If the two first elements of the array are the same, then we can exit
after one comparison. Best case = 1 comparison.
Worst-case situation:
• The basic operation is the comparison in the inner loop. The worst-
case happens for two-kinds of inputs:
– Arrays with no equal elements
– Arrays in which only the last two elements are the pair of equal
elements
Example 3: Element Uniqueness Problem
• For these kinds of inputs, one comparison is made for each repetition
of the innermost loop, i.e., for each value of the loop’s variable j
between its limits i+1 and n-1; and this is repeated for each value of the
outer loop i.e., for each value of the loop’s variable i between its limits 0
and n-2. Accordingly, we get,
= Θ(n2)
Example 4: Insertion Sort
• Given an array A[0…n-1], at any time, we have the array
divided into two parts: A[0,…,i-1] and A[i…n-1].
– The A[0…i-1] is the sorted part and A[i…n-1] is the unsorted part.
– In any iteration, we pick an element v = A[i] and scan through the
sorted sequence A[0…i-1] to insert v at the appropriate position.
• The scanning is proceeded from right to left (i.e., for index j
running from i-1 to 0) until we find the right position for v.
• During this scanning process, v = A[i] is compared with A[j].
• If A[j] > v, then we v has to be placed somewhere before A[j] in the
final sorted sequence. So, A[j] cannot be at its current position (in
the final sorted sequence) and has to move at least one position to
the right. So, we copy A[j] to A[j+1] and decrement the index j, so
that we now compare v with the next element to the left.
∑1 = (n − 1) − 1 + 1 = (n − 1)
i =1
The comparison A[j] > v is the basic operation. = Θ(n)
Worst Case (if the array is reverse-sorted): the element v at A[i] has to be moved
all the way to index 0, by scanning through the entire sequence A[0…i-1].
n −1 0 n −1 i −1 n −1 n −1
n(n − 1)
∑ ∑ 1 = ∑ ∑1 = ∑ (i − 1) − 0 + 1 = ∑ i =
i =1 j = i −1 i =1 j = 0 i =1 i =1 2
= Θ(n2)
Insertion Sort: Analysis and Example
Average Case: On average for a random input sequence, we would be visiting half
of the sorted sequence A[0…i-1] to put A[i] at the proper position.
n −1 ( i −1) / 2 n −1 n −1
(i − 1) (i + 1)
C (n) = ∑ ∑ 1 = ∑ + 1 = ∑ =Θ(n 2 )
i =1 j =i −1 i =1 2 i =1 2
Example: Given sequence (also initial): 45 23 8 12 90 21
Index
-1 Iteration 1 (v = 23): Iteration 4 (v = 90):
45 45 8 12 90 21 8 12 23 45 90 21
23 45 8 12 90 21 9 12 23 45 90 21
Iteration 2 (v = 8):
Iteration 5 (v = 21):
23 45 45 12 90 21
9 12 23 45 90 90
23 23 45 12 90 21
9 12 23 45 45 90
8 23 45 12 90 21
9 12 23 23 45 90
Iteration 3 (v = 12): 9 12 21 23 45 90
8 23 45 45 90 21
8 23 23 45 90 21 The colored elements are in the sorted sequence
8 12 23 45 90 21 and the circled element is at index j of the algorithm.
Time Efficiency of Recursive
Algorithms: General Plan for Analysis
• Decide on a parameter indicating an input’s size.
• Check whether the number of times the basic op. is executed may vary
on different inputs of the same size. (If it may, the worst, average, and
best cases must be investigated separately.)
• Solve the recurrence (or, at the very least, establish its solution’s order
of growth) by backward substitutions or another method.
Recursive Evaluation of n!
Definition: n ! = 1 ∗ 2 ∗ … ∗(n-1) ∗ n for n ≥ 1 and 0! = 1
• Recursive definition of n!: F(n) = F(n-1) ∗ n for n ≥ 1 and
F(0) = 1
# Additions
Since the recursive calls end when n is equal to 1 and there are no additions
made, the initial condition is: A(1) = 0.
Counting the # Bits of an Integer
Solution Approach: If we use the backward substitution method (as we did in
the previous two examples, we will get stuck for values of n that are not powers
of 2).
We proceed by setting n = 2k for k ≥ 0.
New recurrence
relation to solve:
Examples for
Solving
Recurrence
Relations
Master Theorem to Solve
Recurrence Relations
• Assuming that size n is a The same results hold good for O and Ω too.
power of b to simplify analysis, Examples:
we have the following
1) T(n) = 4T(n/2) + n
recurrence for the running a = 4; b = 2; d = 1 a > bd
time, T(n) = a T(n/b) + f(n) ( )
T (n) = Θ n log 2 4 = Θ(n 2 )
– where f(n) is a function that
accounts for the time spent on 2) T(n) = 4T(n/2) + n2
dividing an instance of size n a = 4; b = 2; d = 2 a = bd
into instances of size n/b and
combining their solutions.
(
T (n) = Θ n 2 log n )
• Master Theorem: 3) T(n) = 4T(n/2) + n3
a = 4; b = 2; d = 3 a < bd
( )
T ( n) = Θ n 3
4) T(n) = 2T(n/2) + 1
a = 2; b = 2; d = 0 a > bd
( )
T (n) = Θ n log 2 2 = Θ(n)
Master Theorem: More Problems
Space-Time Tradeoff
In-place vs. Out-of-place Algorithms
• An algorithm is said to be “in-place” if it uses a minimum
and/or constant amount of extra storage space to
transform or process an input to obtain the desired output.
– Depending on the nature of the problem, an in-place algorithm may
sometime overwrite an input to the desired output as the algorithm
executes (as in the case of in-place sorting algorithms); the output
space may sometimes be a constant (for example in the case of
string-matching algorithms).
• Algorithms that use significant amount of extra storage
space (sometimes, additional space as large as the input
– example: merge sort) are said to be out-of-place in
nature.
• Time-Space Complexity Tradeoffs of Sorting Algorithms:
– In-place sorting algorithms like Selection Sort, Bubble Sort, Insertion Sort
and Quick Sort have a worst-case time complexity of Θ(n2).
– On the other hand, Merge sort has a space-complexity of Θ(n), but has a
worst-case time complexity of Θ(nlogn).
Hashing
• A very efficient method for implementing a dictionary, i.e., a set with
the operations: find, insert and delete
• Based on representation-change and space-for-time tradeoff ideas
• We consider the problem of implementing a dictionary of n records with
keys K1, K2, …, Kn.
• Hashing is based on the idea of distributing keys among a one-
dimensional array H[0…m-1] called a hash table.
– The distribution is done by computing, for each of the keys, the value of
some pre-defined function h called the hash function.
– The hash function assigns an integer between 0 and m-1, called the hash
address to a key.
– The size of a hash table m is typically a prime integer.
• Typical hash functions
– For non-negative integers as key, a hash function could be h(K)=K mod m;
– If the keys are letters of some alphabet, the position of the letter in the
alphabet (for example, A is at position 1 in alphabet A – Z) could be used as
the key for the hash function defined above.
– If the key is a character string c0 c1 … cs-1 of characters from an alphabet,
then, the hash function could be:
Collisions and Collision Resolution
If h(K1) = h(K2), there is a collision
• Note: The above solution could also be used to find whether two sets are
disjoint or not. Even if one element in the smaller array is there in the
larger array, we could stop!
Applications of Hashing (1)
Finding whether an array is a Subset of another array
• Example 1: AL = {11, 1, 13, 21, 3, 7}; 0 1 2 3 4
• AS = {11, 3, 7, 1}; AS is a subset of AL.
• Let H(K) = K mod 5.
11 7 13
Hash table approach 1 3
# comparisons = 1 (for 11) + 2 (for 3) + 21
1 (for 7) + 2 (for 1) = 6
Brute-force approach: Pick every element in the smaller array and do a linear
search for it in the larger array.
# comparisons = 1 (for 11) + 5 (for 3) +
6 (for 7) + 2 (for 1) = 14