Slide 1

Slide 1 text

Analysis and Design of Algorithms Analysis of Algorithms II

Slide 2

Slide 2 text

Analysis and Design of Algorithms Maximum Pairwise Product Fibonacci Greatest Common Divisors

Slide 3

Slide 3 text

Analysis and Design of Algorithms Maximum Pairwise Product

Slide 4

Slide 4 text

Analysis and Design of Algorithms Given a sequence of non-negative integers a 0 ,…,a n−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 a i a j where 0≤i≠j≤n−1). Different elements here mean a i and a j with i≠j (it can be the case that a i =a j ).

Slide 5

Slide 5 text

Analysis and Design of Algorithms Constraints 2≤n≤2 * 105 & 0≤a 0 ,…,a n−1 ≤105.

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

Analysis and Design of Algorithms  Sample 3  Input: 4 6 2 6 1  Output:36

Slide 8

Slide 8 text

Analysis and Design of Algorithms  Assume the following array 2 4 3 5 1

Slide 9

Slide 9 text

Analysis and Design of Algorithms  Assume the following array 2 4 3 5 1 i j Result=0

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

Analysis and Design of Algorithms  Assume the following array 2 4 3 5 1 i j If a[i]*a[j] > result result=8

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

Analysis and Design of Algorithms  Naive algorithm

Slide 14

Slide 14 text

Analysis and Design of Algorithms  Python Code:

Slide 15

Slide 15 text

Analysis and Design of Algorithms  Time complexity O(n2)

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

Analysis and Design of Algorithms  Assume the following array 2 4 3 5 1

Slide 18

Slide 18 text

Analysis and Design of Algorithms  Find maximum number1 2 4 3 5 1 max1

Slide 19

Slide 19 text

Analysis and Design of Algorithms  Find maximum number2 but not maximum number1 2 4 3 5 1 max2 max1

Slide 20

Slide 20 text

Analysis and Design of Algorithms  Find maximum number2 but not maximum number1 2 4 3 5 1 max2 max1 return max1*max2

Slide 21

Slide 21 text

Analysis and Design of Algorithms  Efficient algorithm

Slide 22

Slide 22 text

Analysis and Design of Algorithms  Efficient algorithm

Slide 23

Slide 23 text

Analysis and Design of Algorithms  Time complexity O(n)

Slide 24

Slide 24 text

Analysis and Design of Algorithms Fibonacci

Slide 25

Slide 25 text

Analysis and Design of Algorithms 0,1,1,2,3,5,8,13,21,34, …

Slide 26

Slide 26 text

Analysis and Design of Algorithms Definition:  = 0 = 0 1 = 1 −2 + −1 > 1

Slide 27

Slide 27 text

Analysis and Design of Algorithms Examples: 8 = 21 20 = 6765 50 = 12586269025 100 = 354224848179261915075

Slide 28

Slide 28 text

Analysis and Design of Algorithms  Examples: 500 = 1394232245616978801397243828 7040728395007025658769730726 4108962948325571622863290691 557658876222521294125

Slide 29

Slide 29 text

Analysis and Design of Algorithms  Naive algorithm

Slide 30

Slide 30 text

Analysis and Design of Algorithms  Naive algorithm Very Long Time why????

Slide 31

Slide 31 text

Analysis and Design of Algorithms

Slide 32

Slide 32 text

Analysis and Design of Algorithms

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

Analysis and Design of Algorithms Fib algorithm is very slow because of recursion Time complexity = O(2n)

Slide 35

Slide 35 text

Analysis and Design of Algorithms  Efficient algorithm 0 1 1 2 3 5 Create array then insert fibonacci

Slide 36

Slide 36 text

Analysis and Design of Algorithms  Efficient algorithm

Slide 37

Slide 37 text

Analysis and Design of Algorithms  Efficient algorithm short Time why????

Slide 38

Slide 38 text

Analysis and Design of Algorithms Fib_Fast algorithm is fast because of loop + array Time complexity = O(n2)

Slide 39

Slide 39 text

Analysis and Design of Algorithms  Efficient algorithm  Try Very long Time why????

Slide 40

Slide 40 text

Analysis and Design of Algorithms Advanced algorithm No array Need two variable + Loop

Slide 41

Slide 41 text

Analysis and Design of Algorithms  Advanced algorithm  Compute F 6  a=0, b=1 0 1 a b

Slide 42

Slide 42 text

Analysis and Design of Algorithms  Advanced algorithm  Compute F 6  a=b, b=a+b 1 1 a b

Slide 43

Slide 43 text

Analysis and Design of Algorithms  Advanced algorithm  Compute F 6  a=b, b=a+b 1 2 a b

Slide 44

Slide 44 text

Analysis and Design of Algorithms  Advanced algorithm  Compute F 6  a=b, b=a+b 2 3 a b

Slide 45

Slide 45 text

Analysis and Design of Algorithms  Advanced algorithm  Compute F 6  a=b, b=a+b 3 5 a b

Slide 46

Slide 46 text

Analysis and Design of Algorithms  Advanced algorithm  Compute F 6  a=b, b=a+b 5 8 a b

Slide 47

Slide 47 text

Analysis and Design of Algorithms  Advanced algorithm  Compute F 6 =8 5 8 a b

Slide 48

Slide 48 text

Analysis and Design of Algorithms  Advanced algorithm

Slide 49

Slide 49 text

Analysis and Design of Algorithms  Advanced algorithm Very short Time why????

Slide 50

Slide 50 text

Analysis and Design of Algorithms Fib_Faster algorithm is faster because of loop + two variables Time complexity = O(n)

Slide 51

Slide 51 text

Analysis and Design of Algorithms  Advanced algorithm  Try Short Time why????

Slide 52

Slide 52 text

Analysis and Design of Algorithms Greatest Common Divisors

Slide 53

Slide 53 text

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.

Slide 54

Slide 54 text

Analysis and Design of Algorithms Input Integers a,b >=0 Output gcd(a,b)

Slide 55

Slide 55 text

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

Slide 56

Slide 56 text

Analysis and Design of Algorithms  Naive algorithm

Slide 57

Slide 57 text

Analysis and Design of Algorithms

Slide 58

Slide 58 text

Analysis and Design of Algorithms gcd algorithm is slow because of loop Time complexity = O(n) n depend on a,b

Slide 59

Slide 59 text

Analysis and Design of Algorithms  Efficient algorithm

Slide 60

Slide 60 text

Analysis and Design of Algorithms  Efficient algorithm

Slide 61

Slide 61 text

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

Slide 62

Slide 62 text

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

Slide 63

Slide 63 text

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.

Slide 64

Slide 64 text

Analysis and Design of Algorithms facebook.com/mloey [email protected] twitter.com/mloey linkedin.com/in/mloey [email protected] mloey.github.io

Slide 65

Slide 65 text

Analysis and Design of Algorithms www.YourCompany.com © 2020 Companyname PowerPoint Business Theme. All Rights Reserved. THANKS FOR YOUR TIME