com s 228 algorithms and analysis instructor: ying cai department of computer science iowa state...
TRANSCRIPT
![Page 1: COM S 228 Algorithms and Analysis Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff 201](https://reader036.vdocument.in/reader036/viewer/2022062317/5a4d1b0e7f8b9ab05998cf46/html5/thumbnails/1.jpg)
COM S 228Algorithms and Analysis
Instructor: Ying Cai
Department of Computer ScienceIowa State [email protected]: Atanasoff 201
![Page 2: COM S 228 Algorithms and Analysis Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff 201](https://reader036.vdocument.in/reader036/viewer/2022062317/5a4d1b0e7f8b9ab05998cf46/html5/thumbnails/2.jpg)
Algorithm
An algorithm is a strategy for solving a problem, independent of the actual implementation
![Page 3: COM S 228 Algorithms and Analysis Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff 201](https://reader036.vdocument.in/reader036/viewer/2022062317/5a4d1b0e7f8b9ab05998cf46/html5/thumbnails/3.jpg)
![Page 4: COM S 228 Algorithms and Analysis Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff 201](https://reader036.vdocument.in/reader036/viewer/2022062317/5a4d1b0e7f8b9ab05998cf46/html5/thumbnails/4.jpg)
![Page 5: COM S 228 Algorithms and Analysis Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff 201](https://reader036.vdocument.in/reader036/viewer/2022062317/5a4d1b0e7f8b9ab05998cf46/html5/thumbnails/5.jpg)
Which algorithms are faster Implement both algorithms and measure them with a stopwatch or count CPU cyclesThis approach has several problems The system may be running other applications, which share the same CPU
Many other factors impact the speed Memory swapping Garbage collection
Both must be implemented, which is often not practical
It is unclear how the algorithms scale when you give a faster CPU
![Page 6: COM S 228 Algorithms and Analysis Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff 201](https://reader036.vdocument.in/reader036/viewer/2022062317/5a4d1b0e7f8b9ab05998cf46/html5/thumbnails/6.jpg)
Time Complexity (Run Time)The time complexity of an algorithm is a function that describes the number of basic execution steps in terms of the input sizeWe express time complexity using Big-O notation
![Page 7: COM S 228 Algorithms and Analysis Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff 201](https://reader036.vdocument.in/reader036/viewer/2022062317/5a4d1b0e7f8b9ab05998cf46/html5/thumbnails/7.jpg)
Algorithm: Sequential Search
Basic idea
Pseudocode
The running time of the sequential search depends on the particular values in A. If v is the first item, it takes one step; If v is not present, which is the worst case, it takes T(n) = 3n+3. The worst-case time complexity of this algorithm is O(n), or “big-O of N”
![Page 8: COM S 228 Algorithms and Analysis Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff 201](https://reader036.vdocument.in/reader036/viewer/2022062317/5a4d1b0e7f8b9ab05998cf46/html5/thumbnails/8.jpg)
Notion of O(f(n))
![Page 9: COM S 228 Algorithms and Analysis Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff 201](https://reader036.vdocument.in/reader036/viewer/2022062317/5a4d1b0e7f8b9ab05998cf46/html5/thumbnails/9.jpg)
Notion of O(f(n))
![Page 10: COM S 228 Algorithms and Analysis Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff 201](https://reader036.vdocument.in/reader036/viewer/2022062317/5a4d1b0e7f8b9ab05998cf46/html5/thumbnails/10.jpg)
Notion of O(f(n))
![Page 11: COM S 228 Algorithms and Analysis Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff 201](https://reader036.vdocument.in/reader036/viewer/2022062317/5a4d1b0e7f8b9ab05998cf46/html5/thumbnails/11.jpg)
Array Equality Revisited
Algorithm 1
In the worst case, this algorithm needs to search the entire array B for A[i]
![Page 12: COM S 228 Algorithms and Analysis Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff 201](https://reader036.vdocument.in/reader036/viewer/2022062317/5a4d1b0e7f8b9ab05998cf46/html5/thumbnails/12.jpg)
Array Equality Revisited
![Page 13: COM S 228 Algorithms and Analysis Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff 201](https://reader036.vdocument.in/reader036/viewer/2022062317/5a4d1b0e7f8b9ab05998cf46/html5/thumbnails/13.jpg)
![Page 14: COM S 228 Algorithms and Analysis Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff 201](https://reader036.vdocument.in/reader036/viewer/2022062317/5a4d1b0e7f8b9ab05998cf46/html5/thumbnails/14.jpg)
Algorithm Analysis in Practice
In practice, algorithm analysis is seldom done at the level of detail as we have done so far
![Page 15: COM S 228 Algorithms and Analysis Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff 201](https://reader036.vdocument.in/reader036/viewer/2022062317/5a4d1b0e7f8b9ab05998cf46/html5/thumbnails/15.jpg)
Example 1
void printAll(int[] array) {
for (int i=0; i< array.length; i++)
{System.out.println(array[i]);
}}
![Page 16: COM S 228 Algorithms and Analysis Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff 201](https://reader036.vdocument.in/reader036/viewer/2022062317/5a4d1b0e7f8b9ab05998cf46/html5/thumbnails/16.jpg)
Example 2
// assuming array.length >= 1000void sumDouble(double [] array) {
double sum = 0.0;for (int i=0; i< 1000; i++) {
sum = sum + i;}
}
![Page 17: COM S 228 Algorithms and Analysis Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff 201](https://reader036.vdocument.in/reader036/viewer/2022062317/5a4d1b0e7f8b9ab05998cf46/html5/thumbnails/17.jpg)
Example 3
for (int i =0; i< array.length; i++) {
method1(); // take O(n)method2(); // takes O(n2)
}
![Page 18: COM S 228 Algorithms and Analysis Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff 201](https://reader036.vdocument.in/reader036/viewer/2022062317/5a4d1b0e7f8b9ab05998cf46/html5/thumbnails/18.jpg)
Example 4
![Page 19: COM S 228 Algorithms and Analysis Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff 201](https://reader036.vdocument.in/reader036/viewer/2022062317/5a4d1b0e7f8b9ab05998cf46/html5/thumbnails/19.jpg)
![Page 20: COM S 228 Algorithms and Analysis Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff 201](https://reader036.vdocument.in/reader036/viewer/2022062317/5a4d1b0e7f8b9ab05998cf46/html5/thumbnails/20.jpg)
Practical Implication of Asymptotic Analysis
![Page 21: COM S 228 Algorithms and Analysis Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff 201](https://reader036.vdocument.in/reader036/viewer/2022062317/5a4d1b0e7f8b9ab05998cf46/html5/thumbnails/21.jpg)
Computation Time