SlideShare a Scribd company logo
1 of 65
Download to read offline
Analysis and Design of Algorithms
Analysis of Algorithms II
Analysis and Design of Algorithms
Maximum Pairwise Product
Fibonacci
Greatest Common Divisors
Analysis and Design of Algorithms
Maximum Pairwise Product
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).
Analysis and Design of Algorithms
Constraints 2≤n≤2 * 105 & 0≤a0,…,an−1≤105.
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
Analysis and Design of Algorithms
 Sample 3
 Input: 4 6 2 6 1
 Output:36
Analysis and Design of Algorithms
 Assume the following array
2 4 3 5 1
Analysis and Design of Algorithms
 Assume the following array
2 4 3 5 1
i j
Result=0
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
Analysis and Design of Algorithms
 Assume the following array
2 4 3 5 1
i j
If a[i]*a[j] > result
result=8
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
Analysis and Design of Algorithms
 Naive algorithm
Analysis and Design of Algorithms
 Python Code:
Analysis and Design of Algorithms
 Time complexity O(n2)
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
Analysis and Design of Algorithms
 Assume the following array
2 4 3 5 1
Analysis and Design of Algorithms
 Find maximum number1
2 4 3 5 1
max1
Analysis and Design of Algorithms
 Find maximum number2 but not maximum number1
2 4 3 5 1
max2 max1
Analysis and Design of Algorithms
 Find maximum number2 but not maximum number1
2 4 3 5 1
max2 max1
return max1*max2
Analysis and Design of Algorithms
 Efficient
algorithm
Analysis and Design of Algorithms
 Efficient algorithm
Analysis and Design of Algorithms
 Time complexity O(n)
Analysis and Design of Algorithms
Fibonacci
Analysis and Design of Algorithms
0,1,1,2,3,5,8,13,21,34, …
Analysis and Design of Algorithms
Definition:
𝐹𝑛 =
0 𝑛 = 0
1 𝑛 = 1
𝐹𝑛−2 + 𝐹𝑛−1 𝑛 > 1
Analysis and Design of Algorithms
Examples:
𝐹8 = 21
𝐹20 = 6765
𝐹50 = 12586269025
𝐹100 = 354224848179261915075
Analysis and Design of Algorithms
 Examples:
𝐹500 =
1394232245616978801397243828
7040728395007025658769730726
4108962948325571622863290691
557658876222521294125
Analysis and Design of Algorithms
 Naive algorithm
Analysis and Design of Algorithms
 Naive algorithm
Very Long Time why????
Analysis and Design of Algorithms
Analysis and Design of Algorithms
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
Analysis and Design of Algorithms
Fib algorithm is very slow because of
recursion
Time complexity = O(2n)
Analysis and Design of Algorithms
 Efficient algorithm
0 1 1 2 3 5
Create array then insert fibonacci
Analysis and Design of Algorithms
 Efficient algorithm
Analysis and Design of Algorithms
 Efficient algorithm
short Time why????
Analysis and Design of Algorithms
Fib_Fast algorithm is fast because of
loop + array
Time complexity = O(n2)
Analysis and Design of Algorithms
 Efficient algorithm
 Try
Very long Time why????
Analysis and Design of Algorithms
Advanced algorithm
No array
Need two variable + Loop
Analysis and Design of Algorithms
 Advanced algorithm
 Compute F6
 a=0, b=1
0 1
a b
Analysis and Design of Algorithms
 Advanced algorithm
 Compute F6
 a=b, b=a+b
1 1
a b
Analysis and Design of Algorithms
 Advanced algorithm
 Compute F6
 a=b, b=a+b
1 2
a b
Analysis and Design of Algorithms
 Advanced algorithm
 Compute F6
 a=b, b=a+b
2 3
a b
Analysis and Design of Algorithms
 Advanced algorithm
 Compute F6
 a=b, b=a+b
3 5
a b
Analysis and Design of Algorithms
 Advanced algorithm
 Compute F6
 a=b, b=a+b
5 8
a b
Analysis and Design of Algorithms
 Advanced algorithm
 Compute F6=8
5 8
a b
Analysis and Design of Algorithms
 Advanced algorithm
Analysis and Design of Algorithms
 Advanced algorithm
Very short Time why????
Analysis and Design of Algorithms
Fib_Faster algorithm is faster because
of loop + two variables
Time complexity = O(n)
Analysis and Design of Algorithms
 Advanced algorithm
 Try
Short Time why????
Analysis and Design of Algorithms
Greatest Common Divisors
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.
Analysis and Design of Algorithms
Input Integers a,b >=0
Output gcd(a,b)
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
Analysis and Design of Algorithms
 Naive algorithm
