65
Analysis and Design of Algorithms Analysis of Algorithms II

Algorithms Lecture 3: Analysis of Algorithms II

Embed Size (px)

Citation preview

Page 1: Algorithms Lecture 3: Analysis of Algorithms II

Analysis and Design of Algorithms

Analysis of Algorithms II

Page 2: Algorithms Lecture 3: Analysis of Algorithms II

Analysis and Design of Algorithms

Maximum Pairwise Product

Fibonacci

Greatest Common Divisors

Page 3: Algorithms Lecture 3: Analysis of Algorithms II

Analysis and Design of Algorithms

Maximum Pairwise Product

Page 4: Algorithms Lecture 3: Analysis of Algorithms II

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).

Page 5: Algorithms Lecture 3: Analysis of Algorithms II

Analysis and Design of Algorithms

Constraints 2≤n≤2 * 105 & 0≤a0,…,an−1≤105.

Page 6: Algorithms Lecture 3: Analysis of Algorithms II

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

Page 7: Algorithms Lecture 3: Analysis of Algorithms II

Analysis and Design of Algorithms

Sample 3

Input: 4 6 2 6 1

Output:36

Page 8: Algorithms Lecture 3: Analysis of Algorithms II

Analysis and Design of Algorithms

Assume the following array

2 4 3 5 1

Page 9: Algorithms Lecture 3: Analysis of Algorithms II

Analysis and Design of Algorithms

Assume the following array

2 4 3 5 1

i j

Result=0

Page 10: Algorithms Lecture 3: Analysis of Algorithms II

Analysis and Design of Algorithms

Assume the following array

2 4 3 5 1

i j

If a[i]*a[j] > resultresult=a[i]*a[j]=8

Page 11: Algorithms Lecture 3: Analysis of Algorithms II

Analysis and Design of Algorithms

Assume the following array

2 4 3 5 1

i j

If a[i]*a[j] > resultresult=8

Page 12: Algorithms Lecture 3: Analysis of Algorithms II

Analysis and Design of Algorithms

Assume the following array

2 4 3 5 1

i j

If a[i]*a[j] > resultresult= a[i]*a[j] =10

Page 13: Algorithms Lecture 3: Analysis of Algorithms II

Analysis and Design of Algorithms

Naive algorithm

Page 14: Algorithms Lecture 3: Analysis of Algorithms II

Analysis and Design of Algorithms

Python Code:

Page 15: Algorithms Lecture 3: Analysis of Algorithms II

Analysis and Design of Algorithms

Time complexity O(n2)

Page 16: Algorithms Lecture 3: Analysis of Algorithms II

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

Page 17: Algorithms Lecture 3: Analysis of Algorithms II

Analysis and Design of Algorithms

Assume the following array

2 4 3 5 1

Page 18: Algorithms Lecture 3: Analysis of Algorithms II

Analysis and Design of Algorithms

Find maximum number1

2 4 3 5 1

max1

Page 19: Algorithms Lecture 3: Analysis of Algorithms II

Analysis and Design of Algorithms

Find maximum number2 but not maximum number1

2 4 3 5 1

max2 max1

Page 20: Algorithms Lecture 3: Analysis of Algorithms II

Analysis and Design of Algorithms

Find maximum number2 but not maximum number1

2 4 3 5 1

max2 max1

return max1*max2

Page 21: Algorithms Lecture 3: Analysis of Algorithms II

Analysis and Design of Algorithms

Efficient

algorithm

Page 22: Algorithms Lecture 3: Analysis of Algorithms II

Analysis and Design of Algorithms

Efficient algorithm

Page 23: Algorithms Lecture 3: Analysis of Algorithms II

Analysis and Design of Algorithms

Time complexity O(n)

Page 24: Algorithms Lecture 3: Analysis of Algorithms II

Analysis and Design of Algorithms

Fibonacci

Page 25: Algorithms Lecture 3: Analysis of Algorithms II

Analysis and Design of Algorithms

0,1,1,2,3,5,8,13,21,34, …

Page 26: Algorithms Lecture 3: Analysis of Algorithms II

Analysis and Design of Algorithms

Definition:

𝐹𝑛 =0 𝑛 = 01 𝑛 = 1

𝐹𝑛−2 + 𝐹𝑛−1 𝑛 > 1

Page 27: Algorithms Lecture 3: Analysis of Algorithms II

Analysis and Design of Algorithms

Examples:

𝐹8 = 21

𝐹20 = 6765

𝐹50 = 12586269025

𝐹100 = 354224848179261915075

Page 28: Algorithms Lecture 3: Analysis of Algorithms II

Analysis and Design of Algorithms

Examples:

𝐹500 =

1394232245616978801397243828

7040728395007025658769730726

4108962948325571622863290691

557658876222521294125

Page 29: Algorithms Lecture 3: Analysis of Algorithms II

Analysis and Design of Algorithms

Naive algorithm

Page 30: Algorithms Lecture 3: Analysis of Algorithms II

Analysis and Design of Algorithms

Naive algorithm

Very Long Time why????

Page 31: Algorithms Lecture 3: Analysis of Algorithms II

Analysis and Design of Algorithms

