Movatterモバイル変換


[0]ホーム

URL:


Algorithms Analysis & Design - Lecture 4

The document discusses various algorithms, particularly focusing on their recurrence relations and the number of operations they perform, such as multiplications, additions, and comparisons. It includes specific examples like the Tower of Hanoi problem and algorithms for computing powers and finding the smallest element in an array, analyzing their time complexity and operational breakdown. The document emphasizes the efficiency of different approaches and highlights the advantages of non-recursive methods.

Related topics:

Embed presentation

Download as PDF, PPTX
AlgorithmsBy Mohamed Gamal© Mohamed Gamal 2024
a)Let 𝑀(𝑛) be the number of multiplications made by the algorithm.Recurrence relation equation: 𝑀(𝑛) = 𝑀(𝑛 − 1) + 1 and 𝑀 0 = 0• Using forward substitution:𝑀 0 = 0𝑀 1 = 𝑀 0 + 1 = 1𝑀 2 = 𝑀 1 + 1 = 2𝑀 3 = 𝑀 2 + 1 = 3𝑀 𝑛 = 𝑛Basic Operation
a) Another approachLet 𝑀(𝑛) be the number of multiplications made by the algorithm.Recurrence relation equation: 𝑀(𝑛) = 𝑀(𝑛 − 1) + 1 and 𝑀 0 = 0• Using backward substitution:𝑀 𝑛 = 𝑀 𝑛 − 1 + 1= 𝑀 𝑛 − 2 + 1 + 1 = 𝑀 𝑛 − 2 + 2= 𝑀 𝑛 − 3 + 1 + 2 = 𝑀 𝑛 − 3 + 3= 𝑀 𝑛 − 4 + 1 + 3 = 𝑀 𝑛 − 4 + 4𝑀 𝑛 = 𝑀 𝑛 − 𝑖 + 𝑖= 𝑀 0 + 𝑛= 𝑛𝑛 − 𝑖 = 0𝑖 = 𝑛
Example: The Tower of Hanoi PuzzleProblem: move disks from source peg A to destination peg C using an auxiliary (temp) peg B.Constraints:– Only one disk can be moved at a time.– A larger disk cannot be placed on top of a smaller disk.Source DestinationAuxiliary
Try it online!
7 moves in total !
ALGORITHM: Hanoi(n, src, dest, temp)// Input: no. disks, source, destination, temp// Output: Moves to solve the Tower of Hanoi problemif n = 1 thenMOVE disk from src to dest directlyelseHanoi(n-1, src, temp, dest) // Step 1: from src to tempMOVE disk n from src to dest // Step 2: nth disk from src to destHanoi(n-1, temp, dest, src) // Step 3: from temp to dest
Let 𝑀(𝑛) be the number of moves made by the algorithm.Recurrence relation equation: 𝑀 𝑛 = 𝑀 𝑛 − 1 + 1 + 𝑀(𝑛 − 1) and 𝑀 1 = 1= 2𝑀 𝑛 − 1 + 1• Using forward substitution:𝑀(1) = 1𝑀(2) = 2𝑀(1) + 1 = 3𝑀(3) = 2𝑀(2) + 1 = 7𝑀(𝑛) = 2𝑛 − 1Basic Operation
Let 𝑀(𝑛) be the number of moves made by the algorithm.Recurrence relation equation: 𝑀 𝑛 = 2𝑀 𝑛 − 1 + 1 and 𝑀 1 = 1• Using backward substitution:𝑀(𝑛) = 2𝑀 𝑛 − 1 + 1= 2 2𝑀 𝑛 − 2 + 1 + 1 = 4𝑀(𝑛 − 2) + 3= 4 2𝑀 𝑛 − 3 + 1 + 3 = 8𝑀(𝑛 − 3) + 7𝑀(𝑛) = 2𝑖 𝑀(𝑛 − 𝑖) + (2𝑖 − 1)= 2𝑛−1𝑀(1) + (2𝑛−1− 1)= 2𝑛− 1𝑛 − 𝑖 = 1𝑖 = 𝑛 − 1Basic Operation
Determine what this algorithm computes?𝑄(1) = 1𝑄(2) = 𝑄(1) + 2 ∗ 2 − 1 = 1 + 3 = 4𝑄(3) = 𝑄(2) + 2 ∗ 3 − 1 = 4 + 5 = 9𝑄(4) = 𝑄(3) + 2 ∗ 4 − 1 = 9 + 7 = 16𝑄(𝑛) = 𝑛2Then this algorithm computes 𝑛2for each positive 𝑛.Recurrence relation equation: 𝑄(𝑛 − 1) + 2𝑛 − 1 for 𝑛 > 1a)
b)Let 𝑀(𝑛) be the number of multiplications made by the algorithm.Recurrence relation equation: 𝑀(𝑛) = 𝑀(𝑛 − 1) + 1 and 𝑀 1 = 0• Using backward substitution:𝑀 𝑛 = 𝑀 𝑛 − 1 + 1= 𝑀 𝑛 − 2 + 1 + 1 = 𝑀 𝑛 − 2 + 2= 𝑀 𝑛 − 3 + 1 + 2 = 𝑀 𝑛 − 3 + 3= 𝑀 𝑛 − 4 + 1 + 3 = 𝑀 𝑛 − 4 + 4𝑀 𝑛 = 𝑀 𝑛 − 𝑖 + 𝑖= 𝑀 1 + (𝑛 − 1)= (𝑛 − 1)𝑛 − 𝑖 = 1𝑖 = 𝑛 − 1
c)Let 𝐶(𝑛) be the number of additions and subtractions made by the algorithm.Recurrence relation equation: 𝐶(𝑛) = 𝐶(𝑛 − 1) + 2 and 𝐶 1 = 0• Using backward substitution:𝐶 𝑛 = 𝐶 𝑛 − 1 + 2= 𝐶 𝑛 − 2 + 2 + 2 = 𝐶 𝑛 − 2 + 4= 𝐶 𝑛 − 3 + 2 + 4 = 𝐶 𝑛 − 3 + 6= 𝐶 𝑛 − 4 + 2 + 6 = 𝐶 𝑛 − 4 + 8𝐶(𝑛) = 𝐶(𝑛 − 𝑖) + 2 ∗ 𝑖= 𝐶 1 + 2 ∗ 𝑛 − 1= 2(𝑛 − 1)𝑛 − 𝑖 = 1𝑖 = 𝑛 − 1
a)Let 𝑀(𝑛) be the number of multiplications made by the algorithm.Recurrence relation equation: 𝑀(𝑛) = 𝑀(𝑛 − 1) + 2 and 𝑀 1 = 0• Using forward substitution:𝑀(1) = 0𝑀(2) = 𝑀(1) + 2 = 2𝑀(3) = 𝑀(2) + 2 = 4𝑀(4) = 𝑀(3) + 2 = 6𝑀(5) = 𝑀(4) + 2 = 8𝑀 𝑛 = 2 𝑛 − 1Basic Operation
a) Another approachLet 𝑀(𝑛) be the number of multiplications made by the algorithm.Recurrence relation equation: 𝑀(𝑛) = 𝑀(𝑛 − 1) + 2 and 𝑀 1 = 0• Using backward substitution:𝑀(𝑛) = 𝑀(𝑛 − 1) + 2= [ 𝑀(𝑛 − 2) + 2 ] + 2 = 𝑀(𝑛 − 2) + 4= [ 𝑀(𝑛 − 3) + 2 ] + 4 = 𝑀(𝑛 − 3) + 6= [ 𝑀(𝑛 − 4) + 2 ] + 6 = 𝑀(𝑛 − 4) + 8𝑀(𝑛) = 𝑀 𝑛 − 𝑖 + 2𝑖= 𝑀 1 + 2(𝑛 − 1)= 2 𝑛 − 1Basic Operation𝑛 − 𝑖 = 1𝑖 = 𝑛 − 1
ALGORITHM: S(n)// Input: A positive integer n// Output: The sum of the first n cubesS ← 1for i ← 2 to n doS ← S + i * i * ireturn SThe number of multiplications made by this algorithm will be෍𝑖=2𝑛2 =𝑛 − 2 + 12× (2 + 2) = 2(𝑛 − 1)This is exactly the same number as in the recursive version, but the no recursive versiondoesn't carry the time and space overhead associated with the recursion's stack.b) Comparing with the non-recursive algorithm
Question 1:Assignments
Question 2:
ThankYou!
Question 1
Answers
b)Let 𝐴(𝑛) be the number of additions made by the algorithm.Recurrence relation equation: 𝐴 𝑛 = 𝐴 𝑛 − 1 + 𝐴(𝑛 − 1) + 1 and 𝐴 0 = 0= 2𝐴 𝑛 − 1 + 1• Using forward substitution:𝐴(0) = 0𝐴(1) = 2𝐴(0) + 1 = 1𝐴(2) = 2𝐴(1) + 1 = 3𝐴(3) = 2𝐴(2) + 1 = 7𝐴(4) = 2𝐴(3) + 1 = 15𝐴 𝑛 = 2𝑛 − 1Basic Operation
b) Another approachLet 𝐴(𝑛) be the number of additions made by the algorithm.Recurrence relation equation: 𝐴(𝑛) = 2𝐴(𝑛 − 1) + 1 and 𝐴 0 = 0• Using backward substitution:𝐴(𝑛) = 2𝐴(𝑛 − 1) + 1= 2[ 2𝐴(𝑛 − 2) + 1 ] + 1 = 4𝐴(𝑛 − 2) + 3= 4[ 2𝐴(𝑛 − 3) + 1 ] + 3 = 8𝐴(𝑛 − 3) + 7= 8[ 2𝐴(𝑛 − 4) + 1 ] + 7 = 16𝐴(𝑛 − 4) + 15𝐴 𝑛 = 2𝑖𝐴 𝑛 − 𝑖 + (2𝑖 − 1)= 2𝑛 𝑀 0 + (2𝑛 − 1)= 2𝑛 − 1Basic Operation𝑛 − 𝑖 = 0𝑖 = 𝑛
d. It’s a very bad algorithm because it is vastly inferior to the algorithm that simplymultiplies an accumulator by 2 𝑛 times. O(2𝑛)
Question 2
a) The algorithm computes the value of the smallest element in a given array.Let A = [9, 5, 8, 2]▪ n = 4 ≠ 1▪ Call Riddle([9, 5, 8]) (subarray A[0..2]):▪ For Riddle([9, 5, 8]):▪ n = 3 ≠ 1▪ Call Riddle([9, 5]) (subarray A[0..1]):▪ For Riddle([9, 5]):▪ n = 2 ≠ 1▪ Call Riddle([9]) (subarray A[0..0]):▪ n = 1, so it returns 9.▪ Compare temp = 9 with A[1] = 5:▪ 9 > 5, so return 5.▪ Riddle([9, 5]) returns 5.▪ Compare temp = 5 with A[2] = 8:▪ 5 <= 8, so return 5.▪ Riddle([9, 5, 8]) returns 5.▪ Compare temp = 5 with A[3] = 2:▪ 5 > 2, so return 2.
b)Let 𝐶(𝑛) be the number of the number of key comparisons made by the algorithm.Recurrence relation equation: 𝐶(𝑛) = 𝐶(𝑛 − 1) + 1 and 𝐶 1 = 0• Using backward substitution:𝐶 𝑛 = 𝐶(𝑛 − 1) + 1= [ 𝐶(𝑛 − 2) + 1 ] + 1 = 𝐶(𝑛 − 2) + 2= [ 𝐶(𝑛 − 3) + 1 ] + 2 = 𝐶(𝑛 − 3) + 3= [ 𝐶(𝑛 − 4) + 1 ] + 3 = 𝐶(𝑛 − 4) + 4𝐶 𝑛 = 𝐶 𝑛 − 𝑖 + 𝑖= 𝐶 1 + (𝑛 − 1)= 𝑛 − 1Basic Operation𝑛 − 𝑖 = 1𝑖 = 𝑛 − 1

