chapter 2 analysis of algorithmscosc.brocku.ca/~efoxwell/1p03/slides/week02.pdfcosc 1p03...
TRANSCRIPT
![Page 1: Chapter 2 Analysis of Algorithmscosc.brocku.ca/~efoxwell/1P03/slides/Week02.pdfCOSC 1P03 Introduction to Data Structures 2.2 Analysis of Algorithms • multiple algorithms for same](https://reader030.vdocuments.mx/reader030/viewer/2022012923/5b3f6f597f8b9a51528c0caf/html5/thumbnails/1.jpg)
COSC 1P03
Introduction to Data Structures 2.1
Chapter 2 Analysis of Algorithms
![Page 2: Chapter 2 Analysis of Algorithmscosc.brocku.ca/~efoxwell/1P03/slides/Week02.pdfCOSC 1P03 Introduction to Data Structures 2.2 Analysis of Algorithms • multiple algorithms for same](https://reader030.vdocuments.mx/reader030/viewer/2022012923/5b3f6f597f8b9a51528c0caf/html5/thumbnails/2.jpg)
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
![Page 3: Chapter 2 Analysis of Algorithmscosc.brocku.ca/~efoxwell/1P03/slides/Week02.pdfCOSC 1P03 Introduction to Data Structures 2.2 Analysis of Algorithms • multiple algorithms for same](https://reader030.vdocuments.mx/reader030/viewer/2022012923/5b3f6f597f8b9a51528c0caf/html5/thumbnails/3.jpg)
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
![Page 4: Chapter 2 Analysis of Algorithmscosc.brocku.ca/~efoxwell/1P03/slides/Week02.pdfCOSC 1P03 Introduction to Data Structures 2.2 Analysis of Algorithms • multiple algorithms for same](https://reader030.vdocuments.mx/reader030/viewer/2022012923/5b3f6f597f8b9a51528c0caf/html5/thumbnails/4.jpg)
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
![Page 5: Chapter 2 Analysis of Algorithmscosc.brocku.ca/~efoxwell/1P03/slides/Week02.pdfCOSC 1P03 Introduction to Data Structures 2.2 Analysis of Algorithms • multiple algorithms for same](https://reader030.vdocuments.mx/reader030/viewer/2022012923/5b3f6f597f8b9a51528c0caf/html5/thumbnails/5.jpg)
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
![Page 6: Chapter 2 Analysis of Algorithmscosc.brocku.ca/~efoxwell/1P03/slides/Week02.pdfCOSC 1P03 Introduction to Data Structures 2.2 Analysis of Algorithms • multiple algorithms for same](https://reader030.vdocuments.mx/reader030/viewer/2022012923/5b3f6f597f8b9a51528c0caf/html5/thumbnails/6.jpg)
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-Θ
![Page 7: Chapter 2 Analysis of Algorithmscosc.brocku.ca/~efoxwell/1P03/slides/Week02.pdfCOSC 1P03 Introduction to Data Structures 2.2 Analysis of Algorithms • multiple algorithms for same](https://reader030.vdocuments.mx/reader030/viewer/2022012923/5b3f6f597f8b9a51528c0caf/html5/thumbnails/7.jpg)
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
![Page 8: Chapter 2 Analysis of Algorithmscosc.brocku.ca/~efoxwell/1P03/slides/Week02.pdfCOSC 1P03 Introduction to Data Structures 2.2 Analysis of Algorithms • multiple algorithms for same](https://reader030.vdocuments.mx/reader030/viewer/2022012923/5b3f6f597f8b9a51528c0caf/html5/thumbnails/8.jpg)
O(n)
O(lg n)
O(1)
O(n lg n)O(n2)O(n3)O(2n)
Figure 13.2 Orders of algori thms
![Page 9: Chapter 2 Analysis of Algorithmscosc.brocku.ca/~efoxwell/1P03/slides/Week02.pdfCOSC 1P03 Introduction to Data Structures 2.2 Analysis of Algorithms • multiple algorithms for same](https://reader030.vdocuments.mx/reader030/viewer/2022012923/5b3f6f597f8b9a51528c0caf/html5/thumbnails/9.jpg)
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
![Page 10: Chapter 2 Analysis of Algorithmscosc.brocku.ca/~efoxwell/1P03/slides/Week02.pdfCOSC 1P03 Introduction to Data Structures 2.2 Analysis of Algorithms • multiple algorithms for same](https://reader030.vdocuments.mx/reader030/viewer/2022012923/5b3f6f597f8b9a51528c0caf/html5/thumbnails/10.jpg)
some other steps the significant step some other steps
![Page 11: Chapter 2 Analysis of Algorithmscosc.brocku.ca/~efoxwell/1P03/slides/Week02.pdfCOSC 1P03 Introduction to Data Structures 2.2 Analysis of Algorithms • multiple algorithms for same](https://reader030.vdocuments.mx/reader030/viewer/2022012923/5b3f6f597f8b9a51528c0caf/html5/thumbnails/11.jpg)
for ( int i=0 ; i<n ; i++ ) some other steps the significant step some other steps ;
![Page 12: Chapter 2 Analysis of Algorithmscosc.brocku.ca/~efoxwell/1P03/slides/Week02.pdfCOSC 1P03 Introduction to Data Structures 2.2 Analysis of Algorithms • multiple algorithms for same](https://reader030.vdocuments.mx/reader030/viewer/2022012923/5b3f6f597f8b9a51528c0caf/html5/thumbnails/12.jpg)
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 ;
![Page 13: Chapter 2 Analysis of Algorithmscosc.brocku.ca/~efoxwell/1P03/slides/Week02.pdfCOSC 1P03 Introduction to Data Structures 2.2 Analysis of Algorithms • multiple algorithms for same](https://reader030.vdocuments.mx/reader030/viewer/2022012923/5b3f6f597f8b9a51528c0caf/html5/thumbnails/13.jpg)
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).
![Page 14: Chapter 2 Analysis of Algorithmscosc.brocku.ca/~efoxwell/1P03/slides/Week02.pdfCOSC 1P03 Introduction to Data Structures 2.2 Analysis of Algorithms • multiple algorithms for same](https://reader030.vdocuments.mx/reader030/viewer/2022012923/5b3f6f597f8b9a51528c0caf/html5/thumbnails/14.jpg)
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
![Page 15: Chapter 2 Analysis of Algorithmscosc.brocku.ca/~efoxwell/1P03/slides/Week02.pdfCOSC 1P03 Introduction to Data Structures 2.2 Analysis of Algorithms • multiple algorithms for same](https://reader030.vdocuments.mx/reader030/viewer/2022012923/5b3f6f597f8b9a51528c0caf/html5/thumbnails/15.jpg)
long t1, t2, elapsed; : t1 = System.currentTimeMillis(); code to be timed here t2 = System.currentTimeMillis(); elapsed = t2 - t1;
![Page 16: Chapter 2 Analysis of Algorithmscosc.brocku.ca/~efoxwell/1P03/slides/Week02.pdfCOSC 1P03 Introduction to Data Structures 2.2 Analysis of Algorithms • multiple algorithms for same](https://reader030.vdocuments.mx/reader030/viewer/2022012923/5b3f6f597f8b9a51528c0caf/html5/thumbnails/16.jpg)
![Page 17: Chapter 2 Analysis of Algorithmscosc.brocku.ca/~efoxwell/1P03/slides/Week02.pdfCOSC 1P03 Introduction to Data Structures 2.2 Analysis of Algorithms • multiple algorithms for same](https://reader030.vdocuments.mx/reader030/viewer/2022012923/5b3f6f597f8b9a51528c0caf/html5/thumbnails/17.jpg)
COSC 1P03
Introduction to Data Structures 2.17
Ch 10 Searching & Sorting
![Page 18: Chapter 2 Analysis of Algorithmscosc.brocku.ca/~efoxwell/1P03/slides/Week02.pdfCOSC 1P03 Introduction to Data Structures 2.2 Analysis of Algorithms • multiple algorithms for same](https://reader030.vdocuments.mx/reader030/viewer/2022012923/5b3f6f597f8b9a51528c0caf/html5/thumbnails/18.jpg)
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
![Page 19: Chapter 2 Analysis of Algorithmscosc.brocku.ca/~efoxwell/1P03/slides/Week02.pdfCOSC 1P03 Introduction to Data Structures 2.2 Analysis of Algorithms • multiple algorithms for same](https://reader030.vdocuments.mx/reader030/viewer/2022012923/5b3f6f597f8b9a51528c0caf/html5/thumbnails/19.jpg)
![Page 20: Chapter 2 Analysis of Algorithmscosc.brocku.ca/~efoxwell/1P03/slides/Week02.pdfCOSC 1P03 Introduction to Data Structures 2.2 Analysis of Algorithms • multiple algorithms for same](https://reader030.vdocuments.mx/reader030/viewer/2022012923/5b3f6f597f8b9a51528c0caf/html5/thumbnails/20.jpg)
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
![Page 21: Chapter 2 Analysis of Algorithmscosc.brocku.ca/~efoxwell/1P03/slides/Week02.pdfCOSC 1P03 Introduction to Data Structures 2.2 Analysis of Algorithms • multiple algorithms for same](https://reader030.vdocuments.mx/reader030/viewer/2022012923/5b3f6f597f8b9a51528c0caf/html5/thumbnails/21.jpg)
![Page 22: Chapter 2 Analysis of Algorithmscosc.brocku.ca/~efoxwell/1P03/slides/Week02.pdfCOSC 1P03 Introduction to Data Structures 2.2 Analysis of Algorithms • multiple algorithms for same](https://reader030.vdocuments.mx/reader030/viewer/2022012923/5b3f6f597f8b9a51528c0caf/html5/thumbnails/22.jpg)
![Page 23: Chapter 2 Analysis of Algorithmscosc.brocku.ca/~efoxwell/1P03/slides/Week02.pdfCOSC 1P03 Introduction to Data Structures 2.2 Analysis of Algorithms • multiple algorithms for same](https://reader030.vdocuments.mx/reader030/viewer/2022012923/5b3f6f597f8b9a51528c0caf/html5/thumbnails/23.jpg)
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
![Page 24: Chapter 2 Analysis of Algorithmscosc.brocku.ca/~efoxwell/1P03/slides/Week02.pdfCOSC 1P03 Introduction to Data Structures 2.2 Analysis of Algorithms • multiple algorithms for same](https://reader030.vdocuments.mx/reader030/viewer/2022012923/5b3f6f597f8b9a51528c0caf/html5/thumbnails/24.jpg)
![Page 25: Chapter 2 Analysis of Algorithmscosc.brocku.ca/~efoxwell/1P03/slides/Week02.pdfCOSC 1P03 Introduction to Data Structures 2.2 Analysis of Algorithms • multiple algorithms for same](https://reader030.vdocuments.mx/reader030/viewer/2022012923/5b3f6f597f8b9a51528c0caf/html5/thumbnails/25.jpg)
![Page 26: Chapter 2 Analysis of Algorithmscosc.brocku.ca/~efoxwell/1P03/slides/Week02.pdfCOSC 1P03 Introduction to Data Structures 2.2 Analysis of Algorithms • multiple algorithms for same](https://reader030.vdocuments.mx/reader030/viewer/2022012923/5b3f6f597f8b9a51528c0caf/html5/thumbnails/26.jpg)
![Page 27: Chapter 2 Analysis of Algorithmscosc.brocku.ca/~efoxwell/1P03/slides/Week02.pdfCOSC 1P03 Introduction to Data Structures 2.2 Analysis of Algorithms • multiple algorithms for same](https://reader030.vdocuments.mx/reader030/viewer/2022012923/5b3f6f597f8b9a51528c0caf/html5/thumbnails/27.jpg)
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
![Page 28: Chapter 2 Analysis of Algorithmscosc.brocku.ca/~efoxwell/1P03/slides/Week02.pdfCOSC 1P03 Introduction to Data Structures 2.2 Analysis of Algorithms • multiple algorithms for same](https://reader030.vdocuments.mx/reader030/viewer/2022012923/5b3f6f597f8b9a51528c0caf/html5/thumbnails/28.jpg)