Analysis and Design of Algorithms
Analysis and Design of Algorithms
gcd algorithm is slow because of loop
Time complexity = O(n)
n depend on a,b
Analysis and Design of Algorithms
 Efficient algorithm
Analysis and Design of Algorithms
 Efficient algorithm
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
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
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.
Analysis and Design of Algorithms
facebook.com/mloey
mohamedloey@gmail.com
twitter.com/mloey
linkedin.com/in/mloey
mloey@fci.bu.edu.eg
mloey.github.io
Analysis and Design of Algorithms
www.YourCompany.com
© 2020 Companyname PowerPoint Business Theme. All Rights Reserved.
THANKS FOR
YOUR TIME

More Related Content

What's hot

Algorithm Complexity and Main Concepts
Algorithm Complexity and Main ConceptsAlgorithm Complexity and Main Concepts
Algorithm Complexity and Main Concepts
Adelina Ahadova
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithm
Mohd Arif
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
Ehtisham Ali
 

What's hot (20)

Design and Analysis of Algorithms.pptx
Design and Analysis of Algorithms.pptxDesign and Analysis of Algorithms.pptx
Design and Analysis of Algorithms.pptx
 
Analysis of algorithm
Analysis of algorithmAnalysis of algorithm
Analysis of algorithm
 
Quick sort
Quick sortQuick sort
Quick sort
 
Best,worst,average case .17581556 045
Best,worst,average case .17581556 045Best,worst,average case .17581556 045
Best,worst,average case .17581556 045
 
Design and Analysis of Algorithms
Design and Analysis of AlgorithmsDesign and Analysis of Algorithms
Design and Analysis of Algorithms
 
Merge sort algorithm
Merge sort algorithmMerge sort algorithm
Merge sort algorithm
 
Algorithm analysis (All in one)
Algorithm analysis (All in one)Algorithm analysis (All in one)
Algorithm analysis (All in one)
 
Unit 1 chapter 1 Design and Analysis of Algorithms
Unit 1   chapter 1 Design and Analysis of AlgorithmsUnit 1   chapter 1 Design and Analysis of Algorithms
Unit 1 chapter 1 Design and Analysis of Algorithms
 
Algorithm Complexity and Main Concepts
Algorithm Complexity and Main ConceptsAlgorithm Complexity and Main Concepts
Algorithm Complexity and Main Concepts
 
Big o notation
Big o notationBig o notation
Big o notation
 
Algorithm And analysis Lecture 03& 04-time complexity.
 Algorithm And analysis Lecture 03& 04-time complexity. Algorithm And analysis Lecture 03& 04-time complexity.
Algorithm And analysis Lecture 03& 04-time complexity.
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sort
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithm
 
Bruteforce algorithm
Bruteforce algorithmBruteforce algorithm
Bruteforce algorithm
 
Greedy algorithm
Greedy algorithmGreedy algorithm
Greedy algorithm
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
 
Algorithms Lecture 7: Graph Algorithms
Algorithms Lecture 7: Graph AlgorithmsAlgorithms Lecture 7: Graph Algorithms
Algorithms Lecture 7: Graph Algorithms
 
Merge Sort
Merge SortMerge Sort
Merge Sort
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 

Viewers also liked

Viewers also liked (6)

Computer Security Lecture 5: Simplified Advanced Encryption Standard
Computer Security Lecture 5: Simplified Advanced Encryption StandardComputer Security Lecture 5: Simplified Advanced Encryption Standard
Computer Security Lecture 5: Simplified Advanced Encryption Standard
 
Deep Learning - Overview of my work II
Deep Learning - Overview of my work IIDeep Learning - Overview of my work II
Deep Learning - Overview of my work II
 
PMP Lecture 1: Introduction to Project Management
PMP Lecture 1: Introduction to Project ManagementPMP Lecture 1: Introduction to Project Management
PMP Lecture 1: Introduction to Project Management
 
Computer Security Lecture 7: RSA
Computer Security Lecture 7: RSAComputer Security Lecture 7: RSA
Computer Security Lecture 7: RSA
 
Convolutional Neural Network Models - Deep Learning
Convolutional Neural Network Models - Deep LearningConvolutional Neural Network Models - Deep Learning
Convolutional Neural Network Models - Deep Learning
 
C++ Programming Language
C++ Programming Language C++ Programming Language
C++ Programming Language
 

Similar to Algorithms Lecture 3: Analysis of Algorithms II