Recommended

PDF
8.-DAA-LECTURE-8-RECURRENCES-AND-ITERATION-METHOD.pdf
PDF
lec 03wweweweweweweeweweweewewewewee.pdf
PDF
3.pdf
PPT
Analysis Of Algorithms Ii
PDF
Copy of y16 02-2119divide-and-conquer
PPT
CS8451 - Design and Analysis of Algorithms
PPT
algo_vc_lecture8.ppt
PPT
5. Recursive.ppt
PPTX
Algorithm Design and Complexity - Course 3
PPT
Lecture-3.1(Recurrences) Data Structure and pdf
PPT
the ppt on chap6 of brassard and Bratley
PPT
3. Recursion and Recurrences.ppt detail about recursive learning
PPTX
designs and analysis of algorithmss.pptx
PPT
recurrence relations in analysis of algorithm
PPT
recurrence relation is explained in this
PDF
Dynamic programing
PDF
Daa chapter 2
PPT
04-Induction and Recursion.ppt ppt bai tap
PPTX
3. D&C and Recurrence Relation.ppYtxVVVV
PPTX
2.pptx
PPT
Lecture11_12COMPILER DESIGN_unit 2COMPILER DESIGN_unit 2.ppt
PDF
Lecture 5 6_7 - divide and conquer and method of solving recurrences
PPTX
Complexity Analysis of Recursive Function
PDF
02 Notes Divide and Conquer
PDF
Recursive algorithms
PPT
03 mathematical anaylsis
PPTX
Recursive Algorithm Detailed Explanation
PDF
01 Notes Introduction Analysis of Algorithms Notes
PDF
Complier Design - Operations on Languages, RE, Finite Automata

