Sunteți pe pagina 1din 145

CO-303

DESIGN AND ANALYSIS OF


ALGORITHMS
Introduction
• What is Algorithm?

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Introduction
• What is Algorithm?
– An algorithm is a finite set of instructions that, if followed,
accomplishes a particular task.
– Word defined by a Persian Mathematician , Abu Ja’far
Mohammed ibn Musa al Khowarizmi (825 A.D.)

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Introduction…
• Characteristics of an Algorithm

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Introduction…
• Characteristics of an Algorithm
– Input: ?
– Output: ?
– Definiteness: ?
– Finiteness: ?
– Effectiveness: ?

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Introduction…
• Characteristics of an Algorithm
– Input: zero or more inputs, taken from a specified set of objects
– Output: At least one quantity is produced relation to the inputs
– Definiteness: Each instruction must be precisely defined
– Finiteness: It terminates after a finite number of steps
– Effectiveness: All operations to be performed must be sufficiently
basic that they can be done exactly and in finite length.

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Problems vs Algorithms vs Programs

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Problems vs Algorithms vs Programs
– For each problem or class of problems, there may
be many different algorithms.
– For each algorithm, there may be many different
implementations (programs).

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Problems vs Algorithms vs Programs
– For each problem or class of problems, there may
be many different algorithms.
– For each algorithm, there may be many different
implementations (programs).

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Analysis of algorithms

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Analysis of algorithms
• Measuring efficiency of an algorithm
– Time : How long the algorithm takes (running time)
– Space : Memory requirement

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Time and space

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Time and space
• Time depends on processing speed
– Not possible to change for given hardware
• Space is a function of available memory
– Easier to reconfigure
• Typically, we will focus on time, not space

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Measuring running time

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Measuring running time
• Analysis independent of underlying hardware
– Don’t use actual time
– Measure in term of “Basic operations”

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Input size

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Input size
• Running time depends on input size
• Measure time efficiency as function of input size
– Input size 𝑛
– Running time 𝑡(𝑛)
– Typically 𝑡(𝑛) is worst case estimate

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Worst-case analysis

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Worst-case analysis
• Why do we usually focus on the worst case analysis?
– Being upper bound, the worst case guarantees that the algorithm will
not take any longer.
– Fairly occurs in many applications.
– Average case is often roughly as bad as the worst case.

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Example 1: Sorting

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Example 1: Sorting
• Sorting as array with 𝑛 elements

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Example 1: Sorting
• Sorting as array with 𝑛 elements
– Basic algorithms : time proportional to 𝑛2

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Example 1: Sorting
• Sorting as array with 𝑛 elements
– Basic algorithms : time proportional to 𝑛2
– Best algorithms : time proportional to 𝑛 log 𝑛

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Example 1: Sorting
• Sorting as array with 𝑛 elements
– Basic algorithms : time proportional to 𝑛2
– Best algorithms : time proportional to 𝑛 log 𝑛
– Typical CPUs process up to 108 operation per second ( for approximate
calculation)

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Example 1: Sorting
• Sorting as array with 𝑛 elements
– Basic algorithms : time proportional to 𝑛2
– Best algorithms : time proportional to 𝑛 log 𝑛
– Typical CPUs process up to 108 operation per second ( for approximate
calculation)
– Telephone directory for mobile phone users in India

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Example 1: Sorting
• Sorting as array with 𝑛 elements
– Basic algorithms : time proportional to 𝑛2
– Best algorithms : time proportional to 𝑛 log 𝑛
– Typical CPUs process up to 108 operation per second ( for approximate
calculation)
– Telephone directory for mobile phone users in India
– India has about 1 billion = 109 phones

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Example 1: Sorting
• Sorting as array with 𝑛 elements
– Basic algorithms : time proportional to 𝑛2
– Best algorithms : time proportional to 𝑛 log 𝑛
– Typical CPUs process up to 108 operation per second ( for approximate
calculation)
– Telephone directory for mobile phone users in India
– India has about 1 billion = 109 phones
– Basic 𝑛2 algorithm requires 1018 operations

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Example 1: Sorting
• Sorting as array with 𝑛 elements
– Basic algorithms : time proportional to 𝑛2
– Best algorithms : time proportional to 𝑛 log 𝑛
– Typical CPUs process up to 108 operation per second ( for approximate
calculation)
– Telephone directory for mobile phone users in India
– India has about 1 billion = 109 phones
– Basic 𝑛2 algorithm requires 1018 operations
– 108 operation per second => 1010 seconds

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Example 1: Sorting
• Sorting as array with 𝑛 elements
– Basic algorithms : time proportional to 𝑛2
– Best algorithms : time proportional to 𝑛 log 𝑛
– Typical CPUs process up to 108 operation per second ( for approximate
calculation)
– Telephone directory for mobile phone users in India
– India has about 1 billion = 109 phones
– Basic 𝑛2 algorithm requires 1018 operations
– 108 operation per second => 1010 seconds
– 2778000 hours

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Example 1: Sorting
• Sorting as array with 𝑛 elements
– Basic algorithms : time proportional to 𝑛2
– Best algorithms : time proportional to 𝑛 log 𝑛
– Typical CPUs process up to 108 operation per second ( for approximate
calculation)
– Telephone directory for mobile phone users in India
– India has about 1 billion = 109 phones
– Basic 𝑛2 algorithm requires 1018 operations
– 108 operation per second => 1010 seconds
– 2778000 hours
– 115700 days

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Example 1: Sorting
• Sorting as array with 𝑛 elements
– Basic algorithms : time proportional to 𝑛2
– Best algorithms : time proportional to 𝑛 log 𝑛
– Typical CPUs process up to 108 operation per second ( for approximate
calculation)
– Telephone directory for mobile phone users in India
– India has about 1 billion = 109 phones
– Basic 𝑛2 algorithm requires 1018 operations
– 108 operation per second => 1010 seconds
– 2778000 hours
– 115700 days
– 300 years!!!

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Example 1: Sorting
• Sorting as array with 𝑛 elements
– Basic algorithms : time proportional to 𝑛2
– Best algorithms : time proportional to 𝑛 log 𝑛
– Typical CPUs process up to 108 operation per second ( for approximate
calculation)
– Telephone directory for mobile phone users in India
– India has about 1 billion = 109 phones • Best 𝑛 log 𝑛 algorithm
– Basic 𝑛2 algorithm requires 1018 operations takes only about
– 108 operation per second => 1010 seconds 3 × 1010 operations
– 2778000 hours
– 115700 days
– 300 years!!!

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Example 1: Sorting
• Sorting as array with 𝑛 elements
– Basic algorithms : time proportional to 𝑛2
– Best algorithms : time proportional to 𝑛 log 𝑛
– Typical CPUs process up to 108 operation per second ( for approximate
calculation)
– Telephone directory for mobile phone users in India
– India has about 1 billion = 109 phones • Best 𝑛 log 𝑛 algorithm
– Basic 𝑛2 algorithm requires 1018 operations takes only about
– 108 operation per second => 1010 seconds 3 × 1010 operations
– 2778000 hours • About 300 seconds
– 115700 days
– 300 years!!!

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Example 1: Sorting
• Sorting as array with 𝑛 elements
– Basic algorithms : time proportional to 𝑛2
– Best algorithms : time proportional to 𝑛 log 𝑛
– Typical CPUs process up to 108 operation per second ( for approximate
calculation)
– Telephone directory for mobile phone users in India
– India has about 1 billion = 109 phones • Best 𝑛 log 𝑛 algorithm
– Basic 𝑛2 algorithm requires 1018 operations takes only about
– 108 operation per second => 1010 seconds 3 × 1010 operations
– 2778000 hours • About 300 seconds
– 115700 days • About 5 minutes
– 300 years!!!

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Typical functions

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Problem #1
• Problem: print “𝐻𝑒𝑙𝑙𝑜 𝑁𝐼𝑇 𝑆𝑢𝑟𝑎𝑡” for 𝑛 times

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Problem #1
• Problem: print “𝐻𝑒𝑙𝑙𝑜 𝑁𝐼𝑇 𝑆𝑢𝑟𝑎𝑡” for 𝑛 times
• Algorithm: Print
------
------
------

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Problem #1
• Problem: print “𝐻𝑒𝑙𝑙𝑜 𝑁𝐼𝑇 𝑆𝑢𝑟𝑎𝑡” for 𝑛 times
• Algorithm: Print
------
------
------
• What could be the running time of the
solution?

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Analyzing Problem #1
• Prepare a table as shown below
Statement cost times_executed
1
2
3
4
5

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Analyzing Problem #1
• Problem: print “𝐻𝑒𝑙𝑙𝑜 𝑁𝐼𝑇 𝑆𝑢𝑟𝑎𝑡” for 𝑛 times
• Algorithm/ pseudo code: Print (𝑛)
1. 𝑖 = 1
2. While 𝑖 ≤ 𝑛
3. Print “𝐻𝑒𝑙𝑙𝑜 𝑁𝐼𝑇 𝑆𝑢𝑟𝑎𝑡”
4. 𝑖 = 𝑖 + 1
5. Exit

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Analyzing Problem #1
• Problem: print “𝐻𝑒𝑙𝑙𝑜 𝑁𝐼𝑇 𝑆𝑢𝑟𝑎𝑡” for 𝑛 times
• Algorithm/ pseudo code: Print (𝑛)
1. 𝑖 = 1………………………………………….𝐶1
2. While 𝑖 ≤ 𝑛…………………………….….𝐶2
3. Print “𝐻𝑒𝑙𝑙𝑜 𝑁𝐼𝑇 𝑆𝑢𝑟𝑎𝑡”.………𝐶3
4. 𝑖 = 𝑖 + 1…………………………….…𝐶4
5. Exit………………………………………….….𝐶5

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Analyzing Problem #1
• Problem: print “𝐻𝑒𝑙𝑙𝑜 𝑁𝐼𝑇 𝑆𝑢𝑟𝑎𝑡” for 𝑛 times
• Algorithm/ pseudo code: Print (𝑛)
1. 𝑖 = 1………………………………………….𝐶1
2. While 𝑖 ≤ 𝑛………………………………..𝐶2
3. Print “𝐻𝑒𝑙𝑙𝑜 𝑁𝐼𝑇 𝑆𝑢𝑟𝑎𝑡”.………𝐶3
4. 𝑖 = 𝑖 + 1……………………………….𝐶4
5. Exit……………………………………………..𝐶5
Total steps = Total time = 𝑪𝟏 + 𝒏 + 𝟏 𝑪𝟐 + 𝒏𝑪𝟑 + 𝒏𝑪𝟒 + 𝑪𝟓

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Problem #2
• Problem: To determine the largest of 𝑛 nos.

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Analyzing Problem #2
• Problem: To determine the largest of 𝒏 nos.
• Running time if the input is strictly ascending/strictly descending ?
Algorithm Largest1 𝑥𝑖 , 𝑛 Algorithm Largest2 𝑥𝑖 , 𝑛
1 Let 𝑚𝑎𝑥 = 𝑥1 1 For 𝑖 = 1 to (𝑛 − 1)
2 For 𝑖 = 2 to 𝑛 2 Do if 𝑥𝑖 > 𝑥𝑖+1
3 Do if 𝑥𝑖 > 𝑚𝑎𝑥 3 Then swap < 𝑥𝑖 , 𝑥𝑖+1 >
4 Then 𝑚𝑎𝑥 = 𝑥𝑖 4 Print 𝑥𝑛
5 Print 𝑚𝑎𝑥

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Analyzing Problem #2
• Problem: To determine the largest of 𝒏 nos.
• Running time if the input is strictly ascending/strictly descending ?
Algorithm Largest1 𝑥𝑖 , 𝑛 Algorithm Largest2 𝑥𝑖 , 𝑛
1 Let 𝑚𝑎𝑥 = 𝑥1 …………….…𝐶1 1 For 𝑖 = 1 to (𝑛 − 1) ………….……𝐶1
2 For 𝑖 = 2 to 𝑛……………..…𝐶2 2 Do if 𝑥𝑖 > 𝑥𝑖+1 ……………..…….…𝐶2
3 Do if 𝑥𝑖 > 𝑚𝑎𝑥…………..…𝐶3 3 Then swap < 𝑥𝑖 , 𝑥𝑖+1 >…𝐶3
4 Then 𝑚𝑎𝑥 = 𝑥𝑖 …...𝐶4 4 Print 𝑥𝑛 ………………………………..…𝐶4
5 Print 𝑚𝑎𝑥………………….....𝐶5

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Analyzing Problem #2
• Problem: To determine the largest of 𝒏 nos.
• Running time if the input is strictly ascending/strictly descending ?
Algorithm Largest1 𝑥𝑖 , 𝑛 Algorithm Largest2 𝑥𝑖 , 𝑛
1 Let 𝑚𝑎𝑥 = 𝑥1 …………….…𝐶1 1 For 𝑖 = 1 to (𝑛 − 1) ………….……𝐶1
2 For 𝑖 = 2 to 𝑛……………..…𝐶2 2 Do if 𝑥𝑖 > 𝑥𝑖+1 ……………..…….…𝐶2
3 Do if 𝑥𝑖 > 𝑚𝑎𝑥…………..…𝐶3 3 Then swap < 𝑥𝑖 , 𝑥𝑖+1 >…𝐶3
4 Then 𝑚𝑎𝑥 = 𝑥𝑖 …...𝐶4 4 Print 𝑥𝑛 ………………………………..…𝐶4
5 Print 𝑚𝑎𝑥………………….....𝐶5