Ch24 efficient algorithms
Ch24 efficient algorithmsCh24 efficient algorithms
Ch24 efficient algorithms
rajatmay1992
 
Advanced Datastructures and algorithms CP4151unit1b.pdf
Advanced Datastructures and algorithms CP4151unit1b.pdfAdvanced Datastructures and algorithms CP4151unit1b.pdf
Advanced Datastructures and algorithms CP4151unit1b.pdf
Sheba41
 
asymptotic analysis and insertion sort analysis
asymptotic analysis and insertion sort analysisasymptotic analysis and insertion sort analysis
asymptotic analysis and insertion sort analysis
Anindita Kundu
 
Analysis of Algorithm full version 2024.pptx
Analysis of Algorithm  full version  2024.pptxAnalysis of Algorithm  full version  2024.pptx
Analysis of Algorithm full version 2024.pptx
rajesshs31r
 
Design Analysis of Alogorithm 1 ppt 2024.pptx
Design Analysis of Alogorithm 1 ppt 2024.pptxDesign Analysis of Alogorithm 1 ppt 2024.pptx
Design Analysis of Alogorithm 1 ppt 2024.pptx
rajesshs31r
 
Data Structure & Algorithms - Introduction
Data Structure & Algorithms - IntroductionData Structure & Algorithms - Introduction
Data Structure & Algorithms - Introduction
babuk110
 

Similar to Algorithms Lecture 3: Analysis of Algorithms II (20)

3 analysis.gtm
3 analysis.gtm3 analysis.gtm
3 analysis.gtm
 
Ch24 efficient algorithms
Ch24 efficient algorithmsCh24 efficient algorithms
Ch24 efficient algorithms
 
Analysis of algo
Analysis of algoAnalysis of algo
Analysis of algo
 
Chapter one
Chapter oneChapter one
Chapter one
 
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
 
Advanced Datastructures and algorithms CP4151unit1b.pdf
Advanced Datastructures and algorithms CP4151unit1b.pdfAdvanced Datastructures and algorithms CP4151unit1b.pdf
Advanced Datastructures and algorithms CP4151unit1b.pdf
 
Analysis.ppt
Analysis.pptAnalysis.ppt
Analysis.ppt
 
Lec1
Lec1Lec1
Lec1
 
Unit 2 algorithm
Unit   2 algorithmUnit   2 algorithm
Unit 2 algorithm
 
Annotations.pdf
Annotations.pdfAnnotations.pdf
Annotations.pdf
 
Cis435 week01
Cis435 week01Cis435 week01
Cis435 week01
 
Algorithm Design and Analysis
Algorithm Design and AnalysisAlgorithm Design and Analysis
Algorithm Design and Analysis
 
Lec1
Lec1Lec1
Lec1
 
asymptotic analysis and insertion sort analysis
asymptotic analysis and insertion sort analysisasymptotic analysis and insertion sort analysis
asymptotic analysis and insertion sort analysis
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
 
Analysis of Algorithm full version 2024.pptx
Analysis of Algorithm  full version  2024.pptxAnalysis of Algorithm  full version  2024.pptx
Analysis of Algorithm full version 2024.pptx
 
Design Analysis of Alogorithm 1 ppt 2024.pptx
Design Analysis of Alogorithm 1 ppt 2024.pptxDesign Analysis of Alogorithm 1 ppt 2024.pptx
Design Analysis of Alogorithm 1 ppt 2024.pptx
 
UNIT-2-PPTS-DAA.ppt
UNIT-2-PPTS-DAA.pptUNIT-2-PPTS-DAA.ppt
UNIT-2-PPTS-DAA.ppt
 
Data Structure & Algorithms - Introduction
Data Structure & Algorithms - IntroductionData Structure & Algorithms - Introduction
Data Structure & Algorithms - Introduction
 
Design and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture NotesDesign and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture Notes
 

More from Mohamed Loey

Design of an Intelligent System for Improving Classification of Cancer Diseases
Design of an Intelligent System for Improving Classification of Cancer DiseasesDesign of an Intelligent System for Improving Classification of Cancer Diseases
Design of an Intelligent System for Improving Classification of Cancer Diseases
Mohamed Loey
 

More from Mohamed Loey (18)

Lecture 6: Deep Learning Applications
Lecture 6: Deep Learning ApplicationsLecture 6: Deep Learning Applications
Lecture 6: Deep Learning Applications
 
Lecture 5: Convolutional Neural Network Models
Lecture 5: Convolutional Neural Network ModelsLecture 5: Convolutional Neural Network Models
Lecture 5: Convolutional Neural Network Models
 
