Design and Analysis of Algorithms 18CS42 Module 1: Introduction to - - PowerPoint PPT Presentation

design and analysis of algorithms 18cs42 module 1
SMART_READER_LITE
LIVE PREVIEW

Design and Analysis of Algorithms 18CS42 Module 1: Introduction to - - PowerPoint PPT Presentation

Design and Analysis of Algorithms 18CS42 Module 1: Introduction to Algorithms Module 1: Introduction to Algorithms Harivinod N Harivinod N Dept. of Computer Science and Engineering Dept. of Computer Science and Engineering VCET VCET Puttur


slide-1
SLIDE 1

Design and Analysis of Algorithms 18CS42 Module 1: Introduction to Algorithms

Harivinod N Harivinod N

  • Dept. of Computer Science and Engineering
  • Dept. of Computer Science and Engineering

VCET VCET Puttur Puttur

Module 1: Introduction to Algorithms

slide-2
SLIDE 2

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 strategy

  • 3. 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 strategy

  • 3. 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

2 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-3
SLIDE 3

3 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-4
SLIDE 4

Algorithm

4 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-5
SLIDE 5

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

slide-6
SLIDE 6

How to prepare tea?

6 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-7
SLIDE 7

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

slide-8
SLIDE 8

How Kingfisher catches a fish efficiently?

8 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-9
SLIDE 9

Shortest Distance between two cities

9 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-10
SLIDE 10

Text Book -1

10 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-11
SLIDE 11

Text Book -2

11 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-12
SLIDE 12

Course Website: www.techjourney.in

12 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-13
SLIDE 13

Module 1 – Outline Introduction to Algorithms

  • 1. Introduction
  • 2. Performance Analysis
  • 3. Asymptotic Notations
  • 4. Mathematical analysis of Non-Recursive algorithms
  • 5. Mathematical analysis of Recursive algorithms
  • 6. Important Problem Types
  • 7. Fundamental Data Structures
  • 1. Introduction
  • 2. Performance Analysis
  • 3. Asymptotic Notations
  • 4. Mathematical analysis of Non-Recursive algorithms
  • 5. Mathematical analysis of Recursive algorithms
  • 6. Important Problem Types
  • 7. Fundamental Data Structures

13 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-14
SLIDE 14

Module 1 – Outline Introduction to Algorithms

  • 1. Introduction
  • 2. Performance Analysis
  • 3. Asymptotic Notations
  • 4. Mathematical analysis of Non-Recursive algorithms
  • 5. Mathematical analysis of Recursive algorithms
  • 6. Important Problem Types
  • 7. Fundamental Data Structures
  • 1. Introduction
  • 2. Performance Analysis
  • 3. Asymptotic Notations
  • 4. Mathematical analysis of Non-Recursive algorithms
  • 5. Mathematical analysis of Recursive algorithms
  • 6. Important Problem Types
  • 7. Fundamental Data Structures

14 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-15
SLIDE 15

What is an Algorithm?

  • An algorithm is a finite sequence of unambiguous

instructions to solve a particular problem.

  • In addition, all algorithms must satisfy the following

criteria: – 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 unambiguous instructions to solve a particular problem. In addition, all algorithms must satisfy the following criteria: 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

15 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-16
SLIDE 16

Algorithm design and analysis process

16 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-17
SLIDE 17

Algorithm specification

  • An algorithm can be specified in
  • 1. Simple English
  • 2. Graphical representation like flow chart
  • 3. Programming language like c++ / java
  • 4. Combination of above methods.

An algorithm can be specified in

  • 1. Simple English
  • 2. Graphical representation like flow chart
  • 3. Programming language like c++ / java
  • 4. Combination of above methods.

17 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-18
SLIDE 18

18 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-19
SLIDE 19

Recursive Algorithm

  • An algorithm is said to be recursive if the same algorithm is

invoked in the body (direct recursive).

  • Algorithm A is said to be indirect recursive if it calls another