Total steps = Total time = 𝐶1 + 𝑛𝐶2 + Total steps = Total time = 𝑛𝐶1 +
𝑛 − 1 𝐶3 + 𝑛 − 1 𝐶4 + 𝐶5 (𝑛 − 1)𝐶2 + 𝑛 − 1 𝐶3 + 𝐶4

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Problem #3
• Consider the code snippet
1 For 𝑖 = 1 to 𝑛
2 For 𝑗 = 1 to 𝑛
3 Print "𝐷𝐴𝐴 2019"

• What is the cost of execution?

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Analyzing Problem #3
• Consider the code snippet
1 For 𝑖 = 1 to 𝑛………………………….…………..𝐶1
2 For 𝑗 = 1 to 𝑛………………….………….𝐶2
3 Print "𝐷𝐴𝐴 2019"….……….….𝐶3

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Analyzing Problem #3
• Consider the code snippet
1 For 𝑖 = 1 to 𝑛………………………….…………..𝐶1
2 For 𝑗 = 1 to 𝑛………………….………….𝐶2
3 Print "𝐷𝐴𝐴 2019"….……….….𝐶3

Total time = 𝑪𝟏 𝒏 + 𝟏 + 𝑪𝟐 𝒏 𝒏 + 𝟏 + 𝑪𝟑 𝒏𝟐

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Analyzing Problem #3
• Consider the code snippet
1 For 𝑖 = 1 to 𝑛………………………….…………..𝐶1
2 For 𝑗 = 1 to 𝑛………………….………….𝐶2
3 Print "𝐷𝐴𝐴 2019"….……….….𝐶3

