COSC 1P03
Introduction to Data Structures 2.1
Chapter 2 Analysis of Algorithms
COSC 1P03
Introduction to Data Structures 2.2
Analysis of Algorithms
• multiple algorithms for same problem − time-space tradeoff
• same algorithm on various computers • how to compare • complexity analysis
COSC 1P03
Introduction to Data Structures 2.3
Asymptotic Complexity
• comparison independent of actual data and machine used • problem size • significant step • time complexity in terms of problem size
− e.g. g(n) = n2/2 + 5n/2 - 3 • compare functions
− approximation of g(n) ° asymptotic complexity
• dominant term
n n2/2 5n/2 -3 n2/2+5n/2-3 (n2/2+5n/2-3)/(n2/2) 0 0 0 -3 -3 ∞ 1 0.5 2.5 -3 0 0 3 4.5 7.5 -3 9 2 5 12.5 12.5 -3 22 1.76 10 50 25 -3 72 1.44 100 5000 250 -3 5247 1.0494 1000 500,000 2500 -3 502,493 1.004954 10,000 50,000,000 25,000 -3 50,025,997 1.00051994
Table 13.1 Convergence of funct ion to high-order term
COSC 1P03
Introduction to Data Structures 2.5
Methods • big-O notation
− order O(…) − order of dominant term − definition − desire lowest order such function
• big-Ω notation − definition − if g(n) is O(f(n)) then f(n) is Ω(g(n))
• big-Θ notation − definition − if g(n) is Θ(f(n)) then g(n) is O(f(n)) and g(n) is
Ω(f(n)) • comparison • complexity classes
− polynomial − non-polynomial
h(n)g(n)f(n)
g(n) is O(f(n))
g(n) is Ω(h(n))
Figure 13.1 Big-O, big-Ω and big-Θ
class order constant O(1) logarithmic O(lg n) linear O(n) n log n O(n lg n) quadratic O(n2) cubic O(n3) O(n4) O(2n)
Table 13.2 Complexity classes
O(n)
O(lg n)
O(1)
O(n lg n)O(n2)O(n3)O(2n)
Figure 13.2 Orders of algori thms
COSC 1P03
Introduction to Data Structures 2.9
Determining Complexity • informal analysis
− rough count of significant step • constant time (O(1))
− number of times independent of n ° linear code
• loops − if number of times is a factor of n
° order is the order of the factor • nested code
− nesting rule ° sum of the orders
• sequential code − sequence rule
° maximum of the orders • examples
some other steps the significant step some other steps
for ( int i=0 ; i<n ; i++ ) some other steps the significant step some other steps ;
some other steps the significant step some other steps for ( int i=0 ; i<n ; i++ ) some other steps the significant step some other steps ;
for ( int i=5 ; i<2*n ; i=i+5 ) : ;
is O(n) and: for ( int i=n*n ; i>0 ; i-- ) : ;
is O(n2), while: for ( int i=1 ; i<n ; i=i*2 ) : ;
is O(lg n).
COSC 1P03
Introduction to Data Structures 2.14
Timing Algorithms
• determining actual run-time of program − to compare implementations or hardware
• data? − representative set
• System.currentTimeMillis
• timing • example – methods of different order
− results • accuracy of results
long t1, t2, elapsed; : t1 = System.currentTimeMillis(); code to be timed here t2 = System.currentTimeMillis(); elapsed = t2 - t1;
COSC 1P03
Introduction to Data Structures 2.17
Ch 10 Searching & Sorting
COSC 1P03
Introduction to Data Structures 2.18
Searching
• Locating one record (object) from a collection • Search key
− unique vs duplicate • Outcomes
− found/not found • Simple vs exhaustive search • Internal vs external • Order
− significant step ° probe (checking key)
− simple O(n) − best O(lg n)
• Key comparison search
COSC 1P03
Introduction to Data Structures 2.20
Sequential (Linear) Search
• Principle − probe in order
• Algorithm − as variation of basic search
• Simple vs exhaustive • Analysis
− depends on where element is found ° average performance
− each probe eliminates 1 entry − O(n)
• E.g. view student
COSC 1P03
Introduction to Data Structures 2.23
Binary Search
• Large collections − e.g. telephone book
• Requires - sorted by key • Principle
− distribution of keys? − probe at midpoint
• Algorithm − probing − bounds − termination
• Analysis − worst case − each probe eliminates 1/2 remaining entries − O(log n)
• E.g. view students
COSC 1P03
Introduction to Data Structures 2.27
Comparison
• Orders − O(n) vs O(lg n)
• Requirements − unsorted vs sorted
• Complexity of code − programmer time − maintenance
• Size of n − n≤32
• Comparison statistics