algorithm 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 is invoked in the body (direct recursive). Algorithm A is said to be indirect recursive if it calls another algorithm 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

19 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-20
SLIDE 20

Module 1 – Outline Introduction to Algorithms

  • 1. Introduction
  • 2. Performance Analysis
  • 3. Asymptotic Notations
  • 4. Mathematical analysis of Non-Recursive algorithms
  • 5. Mathematical analysis of Recursive algorithms
  • 6. Important Problem Types
  • 7. Fundamental Data Structures
  • 1. Introduction
  • 2. Performance Analysis
  • 3. Asymptotic Notations
  • 4. Mathematical analysis of Non-Recursive algorithms
  • 5. Mathematical analysis of Recursive algorithms
  • 6. Important Problem Types
  • 7. Fundamental Data Structures

20 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-21
SLIDE 21

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

21 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-22
SLIDE 22

Time Complexity

  • Execution time or run-time of the program

is refereed as its time complexity

  • This is the sum of the time taken to

execute all instructions in the program. But, We count only the number of steps in the program. Why? How to count?

Two ways

Execution time or run-time of the program is refereed as its time complexity This is the sum of the time taken to execute all instructions in the program.

  • But, We count only the number of steps in

the program. Why?

  • How to count?

– Two ways

22 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-23
SLIDE 23

Method-1

  • Introduce a count variable
  • Increment count for every operation

23 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-24
SLIDE 24

Method-2

  • Count the steps per execution for every

line of code

  • Note the frequency of execution of

every line and find the total steps.

24

Count the steps per execution for every line of code Note the frequency of execution of every line and find the total steps.

18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-25
SLIDE 25

Method-2

25 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-26
SLIDE 26

Trade-off

  • One has to make a compromise and to exchange

computing time for memory consumption or vice versa, depending on application.

26 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-27
SLIDE 27

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

27 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-28
SLIDE 28

Orders of Growth

  • Order of growth of an algorithm is a way of stating how execution

time 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.

28 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-29
SLIDE 29

29 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-30
SLIDE 30

Analysis Framework

  • Worst-Case
  • Best-Case
  • Average-Case Efficiencies

Worst-Case Best-Case Average-Case Efficiencies

30 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-31
SLIDE 31

Worst Case

  • Definition: The worst-case efficiency of an algorithm is its

efficiency for the worst-case input of size n, for which the algorithm runs the longest among all possible inputs of that size. Definition: The worst-case efficiency of an algorithm is its efficiency for the worst-case input of size n, for which the algorithm runs the longest among all possible inputs of that size. Cworst(n) = n.

31 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-32
SLIDE 32

Best Case

  • Definition: The best-case efficiency of an algorithm is its

efficiency for the best-case input of size n, for which the algorithm runs the fastest among all possible inputs of that size. Definition: The best-case efficiency of an algorithm is its efficiency for the best-case input of size n, for which the algorithm runs the fastest among all possible inputs of that size. Cbest(n) = 1.

32 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-33
SLIDE 33

Average Case

  • Definition: the average-case complexity of an algorithm is the

amount of time used by the algorithm, averaged over all possible inputs.

33 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-34
SLIDE 34

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 for

inputs 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 the algorithm's running time (or extra memory units consumed) as its input size goes to infinity. 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 for

inputs 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 the

algorithm's running time (or extra memory units consumed) as its input size goes to infinity.

34 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-35
SLIDE 35

Module 1 – Outline Introduction to Algorithms

  • 1. Introduction
  • 2. Performance Analysis
  • 3. Asymptotic Notations
  • 4. Mathematical analysis of Non-Recursive algorithms
  • 5. Mathematical analysis of Recursive algorithms
  • 6. Important Problem Types
  • 7. Fundamental Data Structures
  • 1. Introduction
  • 2. Performance Analysis
  • 3. Asymptotic Notations
  • 4. Mathematical analysis of Non-Recursive algorithms
  • 5. Mathematical analysis of Recursive algorithms
  • 6. Important Problem Types
  • 7. Fundamental Data Structures