Total time = 𝑪𝟏 𝒏 + 𝟏 + 𝑪𝟐 𝒏 𝒏 + 𝟏 + 𝑪𝟑 𝒏𝟐
= 𝑪𝟏 𝒏 + 𝟏 + 𝑪𝟐 (𝒏𝟐 +𝒏) + 𝑪𝟑 𝒏𝟐

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Problem #4
• Consider the code snippet
1 For 𝑖 = 1 to 𝑛
2 For 𝑗 = 1 to 𝑖
3 Print "𝐷𝐴𝐴 2019"

• What is the cost of execution?

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Analyzing Problem #4
• Consider the code snippet
1 For 𝑖 = 1 to 𝑛………………………….…………..𝐶1
2 For 𝑗 = 1 to 𝑖………………….………….𝐶2
3 Print "𝐷𝐴𝐴 2019"….……….….𝐶3

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Analyzing Problem #4
• Consider the code snippet
1 For 𝑖 = 1 to 𝑛………………………….…………..𝐶1
2 For 𝑗 = 1 to 𝑖………………….………….𝐶2
3 Print "𝐷𝐴𝐴 2019"….……….….𝐶3

(𝒏+𝟏) 𝒏+𝟐 𝒏 𝒏+𝟏


Total time = 𝑪𝟏 𝒏 + 𝟏 + 𝑪𝟐 − 𝟏 + 𝑪𝟑
𝟐 𝟐

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Analyzing Problem #4
• Consider the code snippet
1 For 𝑖 = 1 to 𝑛………………………….…………..𝐶1
2 For 𝑗 = 1 to 𝑖………………….………….𝐶2
3 Print "𝐷𝐴𝐴 2019"….……….….𝐶3

(𝒏+𝟏) 𝒏+𝟐 𝒏 𝒏+𝟏


Total time = 𝑪𝟏 𝒏 + 𝟏 + 𝑪𝟐 − 𝟏 + 𝑪𝟑
𝟐 𝟐
𝒏𝟐 +𝟑𝒏 𝒏𝟐 +𝒏
=𝑪𝟏 𝒏 + 𝟏 + 𝑪𝟐 + 𝑪𝟑
𝟐 𝟐

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Problem #5
• Consider the code snippet
1 For 𝑖 = 1 to 𝑛
2 For 𝑗 = 𝑖 to 𝑛
3 Print "𝐷𝐴𝐴 2019"

• What is the cost of execution?

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Analyzing Problem #5
• Consider the code snippet
1 For 𝑖 = 1 to 𝑛………………………….…………..𝐶1
2 For 𝑗 = 𝑖 to 𝑛………………….………….𝐶2
3 Print "𝐷𝐴𝐴 2019"….……….….𝐶3

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Analyzing Problem #5
• Consider the code snippet
1 For 𝑖 = 1 to 𝑛………………………….…………..𝐶1
2 For 𝑗 = 𝑖 to 𝑛………………….………….𝐶2
3 Print "𝐷𝐴𝐴 2019"….……….….𝐶3

(𝒏+𝟏) 𝒏+𝟐 𝒏 𝒏+𝟏


Total time = 𝑪𝟏 𝒏 + 𝟏 + 𝑪𝟐 − 𝟏 + 𝑪𝟑
𝟐 𝟐

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Analyzing Problem #5
• Consider the code snippet
1 For 𝑖 = 1 to 𝑛………………………….…………..𝐶1
2 For 𝑗 = 𝑖 to 𝑛………………….………….𝐶2
3 Print "𝐷𝐴𝐴 2019"….……….….𝐶3

(𝒏+𝟏) 𝒏+𝟐 𝒏 𝒏+𝟏


Total time = 𝑪𝟏 𝒏 + 𝟏 + 𝑪𝟐 − 𝟏 + 𝑪𝟑
𝟐 𝟐
𝒏𝟐 +𝟑𝒏 𝒏𝟐 +𝒏
=𝑪𝟏 𝒏 + 𝟏 + 𝑪𝟐 + 𝑪𝟑
𝟐 𝟐

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Problem #6
• Problem: Insertion sort

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Analyzing Problem #6
• Algorithm Insertion-Sort 𝐴[], 𝑛
1 For 𝑗 = 2 to 𝑛
2 Do 𝑘𝑒𝑦 = 𝐴[𝑗]
3 𝑖 =𝑗−1
4 While (𝑖 > 0) and (𝐴 𝑖 > 𝑘𝑒𝑦)
5 Do 𝐴 𝑖 + 1 = 𝐴[𝑖]
6 𝑖 =𝑖−1
7 𝐴 𝑖 + 1 = 𝑘𝑒𝑦

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Analyzing Problem #6
• Algorithm Insertion-Sort 𝐴[], 𝑛
1 For 𝑗 = 2 to 𝑛
2 Do 𝑘𝑒𝑦 = 𝐴[𝑗]
3 𝑖 =𝑗−1
4 While (𝑖 > 0) and (𝐴 𝑖 > 𝑘𝑒𝑦)
5 Do 𝐴 𝑖 + 1 = 𝐴[𝑖]
6 𝑖 =𝑖−1
7 𝐴 𝑖 + 1 = 𝑘𝑒𝑦

• How do we analyze the time complexity?

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Analyzing Problem #6
• Algorithm Insertion-Sort 𝐴[], 𝑛
1 For 𝑗 = 2 to 𝑛
2 Do 𝑘𝑒𝑦 = 𝐴[𝑗]
3 𝑖 =𝑗−1
4 While (𝑖 > 0) and (𝐴 𝑖 > 𝑘𝑒𝑦)
5 Do 𝐴 𝑖 + 1 = 𝐴[𝑖]
6 𝑖 =𝑖−1
7 𝐴 𝑖 + 1 = 𝑘𝑒𝑦

• How do we analyze the time complexity?


• We need to analyze how many times the while loop is executed?

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Analyzing Problem #6
• Algorithm Insertion-Sort 𝐴[], 𝑛
1 For 𝑗 = 2 to 𝑛
2 Do 𝑘𝑒𝑦 = 𝐴[𝑗]
3 𝑖 =𝑗−1
4 While (𝑖 > 0) and (𝐴 𝑖 > 𝑘𝑒𝑦)
5 Do 𝐴 𝑖 + 1 = 𝐴[𝑖]
6 𝑖 =𝑖−1
7 𝐴 𝑖 + 1 = 𝑘𝑒𝑦

• How do we analyze the time complexity?


• We need to analyze how many times the while loop is executed?
– Assume while loop is executed 𝒕𝒋 times…

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Analyzing Problem #6
• Then the running time is given by the expression