Page 32: Algorithms Lecture 3: Analysis of Algorithms II

Analysis and Design of Algorithms

Page 33: Algorithms Lecture 3: Analysis of Algorithms II

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

Page 34: Algorithms Lecture 3: Analysis of Algorithms II

Analysis and Design of Algorithms

Fib algorithm is very slow because of

recursion

Time complexity = O(2n)

Page 35: Algorithms Lecture 3: Analysis of Algorithms II

Analysis and Design of Algorithms

Efficient algorithm

0 1 1 2 3 5

Create array then insert fibonacci

Page 36: Algorithms Lecture 3: Analysis of Algorithms II

Analysis and Design of Algorithms

Efficient algorithm

Page 37: Algorithms Lecture 3: Analysis of Algorithms II

Analysis and Design of Algorithms

Efficient algorithm

short Time why????

Page 38: Algorithms Lecture 3: Analysis of Algorithms II

Analysis and Design of Algorithms

Fib_Fast algorithm is fast because of

loop + array

Time complexity = O(n2)

Page 39: Algorithms Lecture 3: Analysis of Algorithms II

Analysis and Design of Algorithms

Efficient algorithm

Try

Very long Time why????

Page 40: Algorithms Lecture 3: Analysis of Algorithms II

Analysis and Design of Algorithms

Advanced algorithm

No array

Need two variable + Loop

Page 41: Algorithms Lecture 3: Analysis of Algorithms II

Analysis and Design of Algorithms

Advanced algorithm

Compute F6

a=0, b=1

0 1

a b

Page 42: Algorithms Lecture 3: Analysis of Algorithms II

Analysis and Design of Algorithms

Advanced algorithm

Compute F6

a=b, b=a+b

1 1

a b

Page 43: Algorithms Lecture 3: Analysis of Algorithms II

Analysis and Design of Algorithms

Advanced algorithm

Compute F6

a=b, b=a+b

1 2

a b

Page 44: Algorithms Lecture 3: Analysis of Algorithms II

Analysis and Design of Algorithms

Advanced algorithm

Compute F6

a=b, b=a+b

2 3

a b

Page 45: Algorithms Lecture 3: Analysis of Algorithms II

Analysis and Design of Algorithms

Advanced algorithm

Compute F6

a=b, b=a+b

3 5

a b

Page 46: Algorithms Lecture 3: Analysis of Algorithms II

Analysis and Design of Algorithms

Advanced algorithm

Compute F6

a=b, b=a+b

5 8

a b

Page 47: Algorithms Lecture 3: Analysis of Algorithms II

Analysis and Design of Algorithms

Advanced algorithm

Compute F6=8

5 8

a b

Page 48: Algorithms Lecture 3: Analysis of Algorithms II

Analysis and Design of Algorithms

Advanced algorithm

Page 49: Algorithms Lecture 3: Analysis of Algorithms II

Analysis and Design of Algorithms

Advanced algorithm

Very short Time why????

Page 50: Algorithms Lecture 3: Analysis of Algorithms II

Analysis and Design of Algorithms

Fib_Faster algorithm is faster because

of loop + two variables

Time complexity = O(n)

Page 51: Algorithms Lecture 3: Analysis of Algorithms II

Analysis and Design of Algorithms

Advanced algorithm

Try

Short Time why????

Page 52: Algorithms Lecture 3: Analysis of Algorithms II

Analysis and Design of Algorithms

Greatest Common Divisors

Page 53: Algorithms Lecture 3: Analysis of Algorithms II

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.

Page 54: Algorithms Lecture 3: Analysis of Algorithms II

Analysis and Design of Algorithms

Input Integers a,b >=0

Output gcd(a,b)

Page 55: Algorithms Lecture 3: Analysis of Algorithms II

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

Page 56: Algorithms Lecture 3: Analysis of Algorithms II

Analysis and Design of Algorithms

Naive algorithm

Page 57: Algorithms Lecture 3: Analysis of Algorithms II

Analysis and Design of Algorithms

Page 58: Algorithms Lecture 3: Analysis of Algorithms II

Analysis and Design of Algorithms

gcd algorithm is slow because of loop

Time complexity = O(n)

n depend on a,b

Page 59: Algorithms Lecture 3: Analysis of Algorithms II

Analysis and Design of Algorithms

Efficient algorithm

Page 60: Algorithms Lecture 3: Analysis of Algorithms II

Analysis and Design of Algorithms

Efficient algorithm

Page 61: Algorithms Lecture 3: Analysis of Algorithms II

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

Page 62: Algorithms Lecture 3: Analysis of Algorithms II

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

Page 63: Algorithms Lecture 3: Analysis of Algorithms II

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.

Page 64: Algorithms Lecture 3: Analysis of Algorithms II

Analysis and Design of Algorithms

facebook.com/mloey

[email protected]

twitter.com/mloey

linkedin.com/in/mloey

[email protected]

mloey.github.io

Page 65: Algorithms Lecture 3: Analysis of Algorithms II

Analysis and Design of Algorithms

www.YourCompany.com© 2020 Companyname PowerPoint Business Theme. All Rights Reserved.

THANKS FOR YOUR TIME