design and analysis of algorithms 18cs42 module 1...
TRANSCRIPT
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
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
318CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
Algorithm
418CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
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
How topreparetea?
618CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
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
How Kingfisher catches a fish efficiently?
818CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
Shortest Distance between two cities
918CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
Text Book -1
1018CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
Text Book -2
1118CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
Course Website:www.techjourney.in
1218CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
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
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
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
Algorithm design and analysis process
1618CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
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
1818CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
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
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
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
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
Method-1
• Introduce a count variable• Increment count for every operation
2318CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
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
Method-2
2518CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
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
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
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
2918CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
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
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
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
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
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
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
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
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
3818CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
3918CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
Problems on Big O
Harivinod N 18CS42-Design and Analysis of Algorithms Feb-May 2020 40
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
Big Omega t(n)= Ω(g(n))
4218CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
4318CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
4418CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
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
4618CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
Graphical representation
• Which one best to represent order of growth
Harivinod N 18CS42-Design and Analysis of Algorithms Feb-May 2020 47
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
4918CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
5018CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
Problem-3
Harivinod N 18CS42-Design and Analysis of Algorithms Feb-May 2020 51
5218CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
5318CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
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
• For comparing the order of growth limit is usedLittle Oh
5518CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
5618CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
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
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
Class Name Comments
Basic Efficiency classes
5918CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
Class Name Comments
6018CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
Quiz: Which kind of growth best characterizeseach of these functions?
6118CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
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
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
Best, Worst, Average case exist?
6418CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
Best, Worst, Average case exist?
6518CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
Best, Worst, Average case exist?
6618CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
Analysis
Best, Worst, Average case exist?
Harivinod N 18CS42-Design and Analysis of Algorithms Feb-May 2020 67
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
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
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
7118CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
• We can use backward substitutions method to solvethis
7218CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
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
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
7518CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
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
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
• 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
• 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
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
Recurrence relation for basic operation
Harivinod N 18CS42-Design and Analysis of Algorithms Feb-May 2020 81
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
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
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
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
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
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
Fundamental Data structures
• Graph Representations– Adjacency matrix– Adjacency list
8818CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
Fundamental Data structures
• Graph Representations– Weighted graph– cycle
8918CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
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
Harivinod N 18CS42-Design and Analysis of Algorithms Feb-May 2020 91
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
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
Fundamental Data structures
• Dictionaries– Searching– Adding– Deleting
• Dictionaries– Searching– Adding– Deleting
9418CS42-Design and Analysis of Algorithms Feb-May 2020Harivinod N
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
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
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
Assignment-1 Due: Within 5 days
Harivinod N 18CS42-Design and Analysis of Algorithms Feb-May 2020 98
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
End of Module-1
Harivinod N 18CS42-Design and Analysis of Algorithms Feb-May 2020 100