1 For 𝑗 = 2 to 𝑛
2 Do 𝑘𝑒𝑦 = 𝐴[𝑗]
3 𝑖 = 𝑗−1
4 While (𝑖 > 0) and (𝐴 𝑖 > 𝑘𝑒𝑦)
5 Do 𝐴 𝑖 + 1 = 𝐴[𝑖]
6 𝑖 = 𝑖−1
7 𝐴 𝑖 + 1 = 𝑘𝑒𝑦

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Analyzing Problem #6
• Then the running time is given by the expression

𝑛 𝑛 𝑛

𝐶1 𝑛 + 𝐶2 𝑛 − 1 + 𝐶3 𝑛 − 1 + 𝐶4 𝑡𝑗 + 𝐶5 (𝑡𝑗 −1) + 𝐶6 (𝑡𝑗 −1) + 𝐶7 (𝑛 − 1)


𝑗=2 𝑗=2 𝑗=2

1 For 𝑗 = 2 to 𝑛
2 Do 𝑘𝑒𝑦 = 𝐴[𝑗]
3 𝑖 = 𝑗−1
4 While (𝑖 > 0) and (𝐴 𝑖 > 𝑘𝑒𝑦)
5 Do 𝐴 𝑖 + 1 = 𝐴[𝑖]
6 𝑖 = 𝑖−1
7 𝐴 𝑖 + 1 = 𝑘𝑒𝑦

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Analyzing Problem #6
• When does the best case occur?

1 For 𝑗 = 2 to 𝑛
2 Do 𝑘𝑒𝑦 = 𝐴[𝑗]
3 𝑖 = 𝑗−1
4 While (𝑖 > 0) and (𝐴 𝑖 > 𝑘𝑒𝑦)
5 Do 𝐴 𝑖 + 1 = 𝐴[𝑖]
6 𝑖 = 𝑖−1
7 𝐴 𝑖 + 1 = 𝑘𝑒𝑦

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Analyzing Problem #6
• When does the best case occur?
• 𝑡𝑗 = 1 in every case
• i.e. when the array is sorted

1 For 𝑗 = 2 to 𝑛
2 Do 𝑘𝑒𝑦 = 𝐴[𝑗]
3 𝑖 = 𝑗−1
4 While (𝑖 > 0) and (𝐴 𝑖 > 𝑘𝑒𝑦)
5 Do 𝐴 𝑖 + 1 = 𝐴[𝑖]
6 𝑖 = 𝑖−1
7 𝐴 𝑖 + 1 = 𝑘𝑒𝑦

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Analyzing Problem #6
• When does the best case occur?
• 𝑡𝑗 = 1 in every case
• i.e. when the array is sorted
• Then the best case running time is
𝑛 𝑛 𝑛
• 𝐶1 𝑛 + 𝐶2 𝑛 − 1 + 𝐶3 𝑛 − 1 + 𝐶4 𝑗=2 1 + 𝐶5 𝑗=2 0 + 𝐶6 𝑗=2 0 + 𝐶7 (𝑛 − 1)

1 For 𝑗 = 2 to 𝑛
2 Do 𝑘𝑒𝑦 = 𝐴[𝑗]
3 𝑖 = 𝑗−1
4 While (𝑖 > 0) and (𝐴 𝑖 > 𝑘𝑒𝑦)
5 Do 𝐴 𝑖 + 1 = 𝐴[𝑖]
6 𝑖 = 𝑖−1
7 𝐴 𝑖 + 1 = 𝑘𝑒𝑦

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Analyzing Problem #6
• When does the best case occur?
• 𝑡𝑗 = 1 in every case
• i.e. when the array is sorted
• Then the best case running time is
𝑛 𝑛 𝑛

𝐶1 𝑛 + 𝐶2 𝑛 − 1 + 𝐶3 𝑛 − 1 + 𝐶4 1 + 𝐶5 0 + 𝐶6 0 + 𝐶7 𝑛 − 1
𝑗=2 𝑗=2 𝑗=2

= 𝐶1 𝑛 + 𝐶2 𝑛 − 1 + 𝐶3 𝑛 − 1 + 𝐶4 𝑛 − 1 + 𝐶7 𝑛 − 1

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Analyzing Problem #6
• When does the worst case occur?

1 For 𝑗 = 2 to 𝑛
2 Do 𝑘𝑒𝑦 = 𝐴[𝑗]
3 𝑖 = 𝑗−1
4 While (𝑖 > 0) and (𝐴 𝑗 > 𝑘𝑒𝑦)
5 Do 𝐴 𝑖 + 1 = 𝐴[𝑖]
6 𝑖 = 𝑖−1
7 𝐴 𝑖 + 1 = 𝑘𝑒𝑦

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Analyzing Problem #6
• When does the worst case occur?
• At least when the while loop is executed for all the values of 𝑖……
• i.e. when the array is reverse sorted

1 For 𝑗 = 2 to 𝑛
2 Do 𝑘𝑒𝑦 = 𝐴[𝑗]
3 𝑖 = 𝑗−1
4 While (𝑖 > 0) and (𝐴 𝑗 > 𝑘𝑒𝑦)
5 Do 𝐴 𝑖 + 1 = 𝐴[𝑖]
6 𝑖 = 𝑖−1
7 𝐴 𝑖 + 1 = 𝑘𝑒𝑦

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Analyzing Problem #6
• When does the worst case occur?
• At least when the while loop is executed for all the values of 𝑖……
• i.e. when the array is reverse sorted
• Thus, 𝑡𝑗 = 𝑗.

1 For 𝑗 = 2 to 𝑛
2 Do 𝑘𝑒𝑦 = 𝐴[𝑗]
3 𝑖 = 𝑗−1
4 While (𝑖 > 0) and (𝐴 𝑗 > 𝑘𝑒𝑦)
5 Do 𝐴 𝑖 + 1 = 𝐴[𝑖]
6 𝑖 = 𝑖−1
7 𝐴 𝑖 + 1 = 𝑘𝑒𝑦

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Analyzing Problem #6
• When does the worst case occur?
• At least when the while loop is executed for all the values of 𝑖……
• i.e. when the array is reverse sorted
• Thus, 𝑡𝑗 = 𝑗. Therefore the expression is
𝑛 𝑛 𝑛
• 𝐶1 𝑛 + 𝐶2 𝑛 − 1 + 𝐶3 𝑛 − 1 + 𝐶4 𝑗=2 𝑗 + 𝐶5 𝑗=2(𝑗 − 1) + 𝐶6 𝑗=2(𝑗 − 1) +
𝐶7 (𝑛 − 1)

1 For 𝑗 = 2 to 𝑛
2 Do 𝑘𝑒𝑦 = 𝐴[𝑗]
3 𝑖 = 𝑗−1
4 While (𝑖 > 0) and (𝐴 𝑗 > 𝑘𝑒𝑦)
5 Do 𝐴 𝑖 + 1 = 𝐴[𝑖]
6 𝑖 = 𝑖−1
7 𝐴 𝑖 + 1 = 𝑘𝑒𝑦

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Analyzing Problem #6
• When does the worst case occur?
• At least when the while loop is executed for all the values of 𝑖……
• i.e. when the array is reverse sorted
• Thus, 𝑡𝑗 = 𝑗. Therefore the expression is
𝑛 𝑛 𝑛

