Successfully reported this slideshow.                                                                 Upcoming SlideShare
×

# Algorithms Lecture 3: Analysis of Algorithms II

931 views

Published on

We will discuss the following: Maximum Pairwise Product, Fibonacci, Greatest Common Divisors, Naive algorithm is too slow. The Efficient algorithm is much better. Finding the correct algorithm requires knowing something interesting about the problem

Published in: Education
• Full Name
Comment goes here.

Are you sure you want to Yes No • Benha University http://bu.edu.eg/staff/mloey

Are you sure you want to  Yes  No

### Algorithms Lecture 3: Analysis of Algorithms II

1. 1. Analysis and Design of Algorithms Analysis of Algorithms II
2. 2. Analysis and Design of Algorithms Maximum Pairwise Product Fibonacci Greatest Common Divisors
3. 3. Analysis and Design of Algorithms Maximum Pairwise Product
4. 4. Analysis and Design of Algorithms Given a sequence of non-negative integers a0,…,an−1, find the maximum pairwise product, that is, the largest integer that can be obtained by multiplying two different elements from the sequence (or, more formally, max aiaj where 0≤i≠j≤n−1). Different elements here mean ai and aj with i≠j (it can be the case that ai=aj).
5. 5. Analysis and Design of Algorithms Constraints 2≤n≤2 * 105 & 0≤a0,…,an−1≤105.
6. 6. Analysis and Design of Algorithms  Sample 1  Input: 1 2 3  Output:6  Sample 2  Input: 7 5 14 2 8 8 10 1 2 3  Output:140
7. 7. Analysis and Design of Algorithms  Sample 3  Input: 4 6 2 6 1  Output:36
8. 8. Analysis and Design of Algorithms  Assume the following array 2 4 3 5 1
9. 9. Analysis and Design of Algorithms  Assume the following array 2 4 3 5 1 i j Result=0
10. 10. Analysis and Design of Algorithms  Assume the following array 2 4 3 5 1 i j If a[i]*a[j] > result result=a[i]*a[j]=8
11. 11. Analysis and Design of Algorithms  Assume the following array 2 4 3 5 1 i j If a[i]*a[j] > result result=8
12. 12. Analysis and Design of Algorithms  Assume the following array 2 4 3 5 1 i j If a[i]*a[j] > result result= a[i]*a[j] =10
13. 13. Analysis and Design of Algorithms  Naive algorithm
14. 14. Analysis and Design of Algorithms  Python Code:
15. 15. Analysis and Design of Algorithms  Time complexity O(n2)
16. 16. Analysis and Design of Algorithms we need a faster algorithm. This is because our program performs about n2 steps on a sequence of length n. For the maximal possible value n=200,000 = 2*105, the number of steps is 40,000,000,000 = 4*1010. This is too much. Recall that modern machines can perform roughly 109 basic operations per second
17. 17. Analysis and Design of Algorithms  Assume the following array 2 4 3 5 1
18. 18. Analysis and Design of Algorithms  Find maximum number1 2 4 3 5 1 max1
19. 19. Analysis and Design of Algorithms  Find maximum number2 but not maximum number1 2 4 3 5 1 max2 max1
20. 20. Analysis and Design of Algorithms  Find maximum number2 but not maximum number1 2 4 3 5 1 max2 max1 return max1*max2
21. 21. Analysis and Design of Algorithms  Efficient algorithm
22. 22. Analysis and Design of Algorithms  Efficient algorithm
23. 23. Analysis and Design of Algorithms  Time complexity O(n)
24. 24. Analysis and Design of Algorithms Fibonacci
25. 25. Analysis and Design of Algorithms 0,1,1,2,3,5,8,13,21,34, …
26. 26. Analysis and Design of Algorithms Definition: 𝐹𝑛 = 0 𝑛 = 0 1 𝑛 = 1 𝐹𝑛−2 + 𝐹𝑛−1 𝑛 > 1
27. 27. Analysis and Design of Algorithms Examples: 𝐹8 = 21 𝐹20 = 6765 𝐹50 = 12586269025 𝐹100 = 354224848179261915075
28. 28. Analysis and Design of Algorithms  Examples: 𝐹500 = 1394232245616978801397243828 7040728395007025658769730726 4108962948325571622863290691 557658876222521294125
29. 29. Analysis and Design of Algorithms  Naive algorithm
30. 30. Analysis and Design of Algorithms  Naive algorithm Very Long Time why????
31. 31. Analysis and Design of Algorithms
32. 32. Analysis and Design of Algorithms
33. 33. Analysis and Design of Algorithms F6 F5 F4 F3 F2 F1 F0 F0 F1 F0 F2 F1 F0 F0 F3 F2 F1 F0 F0 F1 F0 F4 F3 F2 F1 F0 F0 F1 F0 F2 F1 F0 F0
34. 34. Analysis and Design of Algorithms Fib algorithm is very slow because of recursion Time complexity = O(2n)
35. 35. Analysis and Design of Algorithms  Efficient algorithm 0 1 1 2 3 5 Create array then insert fibonacci
36. 36. Analysis and Design of Algorithms  Efficient algorithm
37. 37. Analysis and Design of Algorithms  Efficient algorithm short Time why????
38. 38. Analysis and Design of Algorithms Fib_Fast algorithm is fast because of loop + array Time complexity = O(n2)
39. 39. Analysis and Design of Algorithms  Efficient algorithm  Try Very long Time why????
40. 40. Analysis and Design of Algorithms Advanced algorithm No array Need two variable + Loop
41. 41. Analysis and Design of Algorithms  Advanced algorithm  Compute F6  a=0, b=1 0 1 a b
42. 42. Analysis and Design of Algorithms  Advanced algorithm  Compute F6  a=b, b=a+b 1 1 a b
43. 43. Analysis and Design of Algorithms  Advanced algorithm  Compute F6  a=b, b=a+b 1 2 a b
44. 44. Analysis and Design of Algorithms  Advanced algorithm  Compute F6  a=b, b=a+b 2 3 a b
45. 45. Analysis and Design of Algorithms  Advanced algorithm  Compute F6  a=b, b=a+b 3 5 a b
46. 46. Analysis and Design of Algorithms  Advanced algorithm  Compute F6  a=b, b=a+b 5 8 a b
47. 47. Analysis and Design of Algorithms  Advanced algorithm  Compute F6=8 5 8 a b
48. 48. Analysis and Design of Algorithms  Advanced algorithm
49. 49. Analysis and Design of Algorithms  Advanced algorithm Very short Time why????
50. 50. Analysis and Design of Algorithms Fib_Faster algorithm is faster because of loop + two variables Time complexity = O(n)
51. 51. Analysis and Design of Algorithms  Advanced algorithm  Try Short Time why????
52. 52. Analysis and Design of Algorithms Greatest Common Divisors
53. 53. Analysis and Design of Algorithms In mathematics, the greatest common divisor (gcd) of two or more integers, which are not all zero, is the largest positive integer that divides each of the integers.
54. 54. Analysis and Design of Algorithms Input Integers a,b >=0 Output gcd(a,b)
55. 55. Analysis and Design of Algorithms  What is the greatest common divisor of 54 and 24?  The divisors of 54 are: 1,2,3,6,9,18,27,54  Similarly, the divisors of 24 are: 1,2,3,4,6,8,12,24  The numbers that these two lists share in common are the common divisors of 54 and 24: 1,2,3,6  The greatest of these is 6. That is, the greatest common divisor of 54 and 24. gcd(54,24)=6
56. 56. Analysis and Design of Algorithms  Naive algorithm
57. 57. Analysis and Design of Algorithms
58. 58. Analysis and Design of Algorithms gcd algorithm is slow because of loop Time complexity = O(n) n depend on a,b
59. 59. Analysis and Design of Algorithms  Efficient algorithm
60. 60. Analysis and Design of Algorithms  Efficient algorithm
61. 61. Analysis and Design of Algorithms  Efficient algorithm gcd_fast((3918848, 1653264)) gcd_fast((1653264, 612320)) gcd_fast((612320, 428624)) gcd_fast((428624, 183696)) gcd_fast((183696, 61232)) return 61232
62. 62. Analysis and Design of Algorithms Efficient algorithm Take 5 steps to solve gcd_fast( ( 3918848, 1653264 ) ) Time complexity = O(log(n))  n depend on a,b
63. 63. Analysis and Design of Algorithms Naive algorithm is too slow. The Efficient algorithm is much better. Finding the correct algorithm requires knowing something interesting about the problem.