design and analysis of algorithms 18cs42 module 1...
TRANSCRIPT
![Page 1: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/1.jpg)
Design and Analysis of Algorithms18CS42
Module 1: Introduction to Algorithms
Harivinod NHarivinod NDept. of Computer Science and EngineeringDept. of Computer Science and Engineering
VCETVCET PutturPuttur
Module 1: Introduction to Algorithms
![Page 2: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/2.jpg)
Course Outcome
• At the end of the course, you will be able to;1. Estimate the computational complexity of different
algorithms along with its representation notations.2. Design and analyze problem solving using divide and
conquer strategy3. Apply greedy method to solve problems.4. Apply dynamic programming to solve problems using the
solutions of similar subproblems.5. Design and apply backtracking technique for problem
solving
• At the end of the course, you will be able to;1. Estimate the computational complexity of different
algorithms along with its representation notations.2. Design and analyze problem solving using divide and
conquer strategy3. Apply greedy method to solve problems.4. Apply dynamic programming to solve problems using the
solutions of similar subproblems.5. Design and apply backtracking technique for problem
solving
218CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 3: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/3.jpg)
318CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 4: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/4.jpg)
Algorithm
418CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 5: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/5.jpg)
al-Khorezmi
• Muhammad ibn Musa al-Khwarizmi• The Father of Algebra• Persian Mathematician
• Muhammad ibn Musa al-Khwarizmi• The Father of Algebra• Persian Mathematician
Harivinod N 18CS42-Design and Analysis of Algorithms Feb-May 2020 5
![Page 6: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/6.jpg)
How topreparetea?
618CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 7: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/7.jpg)
How to prepare Maggi noodles?
1. Take one and a half cup of Water in a pan.2. Heat the pan on medium flame.3. When the Water comes to boil, add the Maggi to the pan.4. Cover it with a lid for a minute.5. After a minute, uncover the lid and add the tastemaker to
the pan.6. Mix it well, Without breaking the Noodles.7. Just when all of your Water is boiled, switch off the flame.8. Enjoy the hot Maggi.
1. Take one and a half cup of Water in a pan.2. Heat the pan on medium flame.3. When the Water comes to boil, add the Maggi to the pan.4. Cover it with a lid for a minute.5. After a minute, uncover the lid and add the tastemaker to
the pan.6. Mix it well, Without breaking the Noodles.7. Just when all of your Water is boiled, switch off the flame.8. Enjoy the hot Maggi.
Harivinod N 18CS42-Design and Analysis of Algorithms Feb-May 2020 7
![Page 8: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/8.jpg)
How Kingfisher catches a fish efficiently?
818CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 9: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/9.jpg)
Shortest Distance between two cities
918CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 10: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/10.jpg)
Text Book -1
1018CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 11: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/11.jpg)
Text Book -2
1118CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 12: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/12.jpg)
Course Website:www.techjourney.in
1218CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 13: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/13.jpg)
Module 1 – OutlineIntroduction to Algorithms
1. Introduction2. Performance Analysis3. Asymptotic Notations4. Mathematical analysis of Non-Recursive algorithms5. Mathematical analysis of Recursive algorithms6. Important Problem Types7. Fundamental Data Structures
1. Introduction2. Performance Analysis3. Asymptotic Notations4. Mathematical analysis of Non-Recursive algorithms5. Mathematical analysis of Recursive algorithms6. Important Problem Types7. Fundamental Data Structures
1318CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 14: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/14.jpg)
Module 1 – OutlineIntroduction to Algorithms
1. Introduction2. Performance Analysis3. Asymptotic Notations4. Mathematical analysis of Non-Recursive algorithms5. Mathematical analysis of Recursive algorithms6. Important Problem Types7. Fundamental Data Structures
1. Introduction2. Performance Analysis3. Asymptotic Notations4. Mathematical analysis of Non-Recursive algorithms5. Mathematical analysis of Recursive algorithms6. Important Problem Types7. Fundamental Data Structures
1418CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 15: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/15.jpg)
What is an Algorithm?
• An algorithm is a finite sequence of unambiguousinstructions to solve a particular problem.
• In addition, all algorithms must satisfy the followingcriteria:– Input: Zero or more quantities are externally supplied.– Output: At least one quantity is produced.– Definiteness: Each instruction is clear and unambiguous.– Finiteness: algorithm terminates after a finite number of
steps.– Correctness– Effectiveness
• An algorithm is a finite sequence of unambiguousinstructions to solve a particular problem.
• In addition, all algorithms must satisfy the followingcriteria:– Input: Zero or more quantities are externally supplied.– Output: At least one quantity is produced.– Definiteness: Each instruction is clear and unambiguous.– Finiteness: algorithm terminates after a finite number of
steps.– Correctness– Effectiveness
1518CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 16: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/16.jpg)
Algorithm design and analysis process
1618CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 17: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/17.jpg)
Algorithm specification
• An algorithm can be specified in1. Simple English2. Graphical representation like flow chart3. Programming language like c++ / java4. Combination of above methods.
• An algorithm can be specified in1. Simple English2. Graphical representation like flow chart3. Programming language like c++ / java4. Combination of above methods.
1718CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 18: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/18.jpg)
1818CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 19: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/19.jpg)
Recursive Algorithm
• An algorithm is said to be recursive if the same algorithm isinvoked in the body (direct recursive).
• Algorithm A is said to be indirect recursive if it calls anotheralgorithm which in turn calls A.
• Example 1: Factorial computation n! = n * (n-1)!• Example 2: Binomial coefficient computation
• Example 3: Tower of Hanoi problem• Example 4: Permutation Generator
• An algorithm is said to be recursive if the same algorithm isinvoked in the body (direct recursive).
• Algorithm A is said to be indirect recursive if it calls anotheralgorithm which in turn calls A.
• Example 1: Factorial computation n! = n * (n-1)!• Example 2: Binomial coefficient computation
• Example 3: Tower of Hanoi problem• Example 4: Permutation Generator
1918CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 20: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/20.jpg)
Module 1 – OutlineIntroduction to Algorithms
1. Introduction2. Performance Analysis3. Asymptotic Notations4. Mathematical analysis of Non-Recursive algorithms5. Mathematical analysis of Recursive algorithms6. Important Problem Types7. Fundamental Data Structures
1. Introduction2. Performance Analysis3. Asymptotic Notations4. Mathematical analysis of Non-Recursive algorithms5. Mathematical analysis of Recursive algorithms6. Important Problem Types7. Fundamental Data Structures
2018CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 21: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/21.jpg)
Performance Analysis
• Space complexity– Space Complexity of an algorithm is total space taken by
the algorithm with respect to the input size.– Space complexity includes both Auxiliary space and space
used by input.
• Time complexity
• Space complexity– Space Complexity of an algorithm is total space taken by
the algorithm with respect to the input size.– Space complexity includes both Auxiliary space and space
used by input.
• Time complexity
2118CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 22: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/22.jpg)
Time Complexity
• Execution time or run-time of the programis refereed as its time complexity
• This is the sum of the time taken toexecute all instructions in the program.
• But, We count only the number of steps inthe program. Why?
• How to count?– Two ways
• Execution time or run-time of the programis refereed as its time complexity
• This is the sum of the time taken toexecute all instructions in the program.
• But, We count only the number of steps inthe program. Why?
• How to count?– Two ways
2218CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 23: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/23.jpg)
Method-1
• Introduce a count variable• Increment count for every operation
2318CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 24: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/24.jpg)
Method-2
• Count the steps per execution for everyline of code
• Note the frequency of execution ofevery line and find the total steps.
24
• Count the steps per execution for everyline of code
• Note the frequency of execution ofevery line and find the total steps.
18CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 25: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/25.jpg)
Method-2
2518CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 26: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/26.jpg)
Trade-off
• One has to make a compromise and to exchangecomputing time for memory consumption or viceversa, depending on application.
2618CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 27: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/27.jpg)
Analysis Framework
• Space complexity• Time complexity
• Measuring an Input’s Size– run longer on larger inputs
• Units for Measuring Running lime– Basic operation
• Order of Growth
• Space complexity• Time complexity
• Measuring an Input’s Size– run longer on larger inputs
• Units for Measuring Running lime– Basic operation
• Order of Growth
2718CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 28: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/28.jpg)
Orders of Growth
• Order of growth of an algorithm is a way of stating how executiontime or memory occupied by it changes with the input size.
• Why emphasis on large input sizes?• Because for large values of n, it is the function's order of growth
that counts.
2818CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 29: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/29.jpg)
2918CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 30: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/30.jpg)
Analysis Framework
• Worst-Case• Best-Case• Average-Case Efficiencies
• Worst-Case• Best-Case• Average-Case Efficiencies
3018CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 31: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/31.jpg)
Worst Case
• Definition: The worst-case efficiency of an algorithm is itsefficiency for the worst-case input of size n, for which thealgorithm runs the longest among all possible inputs of thatsize.
• Definition: The worst-case efficiency of an algorithm is itsefficiency for the worst-case input of size n, for which thealgorithm runs the longest among all possible inputs of thatsize.
Cworst(n) = n.
3118CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 32: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/32.jpg)
Best Case
• Definition: The best-case efficiency of an algorithm is itsefficiency for the best-case input of size n, for which thealgorithm runs the fastest among all possible inputs of thatsize.
• Definition: The best-case efficiency of an algorithm is itsefficiency for the best-case input of size n, for which thealgorithm runs the fastest among all possible inputs of thatsize.
Cbest(n) = 1.
3218CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 33: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/33.jpg)
Average Case
• Definition: the average-case complexity of an algorithm is theamount of time used by the algorithm, averaged over allpossible inputs.
3318CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 34: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/34.jpg)
Summary of analysis framework• Both time and space efficiencies are measured as functions of
the algorithm's input size.– Time efficiency - basic operation– Space efficiency - extra memory units
• The efficiencies of some algorithms may differ significantly forinputs of the same size.– For such algorithms, we need to distinguish between the
worst-case, average-case, and best-case efficiencies.
• The primary interest lies in the order of growth of thealgorithm's running time (or extra memory units consumed)as its input size goes to infinity.
• Both time and space efficiencies are measured as functions ofthe algorithm's input size.– Time efficiency - basic operation– Space efficiency - extra memory units
• The efficiencies of some algorithms may differ significantly forinputs of the same size.– For such algorithms, we need to distinguish between the
worst-case, average-case, and best-case efficiencies.
• The primary interest lies in the order of growth of thealgorithm's running time (or extra memory units consumed)as its input size goes to infinity.
3418CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 35: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/35.jpg)
Module 1 – OutlineIntroduction to Algorithms
1. Introduction2. Performance Analysis3. Asymptotic Notations4. Mathematical analysis of Non-Recursive algorithms5. Mathematical analysis of Recursive algorithms6. Important Problem Types7. Fundamental Data Structures
1. Introduction2. Performance Analysis3. Asymptotic Notations4. Mathematical analysis of Non-Recursive algorithms5. Mathematical analysis of Recursive algorithms6. Important Problem Types7. Fundamental Data Structures
3518CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 36: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/36.jpg)
Asymptotic Notations
• To compare orders of growth, computer scientistsuse three notations:– O(big oh),– Ω(big omega),– Θ (big theta) and
– o(little oh)
• To compare orders of growth, computer scientistsuse three notations:– O(big oh),– Ω(big omega),– Θ (big theta) and
– o(little oh)
3618CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 37: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/37.jpg)
Big-Oh notation
A function t(n) is said to be in O(g(n)),denoted t(n)∈ O(g(n)),if t (n) is bounded above by some constant multiple of
g(n) for all large n,i.e., if there exist some positive constant c and some
nonnegative integer n0 such thatt(n) ≤ c g(n) for all n ≥ n0.
A function t(n) is said to be in O(g(n)),denoted t(n)∈ O(g(n)),if t (n) is bounded above by some constant multiple of
g(n) for all large n,i.e., if there exist some positive constant c and some
nonnegative integer n0 such thatt(n) ≤ c g(n) for all n ≥ n0.
3718CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 38: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/38.jpg)
3818CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 39: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/39.jpg)
3918CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 40: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/40.jpg)
Problems on Big O
Harivinod N 18CS42-Design and Analysis of Algorithms Feb-May 2020 40
![Page 41: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/41.jpg)
Omega notation
A function t(n) is said to be in Ω(g(n)),denoted t(n)∈ Ω(g(n)),if t(n) is bounded below by some positive constant
multiple of g(n) for all large n,i.e., if there exist some positive constant c and some
nonnegative integer n0 such thatt(n) ≥ c g(n) for all n ≥ n0.
A function t(n) is said to be in Ω(g(n)),denoted t(n)∈ Ω(g(n)),if t(n) is bounded below by some positive constant
multiple of g(n) for all large n,i.e., if there exist some positive constant c and some
nonnegative integer n0 such thatt(n) ≥ c g(n) for all n ≥ n0.
4118CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 42: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/42.jpg)
Big Omega t(n)= Ω(g(n))
4218CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 43: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/43.jpg)
4318CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 44: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/44.jpg)
4418CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 45: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/45.jpg)
Theta Notation
A function t(n) is said to be in Θ(g(n)), denoted t(n) ∈Θ(g(n)),
if t (n) is bounded both above and below by somepositive constant multiples of g(n) for all large n,
i.e., if there exist some positive constants c1 and c2 andsome nonnegative integer n0 such that
c2 g(n) ≤ t(n) ≤ c1g(n) for all n ≥ n0.
A function t(n) is said to be in Θ(g(n)), denoted t(n) ∈Θ(g(n)),
if t (n) is bounded both above and below by somepositive constant multiples of g(n) for all large n,
i.e., if there exist some positive constants c1 and c2 andsome nonnegative integer n0 such that
c2 g(n) ≤ t(n) ≤ c1g(n) for all n ≥ n0.
4518CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 46: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/46.jpg)
4618CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 47: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/47.jpg)
Graphical representation
• Which one best to represent order of growth
Harivinod N 18CS42-Design and Analysis of Algorithms Feb-May 2020 47
![Page 48: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/48.jpg)
Strategies for Ω and Θ
• Proving that a f(n) = Ω(g(n)) often requires more thought.– Quite often, we have to pick c < 1.– A good strategy is to pick a value of c which you think will
work, and determine which value of n0 is needed.– Being able to do a little algebra helps.– We can sometimes simplify by ignoring terms of f(n) with the
positive coefficients.• The following theorem shows us that proving f(n) = Θ(g(n)) is
nothing new:– Theorem: f(n) = Θ(g(n)) if and only iff(n) = O(g(n)) and f(n) =
Ω(g(n)).– Thus, we just apply the previous two strategies.
• Proving that a f(n) = Ω(g(n)) often requires more thought.– Quite often, we have to pick c < 1.– A good strategy is to pick a value of c which you think will
work, and determine which value of n0 is needed.– Being able to do a little algebra helps.– We can sometimes simplify by ignoring terms of f(n) with the
positive coefficients.• The following theorem shows us that proving f(n) = Θ(g(n)) is
nothing new:– Theorem: f(n) = Θ(g(n)) if and only iff(n) = O(g(n)) and f(n) =
Ω(g(n)).– Thus, we just apply the previous two strategies.
Harivinod N 18CS42-Design and Analysis of Algorithms Feb-May 2020 48
![Page 49: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/49.jpg)
4918CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 50: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/50.jpg)
5018CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 51: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/51.jpg)
Problem-3
Harivinod N 18CS42-Design and Analysis of Algorithms Feb-May 2020 51
![Page 52: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/52.jpg)
5218CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 53: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/53.jpg)
5318CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 54: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/54.jpg)
Little Oh
• The function f(n) = o(g(n)) [ i.e f of n is a little oh of gof n ] if and only if
5418CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 55: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/55.jpg)
• For comparing the order of growth limit is usedLittle Oh
5518CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 56: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/56.jpg)
5618CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 57: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/57.jpg)
Theorem: If t1(n) ∈ O(g1(n)) and t2(n) ∈ O(g2(n)), then t1(n) + t2(n) ∈O(max{g1(n), g2(n)}).(The analogous assertions are true for the Ω and Ө notations as well.)
Proof:The proof extends to orders of growth the following simple fact
about four arbitrary real numbers a1, b1, a2, b2:if a1 ≤ b1 and a2 ≤ b2, then a1 + a2 ≤ 2 max{b1, b2}.
Since t1(n) ∈ O(g1(n)), there exist some positive constant c1 andsome nonnegative integer n1 such that
t1(n) ≤ c1g1(n) for all n ≥ n1.
Similarly, since t2(n) ∈ O(g2(n)), t2(n) ≤ c2g2(n) for all n ≥ n2.
Proof:The proof extends to orders of growth the following simple fact
about four arbitrary real numbers a1, b1, a2, b2:if a1 ≤ b1 and a2 ≤ b2, then a1 + a2 ≤ 2 max{b1, b2}.
Since t1(n) ∈ O(g1(n)), there exist some positive constant c1 andsome nonnegative integer n1 such that
t1(n) ≤ c1g1(n) for all n ≥ n1.
Similarly, since t2(n) ∈ O(g2(n)), t2(n) ≤ c2g2(n) for all n ≥ n2.
Harivinod N 18CS42-Design and Analysis of Algorithms Feb-May 2020 57
![Page 58: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/58.jpg)
Theorem: If t1(n) ∈ O(g1(n)) and t2(n) ∈ O(g2(n)), then t1(n) + t2(n) ∈O(max{g1(n), g2(n)}).(The analogous assertions are true for the Ω and Ө notations as well.)
Proof: (continued):Let us denote c3 = max{c1, c2} and consider n ≥ max{n1, n2} so
that we can use both inequalities.Adding them yields the following:
t1(n) + t2(n) ≤ c1g1(n) + c2g2(n)≤ c3 g1(n) + c3g2(n) = c3[g1(n) + g2(n)]≤ c32 max{g1(n), g2(n)}.
Hence, t1(n) + t2(n) ∈ O(max{g1(n), g2(n)}), with the constants cand n0 required by the O definition being 2c3 = 2 max{c1, c2}and max{n1, n2}, respectively.
Proof: (continued):Let us denote c3 = max{c1, c2} and consider n ≥ max{n1, n2} so
that we can use both inequalities.Adding them yields the following:
t1(n) + t2(n) ≤ c1g1(n) + c2g2(n)≤ c3 g1(n) + c3g2(n) = c3[g1(n) + g2(n)]≤ c32 max{g1(n), g2(n)}.
Hence, t1(n) + t2(n) ∈ O(max{g1(n), g2(n)}), with the constants cand n0 required by the O definition being 2c3 = 2 max{c1, c2}and max{n1, n2}, respectively.
Harivinod N 18CS42-Design and Analysis of Algorithms Feb-May 2020 58
![Page 59: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/59.jpg)
Class Name Comments
Basic Efficiency classes
5918CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 60: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/60.jpg)
Class Name Comments
6018CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 61: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/61.jpg)
Quiz: Which kind of growth best characterizeseach of these functions?
6118CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 62: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/62.jpg)
Module 1 – OutlineIntroduction to Algorithms
1. Introduction2. Performance Analysis3. Asymptotic Notations4. Mathematical analysis of Non-Recursive algorithms5. Mathematical analysis of Recursive algorithms6. Important Problem Types7. Fundamental Data Structures
1. Introduction2. Performance Analysis3. Asymptotic Notations4. Mathematical analysis of Non-Recursive algorithms5. Mathematical analysis of Recursive algorithms6. Important Problem Types7. Fundamental Data Structures
6218CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 63: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/63.jpg)
Mathematical Analysis of Non-recursive Algorithms
1. Decide on a parameters indicating an input’s size.2. Identify the algorithm’s basic operation.3. Check whether the number of times the basic operation is
executed depends only on the size of an input.If it also depends on some additional property, the worst-case, average-case, and, if necessary, best-case efficiencieshave to be investigated separately.
4. Set up a sum expressing the number of times the algorithm’sbasic operation is executed.
5. Using standard formulas and rules of sum manipulation,either find a closed form formula for the count or, at thevery least, establish its order of growth.
1. Decide on a parameters indicating an input’s size.2. Identify the algorithm’s basic operation.3. Check whether the number of times the basic operation is
executed depends only on the size of an input.If it also depends on some additional property, the worst-case, average-case, and, if necessary, best-case efficiencieshave to be investigated separately.
4. Set up a sum expressing the number of times the algorithm’sbasic operation is executed.
5. Using standard formulas and rules of sum manipulation,either find a closed form formula for the count or, at thevery least, establish its order of growth.
6318CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 64: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/64.jpg)
Best, Worst, Average case exist?
6418CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 65: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/65.jpg)
Best, Worst, Average case exist?
6518CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 66: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/66.jpg)
Best, Worst, Average case exist?
6618CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 67: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/67.jpg)
Analysis
Best, Worst, Average case exist?
Harivinod N 18CS42-Design and Analysis of Algorithms Feb-May 2020 67
![Page 68: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/68.jpg)
Best, Worst, Average case exist?
The basic operation is count=count + 1 repeats number oftimes
6818CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 69: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/69.jpg)
Module 1 – OutlineIntroduction to Algorithms
1. Introduction2. Performance Analysis3. Asymptotic Notations4. Mathematical analysis of Non-Recursive algorithms5. Mathematical analysis of Recursive algorithms6. Important Problem Types7. Fundamental Data Structures
1. Introduction2. Performance Analysis3. Asymptotic Notations4. Mathematical analysis of Non-Recursive algorithms5. Mathematical analysis of Recursive algorithms6. Important Problem Types7. Fundamental Data Structures
6918CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 70: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/70.jpg)
Analysis of Recursive Algorithms
1. Decide on a parameter indicating an input’s size.2. Identify the algorithm’s basic operation.3. Check whether the number of times the basic operation is
executed can vary on different inputs of the same size;if it can, the worst-case, average-case, and best-caseefficiencies must be investigated separately.
4. Set up a recurrence relation, with an appropriate initialcondition, for the number of times the basic operation isexecuted.
5. Solve the recurrence or, at least, ascertain the order ofgrowth of its solution.
1. Decide on a parameter indicating an input’s size.2. Identify the algorithm’s basic operation.3. Check whether the number of times the basic operation is
executed can vary on different inputs of the same size;if it can, the worst-case, average-case, and best-caseefficiencies must be investigated separately.
4. Set up a recurrence relation, with an appropriate initialcondition, for the number of times the basic operation isexecuted.
5. Solve the recurrence or, at least, ascertain the order ofgrowth of its solution.
7018CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 71: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/71.jpg)
7118CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 72: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/72.jpg)
• We can use backward substitutions method to solvethis
7218CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 73: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/73.jpg)
Tower of Hanoi puzzle.
• In this puzzle, There are n disks of different sizes that can slideonto any of three pegs.
• Initially, all the disks are on the first peg in order of size, thelargest on the bottom and the smallest on top.
• The goal is to move all the disks to the third peg, using thesecond one as an auxiliary, if necessary.
• We can move only one disk at a time, and it is forbidden to placea larger disk on top of a smaller one.
• In this puzzle, There are n disks of different sizes that can slideonto any of three pegs.
• Initially, all the disks are on the first peg in order of size, thelargest on the bottom and the smallest on top.
• The goal is to move all the disks to the third peg, using thesecond one as an auxiliary, if necessary.
• We can move only one disk at a time, and it is forbidden to placea larger disk on top of a smaller one.
7318CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 74: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/74.jpg)
Tower of Hanoi puzzle.
• The problem has an elegant recursive solution• To move n>1 disks from peg 1 to peg 3 (with peg 2 as
auxiliary),– we first move recursively n-1 disks from peg 1 to peg 2 (with
peg 3 as auxiliary),– then move the largest disk directly from peg 1 to peg 3, and,– finally, move recursively n-1 disks from peg 2 to peg 3 (using
peg 1 as auxiliary).
• If n = 1, we move the single disk directly from the sourcepeg to the destination peg.
• The problem has an elegant recursive solution• To move n>1 disks from peg 1 to peg 3 (with peg 2 as
auxiliary),– we first move recursively n-1 disks from peg 1 to peg 2 (with
peg 3 as auxiliary),– then move the largest disk directly from peg 1 to peg 3, and,– finally, move recursively n-1 disks from peg 2 to peg 3 (using
peg 1 as auxiliary).
• If n = 1, we move the single disk directly from the sourcepeg to the destination peg.
7418CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 75: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/75.jpg)
7518CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 76: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/76.jpg)
Algorithm
TowerOfHanoi(n, source, dest, aux)If n == 1, then
move disk from source to destelse
TowerOfHanoi (n - 1, source, aux, dest)move disk from source to destTowerOfHanoi (n - 1, aux, dest, source)
End if
TowerOfHanoi(n, source, dest, aux)If n == 1, then
move disk from source to destelse
TowerOfHanoi (n - 1, source, aux, dest)move disk from source to destTowerOfHanoi (n - 1, aux, dest, source)
End if
Harivinod N 18CS42-Design and Analysis of Algorithms Feb-May 2020 76
![Page 77: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/77.jpg)
Recurrence relation for total number of moves
The number of moves M(n) depends only on n. Therecurrence equation is
We have the following recurrence relation for thenumber of moves M(n):
The number of moves M(n) depends only on n. Therecurrence equation is
We have the following recurrence relation for thenumber of moves M(n):
Harivinod N 18CS42-Design and Analysis of Algorithms Feb-May 2020 77
![Page 78: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/78.jpg)
• We solve this recurrence by the same method ofbackward substitutions:
• The pattern of the first three sums on the leftsuggests that the next one will be24 M(n − 4) + 23 + 22 + 2 + 1, andgenerally, after i substitutions, we get
• We solve this recurrence by the same method ofbackward substitutions:
• The pattern of the first three sums on the leftsuggests that the next one will be24 M(n − 4) + 23 + 22 + 2 + 1, andgenerally, after i substitutions, we get
7818CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 79: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/79.jpg)
• Since the initial condition is specified for n = 1, whichis achieved for i = n - 1,we get the following formula for the solution torecurrence
7918CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 80: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/80.jpg)
Example 3
• Basic operation is Addition• The recurrence relation can be written as
• Assuming n = 2k
• Basic operation is Addition• The recurrence relation can be written as
• Assuming n = 2k
Harivinod N 18CS42-Design and Analysis of Algorithms Feb-May 2020 80
![Page 81: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/81.jpg)
Recurrence relation for basic operation
Harivinod N 18CS42-Design and Analysis of Algorithms Feb-May 2020 81
![Page 82: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/82.jpg)
Module 1 – OutlineIntroduction to Algorithms
1. Introduction2. Performance Analysis3. Asymptotic Notations4. Mathematical analysis of Non-Recursive algorithms5. Mathematical analysis of Recursive algorithms6. Important Problem Types7. Fundamental Data Structures
1. Introduction2. Performance Analysis3. Asymptotic Notations4. Mathematical analysis of Non-Recursive algorithms5. Mathematical analysis of Recursive algorithms6. Important Problem Types7. Fundamental Data Structures
8218CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 83: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/83.jpg)
Important Problem Types
• Sorting– rearrange the items of a given list in non-decreasing order– there is no algorithm that would be the best solution in all
situationsTwo properties– Algorithm is stable if it preserves the relative order of any
two equal elements in its input– in-place - no extra memory
• Searching– Linear search– Binary search
• Sorting– rearrange the items of a given list in non-decreasing order– there is no algorithm that would be the best solution in all
situationsTwo properties– Algorithm is stable if it preserves the relative order of any
two equal elements in its input– in-place - no extra memory
• Searching– Linear search– Binary search
8318CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 84: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/84.jpg)
Important Problem Types
• String Processing• Graph Problems
– Oldest problems
• Combinatorial Problems– grows extremely fast with a problem’s size– there are no known algorithms for solving such problems
exactly in an acceptable amount of time
• String Processing• Graph Problems
– Oldest problems
• Combinatorial Problems– grows extremely fast with a problem’s size– there are no known algorithms for solving such problems
exactly in an acceptable amount of time
8418CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 85: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/85.jpg)
Module 1 – OutlineIntroduction to Algorithms
1. Introduction2. Performance Analysis3. Asymptotic Notations4. Mathematical analysis of Non-Recursive algorithms5. Mathematical analysis of Recursive algorithms6. Important Problem Types7. Fundamental Data Structures
1. Introduction2. Performance Analysis3. Asymptotic Notations4. Mathematical analysis of Non-Recursive algorithms5. Mathematical analysis of Recursive algorithms6. Important Problem Types7. Fundamental Data Structures
8518CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 86: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/86.jpg)
Fundamental Data structures
• Linear data structures– Array– Linked list
• Singly linked list• Doubly linked list
– List• Stack• Queue, Priority queue
• Linear data structures– Array– Linked list
• Singly linked list• Doubly linked list
– List• Stack• Queue, Priority queue
8618CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 87: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/87.jpg)
Fundamental Data structures
• Graphs– Undirected– Directed (Digraph)
– Weighted graph– cycle
• Graphs– Undirected– Directed (Digraph)
– Weighted graph– cycle
8718CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 88: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/88.jpg)
Fundamental Data structures
• Graph Representations– Adjacency matrix– Adjacency list
8818CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 89: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/89.jpg)
Fundamental Data structures
• Graph Representations– Weighted graph– cycle
8918CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 90: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/90.jpg)
Fundamental Data structures
• Trees, Forests– Rooted tree– Depth of vertex v– Height of the tree
• Trees, Forests– Rooted tree– Depth of vertex v– Height of the tree
9018CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 91: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/91.jpg)
Harivinod N 18CS42-Design and Analysis of Algorithms Feb-May 2020 91
![Page 92: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/92.jpg)
Fundamental Data structures
• Trees, Forests– Ordered tree
• Binary tree• Binary search tree
• Trees, Forests– Ordered tree
• Binary tree• Binary search tree
9218CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 93: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/93.jpg)
Fundamental Data structures
• Sets– Set operations
• Check membership, Union, Intersection– Implementations
• Bit vector, List
• Sets– Set operations
• Check membership, Union, Intersection– Implementations
• Bit vector, List
9318CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 94: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/94.jpg)
Fundamental Data structures
• Dictionaries– Searching– Adding– Deleting
• Dictionaries– Searching– Adding– Deleting
9418CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
![Page 95: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/95.jpg)
Extra Byte-1.1: Min Distance
Consider the following algorithm for finding the distance between the twoclosest elements in an array of numbers. Make as many improvements as youcan in this algorithmic solution to the problem. If you need to, you maychange the algorithm altogether; if not, improve the implementation given.
Harivinod N 18CS42-Design and Analysis of Algorithms Feb-May 2020 95
![Page 96: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/96.jpg)
Extra Byte-1.2: Secret
Consider the following algorithm.
a. What does this algorithm compute?b. What is its basic operation?c. How many times is the basic operation executed?d. What is the efficiency class of this algorithm?e. Suggest an improvement/better algorithm, and indicate its efficiency class.
If you cannot do it, try to prove that, in fact, it cannot be done.
Consider the following algorithm.
a. What does this algorithm compute?b. What is its basic operation?c. How many times is the basic operation executed?d. What is the efficiency class of this algorithm?e. Suggest an improvement/better algorithm, and indicate its efficiency class.
If you cannot do it, try to prove that, in fact, it cannot be done.
Harivinod N 18CS42-Design and Analysis of Algorithms Feb-May 2020 96
![Page 97: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/97.jpg)
Extra Byte-1.3: Enigma
Consider the following algorithm.
a. What does this algorithm compute?b. What is its basic operation?c. How many times is the basic operation executed?d. What is the efficiency class of this algorithm?e. Suggest an improvement/better algorithm, and indicate its efficiency class.
If you cannot do it, try to prove that, in fact, it cannot be done.
Consider the following algorithm.
a. What does this algorithm compute?b. What is its basic operation?c. How many times is the basic operation executed?d. What is the efficiency class of this algorithm?e. Suggest an improvement/better algorithm, and indicate its efficiency class.
If you cannot do it, try to prove that, in fact, it cannot be done.
Harivinod N 18CS42-Design and Analysis of Algorithms Feb-May 2020 97
![Page 98: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/98.jpg)
Assignment-1 Due: Within 5 days
Harivinod N 18CS42-Design and Analysis of Algorithms Feb-May 2020 98
![Page 99: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/99.jpg)
1. Consider the following algorithm (6M)a. What does this algorithm compute?b. What is its basic operation?c. How many times is the basic operation executed?d. What is the efficiency class of this algorithm?e. Suggest an better algorithm if any and indicate its efficiency class.
2. Solve the recurrence relation (6M)M(n) = 2M(n-1) +1 for n>1; M(1)=1
3. Define best case, worst case and average case efficiency. Write the algorithm andgive these efficiencies for sequential search. (8M)
Class test-1 Max Marks: 20 Duration:45 Mins
1. Consider the following algorithm (6M)a. What does this algorithm compute?b. What is its basic operation?c. How many times is the basic operation executed?d. What is the efficiency class of this algorithm?e. Suggest an better algorithm if any and indicate its efficiency class.
2. Solve the recurrence relation (6M)M(n) = 2M(n-1) +1 for n>1; M(1)=1
3. Define best case, worst case and average case efficiency. Write the algorithm andgive these efficiencies for sequential search. (8M)
Harivinod N 18CS42-Design and Analysis of Algorithms Feb-May 2020 99
![Page 100: Design and Analysis of Algorithms 18CS42 Module 1 ...techjourney.in/docs/DAA/hn-daa-m1-slides.pdf · Course Outcome • At the end of the course, you will be able to; 1. Estimate](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af1b97e708231d42e1bb1/html5/thumbnails/100.jpg)
End of Module-1
Harivinod N 18CS42-Design and Analysis of Algorithms Feb-May 2020 100