𝐶1 𝑛 + 𝐶2 𝑛 − 1 + 𝐶3 𝑛 − 1 + 𝐶4 𝑗 + 𝐶5 (𝑗 − 1) + 𝐶6 (𝑗 − 1) + 𝐶7 (𝑛 − 1)
𝑗=2 𝑗=2 𝑗=2

𝑛 𝑛+1 𝑛 𝑛−1 𝑛 𝑛−1


= 𝐶1 𝑛 + 𝐶2 𝑛 − 1 + 𝐶3 𝑛 − 1 + 𝐶4 − 1 + 𝐶5 + 𝐶6
2 2 2
+ 𝐶7 (𝑛 − 1)

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Analyzing Problem #6
• When does the worst case occur?
• At least when the while loop is executed for all the values of 𝑖……
• i.e. when the array is reverse sorted
• Thus, 𝑡𝑗 = 𝑗. Therefore the expression is
𝑛 𝑛 𝑛

𝐶1 𝑛 + 𝐶2 𝑛 − 1 + 𝐶3 𝑛 − 1 + 𝐶4 𝑗 + 𝐶5 (𝑗 − 1) + 𝐶6 (𝑗 − 1) + 𝐶7 (𝑛 − 1)
𝑗=2 𝑗=2 𝑗=2

𝑛 𝑛+1 𝑛 𝑛−1 𝑛 𝑛−1


= 𝐶1 𝑛 + 𝐶2 𝑛 − 1 + 𝐶3 𝑛 − 1 + 𝐶4 − 1 + 𝐶5 + 𝐶6
2 2 2
+ 𝐶7 (𝑛 − 1)

𝑛2 + 𝑛 − 2 𝑛2 − 𝑛 𝑛2 − 𝑛
= 𝐶1 𝑛 + 𝐶2 𝑛 − 1 + 𝐶3 𝑛 − 1 + 𝐶4 + 𝐶5 + 𝐶6
2 2 2
+ 𝐶7 (𝑛 − 1)

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Growth of functions
• Consider
• An algorithm A which for a problem, does 𝟐𝒏 basic operation & 𝟐𝑪𝟏 𝒏
total operations, while some other algorithm B does 𝟒. 𝟓𝒏 basic
operations & 𝟒. 𝟓𝑪𝟐 𝒏 total operations.

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Growth of functions
• Consider
• An algorithm A which for a problem, does 𝟐𝒏 basic operation & 𝟐𝑪𝟏 𝒏
total operations, while some other algorithm B does 𝟒. 𝟓𝒏 basic
operations & 𝟒. 𝟓𝑪𝟐 𝒏 total operations.
• Consider constant of proportionality representing overhead operations.

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Growth of functions
• Consider
• An algorithm A which for a problem, does 𝟐𝒏 basic operation & 𝟐𝑪𝟏 𝒏
total operations, while some other algorithm B does 𝟒. 𝟓𝒏 basic
operations & 𝟒. 𝟓𝑪𝟐 𝒏 total operations.
• Consider constant of proportionality representing overhead operations.

• Which algorithm of the two do you think is better?

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Growth of functions …
𝒏 𝟐𝒏 𝟒. 𝟓𝒏
5 10 22
10 20 45
100 200 450
1000 2000 4500
10000 20000 45000
100000 2.0 ∗ 105 4.5 ∗ 105
1000000 = 106 2.0 ∗ 106 4.5 ∗ 106

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Growth of functions
• Consider
𝒏𝟑
• Another such example with algo1 taking multiplicative steps while
𝟐
algo2 taking 𝟓𝒏𝟐 steps.

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Growth of functions
• Consider
𝒏𝟑
• Another such example with algo1 taking multiplicative steps while
𝟐
algo2 taking 𝟓𝒏𝟐 steps.
• Consider constant of proportionality representing overhead operations.

• Which algorithm of the two do you think is better?

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Growth of functions …
𝒏 𝟐𝒏 𝟒. 𝟓𝒏 𝒏𝟑 /𝟐 𝟓𝒏𝟐
5 10 22 62 125
10 20 45 500 500
100 200 450 5 ∗ 105 5 ∗ 104
1000 2000 4500 5 ∗ 108 5 ∗ 106
10000 20000 45000 5 ∗ 1011 5 ∗ 108
100000 2.0 ∗ 105 4.5 ∗ 105 5 ∗ 1014 5 ∗ 1010
1000000 2.0 ∗ 106 4.5 ∗ 106 5 ∗ 1017 5 ∗ 1012
= 106

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Growth of functions …
• A relook at costs of insertion sort with 𝑪′𝒊 𝒔 = 𝟏

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Growth of functions …
• A relook at costs of insertion sort with 𝑪′𝒊 𝒔 = 𝟏
• Best case
𝑇 𝑛 = 𝐶1 𝑛 + 𝐶2 𝑛 − 1 + 𝐶3 𝑛 − 1 + 𝐶4 𝑛 − 1 + 𝐶7 𝑛 − 1

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Growth of functions …
• A relook at costs of insertion sort with 𝑪′𝒊 𝒔 = 𝟏
• Best case
𝑇 𝑛 = 𝐶1 𝑛 + 𝐶2 𝑛 − 1 + 𝐶3 𝑛 − 1 + 𝐶4 𝑛 − 1 + 𝐶7 𝑛 − 1
= 5𝑛 − 4

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Growth of functions …
• A relook at costs of insertion sort with 𝑪′𝒊 𝒔 = 𝟏
• Best case
𝑇 𝑛 = 𝐶1 𝑛 + 𝐶2 𝑛 − 1 + 𝐶3 𝑛 − 1 + 𝐶4 𝑛 − 1 + 𝐶7 𝑛 − 1
= 5𝑛 − 4
• Worst Case
𝑛2 +𝑛−2 𝑛2 −𝑛
𝑇 𝑛 = 𝐶1 𝑛 + 𝐶2 𝑛 − 1 + 𝐶3 𝑛 − 1 + 𝐶4 + 𝐶5
2 2
𝑛2 − 𝑛
+𝐶6 + 𝐶7 (𝑛 − 1)
2

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Growth of functions …
• A relook at costs of insertion sort with 𝑪′𝒊 𝒔 = 𝟏
• Best case
𝑇 𝑛 = 𝐶1 𝑛 + 𝐶2 𝑛 − 1 + 𝐶3 𝑛 − 1 + 𝐶4 𝑛 − 1 + 𝐶7 𝑛 − 1
= 5𝑛 − 4
• Worst Case
𝑛2 +𝑛−2 𝑛2 −𝑛
𝑇 𝑛 = 𝐶1 𝑛 + 𝐶2 𝑛 − 1 + 𝐶3 𝑛 − 1 + 𝐶4 + 𝐶5
2 2
𝑛2 − 𝑛
+𝐶6 + 𝐶7 (𝑛 − 1)
2
1
= (3𝑛2 + 7𝑛 − 8)
2

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Growth of functions …
• A relook at costs of insertion sort with 𝑪′𝒊 𝒔 = 𝟏
• Best case
𝑇 𝑛 = 𝐶1 𝑛 + 𝐶2 𝑛 − 1 + 𝐶3 𝑛 − 1 + 𝐶4 𝑛 − 1 + 𝐶7 𝑛 − 1
= 5𝑛 − 4
• Worst Case
𝑛2 +𝑛−2 𝑛2 −𝑛
𝑇 𝑛 = 𝐶1 𝑛 + 𝐶2 𝑛 − 1 + 𝐶3 𝑛 − 1 + 𝐶4 + 𝐶5
2 2
𝑛2 − 𝑛
+𝐶6 + 𝐶7 (𝑛 − 1)
2
1
= (3𝑛2 + 7𝑛 − 8)
2
• Which term dominates the overall result in the above expression,
especially at large values of 𝑛 ?

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Growth of functions …
• Which term dominates the overall result in the above expression,
especially at large values of 𝑛 ?