Lecture 4: Deep Learning Frameworks
Lecture 4: Deep Learning FrameworksLecture 4: Deep Learning Frameworks
Lecture 4: Deep Learning Frameworks
 
Lecture 4: How it Works: Convolutional Neural Networks
Lecture 4: How it Works: Convolutional Neural NetworksLecture 4: How it Works: Convolutional Neural Networks
Lecture 4: How it Works: Convolutional Neural Networks
 
Lecture 3: Convolutional Neural Networks
Lecture 3: Convolutional Neural NetworksLecture 3: Convolutional Neural Networks
Lecture 3: Convolutional Neural Networks
 
Lecture 2: Artificial Neural Network
Lecture 2: Artificial Neural NetworkLecture 2: Artificial Neural Network
Lecture 2: Artificial Neural Network
 
Lecture 1: Deep Learning for Computer Vision
Lecture 1: Deep Learning for Computer VisionLecture 1: Deep Learning for Computer Vision
Lecture 1: Deep Learning for Computer Vision
 
Design of an Intelligent System for Improving Classification of Cancer Diseases
Design of an Intelligent System for Improving Classification of Cancer DiseasesDesign of an Intelligent System for Improving Classification of Cancer Diseases
Design of an Intelligent System for Improving Classification of Cancer Diseases
 
Computer Security - CCNA Security - Lecture 2
Computer Security - CCNA Security - Lecture 2Computer Security - CCNA Security - Lecture 2
Computer Security - CCNA Security - Lecture 2
 
Computer Security - CCNA Security - Lecture 1
Computer Security - CCNA Security - Lecture 1Computer Security - CCNA Security - Lecture 1
Computer Security - CCNA Security - Lecture 1
 
Computer Security Lecture 4.1: DES Supplementary Material
Computer Security Lecture 4.1: DES Supplementary MaterialComputer Security Lecture 4.1: DES Supplementary Material
Computer Security Lecture 4.1: DES Supplementary Material
 
PMP Lecture 4: Project Integration Management
PMP Lecture 4: Project Integration ManagementPMP Lecture 4: Project Integration Management
PMP Lecture 4: Project Integration Management
 
Computer Security Lecture 4: Block Ciphers and the Data Encryption Standard
Computer Security Lecture 4: Block Ciphers and the Data Encryption StandardComputer Security Lecture 4: Block Ciphers and the Data Encryption Standard
Computer Security Lecture 4: Block Ciphers and the Data Encryption Standard
 
Computer Security Lecture 3: Classical Encryption Techniques 2
Computer Security Lecture 3: Classical Encryption Techniques 2Computer Security Lecture 3: Classical Encryption Techniques 2
Computer Security Lecture 3: Classical Encryption Techniques 2
 
Computer Security Lecture 2: Classical Encryption Techniques 1
Computer Security Lecture 2: Classical Encryption Techniques 1Computer Security Lecture 2: Classical Encryption Techniques 1
Computer Security Lecture 2: Classical Encryption Techniques 1
 
Computer Security Lecture 1: Overview
Computer Security Lecture 1: OverviewComputer Security Lecture 1: Overview
Computer Security Lecture 1: Overview
 
PMP Lecture 3: Project Management Processes
PMP Lecture 3: Project Management ProcessesPMP Lecture 3: Project Management Processes
PMP Lecture 3: Project Management Processes
 
PMP Lecture 2: Project Management Framework
PMP Lecture 2: Project Management FrameworkPMP Lecture 2: Project Management Framework
PMP Lecture 2: Project Management Framework
 

Recently uploaded

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Recently uploaded (20)

Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 

