Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
• Exact formula
e.g., C(n) = n(n-1)/2
v1.2 1 v1.2 2
• Example:
– How much faster will algorithm run on computer that is
twice as fast?
v1.2 3 v1.2 4
1
Big-omega Big-theta
v1.2 7 v1.2 8
Examples: Then
lim f(n) lim f ´(
´(nn)
• 10n vs. 2 n2 =
n? 8 g (n) n? 8g ´(
´(nn)
• n(n+1)/2 vs. n2
v1.2 9 v1.2 10
• 5n+20 is O(10n)
v1.2 11 v1.2 12
2
More Big-Oh Examples Big-Oh Rules
7n-2
7n-2 is O(n)
need c > 0 and n0 ≥ 1 such that 7n-2 ≤ c•n for n ≥ n0 • If is f(n) a polynomial of degree d, then f(n) is
this is true for c = 7 and n0 = 1 O(nd ), i.e.,
n 3n3 + 20n2 + 5 1. Drop lower- order terms
3n3 + 20n2 + 5 is O(n3 ) 2. Drop constant factors
need c > 0 and n0 ≥ 1 such that 3n3 + 20n2 + 5 ≤ c•n3 for n ≥ n0
this is true for c = 4 and n0 = 21
• Use the smallest possible class of functions
– Say “2n is O(n)” instead of “2n is O(n2)”
n 3 log n + log log n
3 log n + log log n is O(log n)
• Use the simplest expression of the class
need c > 0 and n0 ≥ 1 such that 3 log n + log log n ≤ c•log n for n ≥ n0 – Say “3n + 5 is O(n)” instead of “3n + 5 is O(3n)”
this is true for c = 4 and n0 = 2
v1.2 13 v1.2 14
4. Sequential search
v1.2 15 v1.2 16
More brute force algorithm examples: Brute force strengths and weaknesses
• Closest pair • Strengths:
– wide applicability
– Problem : find closest among n points in k-
– simplicity
dimensional space
– yields reasonable algorithms for some important problems
– Algorithm : Compute distance between each pair of
• searching
points • string matching
– Efficiency : • matrix multiplication
– yields standard algorithms for simple computational tasks
• sum/product of n numbers
• Convex hull • finding max/min in a list
– Problem : find smallest convex polygon enclosing n • Weaknesses:
points on the plane – rarely yields efficient algorithms
– Algorithm : For each pair of points p1 and p2 – some brute force algorithms unacceptably slow
determine whether all other points lie to the same – not as constructive/creative as some other design
side of the straight line through p1 and p2 techniques
v1.2 17 v1.2 18
– Efficiency :
3
Divide and Conquer Divide-and-conquer technique
The most well known algorithm design a problem of size n
strategy:
1. Divide instance of problem into two or subproblem 1 subproblem 2
more smaller instances of size n/2 of size n/2
a solution to a solution to
2. Solve smaller instances recursively subproblem 1 subproblem 2
v1.2 19 v1.2 20
4
Efficiency of mergesort Quicksort
• All cases have same efficiency: T ( n log n) • Select a pivot (partitioning element)
• Rearrange the list so that all the elements in the
positions before the pivot are smaller than or equal to
• Number of comparisons is close to theoretical
the pivot and those after the pivot are larger than the
minimum for comparison-based sorting:
pivot (See algorithm Partition in section 4.2)
– log n ! ˜ n lg n - 1.44 n • Exchange the pivot with the last element in the first
(i.e., = sublist) – the pivot is now in its final position
• Space requirement: T ( n ) (NOT in-place) • Sort the two sublists
p
• Can be implemented without recursion
(bottom-up)
A[i]=p A[i]>p
v1.2 25 v1.2 26
v1.2 27 v1.2 28
5
Efficiency of QuickHull algorithm
• Finding point farthest away from line P1P2 can be
done in linear time
• This gives same efficiency as quicksort :
– Worst case: T ( n2)
– Average case: T ( n log n)
v1.2 31