𝒏 𝑻 𝒏 = 𝟑𝒏𝟐 + 𝟕𝒏 − 𝟖 𝑻 𝒏 = 𝟑𝒏𝟐
10 362 300
100 30692 30000
1000 3.006992 ∗ 106 3.00 ∗ 106
10000 3.0000699992 ∗ 1010 3.00 ∗ 1010

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Growth of functions …
• Does constant of proportionality matter when 𝑛 gets very
large?

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Growth of functions …
• Does constant of proportionality matter when 𝑛 gets very
large?
• Then, what is asymptotic growth rate, asymptotic order or
order of functions?

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Growth of functions …
• Does constant of proportionality matter when 𝑛 gets very
large?
• Then, what is asymptotic growth rate, asymptotic order or
order of functions?
• Is it reasonable to ignore smaller values and constants?

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Growth of functions …
• Hence, we shall now also drop the all the terms

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Growth of functions …
• Hence, we shall now also drop the all the terms
• Except the highest degree of the polynomial for the running time of
the algorithm

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Growth of functions …
• Hence, we shall now also drop the all the terms
• Except the highest degree of the polynomial for the running time of
the algorithm
• Example

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Growth of functions …
• Hence, we shall now also drop the all the terms
• Except the highest degree of the polynomial for the running time of
the algorithm
• Example
• Insertion sort Best case complexity …
𝑻 𝒏 = 𝟓𝒏 − 𝟒
• So, we will say that complexity is of the order of 𝒏

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Growth of functions …
• Hence, we shall now also drop the all the terms
• Except the highest degree of the polynomial for the running time of
the algorithm
• Example
• Insertion sort Best case complexity …
𝑻 𝒏 = 𝟓𝒏 − 𝟒
• So, we will say that complexity is of the order of 𝒏
• Insertion sort Best case complexity …
𝟏
𝑻 𝒏 = (𝟑𝒏𝟐 + 𝟕𝒏 − 𝟖)
𝟐
• So, we will say that complexity is of the order of 𝒏𝟐

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Order of growth and abstractions
• So, now we have assumed/abstracted at three different levels
viz.

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Order of growth and abstractions
• So, now we have assumed/abstracted at three different levels
viz.
• Level 1 – ignored the actual cost of execution of each statement.

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Order of growth and abstractions
• So, now we have assumed/abstracted at three different levels
viz.
• Level 1 – ignored the actual cost of execution of each statement.
• Level 2 – ignored even the abstract cost (𝐶𝑖 ) of each statement.

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Order of growth and abstractions
• So, now we have assumed/abstracted at three different levels
viz.
• Level 1 – ignored the actual cost of execution of each statement.
• Level 2 – ignored even the abstract cost (𝐶𝑖 ) of each statement.
• Level 3 – ignore all the terms except for the one with the highest
degree in the expression of time complexity

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Order of growth and abstractions
• So, now we have assumed/abstracted at three different levels
viz.
• Level 1 – ignored the actual cost of execution of each statement.
• Level 2 – ignored even the abstract cost (𝐶𝑖 ) of each statement.
• Level 3 – ignore all the terms except for the one with the highest
degree in the expression of time complexity

• Such analysis is based on the asymptotic growth rate,

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Order of growth and abstractions
• So, now we have assumed/abstracted at three different levels
viz.
• Level 1 – ignored the actual cost of execution of each statement.
• Level 2 – ignored even the abstract cost (𝐶𝑖 ) of each statement.
• Level 3 – ignore all the terms except for the one with the highest
degree in the expression of time complexity

• Such analysis is based on the asymptotic growth rate,


• Asymptotic order or order or functions and called asymptotic analysis

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Primary Observations …
• There can be at least three different ways of analyzing the
algorithms
• Empirical analysis
• Mathematical analysis
• Asymptotic analysis

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Typical functions
• We are interested in order of magnitude
• 𝑡(𝑛) may proportional to log 𝑛, … , 𝑛2, 𝑛3, … , 2𝑛
• Logarithmic, polynomial, exponential …

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Basic Asymptotic Efficiency classes
1 Constant
log 𝑛 Logarithmic
𝑛 Linear
𝑛 log 𝑛 𝑛 log 𝑛
𝑛2 Quadratic
𝑛3 Cubic
2𝑛 Exponential
𝑛! Factorial

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Typical functions 𝑡(𝑛)
Input 𝒍𝒐𝒈 𝒏 𝒏 𝒏 𝒍𝒐𝒈 𝒏 𝒏𝟐 𝒏𝟑 𝟐𝒏 𝒏!
𝟏𝟎 3.3 10 33 100 1000 1000 106
𝟏𝟎𝟎 6.6 100 66 104 106 1030 10157
𝟏𝟎𝟎𝟎 10 1000 104 106 109
𝟏𝟎𝟒 13 104 105 108 1012
𝟏𝟎𝟓 17 105 106 1010
𝟏𝟎𝟔 20 106 107
𝟏𝟎𝟕 23 107 108
𝟏𝟎𝟖 27 108 109
𝟏𝟎𝟗 30 109 1010
𝟏𝟎𝟏𝟎 33 1010

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Typical functions 𝑡(𝑛)

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
An interesting “seconds” conversion
102 1.7 min
104 2.8 hours
105 1.1 days
106 1.6 weeks
107 3.8 months
108 3.1 years
109 3.1 decades
1010 3.1 centuries

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Asymptotic notations: The Big-O notation

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Asymptotic notations: The Big-O notation

• Definition
• For a given function 𝒈 𝒏 , we say that
𝑶 𝒈 𝒏 = 𝒇 𝒏 if there exists positive constants
𝒄 and 𝒏𝟎 such that 𝟎 ≤ 𝒇 𝒏 ≤ 𝒄𝒈(𝒏)
for all 𝒏 ≥ 𝒏𝟎 }

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Asymptotic notations: The Big-O notation

• Definition
• For a given function 𝒈 𝒏 , we say that
𝑶 𝒈 𝒏 = 𝒇 𝒏 if there exists positive constants
𝒄 and 𝒏𝟎 such that 𝟎 ≤ 𝒇 𝒏 ≤ 𝒄𝒈(𝒏)
for all 𝒏 ≥ 𝒏𝟎 }
• Defines an upper bound for a function within a constant factor.

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Asymptotic notations: The Big-O notation