Algorithms Lecture 3: Analysis of Algorithms II

  • 1. Analysis and Design of Algorithms Analysis of Algorithms II
  • 2. Analysis and Design of Algorithms Maximum Pairwise Product Fibonacci Greatest Common Divisors
  • 3. Analysis and Design of Algorithms Maximum Pairwise Product
  • 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. Analysis and Design of Algorithms Constraints 2≤n≤2 * 105 & 0≤a0,…,an−1≤105.
  • 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. Analysis and Design of Algorithms  Sample 3  Input: 4 6 2 6 1  Output:36
  • 8. Analysis and Design of Algorithms  Assume the following array 2 4 3 5 1
  • 9. Analysis and Design of Algorithms  Assume the following array 2 4 3 5 1 i j Result=0
  • 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. 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. 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. Analysis and Design of Algorithms  Naive algorithm
  • 14. Analysis and Design of Algorithms  Python Code:
  • 15. Analysis and Design of Algorithms  Time complexity O(n2)
  • 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. Analysis and Design of Algorithms  Assume the following array 2 4 3 5 1
  • 18. Analysis and Design of Algorithms  Find maximum number1 2 4 3 5 1 max1
  • 19. Analysis and Design of Algorithms  Find maximum number2 but not maximum number1 2 4 3 5 1 max2 max1
  • 20. Analysis and Design of Algorithms  Find maximum number2 but not maximum number1 2 4 3 5 1 max2 max1 return max1*max2
  • 21. Analysis and Design of Algorithms  Efficient algorithm
  • 22. Analysis and Design of Algorithms  Efficient algorithm
  • 23. Analysis and Design of Algorithms  Time complexity O(n)
  • 24. Analysis and Design of Algorithms Fibonacci
  • 25. Analysis and Design of Algorithms 0,1,1,2,3,5,8,13,21,34, …
  • 26. Analysis and Design of Algorithms Definition: 𝐹𝑛 = 0 𝑛 = 0 1 𝑛 = 1 𝐹𝑛−2 + 𝐹𝑛−1 𝑛 > 1
  • 27. Analysis and Design of Algorithms Examples: 𝐹8 = 21 𝐹20 = 6765 𝐹50 = 12586269025 𝐹100 = 354224848179261915075
  • 28. Analysis and Design of Algorithms  Examples: 𝐹500 = 1394232245616978801397243828 7040728395007025658769730726 4108962948325571622863290691 557658876222521294125
  • 29. Analysis and Design of Algorithms  Naive algorithm
  • 30. Analysis and Design of Algorithms  Naive algorithm Very Long Time why????
  • 31. Analysis and Design of Algorithms
  • 32. Analysis and Design of Algorithms
  • 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. Analysis and Design of Algorithms Fib algorithm is very slow because of recursion Time complexity = O(2n)
  • 35. Analysis and Design of Algorithms  Efficient algorithm 0 1 1 2 3 5 Create array then insert fibonacci
  • 36. Analysis and Design of Algorithms  Efficient algorithm
  • 37. Analysis and Design of Algorithms  Efficient algorithm short Time why????
  • 38. Analysis and Design of Algorithms Fib_Fast algorithm is fast because of loop + array Time complexity = O(n2)
  • 39. Analysis and Design of Algorithms  Efficient algorithm  Try Very long Time why????
  • 40. Analysis and Design of Algorithms Advanced algorithm No array Need two variable + Loop
  • 41. Analysis and Design of Algorithms  Advanced algorithm  Compute F6  a=0, b=1 0 1 a b
  • 42. Analysis and Design of Algorithms  Advanced algorithm  Compute F6  a=b, b=a+b 1 1 a b
  • 43. Analysis and Design of Algorithms  Advanced algorithm  Compute F6  a=b, b=a+b 1 2 a b
  • 44. Analysis and Design of Algorithms  Advanced algorithm  Compute F6  a=b, b=a+b 2 3 a b
  • 45. Analysis and Design of Algorithms  Advanced algorithm  Compute F6  a=b, b=a+b 3 5 a b
  • 46. Analysis and Design of Algorithms  Advanced algorithm  Compute F6  a=b, b=a+b 5 8 a b
  • 47. Analysis and Design of Algorithms  Advanced algorithm  Compute F6=8 5 8 a b
  • 48. Analysis and Design of Algorithms  Advanced algorithm
  • 49. Analysis and Design of Algorithms  Advanced algorithm Very short Time why????
  • 50. Analysis and Design of Algorithms Fib_Faster algorithm is faster because of loop + two variables Time complexity = O(n)
  • 51. Analysis and Design of Algorithms  Advanced algorithm  Try Short Time why????
  • 52. Analysis and Design of Algorithms Greatest Common Divisors
  • 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. Analysis and Design of Algorithms Input Integers a,b >=0 Output gcd(a,b)
  • 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. Analysis and Design of Algorithms  Naive algorithm
  • 57. Analysis and Design of Algorithms
  • 58. Analysis and Design of Algorithms gcd algorithm is slow because of loop Time complexity = O(n) n depend on a,b
  • 59. Analysis and Design of Algorithms  Efficient algorithm
  • 60. Analysis and Design of Algorithms  Efficient algorithm
  • 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. 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. 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.
  • 64. Analysis and Design of Algorithms facebook.com/mloey mohamedloey@gmail.com twitter.com/mloey linkedin.com/in/mloey mloey@fci.bu.edu.eg mloey.github.io
  • 65. Analysis and Design of Algorithms www.YourCompany.com © 2020 Companyname PowerPoint Business Theme. All Rights Reserved. THANKS FOR YOUR TIME