More Related Content

PDF
8.-DAA-LECTURE-8-RECURRENCES-AND-ITERATION-METHOD.pdf
PDF
lec 03wweweweweweweeweweweewewewewee.pdf
PDF
3.pdf
PPT
Analysis Of Algorithms Ii
PDF
Copy of y16 02-2119divide-and-conquer
PPT
CS8451 - Design and Analysis of Algorithms
PPT
algo_vc_lecture8.ppt
PPT
5. Recursive.ppt
8.-DAA-LECTURE-8-RECURRENCES-AND-ITERATION-METHOD.pdf
lec 03wweweweweweweeweweweewewewewee.pdf
3.pdf
Analysis Of Algorithms Ii
Copy of y16 02-2119divide-and-conquer
CS8451 - Design and Analysis of Algorithms
algo_vc_lecture8.ppt
5. Recursive.ppt

Similar to Algorithms Analysis & Design - Lecture 4

PPTX
Algorithm Design and Complexity - Course 3
PPT
Lecture-3.1(Recurrences) Data Structure and pdf
PPT
the ppt on chap6 of brassard and Bratley
PPT
3. Recursion and Recurrences.ppt detail about recursive learning
PPTX
designs and analysis of algorithmss.pptx
PPT
recurrence relations in analysis of algorithm
PPT
recurrence relation is explained in this
PDF
Dynamic programing
PDF
Daa chapter 2
PPT
04-Induction and Recursion.ppt ppt bai tap
PPTX
3. D&C and Recurrence Relation.ppYtxVVVV
PPTX
2.pptx
PPT
Lecture11_12COMPILER DESIGN_unit 2COMPILER DESIGN_unit 2.ppt
PDF
Lecture 5 6_7 - divide and conquer and method of solving recurrences
PPTX
Complexity Analysis of Recursive Function
PDF
02 Notes Divide and Conquer
PDF
Recursive algorithms
PPT
03 mathematical anaylsis
PPTX
Recursive Algorithm Detailed Explanation
PDF
01 Notes Introduction Analysis of Algorithms Notes
Algorithm Design and Complexity - Course 3
Lecture-3.1(Recurrences) Data Structure and pdf
the ppt on chap6 of brassard and Bratley
3. Recursion and Recurrences.ppt detail about recursive learning
designs and analysis of algorithmss.pptx
recurrence relations in analysis of algorithm
recurrence relation is explained in this
Dynamic programing
Daa chapter 2
04-Induction and Recursion.ppt ppt bai tap
3. D&C and Recurrence Relation.ppYtxVVVV
2.pptx
Lecture11_12COMPILER DESIGN_unit 2COMPILER DESIGN_unit 2.ppt
Lecture 5 6_7 - divide and conquer and method of solving recurrences
Complexity Analysis of Recursive Function
02 Notes Divide and Conquer
Recursive algorithms
03 mathematical anaylsis
Recursive Algorithm Detailed Explanation
01 Notes Introduction Analysis of Algorithms Notes