• Definition
• For a given function 𝒈 𝒏 , we say that
𝑶 𝒈 𝒏 = 𝒇 𝒏 if there exists positive constants
𝒄 and 𝒏𝟎 such that 𝟎 ≤ 𝒇 𝒏 ≤ 𝒄𝒈(𝒏)
for all 𝒏 ≥ 𝒏𝟎 }
• Defines an upper bound for a function within a constant factor.
• 𝒇 𝒏 =𝑶 𝒈 𝒏 ⇒
• 𝒇(𝒏) is dominated in the growth by 𝒈 𝒏
• 𝒇(𝒏) is of the order at most 𝒈 𝒏
• 𝒈(𝒏) grows at least as fast as 𝒇 𝒏

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Asymptotic notations: The Big-O notation

• Definition
• For a given function 𝒈 𝒏 , we say that
𝑶 𝒈 𝒏 = 𝒇 𝒏 if there exists positive constants
𝒄 and 𝒏𝟎 such that 𝟎 ≤ 𝒇 𝒏 ≤ 𝒄𝒈(𝒏)
for all 𝒏 ≥ 𝒏𝟎 }
• Defines an upper bound for a function within a constant factor.
• 𝒇 𝒏 =𝑶 𝒈 𝒏 ⇒
• 𝒇(𝒏) is dominated in the growth by 𝒈 𝒏
• 𝒇(𝒏) is of the order at most 𝒈 𝒏
• 𝒈(𝒏) grows at least as fast as 𝒇 𝒏
• Can 𝒇(𝒏) grow faster than 𝒈(𝒏)?
• Can 𝒈(𝒏) grow faster than 𝒇(𝒏)?

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
The Big-oh notation

Figure: Big-oh notation: 𝑡 𝑛 𝜖 𝑂(𝑔 𝑛 )


Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
The Big-O notation: Illustrations
Function Notation in O
𝑓 𝑛 = 5𝑛 + 8 𝑓 𝑛 = 𝑂(? )
𝑓 𝑛 = 𝑛2 + 3𝑛 − 8 𝑓 𝑛 = 𝑂(? )
𝑓 𝑛 = 12𝑛2 − 11 𝑓 𝑛 = 𝑂(? )
𝑓 𝑛 = 5 ∗ 2𝑛 + 𝑛2 𝑓 𝑛 = 𝑂(? )
𝑓 𝑛 = 3𝑛 + 8 𝑓 𝑛 = 𝑂 𝑛2 ?
𝑓 𝑛 = 5𝑛 + 8 𝑓 𝑛 =𝑂 1 ?

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
The Big-O notation: Illustrations

• The big-O notation


• Allows us to keep track of the leading team while ignoring
smaller terms…
• Allows us to make concise statements that give
approximations to the quantities to analyze.
• If 𝑓 𝑛 = 𝑂 𝑔 𝑛
• 𝑔(𝑛) is the upper bound, but do we specify how tight this upper
bound is?
• Consider that 𝑓 𝑛 = 𝑂 𝑛 & 𝑔 𝑛 = 𝑂 𝑛2
• Is 𝑓 𝑛 = 𝑂(𝑔(𝑛)) saying the same as reverse i.e. 𝑔 𝑛 = 𝑂 𝑓 𝑛 ?
• The symbol “=” is not proper
• Truly it is 𝜖 which should be used i.e. 𝑓 𝑛 𝜖 𝑂(𝑔 𝑛 )

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Asymptotic notations: The Big- Ω notation

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Asymptotic notations: The Big- Ω notation

• Definition
• For a given function 𝒈 𝒏 , we say that
𝛀 𝒈 𝒏 = 𝒇 𝒏 if there exists positive constants
𝒄 and 𝒏𝟎 such that 𝟎 ≤ 𝒄𝒈 𝒏 ≤ 𝒇(𝒏)
for all 𝒏 ≥ 𝒏𝟎 }

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Asymptotic notations: The Big- Ω notation

• Definition
• For a given function 𝒈 𝒏 , we say that
𝛀 𝒈 𝒏
= 𝒇 𝒏 if there exists positive constants
𝒄 and 𝒏𝟎 such that 𝟎 ≤ 𝒄𝒈 𝒏 ≤ 𝒇(𝒏)
for all 𝒏 ≥ 𝒏𝟎 }
• Defines an lower bound for a function within a constant factor.

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
The Big-omega notation

Figure: Big-omega notation: 𝑡 𝑛 𝜖 Ω(𝑔 𝑛 )


Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
The Big-omega notation: Illustrations
Function Notation in Ω
𝑓 𝑛 = 3𝑛 + 8 𝑓 𝑛 = Ω(? )
𝑓 𝑛 = 𝑛2 + 3𝑛 − 8 𝑓 𝑛 = Ω(? )
𝑓 𝑛 = 12𝑛2 − 11 𝑓 𝑛 = Ω(? )
𝑓 𝑛 = 6 ∗ 2𝑛 + 𝑛2 𝑓 𝑛 = Ω(? )
𝑓 𝑛 = 3𝑛 + 8 𝑓 𝑛 = Ω 𝑛2 ?
𝑓 𝑛 = 5𝑛 + 8 𝑓 𝑛 =Ω 1 ?

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Asymptotic notations: The Big- 𝜃 notation

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Asymptotic notations: The Big- 𝜃 notation

• Definition
• For a given function 𝒈 𝒏 , we say that
𝜽 𝒈 𝒏 = 𝒇 𝒏 if there exists positive constants
𝒄 and 𝒏𝟎 such that 𝟎 ≤ 𝒄𝟏 𝒈 𝒏 ≤ 𝒇 𝒏 ≤ 𝒄𝟐 𝒈(𝒏)
for all 𝒏 ≥ 𝒏𝟎 }

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Asymptotic notations: The Big- 𝜃 notation

• Definition
• For a given function 𝒈 𝒏 , we say that
𝜽 𝒈 𝒏
= 𝒇 𝒏 if there exists positive constants
𝒄 and 𝒏𝟎 such that 𝟎 ≤ 𝒄𝟏 𝒈 𝒏 ≤ 𝒇 𝒏 ≤ 𝒄𝟐 𝒈(𝒏)
for all 𝒏 ≥ 𝒏𝟎 }
• 𝒇 𝒏 = 𝜽(𝒈 𝒏 ) iff 𝒇 𝒏 = 𝑶(𝒈 𝒏 ) and 𝒇 𝒏 = 𝛀(𝒈 𝒏 )

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Asymptotic notations: The Big- 𝜃 notation

• Definition
• For a given function 𝒈 𝒏 , we say that
𝜽 𝒈 𝒏
= 𝒇 𝒏 if there exists positive constants
𝒄 and 𝒏𝟎 such that 𝟎 ≤ 𝒄𝟏 𝒈 𝒏 ≤ 𝒇 𝒏 ≤ 𝒄𝟐 𝒈(𝒏)
for all 𝒏 ≥ 𝒏𝟎 }
• 𝒇 𝒏 = 𝜽(𝒈 𝒏 ) iff 𝒇 𝒏 = 𝑶(𝒈 𝒏 ) and 𝒇 𝒏 = 𝛀(𝒈 𝒏 )
• Neither the big-O notation nor the big-Ω notation describe
the asymptotically tight bounds.

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Asymptotic notations: The Big- 𝜃 notation

