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
PPT
CS8451 - Design and Analysis of Algorithms
PPT
Analysis Of Algorithms Ii
PDF
3.pdf
PDF
lec 03wweweweweweweeweweweewewewewee.pdf
PPT
5. Recursive.ppt
PDF
Copy of y16 02-2119divide-and-conquer
PPT
algo_vc_lecture8.ppt
PPTX
Complexity Analysis of Recursive Function
PPT
recurrence relation is explained in this
PPT
3. Recursion and Recurrences.ppt detail about recursive learning
PPTX
Algorithm Design and Complexity - Course 3
PDF
02 Notes Divide and Conquer
PDF
Recursive algorithms
PPTX
2.pptx
PDF
Lecture 5 6_7 - divide and conquer and method of solving recurrences
PPTX
3. D&C and Recurrence Relation.ppYtxVVVV
PDF
Daa chapter 2
PDF
01 Notes Introduction Analysis of Algorithms Notes
PDF
Dynamic programing
PPT
Lecture-3.1(Recurrences) Data Structure and pdf
PPTX
designs and analysis of algorithmss.pptx
PPT
Lecture11_12COMPILER DESIGN_unit 2COMPILER DESIGN_unit 2.ppt
PPT
recurrence relations in analysis of algorithm
PPT
03 mathematical anaylsis
PPT
04-Induction and Recursion.ppt ppt bai tap
PPT
the ppt on chap6 of brassard and Bratley
PPTX
Recursive Algorithm Detailed Explanation
PDF
How to install CS50 Library (Step-by-step guide)
PDF
Understanding Singular Value Decomposition (SVD)

More Related Content

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

Similar to Algorithms Analysis & Design - Lecture 4

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

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

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

Recently uploaded

PDF
DSD-INT 2025 Building-Aware Flood and Lifeline Scour Modeling with Delft3D FM...
PDF
DSD-INT 2025 Coupling SFINCS to a flood risk model to evaluate the effects of...
PDF
Data Integration with Salesforce Bootcamp
PDF
DSD-INT 2025 Century-Scale Impacts of Sediment-Based Coastal Restoration unde...
PPTX
Future of Software Testing: AI-Powered Open Source Testing Tools
PDF
ECFT Case Study: Digital Pilot Transportation System
PDF
SWEBOK: the software engineering body of knowledge (SC25 Workshop Research So...
PDF
DSD-INT 2025 From Software to Impact - Water Quality Modelling for the UN Oce...
PPTX
Moving Cloud 360:- Busy Software Provider In Delhi NCR
PDF
PROJECT REPORT cat2.pdf
PDF
Microservices Architecture Benefits For Mobile Development.pdf
PPTX
The Sync Strikes Back: Tales from the MOPs Trenches
PDF
inSis suite - Laboratory Information Management System
PDF
DSD-INT 2025 Next-Generation Flood Inundation Mapping for Taiwan - Challenges...
PDF
DSD-INT 2025 3D Modeling of Shallow Water Dynamics Using Delft3D FM - Martins
PDF
DSD-INT 2025 Understanding the Paraguay River Response to Hard Bottom Dredgin...
PDF
DSD-INT 2025 Delft3D FM Suite 2026.01 2D3D - New features + Improvements - Sp...
PDF
CCM_External_Sales_Commissions_Standard_Configuration_2022-3.pdf
 
PDF
Building Custom Insurance Applications With
PDF
DSD-INT 2025 The Singapore Regional Model in Delft3D FM - Zijl
DSD-INT 2025 Building-Aware Flood and Lifeline Scour Modeling with Delft3D FM...
DSD-INT 2025 Coupling SFINCS to a flood risk model to evaluate the effects of...
Data Integration with Salesforce Bootcamp
DSD-INT 2025 Century-Scale Impacts of Sediment-Based Coastal Restoration unde...
Future of Software Testing: AI-Powered Open Source Testing Tools
ECFT Case Study: Digital Pilot Transportation System
SWEBOK: the software engineering body of knowledge (SC25 Workshop Research So...
DSD-INT 2025 From Software to Impact - Water Quality Modelling for the UN Oce...
Moving Cloud 360:- Busy Software Provider In Delhi NCR
PROJECT REPORT cat2.pdf
Microservices Architecture Benefits For Mobile Development.pdf
The Sync Strikes Back: Tales from the MOPs Trenches
inSis suite - Laboratory Information Management System
DSD-INT 2025 Next-Generation Flood Inundation Mapping for Taiwan - Challenges...
DSD-INT 2025 3D Modeling of Shallow Water Dynamics Using Delft3D FM - Martins
DSD-INT 2025 Understanding the Paraguay River Response to Hard Bottom Dredgin...
DSD-INT 2025 Delft3D FM Suite 2026.01 2D3D - New features + Improvements - Sp...
CCM_External_Sales_Commissions_Standard_Configuration_2022-3.pdf
 
Building Custom Insurance Applications With
DSD-INT 2025 The Singapore Regional Model in Delft3D FM - Zijl

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