More from Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt

PDF
Complier Design - Operations on Languages, RE, Finite Automata
PDF
Understanding Convolutional Neural Networks (CNN)
PDF
Object Oriented Programming (OOP) using C++ - Lecture 5
PDF
PDF
Understanding K-Nearest Neighbor (KNN) Algorithm
PDF
Understanding Singular Value Decomposition (SVD)
PDF
PDF
PDF
Object Oriented Programming (OOP) using C++ - Lecture 2
PDF
Object Oriented Programming (OOP) using C++ - Lecture 3
PDF
Object Oriented Programming (OOP) using C++ - Lecture 4
PDF
Luhn's algorithm to validate Egyptian ID numbers
PDF
Object Oriented Programming (OOP) using C++ - Lecture 1
PDF
PDF
PDF
How to install CS50 Library (Step-by-step guide)
Complier Design - Operations on Languages, RE, Finite Automata
Understanding Convolutional Neural Networks (CNN)
Object Oriented Programming (OOP) using C++ - Lecture 5
Understanding K-Nearest Neighbor (KNN) Algorithm
Understanding Singular Value Decomposition (SVD)
Object Oriented Programming (OOP) using C++ - Lecture 2
Object Oriented Programming (OOP) using C++ - Lecture 3
Object Oriented Programming (OOP) using C++ - Lecture 4
Luhn's algorithm to validate Egyptian ID numbers
Object Oriented Programming (OOP) using C++ - Lecture 1
How to install CS50 Library (Step-by-step guide)