• Definition
• For a given function 𝒈 𝒏 , we say that
𝜽 𝒈 𝒏
= 𝒇 𝒏 if there exists positive constants
𝒄 and 𝒏𝟎 such that 𝟎 ≤ 𝒄𝟏 𝒈 𝒏 ≤ 𝒇 𝒏 ≤ 𝒄𝟐 𝒈(𝒏)
for all 𝒏 ≥ 𝒏𝟎 }
• 𝒇 𝒏 = 𝜽(𝒈 𝒏 ) iff 𝒇 𝒏 = 𝑶(𝒈 𝒏 ) and 𝒇 𝒏 = 𝛀(𝒈 𝒏 )
• Neither the big-O notation nor the big-Ω notation describe
the asymptotically tight bounds.
• 𝜃-notation to express tighter bounds

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
The Big-theta notation

Figure: Big-theta notation: 𝑡 𝑛 𝜖 𝜃(𝑔 𝑛 )


Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
The Big-theta notation: Illustrations
Function Notation in Ω
𝑓 𝑛 = 3𝑛 + 8 𝑓 𝑛 = 𝜃(? )
𝑓 𝑛 = 10𝑛2 + 3𝑛 − 8 𝑓 𝑛 = 𝜃(? )
𝑓 𝑛 = 12𝑛2 − 11 𝑓 𝑛 = 𝜃(? )
𝑓 𝑛 = 6 ∗ 2𝑛 + 𝑛2 𝑓 𝑛 = 𝜃 2𝑛 ?
𝑓 𝑛 = 6 ∗ 2𝑛 + 𝑛2 𝑓 𝑛 = 𝜃 𝑛2 ?
𝑓 𝑛 = 3𝑛 + 8 𝑓 𝑛 = 𝜃 𝑛2 ?
𝑓 𝑛 = 5𝑛 + 8 𝑓 𝑛 =𝜃 1 ?

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Calculating complexity

• Iterative programs
• Recursive programs

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Example 1

• Problem: Maximum value in an array

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Example 1

• Problem: Maximum value in an array


• Solution:
Function 𝑀𝑎𝑥𝐸𝑙𝑒𝑚𝑒𝑛𝑡(𝐴, 𝑛)
1 𝑚𝑎𝑥𝑣𝑎𝑙 = 𝐴[1]
2 For 𝑖 = 2 to 𝑛
3 If 𝐴 𝑖 > 𝑚𝑎𝑥𝑣𝑎𝑙
4 𝑚𝑎𝑥𝑣𝑎𝑙 = 𝐴[𝑖]
5 Return 𝑚𝑎𝑥𝑣𝑎𝑙

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Example 2

• Problem: Check if all element in an array are


distinct

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Example 2

• Problem: Check if all element in an array are


distinct
• Solution:
Function 𝑁𝑜𝐷𝑢𝑝𝑙𝑖𝑐𝑎𝑡𝑒𝑠(𝐴, 𝑛)
1 For 𝑖 = 1 to 𝑛
2 For 𝑗 = 𝑖 + 1 to 𝑛
3 If 𝐴 𝑖 == 𝐴[𝑗]
4 Return 𝐹𝑎𝑙𝑠𝑒
5 Return 𝑇𝑟𝑢𝑒
Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Example 3

• Problem: Matrix multiplication

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Example 3

• Problem: Matrix multiplication


• Solution:
Function 𝑀𝑎𝑡𝑟𝑖𝑥𝑀𝑢𝑙𝑡𝑖𝑝𝑙𝑦(𝐴, 𝐵)
1.For 𝑖 = 1 to 𝑛
2. For 𝑗 = 1 to 𝑛
3. 𝐶[𝑖][𝑗] = 0
4. For 𝑘 = 1 to 𝑛
5. 𝐶 𝑖 𝑗 = 𝑐 𝑖 𝑗 + 𝐴 𝑖 [𝑘] × 𝐵[𝑘][𝑗]
6.Return 𝐶

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Example 4

• Problem: Towers of Hanoi

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Example 4

• Problem: Towers of Hanoi


• Three pegs, 𝐴, 𝐵 and 𝐶
• Move 𝑛 disks from 𝐴 to 𝐵
• Never put a larger disk above a smaller one
• 𝐶 is transit peg

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Example 4 …

• Recursive solution

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Example 4 …

• Recursive solution
• Move 𝑛 − 1 disks from 𝐴 to 𝐶, using 𝐵 as transit peg
• Move largest disk from 𝐴 to 𝐵
• Move 𝑛 − 1 disks from 𝐶 to 𝐵, using 𝐴 as transit peg

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Example 4 …

• Recursive solution
• Move 𝑛 − 1 disks from 𝐴 to 𝐶, using 𝐵 as transit peg
• Move largest disk from 𝐴 to 𝐵
• Move 𝑛 − 1 disks from 𝐶 to 𝐵, using 𝐴 as transit peg
• Solve recurrence by repeated substitution
• 𝑀 𝑛 : Number of moves to transfer 𝑛 disks

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Example 4 …

• Recursive solution
• Move 𝑛 − 1 disks from 𝐴 to 𝐶, using 𝐵 as transit peg
• Move largest disk from 𝐴 to 𝐵
• Move 𝑛 − 1 disks from 𝐶 to 𝐵, using 𝐴 as transit peg
• Solve recurrence by repeated substitution
• 𝑀 𝑛 : Number of moves to transfer 𝑛 disks
• 𝑀 𝑛 = 𝑀 𝑛 − 1 + 1 + 𝑀(𝑛 − 1)

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Example 4 …

• Recursive solution
• Move 𝑛 − 1 disks from 𝐴 to 𝐶, using 𝐵 as transit peg
• Move largest disk from 𝐴 to 𝐵
• Move 𝑛 − 1 disks from 𝐶 to 𝐵, using 𝐴 as transit peg
• Solve recurrence by repeated substitution
• 𝑀 𝑛 : Number of moves to transfer 𝑛 disks
• 𝑀 𝑛 = 𝑀 𝑛 − 1 + 1 + 𝑀(𝑛 − 1)
• 𝑀 1 =1

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Example 4 …

• Recursive solution
• Move 𝑛 − 1 disks from 𝐴 to 𝐶, using 𝐵 as transit peg
• Move largest disk from 𝐴 to 𝐵
• Move 𝑛 − 1 disks from 𝐶 to 𝐵, using 𝐴 as transit peg
• Solve recurrence by repeated substitution
• 𝑀 𝑛 : Number of moves to transfer 𝑛 disks
• 𝑀 𝑛 = 𝑀 𝑛 − 1 + 1 + 𝑀(𝑛 − 1)
• 𝑀 1 =1
• Answer ?

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20
Example 4 …

• Recursive solution
• Move 𝑛 − 1 disks from 𝐴 to 𝐶, using 𝐵 as transit peg
• Move largest disk from 𝐴 to 𝐵
• Move 𝑛 − 1 disks from 𝐶 to 𝐵, using 𝐴 as transit peg
• Solve recurrence by repeated substitution
• 𝑀 𝑛 : Number of moves to transfer 𝑛 disks
• 𝑀 𝑛 = 𝑀 𝑛 − 1 + 1 + 𝑀(𝑛 − 1)
• 𝑀 1 =1
• Answer ⇒ 𝑴 𝒏 = 𝟐𝒏 − 𝟏

Mr. Jignesh Joshi, Design and Analysis of Algorithms, B.Tech-III (CoED), SVNIT, 2019-20

S-ar putea să vă placă și