algorithm: what is it ? - computer science
TRANSCRIPT
![Page 1: Algorithm: what is it ? - Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022020703/61fb27d82e268c58cd5ace8a/html5/thumbnails/1.jpg)
AlgorithmsAlgorithm: what is it ?
![Page 2: Algorithm: what is it ? - Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022020703/61fb27d82e268c58cd5ace8a/html5/thumbnails/2.jpg)
AlgorithmsAlgorithm: what is it ?
Some representative problems :
- Interval Scheduling
![Page 3: Algorithm: what is it ? - Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022020703/61fb27d82e268c58cd5ace8a/html5/thumbnails/3.jpg)
AlgorithmsAlgorithm: what is it ?
Some representative problems :
- Interval Scheduling
- Bipartite Matching
![Page 4: Algorithm: what is it ? - Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022020703/61fb27d82e268c58cd5ace8a/html5/thumbnails/4.jpg)
AlgorithmsAlgorithm: what is it ?
Some representative problems :
- Interval Scheduling
- Bipartite Matching
- Independent Set
![Page 5: Algorithm: what is it ? - Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022020703/61fb27d82e268c58cd5ace8a/html5/thumbnails/5.jpg)
AlgorithmsAlgorithm: what is it ?
Some representative problems :
- Interval Scheduling
- Bipartite Matching
- Independent Set
- Area of a Polygon
![Page 6: Algorithm: what is it ? - Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022020703/61fb27d82e268c58cd5ace8a/html5/thumbnails/6.jpg)
AlgorithmsHow to decide which algorithm is better ?
Search problem
Input : a sequence of n numbers (in an array A) and a number x
Output : YES, if A contains x, NO otherwise
![Page 7: Algorithm: what is it ? - Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022020703/61fb27d82e268c58cd5ace8a/html5/thumbnails/7.jpg)
AlgorithmsHow to decide which algorithm is better ?
Search problem
Input : a sequence of n numbers (in an array A) and a number x
Output : YES, if A contains x, NO otherwise
What if A is already sorted ?
![Page 8: Algorithm: what is it ? - Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022020703/61fb27d82e268c58cd5ace8a/html5/thumbnails/8.jpg)
Running TimeO(n) – running time of the linear search
O(log n) – running time of the binary search
Def : Big-Oh (asymptotic upper bound)
f(n) = O(g(n)) if there exists a constant c > 0 and a constant n0such that for every n ≥ n0 we have f(n) ≤ c g(n)
Examples :
n, n3, log n, 2n, 7n2 + n3/3, 1, 1 + log n, n log n, n + log n
![Page 9: Algorithm: what is it ? - Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022020703/61fb27d82e268c58cd5ace8a/html5/thumbnails/9.jpg)
Running TimeDef : Big-Oh (asymptotic upper bound)
f(n) = O(g(n)) if there exists a constant c > 0 and a constant n0such that for every n ≥ n0 we have f(n) ≤ c g(n)
Example: Prove that n = O(n3)
![Page 10: Algorithm: what is it ? - Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022020703/61fb27d82e268c58cd5ace8a/html5/thumbnails/10.jpg)
Running TimeDef : Big-Oh (asymptotic upper bound)
f(n) = O(g(n)) if there exists a constant c > 0 and a constant n0such that for every n ≥ n0 we have f(n) ≤ c g(n)
Example: Prove that n3 = O(7n2+n3/3)
![Page 11: Algorithm: what is it ? - Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022020703/61fb27d82e268c58cd5ace8a/html5/thumbnails/11.jpg)
Running TimeDef : Big-Oh (asymptotic upper bound)
f(n) = O(g(n)) if there exists a constant c > 0 and a constant n0such that for every n ≥ n0 we have f(n) ≤ c g(n)
Example: Prove that log10 n = O(log n)
![Page 12: Algorithm: what is it ? - Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022020703/61fb27d82e268c58cd5ace8a/html5/thumbnails/12.jpg)
Running TimeDef : Big-Oh (asymptotic upper bound)
f(n) = O(g(n)) if there exists a constant c > 0 and a constant n0such that for every n ≥ n0 we have f(n) ≤ c g(n)
Example: what about 3n and 2n
![Page 13: Algorithm: what is it ? - Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022020703/61fb27d82e268c58cd5ace8a/html5/thumbnails/13.jpg)
Running TimeO(n) – running time of the linear search
O(log n) – running time of the binary search
Def : Big-Omega (asymptotic lower bound)
f(n) = (g(n)) if there exists a constant c > 0 and a constant n0such that for every n ≥ n0 we have f(n) ≥ c g(n)
Examples :
n, n3, log n, 2n, 7n2 + n3/3, 1, 1 + log n, n log n, n + log n
![Page 14: Algorithm: what is it ? - Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022020703/61fb27d82e268c58cd5ace8a/html5/thumbnails/14.jpg)
Running TimeO(n) – running time of the linear search
O(log n) – running time of the binary search
Def : Theta (asymptotically tight bound)
f(n) = (g(n)) if there exists constants c1, c2 > 0 and a constant n0 such that for every n ≥ n0 we have c1 g(n) ≤ f(n) ≤ c2 g(n)
Examples :
n, n3, log n, 2n, 7n2 + n3/3, 1, 1 + log n, n log n, n + log n
![Page 15: Algorithm: what is it ? - Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022020703/61fb27d82e268c58cd5ace8a/html5/thumbnails/15.jpg)
A survey of common running times
1. for i=1 to n do2. something
Linear
Also linear :
1. for i=1 to n do2. something3. for i=1 to n do4. something else
![Page 16: Algorithm: what is it ? - Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022020703/61fb27d82e268c58cd5ace8a/html5/thumbnails/16.jpg)
A survey of common running times
Example (linear time):
Given is a point A=(ax, ay) and n points (x1,y1), (x2,y2), …, (xn,yn) specifying a polygon. Decide if A lies inside or outside the polygon.
![Page 17: Algorithm: what is it ? - Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022020703/61fb27d82e268c58cd5ace8a/html5/thumbnails/17.jpg)
A survey of common running times
Example (linear time):
Given are n points (x1,y1), (x2,y2), …, (xn,yn) specifying a polygon. Compute the area of the polygon.
![Page 18: Algorithm: what is it ? - Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022020703/61fb27d82e268c58cd5ace8a/html5/thumbnails/18.jpg)
A survey of common running times
Example (linear time):
Given are n points (x1,y1), (x2,y2), …, (xn,yn) specifying a polygon. Compute the area of the polygon.
![Page 19: Algorithm: what is it ? - Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022020703/61fb27d82e268c58cd5ace8a/html5/thumbnails/19.jpg)
A survey of common running times
O(n log n)
Or:
1. for i=1 to n do2. for j=1 to log(n) do3. something
1. for i=1 to n do2. j=n3. while j>1 do4. something5. j = j/2
![Page 20: Algorithm: what is it ? - Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022020703/61fb27d82e268c58cd5ace8a/html5/thumbnails/20.jpg)
A survey of common running times
Quadratic
1. for i=1 to n do2. for j=1 to n do3. something
![Page 21: Algorithm: what is it ? - Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022020703/61fb27d82e268c58cd5ace8a/html5/thumbnails/21.jpg)
A survey of common running times
Cubic
![Page 22: Algorithm: what is it ? - Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022020703/61fb27d82e268c58cd5ace8a/html5/thumbnails/22.jpg)
A survey of common running times
O(nk) – polynomial (if k is a constant)
![Page 23: Algorithm: what is it ? - Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022020703/61fb27d82e268c58cd5ace8a/html5/thumbnails/23.jpg)
A survey of common running times
Exponential, e.g., O(2k)