Recently uploaded

PPT
Presentation on TCL/TK scripting language
PDF
Presentation Empowerment Technology in ICT
PDF
DSD-INT 2025 MeshKernel and D-Grid Editor - Stout
PDF
DSD-INT 2025 Hydrodynamic and Morphodynamic Modeling with Delft3D FM for an I...
PDF
DevOps Monitoring Tools: The 2025 Guide to Performance & Observability
PDF
inSis suite - Laboratory Information Management System
PDF
DSD-INT 2025 Next-Generation Flood Inundation Mapping for Taiwan - Challenges...
PDF
DSD-INT 2025 Validation of SFINCS on Historical River Floods at the Global Sc...
PDF
DSD-INT 2025 DevOps - Automated testing and delivery of Delft3D FM - van West...
PDF
DSD-INT 2025 Quantifying Flood Mitigation Strategies Under Sea Level Rise - H...
PDF
DSD-INT 2025 Flood Early Warning System for the Trans-African Hydrometeorolog...
PPTX
Presentation on AWS Cloud RDS Deep dive
PDF
Custom MGA Software Development: Better Apporach
PDF
DSD-INT 2025 Understanding the Paraguay River Response to Hard Bottom Dredgin...
PDF
DSD-INT 2025 Modernizing Hydrodynamics in Large Flood Forecasting System - Mi...
PDF
Breaking the Vulnerability Management Cycle with Anchore and Echo
PPTX
operating sys about shells and terminals commands .pptx
PDF
DSD-INT 2025 3D-modelling of salt intrusion into Germany’s busiest waterway -...
PPTX
Building AI agents in Java - Devoxx Belgium 2025
PPTX
Zotero Software Demonstration. Biomedical Perspective
Presentation on TCL/TK scripting language
Presentation Empowerment Technology in ICT
DSD-INT 2025 MeshKernel and D-Grid Editor - Stout
DSD-INT 2025 Hydrodynamic and Morphodynamic Modeling with Delft3D FM for an I...
DevOps Monitoring Tools: The 2025 Guide to Performance & Observability
inSis suite - Laboratory Information Management System
DSD-INT 2025 Next-Generation Flood Inundation Mapping for Taiwan - Challenges...
DSD-INT 2025 Validation of SFINCS on Historical River Floods at the Global Sc...
DSD-INT 2025 DevOps - Automated testing and delivery of Delft3D FM - van West...
DSD-INT 2025 Quantifying Flood Mitigation Strategies Under Sea Level Rise - H...
DSD-INT 2025 Flood Early Warning System for the Trans-African Hydrometeorolog...
Presentation on AWS Cloud RDS Deep dive
Custom MGA Software Development: Better Apporach
DSD-INT 2025 Understanding the Paraguay River Response to Hard Bottom Dredgin...
DSD-INT 2025 Modernizing Hydrodynamics in Large Flood Forecasting System - Mi...
Breaking the Vulnerability Management Cycle with Anchore and Echo
operating sys about shells and terminals commands .pptx
DSD-INT 2025 3D-modelling of salt intrusion into Germany’s busiest waterway -...
Building AI agents in Java - Devoxx Belgium 2025
Zotero Software Demonstration. Biomedical Perspective