35 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-36
SLIDE 36

Asymptotic Notations

  • To compare orders of growth, computer scientists

use three notations:

– O(big oh), – Ω(big omega), – Θ (big theta) and

  • (little oh)

To compare orders of growth, computer scientists use three notations:

O(big oh), Ω(big omega), Θ (big theta) and – o(little oh)

36 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-37
SLIDE 37

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 that t(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 that t(n) ≤ c g(n) for all n ≥ n0.

37 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-38
SLIDE 38

38 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-39
SLIDE 39

39 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-40
SLIDE 40

Problems on Big O

Harivinod N 18CS42-Design and Analysis of Algorithms Feb-May 2020 40

slide-41
SLIDE 41

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 that t(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 that t(n) ≥ c g(n) for all n ≥ n0.

41 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-42
SLIDE 42

Big Omega t(n)= Ω(g(n))

42 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-43
SLIDE 43

43 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-44
SLIDE 44

44 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-45
SLIDE 45

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 some positive constant multiples of g(n) for all large n, i.e., if there exist some positive constants c1 and c2 and some 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 some positive constant multiples of g(n) for all large n, i.e., if there exist some positive constants c1 and c2 and some nonnegative integer n0 such that c2 g(n) ≤ t(n) ≤ c1g(n) for all n ≥ n0.

45 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-46
SLIDE 46

46 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-47
SLIDE 47

Graphical representation

  • Which one best to represent order of growth

Harivinod N 18CS42-Design and Analysis of Algorithms Feb-May 2020 47

slide-48
SLIDE 48

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

slide-49
SLIDE 49

49 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-50
SLIDE 50

50 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-51
SLIDE 51

Problem-3

Harivinod N 18CS42-Design and Analysis of Algorithms Feb-May 2020 51

slide-52
SLIDE 52

52 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-53
SLIDE 53

53 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-54
SLIDE 54

Little Oh

  • The function f(n) = o(g(n)) [ i.e f of n is a little oh of g
  • f n ] if and only if

54 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-55
SLIDE 55
  • For comparing the order of growth limit is used

Little Oh

55 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-56
SLIDE 56

56 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-57
SLIDE 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: 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 and some 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 and some 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

slide-58
SLIDE 58

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 c and 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 c and 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

slide-59
SLIDE 59

Class Name Comments

Basic Efficiency classes

59 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-60
SLIDE 60

Class Name Comments

60 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-61
SLIDE 61

Quiz: Which kind of growth best characterizes each of these functions?

61 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-62
SLIDE 62

Module 1 – Outline Introduction to Algorithms

  • 1. Introduction
  • 2. Performance Analysis
  • 3. Asymptotic Notations
  • 4. Mathematical analysis of Non-Recursive algorithms
  • 5. Mathematical analysis of Recursive algorithms
  • 6. Important Problem Types
  • 7. Fundamental Data Structures
  • 1. Introduction
  • 2. Performance Analysis
  • 3. Asymptotic Notations
  • 4. Mathematical analysis of Non-Recursive algorithms
  • 5. Mathematical analysis of Recursive algorithms
  • 6. Important Problem Types
  • 7. Fundamental Data Structures

62 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-63
SLIDE 63

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 efficiencies have to be investigated separately. 4. Set up a sum expressing the number of times the algorithm’s basic operation is executed. 5. Using standard formulas and rules of sum manipulation, either find a closed form formula for the count or, at the very 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 efficiencies have to be investigated separately. 4. Set up a sum expressing the number of times the algorithm’s basic operation is executed. 5. Using standard formulas and rules of sum manipulation, either find a closed form formula for the count or, at the very least, establish its order of growth.

63 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-64
SLIDE 64

Best, Worst, Average case exist?

64 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-65
SLIDE 65

Best, Worst, Average case exist?

65 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-66
SLIDE 66

Best, Worst, Average case exist?

66 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-67
SLIDE 67

Analysis

Best, Worst, Average case exist?

Harivinod N 18CS42-Design and Analysis of Algorithms Feb-May 2020 67

slide-68
SLIDE 68

Best, Worst, Average case exist?

The basic operation is count=count + 1 repeats number of times

68 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-69
SLIDE 69

Module 1 – Outline Introduction to Algorithms

  • 1. Introduction
  • 2. Performance Analysis
  • 3. Asymptotic Notations
  • 4. Mathematical analysis of Non-Recursive algorithms
  • 5. Mathematical analysis of Recursive algorithms
  • 6. Important Problem Types
  • 7. Fundamental Data Structures
  • 1. Introduction
  • 2. Performance Analysis
  • 3. Asymptotic Notations
  • 4. Mathematical analysis of Non-Recursive algorithms
  • 5. Mathematical analysis of Recursive algorithms
  • 6. Important Problem Types
  • 7. Fundamental Data Structures

69 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-70
SLIDE 70

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-case efficiencies must be investigated separately.

  • 4. Set up a recurrence relation, with an appropriate initial

condition, for the number of times the basic operation is executed. 5. Solve the recurrence or, at least, ascertain the order of growth 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-case efficiencies must be investigated separately.

  • 4. Set up a recurrence relation, with an appropriate initial

condition, for the number of times the basic operation is executed. 5. Solve the recurrence or, at least, ascertain the order of growth of its solution.

70 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-71
SLIDE 71

71 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-72
SLIDE 72
  • We can use backward substitutions method to solve

this

72 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-73
SLIDE 73

Tower of Hanoi puzzle.

  • In this puzzle, There are n disks of different sizes that can slide
  • nto any of three pegs.
  • Initially, all the disks are on the first peg in order of size, the

largest on the bottom and the smallest on top.

  • The goal is to move all the disks to the third peg, using the

second one as an auxiliary, if necessary. We can move only one disk at a time, and it is forbidden to place a larger disk on top of a smaller one. In this puzzle, There are n disks of different sizes that can slide

  • nto any of three pegs.

Initially, all the disks are on the first peg in order of size, the largest on the bottom and the smallest on top. The goal is to move all the disks to the third peg, using the second one as an auxiliary, if necessary.

  • We can move only one disk at a time, and it is forbidden to place

a larger disk on top of a smaller one.

73 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-74
SLIDE 74

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 source peg 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 source

peg to the destination peg.

74 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-75
SLIDE 75

75 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-76
SLIDE 76

Algorithm

TowerOfHanoi(n, source, dest, aux) If n == 1, then move disk from source to dest else TowerOfHanoi (n - 1, source, aux, dest) move disk from source to dest TowerOfHanoi (n - 1, aux, dest, source) End if TowerOfHanoi(n, source, dest, aux) If n == 1, then move disk from source to dest else TowerOfHanoi (n - 1, source, aux, dest) move disk from source to dest TowerOfHanoi (n - 1, aux, dest, source) End if

Harivinod N 18CS42-Design and Analysis of Algorithms Feb-May 2020 76

slide-77
SLIDE 77

Recurrence relation for total number of moves

The number of moves M(n) depends only on n. The recurrence equation is We have the following recurrence relation for the number of moves M(n): The number of moves M(n) depends only on n. The recurrence equation is We have the following recurrence relation for the number of moves M(n):

Harivinod N 18CS42-Design and Analysis of Algorithms Feb-May 2020 77

slide-78
SLIDE 78
  • We solve this recurrence by the same method of

backward substitutions:

  • The pattern of the first three sums on the left

suggests that the next one will be 24M(n − 4) + 23 + 22 + 2 + 1, and generally, after i substitutions, we get We solve this recurrence by the same method of backward substitutions:

  • The pattern of the first three sums on the left

suggests that the next one will be 24M(n − 4) + 23 + 22 + 2 + 1, and generally, after i substitutions, we get

78 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-79
SLIDE 79
  • Since the initial condition is specified for n = 1, which

is achieved for i = n - 1, we get the following formula for the solution to recurrence

79 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-80
SLIDE 80

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

slide-81
SLIDE 81

Recurrence relation for basic operation

Harivinod N 18CS42-Design and Analysis of Algorithms Feb-May 2020 81

slide-82
SLIDE 82

Module 1 – Outline Introduction to Algorithms

  • 1. Introduction
  • 2. Performance Analysis
  • 3. Asymptotic Notations
  • 4. Mathematical analysis of Non-Recursive algorithms
  • 5. Mathematical analysis of Recursive algorithms
  • 6. Important Problem Types
  • 7. Fundamental Data Structures
  • 1. Introduction
  • 2. Performance Analysis
  • 3. Asymptotic Notations
  • 4. Mathematical analysis of Non-Recursive algorithms
  • 5. Mathematical analysis of Recursive algorithms
  • 6. Important Problem Types
  • 7. Fundamental Data Structures

82 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-83
SLIDE 83

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 situations Two 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 situations Two 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

83 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-84
SLIDE 84

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

84 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-85
SLIDE 85

Module 1 – Outline Introduction to Algorithms

  • 1. Introduction
  • 2. Performance Analysis
  • 3. Asymptotic Notations
  • 4. Mathematical analysis of Non-Recursive algorithms
  • 5. Mathematical analysis of Recursive algorithms
  • 6. Important Problem Types
  • 7. Fundamental Data Structures
  • 1. Introduction
  • 2. Performance Analysis
  • 3. Asymptotic Notations
  • 4. Mathematical analysis of Non-Recursive algorithms
  • 5. Mathematical analysis of Recursive algorithms
  • 6. Important Problem Types
  • 7. Fundamental Data Structures

85 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-86
SLIDE 86

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

86 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-87
SLIDE 87

Fundamental Data structures

  • Graphs

– Undirected – Directed (Digraph) Weighted graph cycle

Graphs

Undirected Directed (Digraph) – Weighted graph – cycle

87 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-88
SLIDE 88

Fundamental Data structures

  • Graph Representations

– Adjacency matrix – Adjacency list

88 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-89
SLIDE 89

Fundamental Data structures

  • Graph Representations

– Weighted graph – cycle

89 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-90
SLIDE 90

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

90 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-91
SLIDE 91

Harivinod N 18CS42-Design and Analysis of Algorithms Feb-May 2020 91

slide-92
SLIDE 92

Fundamental Data structures

  • Trees, Forests

– Ordered tree

  • Binary tree
  • Binary search tree

Trees, Forests

Ordered tree Binary tree Binary search tree

92 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-93
SLIDE 93

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

93 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-94
SLIDE 94

Fundamental Data structures

  • Dictionaries

– Searching – Adding – Deleting

Dictionaries

Searching Adding Deleting

94 18CS42-Design and Analysis of Algorithms Feb-May 2020 Harivinod N

slide-95
SLIDE 95

Extra Byte-1.1: Min Distance

Consider the following algorithm for finding the distance between the two closest elements in an array of numbers. Make as many improvements as you can in this algorithmic solution to the problem. If you need to, you may change the algorithm altogether; if not, improve the implementation given.

Harivinod N 18CS42-Design and Analysis of Algorithms Feb-May 2020 95

slide-96
SLIDE 96

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

slide-97
SLIDE 97

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

slide-98
SLIDE 98

Assignment-1

Due: Within 5 days

Harivinod N 18CS42-Design and Analysis of Algorithms Feb-May 2020 98

slide-99
SLIDE 99
  • 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 and

give 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 and

give these efficiencies for sequential search. (8M)

Harivinod N 18CS42-Design and Analysis of Algorithms Feb-May 2020 99

slide-100
SLIDE 100

End of Module-1

Harivinod N 18CS42-Design and Analysis of Algorithms Feb-May 2020 10