Algorithms Analysis & Design - Lecture 4

  • 1.
  • 4.
    a)Let 𝑀(𝑛) bethe number of multiplications made by the algorithm.Recurrence relation equation: 𝑀(𝑛) = 𝑀(𝑛 − 1) + 1 and 𝑀 0 = 0• Using forward substitution:𝑀 0 = 0𝑀 1 = 𝑀 0 + 1 = 1𝑀 2 = 𝑀 1 + 1 = 2𝑀 3 = 𝑀 2 + 1 = 3𝑀 𝑛 = 𝑛Basic Operation
  • 5.
    a) Another approachLet𝑀(𝑛) be the number of multiplications made by the algorithm.Recurrence relation equation: 𝑀(𝑛) = 𝑀(𝑛 − 1) + 1 and 𝑀 0 = 0• Using backward substitution:𝑀 𝑛 = 𝑀 𝑛 − 1 + 1= 𝑀 𝑛 − 2 + 1 + 1 = 𝑀 𝑛 − 2 + 2= 𝑀 𝑛 − 3 + 1 + 2 = 𝑀 𝑛 − 3 + 3= 𝑀 𝑛 − 4 + 1 + 3 = 𝑀 𝑛 − 4 + 4𝑀 𝑛 = 𝑀 𝑛 − 𝑖 + 𝑖= 𝑀 0 + 𝑛= 𝑛𝑛 − 𝑖 = 0𝑖 = 𝑛
  • 6.
    Example: The Towerof Hanoi PuzzleProblem: move disks from source peg A to destination peg C using an auxiliary (temp) peg B.Constraints:– Only one disk can be moved at a time.– A larger disk cannot be placed on top of a smaller disk.Source DestinationAuxiliary
  • 7.
  • 8.
    7 moves intotal !
  • 9.
    ALGORITHM: Hanoi(n, src,dest, temp)// Input: no. disks, source, destination, temp// Output: Moves to solve the Tower of Hanoi problemif n = 1 thenMOVE disk from src to dest directlyelseHanoi(n-1, src, temp, dest) // Step 1: from src to tempMOVE disk n from src to dest // Step 2: nth disk from src to destHanoi(n-1, temp, dest, src) // Step 3: from temp to dest
  • 10.
    Let 𝑀(𝑛) bethe number of moves made by the algorithm.Recurrence relation equation: 𝑀 𝑛 = 𝑀 𝑛 − 1 + 1 + 𝑀(𝑛 − 1) and 𝑀 1 = 1= 2𝑀 𝑛 − 1 + 1• Using forward substitution:𝑀(1) = 1𝑀(2) = 2𝑀(1) + 1 = 3𝑀(3) = 2𝑀(2) + 1 = 7𝑀(𝑛) = 2𝑛 − 1Basic Operation
  • 11.
    Let 𝑀(𝑛) bethe number of moves made by the algorithm.Recurrence relation equation: 𝑀 𝑛 = 2𝑀 𝑛 − 1 + 1 and 𝑀 1 = 1• Using backward substitution:𝑀(𝑛) = 2𝑀 𝑛 − 1 + 1= 2 2𝑀 𝑛 − 2 + 1 + 1 = 4𝑀(𝑛 − 2) + 3= 4 2𝑀 𝑛 − 3 + 1 + 3 = 8𝑀(𝑛 − 3) + 7𝑀(𝑛) = 2𝑖 𝑀(𝑛 − 𝑖) + (2𝑖 − 1)= 2𝑛−1𝑀(1) + (2𝑛−1− 1)= 2𝑛− 1𝑛 − 𝑖 = 1𝑖 = 𝑛 − 1Basic Operation
  • 13.
    Determine what thisalgorithm computes?𝑄(1) = 1𝑄(2) = 𝑄(1) + 2 ∗ 2 − 1 = 1 + 3 = 4𝑄(3) = 𝑄(2) + 2 ∗ 3 − 1 = 4 + 5 = 9𝑄(4) = 𝑄(3) + 2 ∗ 4 − 1 = 9 + 7 = 16𝑄(𝑛) = 𝑛2Then this algorithm computes 𝑛2for each positive 𝑛.Recurrence relation equation: 𝑄(𝑛 − 1) + 2𝑛 − 1 for 𝑛 > 1a)
  • 14.
    b)Let 𝑀(𝑛) bethe number of multiplications made by the algorithm.Recurrence relation equation: 𝑀(𝑛) = 𝑀(𝑛 − 1) + 1 and 𝑀 1 = 0• Using backward substitution:𝑀 𝑛 = 𝑀 𝑛 − 1 + 1= 𝑀 𝑛 − 2 + 1 + 1 = 𝑀 𝑛 − 2 + 2= 𝑀 𝑛 − 3 + 1 + 2 = 𝑀 𝑛 − 3 + 3= 𝑀 𝑛 − 4 + 1 + 3 = 𝑀 𝑛 − 4 + 4𝑀 𝑛 = 𝑀 𝑛 − 𝑖 + 𝑖= 𝑀 1 + (𝑛 − 1)= (𝑛 − 1)𝑛 − 𝑖 = 1𝑖 = 𝑛 − 1
  • 15.
    c)Let 𝐶(𝑛) bethe number of additions and subtractions made by the algorithm.Recurrence relation equation: 𝐶(𝑛) = 𝐶(𝑛 − 1) + 2 and 𝐶 1 = 0• Using backward substitution:𝐶 𝑛 = 𝐶 𝑛 − 1 + 2= 𝐶 𝑛 − 2 + 2 + 2 = 𝐶 𝑛 − 2 + 4= 𝐶 𝑛 − 3 + 2 + 4 = 𝐶 𝑛 − 3 + 6= 𝐶 𝑛 − 4 + 2 + 6 = 𝐶 𝑛 − 4 + 8𝐶(𝑛) = 𝐶(𝑛 − 𝑖) + 2 ∗ 𝑖= 𝐶 1 + 2 ∗ 𝑛 − 1= 2(𝑛 − 1)𝑛 − 𝑖 = 1𝑖 = 𝑛 − 1
  • 17.
    a)Let 𝑀(𝑛) bethe number of multiplications made by the algorithm.Recurrence relation equation: 𝑀(𝑛) = 𝑀(𝑛 − 1) + 2 and 𝑀 1 = 0• Using forward substitution:𝑀(1) = 0𝑀(2) = 𝑀(1) + 2 = 2𝑀(3) = 𝑀(2) + 2 = 4𝑀(4) = 𝑀(3) + 2 = 6𝑀(5) = 𝑀(4) + 2 = 8𝑀 𝑛 = 2 𝑛 − 1Basic Operation
  • 18.
    a) Another approachLet𝑀(𝑛) be the number of multiplications made by the algorithm.Recurrence relation equation: 𝑀(𝑛) = 𝑀(𝑛 − 1) + 2 and 𝑀 1 = 0• Using backward substitution:𝑀(𝑛) = 𝑀(𝑛 − 1) + 2= [ 𝑀(𝑛 − 2) + 2 ] + 2 = 𝑀(𝑛 − 2) + 4= [ 𝑀(𝑛 − 3) + 2 ] + 4 = 𝑀(𝑛 − 3) + 6= [ 𝑀(𝑛 − 4) + 2 ] + 6 = 𝑀(𝑛 − 4) + 8𝑀(𝑛) = 𝑀 𝑛 − 𝑖 + 2𝑖= 𝑀 1 + 2(𝑛 − 1)= 2 𝑛 − 1Basic Operation𝑛 − 𝑖 = 1𝑖 = 𝑛 − 1
  • 19.
    ALGORITHM: S(n)// Input:A positive integer n// Output: The sum of the first n cubesS ← 1for i ← 2 to n doS ← S + i * i * ireturn SThe number of multiplications made by this algorithm will be෍𝑖=2𝑛2 =𝑛 − 2 + 12× (2 + 2) = 2(𝑛 − 1)This is exactly the same number as in the recursive version, but the no recursive versiondoesn't carry the time and space overhead associated with the recursion's stack.b) Comparing with the non-recursive algorithm
  • 20.
  • 21.
  • 22.
  • 24.
  • 25.
  • 26.
    b)Let 𝐴(𝑛) bethe number of additions made by the algorithm.Recurrence relation equation: 𝐴 𝑛 = 𝐴 𝑛 − 1 + 𝐴(𝑛 − 1) + 1 and 𝐴 0 = 0= 2𝐴 𝑛 − 1 + 1• Using forward substitution:𝐴(0) = 0𝐴(1) = 2𝐴(0) + 1 = 1𝐴(2) = 2𝐴(1) + 1 = 3𝐴(3) = 2𝐴(2) + 1 = 7𝐴(4) = 2𝐴(3) + 1 = 15𝐴 𝑛 = 2𝑛 − 1Basic Operation
  • 27.
    b) Another approachLet𝐴(𝑛) be the number of additions made by the algorithm.Recurrence relation equation: 𝐴(𝑛) = 2𝐴(𝑛 − 1) + 1 and 𝐴 0 = 0• Using backward substitution:𝐴(𝑛) = 2𝐴(𝑛 − 1) + 1= 2[ 2𝐴(𝑛 − 2) + 1 ] + 1 = 4𝐴(𝑛 − 2) + 3= 4[ 2𝐴(𝑛 − 3) + 1 ] + 3 = 8𝐴(𝑛 − 3) + 7= 8[ 2𝐴(𝑛 − 4) + 1 ] + 7 = 16𝐴(𝑛 − 4) + 15𝐴 𝑛 = 2𝑖𝐴 𝑛 − 𝑖 + (2𝑖 − 1)= 2𝑛 𝑀 0 + (2𝑛 − 1)= 2𝑛 − 1Basic Operation𝑛 − 𝑖 = 0𝑖 = 𝑛
  • 28.
    d. It’s avery bad algorithm because it is vastly inferior to the algorithm that simplymultiplies an accumulator by 2 𝑛 times. O(2𝑛)
  • 29.
  • 30.
    a) The algorithmcomputes the value of the smallest element in a given array.Let A = [9, 5, 8, 2]▪ n = 4 ≠ 1▪ Call Riddle([9, 5, 8]) (subarray A[0..2]):▪ For Riddle([9, 5, 8]):▪ n = 3 ≠ 1▪ Call Riddle([9, 5]) (subarray A[0..1]):▪ For Riddle([9, 5]):▪ n = 2 ≠ 1▪ Call Riddle([9]) (subarray A[0..0]):▪ n = 1, so it returns 9.▪ Compare temp = 9 with A[1] = 5:▪ 9 > 5, so return 5.▪ Riddle([9, 5]) returns 5.▪ Compare temp = 5 with A[2] = 8:▪ 5 <= 8, so return 5.▪ Riddle([9, 5, 8]) returns 5.▪ Compare temp = 5 with A[3] = 2:▪ 5 > 2, so return 2.
  • 31.
    b)Let 𝐶(𝑛) bethe number of the number of key comparisons made by the algorithm.Recurrence relation equation: 𝐶(𝑛) = 𝐶(𝑛 − 1) + 1 and 𝐶 1 = 0• Using backward substitution:𝐶 𝑛 = 𝐶(𝑛 − 1) + 1= [ 𝐶(𝑛 − 2) + 1 ] + 1 = 𝐶(𝑛 − 2) + 2= [ 𝐶(𝑛 − 3) + 1 ] + 2 = 𝐶(𝑛 − 3) + 3= [ 𝐶(𝑛 − 4) + 1 ] + 3 = 𝐶(𝑛 − 4) + 4𝐶 𝑛 = 𝐶 𝑛 − 𝑖 + 𝑖= 𝐶 1 + (𝑛 − 1)= 𝑛 − 1Basic Operation𝑛 − 𝑖 = 1𝑖 = 𝑛 − 1

[8]ページ先頭

©2009-2025 Movatter.jp