design and analysis of algorithms - 01 - sanlp.org and analysis of algorithms - 01.pdf · course...

51
Design and Analysis of Algorithms Dr. M. G. Abbas Malik [email protected] [email protected] Assistant Professor Assistant Professor COMSATS Institute of Information Technology, Lahore

Upload: ngodieu

Post on 26-May-2018

220 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Design and Analysis of Algorithms - 01 - sanlp.org and Analysis of Algorithms - 01.pdf · Course yPurpose: a thorough introduction to the design andPurpose: a thorough introduction

Design and Analysis of Algorithms

Dr. M. G. Abbas [email protected]

[email protected] Assistant ProfessorAssistant Professor

COMSATS Institute of Information Technology, Lahore

Page 2: Design and Analysis of Algorithms - 01 - sanlp.org and Analysis of Algorithms - 01.pdf · Course yPurpose: a thorough introduction to the design andPurpose: a thorough introduction

CoursePurpose: a thorough introduction to the design andPurpose: a thorough introduction to the design and analysis of algorithms

Interesting and important, but theoretical courseg p ,Equations and Formula, but not a Math cou

Text Book:Introduction to Algorithms; Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest & Clifford Stein

Additional Reference Books:Introduction to the design and analysis of algorithms; R C T Lee S S Tseng R C Chang & Y T TsaiR.C. T. Lee, S. S. Tseng, R. C. Chang & Y. T. TsaiThe design and analysis of computer algorithms; Aho, Hopcroft & Ullmanp

2 Dr. M. G. Abbas Malik; COMSATS IIT Lahore

Page 3: Design and Analysis of Algorithms - 01 - sanlp.org and Analysis of Algorithms - 01.pdf · Course yPurpose: a thorough introduction to the design andPurpose: a thorough introduction

Course1st Mid term Exam 10%1st Mid-term Exam 10%2nd Mid-term Exam 15%Final Exam 50%Assignments and Quizzes 25%gPlus Class participation

80% attendance is compulsory to sit in the final80% attendance is compulsory to sit in the final exam.

3 Dr. M. G. Abbas Malik; COMSATS IIT Lahore

Page 4: Design and Analysis of Algorithms - 01 - sanlp.org and Analysis of Algorithms - 01.pdf · Course yPurpose: a thorough introduction to the design andPurpose: a thorough introduction

AlgorithmThe word “algorithm” derived fromThe word algorithm derived from Mohammed Al-Khowarizmi, 9th century Persian mathematicianPersian mathematician.A procedure to solve a problemWhat is the best algorithm for a given problem?Three things we will learn:

Design a good algorithmg g gAnalyze itKnow when to stopKnow when to stop

4 Dr. M. G. Abbas Malik; COMSATS IIT Lahore

Page 5: Design and Analysis of Algorithms - 01 - sanlp.org and Analysis of Algorithms - 01.pdf · Course yPurpose: a thorough introduction to the design andPurpose: a thorough introduction

Algorithm - DefinitionAn algorithm is any well definedAn algorithm is any well defined computational procedure that takes

fsome value or set of values as input and produces some value or set of values as poutput.A correct algorithm halts with the correctA correct algorithm halts with the correct output for every instance. We can then

th t l ith l th blsay that algorithm solves the problem.

5 Dr. M. G. Abbas Malik; COMSATS IIT Lahore

Page 6: Design and Analysis of Algorithms - 01 - sanlp.org and Analysis of Algorithms - 01.pdf · Course yPurpose: a thorough introduction to the design andPurpose: a thorough introduction

AlgorithmProblem: Add numbersProblem: Add numbersDesign:

an Divide and Conquer: make an algorithm for the addition of two numbers (base case)Generalize this base case to solve the bigger problemgg

Analysis:Is this is a good algorithm?Is this is a good algorithm?What is its complexity?

6 Dr. M. G. Abbas Malik; COMSATS IIT Lahore

Page 7: Design and Analysis of Algorithms - 01 - sanlp.org and Analysis of Algorithms - 01.pdf · Course yPurpose: a thorough introduction to the design andPurpose: a thorough introduction

Algorithm – Addition of NumbersAlgorithm for Base case:Algorithm for Base case:

Add ( X , Y ){

Z ← X + YReturn Z

}}Generalize Algorithm:

Add ( X1 X2 X )Add ( X1 , X2 , … , Xn ){

Z ← X1 + X2 + … + Xn1 2 nReturn Z

}

7 Dr. M. G. Abbas Malik; COMSATS IIT Lahore

Page 8: Design and Analysis of Algorithms - 01 - sanlp.org and Analysis of Algorithms - 01.pdf · Course yPurpose: a thorough introduction to the design andPurpose: a thorough introduction

Famous AlgorithmsC t ti f E lidConstruction of EuclidNewton’s root findingFast Fourier TransformCompression (Huffman, Lempel-Ziv, GIF, MPEG)p ( , p , , )DES, RSA encryptionSimplex algorithm for linear programmingSimplex algorithm for linear programmingShortest Path Algorithm (Dijkstra, Bellman Ford)Dynamic ProgrammingError correcting codes (CDs, DVDs)g ( )

8 Dr. M. G. Abbas Malik; COMSATS IIT Lahore

Page 9: Design and Analysis of Algorithms - 01 - sanlp.org and Analysis of Algorithms - 01.pdf · Course yPurpose: a thorough introduction to the design andPurpose: a thorough introduction

Famous AlgorithmsTCP congestion control IP routingTCP congestion control, IP routingPattern matching (Genomics)Delaunay Triangulation (FEM. Simulation)

9 Dr. M. G. Abbas Malik; COMSATS IIT Lahore

Page 10: Design and Analysis of Algorithms - 01 - sanlp.org and Analysis of Algorithms - 01.pdf · Course yPurpose: a thorough introduction to the design andPurpose: a thorough introduction

Review: InductionSupposeSuppose

S(k) is true for fixed constant k Often k = 0 or 1

S(n) S(n+1) for all n >= kS(n) S(n+1) for all n > kThen S(n) is true for all n >= k

10 Dr. M. G. Abbas Malik; COMSATS IIT Lahore

Page 11: Design and Analysis of Algorithms - 01 - sanlp.org and Analysis of Algorithms - 01.pdf · Course yPurpose: a thorough introduction to the design andPurpose: a thorough introduction

Proof By InductionClaim: S(n) is true for all n >= kClaim: S(n) is true for all n >= kBase case:

Sh f l i t h kShow formula is true when n = kInductive hypothesis:

Assume formula is true for an arbitrary nStep:Step:

Show that formula is then true for n+1

11 Dr. M. G. Abbas Malik; COMSATS IIT Lahore

Page 12: Design and Analysis of Algorithms - 01 - sanlp.org and Analysis of Algorithms - 01.pdf · Course yPurpose: a thorough introduction to the design andPurpose: a thorough introduction

Induction Example: Gaussian Closed Form

Prove 1 + 2 + 3 + + n = n(n+1) / 2Prove 1 + 2 + 3 + … + n = n(n+1) / 2Base case:

If n = 0 then 0 = 0(0+1) / 2If n 0, then 0 0(0+1) / 2If n = 1, then 1 = 1(1+1) / 2

Inductive hypothesis:Inductive hypothesis:Assume 1 + 2 + 3 + … + n = n(n+1) / 2

Step (show true for n+1):Step (show true for n 1):1 + 2 + … + n + n+1 = (1 + 2 + …+ n) + (n+1)= n(n+1)/2 + n+1 = [n(n+1) + 2(n+1)]/2 ( ) [ ( ) ( )]= (n+1)(n+2)/2 = (n+1)(n+1 + 1) / 2

12 Dr. M. G. Abbas Malik; COMSATS IIT Lahore

Page 13: Design and Analysis of Algorithms - 01 - sanlp.org and Analysis of Algorithms - 01.pdf · Course yPurpose: a thorough introduction to the design andPurpose: a thorough introduction

Induction Example:Geometric Closed Form

Prove a0 + a1 + + an = (an+1 - 1)/(a - 1) forProve a + a + … + a = (a - 1)/(a - 1) for all a ≠ 1

Base case: show that a0 = (a0+1 - 1)/(a - 1)Base case: show that a (a 1)/(a 1) a0 = 1 = (a1 - 1)/(a - 1)Inductive hypothesis:Inductive hypothesis:

Assume a0 + a1 + … + an = (an+1 - 1)/(a - 1) Step (show true for n+1):Step (show true for n+1):a0 + a1 + … + an+1 = (a0 + a1 + … + an) +

an+1a= (an+1 - 1)/(a - 1) + an+1 = (an+1+1 - 1)/(a - 1)

13 Dr. M. G. Abbas Malik; COMSATS IIT Lahore

Page 14: Design and Analysis of Algorithms - 01 - sanlp.org and Analysis of Algorithms - 01.pdf · Course yPurpose: a thorough introduction to the design andPurpose: a thorough introduction

Analyzing AlgorithmHow does the algorithm behave as theHow does the algorithm behave as the problem size gets very large?

Running timeMemory/storage requirementsMemory/storage requirementsBandwidth/power requirements/logic gates/etcgates/etc.

14 Dr. M. G. Abbas Malik; COMSATS IIT Lahore

Page 15: Design and Analysis of Algorithms - 01 - sanlp.org and Analysis of Algorithms - 01.pdf · Course yPurpose: a thorough introduction to the design andPurpose: a thorough introduction

Input SizeTime and space complexityTime and space complexity

This is generally a function of the input sizeti lti li tisorting, multiplication

How we characterize input size depends:Sorting: number of input itemsMultiplication: total number of bitspGraph algorithms: number of nodes & edgesg

15 Dr. M. G. Abbas Malik; COMSATS IIT Lahore

Page 16: Design and Analysis of Algorithms - 01 - sanlp.org and Analysis of Algorithms - 01.pdf · Course yPurpose: a thorough introduction to the design andPurpose: a thorough introduction

Running TimeNumber of primitive steps that are executedNumber of primitive steps that are executed

Except for time of executing a function call most statements roughly require the samemost statements roughly require the same amount of time

* by = m * x + bc = 5 / 9 * (t - 32 )z = f(x) + g(y)

16 Dr. M. G. Abbas Malik; COMSATS IIT Lahore

Page 17: Design and Analysis of Algorithms - 01 - sanlp.org and Analysis of Algorithms - 01.pdf · Course yPurpose: a thorough introduction to the design andPurpose: a thorough introduction

AnalysisWorst caseWorst case

Provides an upper bound on running timeA b l t tAn absolute guarantee

Average caseProvides the expected running timeVery useful, but treat with care: what is y ,“average”?

Random (equally likely) inputsRandom (equally likely) inputsReal-life inputs

17 Dr. M. G. Abbas Malik; COMSATS IIT Lahore

Page 18: Design and Analysis of Algorithms - 01 - sanlp.org and Analysis of Algorithms - 01.pdf · Course yPurpose: a thorough introduction to the design andPurpose: a thorough introduction

An Example: Insertion SortInsertionSort(A n) {InsertionSort(A, n) {for i = 2 to n {

key = A[i]j = i - 1;while (j > 0) and (A[j] > key) {

A[j+1] A[j]A[j+1] = A[j]j = j - 1

}}A[j+1] = key

}}

18 Dr. M. G. Abbas Malik; COMSATS IIT Lahore

Page 19: Design and Analysis of Algorithms - 01 - sanlp.org and Analysis of Algorithms - 01.pdf · Course yPurpose: a thorough introduction to the design andPurpose: a thorough introduction

An Example: Insertion Sort30 10 40 20 i = ∅ j = ∅ key = ∅

1 2 3 4

j yA[j] = ∅ A[j+1] = ∅

InsertionSort(A, n) {for i = 2 to n {

key = A[i]j i 1j = i - 1;while (j > 0) and (A[j] > key) {

A[j+1] = A[j]j j 1j = j - 1

}A[j+1] = key

}}}

19 Dr. M. G. Abbas Malik; COMSATS IIT Lahore

Page 20: Design and Analysis of Algorithms - 01 - sanlp.org and Analysis of Algorithms - 01.pdf · Course yPurpose: a thorough introduction to the design andPurpose: a thorough introduction

An Example: Insertion Sort30 10 40 20 i = 2 j = 1 key = 10

1 2 3 4

j yA[j] = 30 A[j+1] = 10

InsertionSort(A, n) {for i = 2 to n {

key = A[i]j i 1j = i - 1;while (j > 0) and (A[j] > key) {

A[j+1] = A[j]j j 1j = j - 1

}A[j+1] = key

}}}

20 Dr. M. G. Abbas Malik; COMSATS IIT Lahore

Page 21: Design and Analysis of Algorithms - 01 - sanlp.org and Analysis of Algorithms - 01.pdf · Course yPurpose: a thorough introduction to the design andPurpose: a thorough introduction

An Example: Insertion Sort30 30 40 20 i = 2 j = 1 key = 10

1 2 3 4

j yA[j] = 30 A[j+1] = 30

InsertionSort(A, n) {for i = 2 to n {

key = A[i]j i 1j = i - 1;while (j > 0) and (A[j] > key) {

A[j+1] = A[j]j j 1j = j - 1

}A[j+1] = key

}}}

21 Dr. M. G. Abbas Malik; COMSATS IIT Lahore

Page 22: Design and Analysis of Algorithms - 01 - sanlp.org and Analysis of Algorithms - 01.pdf · Course yPurpose: a thorough introduction to the design andPurpose: a thorough introduction

An Example: Insertion Sort30 30 40 20 i = 2 j = 1 key = 10

1 2 3 4

j yA[j] = 30 A[j+1] = 30

InsertionSort(A, n) {for i = 2 to n {

key = A[i]j i 1j = i - 1;while (j > 0) and (A[j] > key) {

A[j+1] = A[j]j j 1j = j - 1

}A[j+1] = key

}}}

22 Dr. M. G. Abbas Malik; COMSATS IIT Lahore

Page 23: Design and Analysis of Algorithms - 01 - sanlp.org and Analysis of Algorithms - 01.pdf · Course yPurpose: a thorough introduction to the design andPurpose: a thorough introduction

An Example: Insertion Sort30 30 40 20 i = 2 j = 0 key = 10

1 2 3 4

j yA[j] = ∅ A[j+1] = 30

InsertionSort(A, n) {for i = 2 to n {

key = A[i]j i 1j = i - 1;while (j > 0) and (A[j] > key) {

A[j+1] = A[j]j j 1j = j - 1

}A[j+1] = key

}}}

23 Dr. M. G. Abbas Malik; COMSATS IIT Lahore

Page 24: Design and Analysis of Algorithms - 01 - sanlp.org and Analysis of Algorithms - 01.pdf · Course yPurpose: a thorough introduction to the design andPurpose: a thorough introduction

An Example: Insertion Sort30 30 40 20 i = 2 j = 0 key = 10

1 2 3 4

j yA[j] = ∅ A[j+1] = 30

InsertionSort(A, n) {for i = 2 to n {

key = A[i]j i 1j = i - 1;while (j > 0) and (A[j] > key) {

A[j+1] = A[j]j j 1j = j - 1

}A[j+1] = key

}}}

24 Dr. M. G. Abbas Malik; COMSATS IIT Lahore

Page 25: Design and Analysis of Algorithms - 01 - sanlp.org and Analysis of Algorithms - 01.pdf · Course yPurpose: a thorough introduction to the design andPurpose: a thorough introduction

An Example: Insertion Sort10 30 40 20 i = 2 j = 0 key = 10

1 2 3 4

j yA[j] = ∅ A[j+1] = 10

InsertionSort(A, n) {for i = 2 to n {

key = A[i]j i 1j = i - 1;while (j > 0) and (A[j] > key) {

A[j+1] = A[j]j j 1j = j - 1

}A[j+1] = key

}}}

25 Dr. M. G. Abbas Malik; COMSATS IIT Lahore

Page 26: Design and Analysis of Algorithms - 01 - sanlp.org and Analysis of Algorithms - 01.pdf · Course yPurpose: a thorough introduction to the design andPurpose: a thorough introduction

An Example: Insertion Sort10 30 40 20 i = 3 j = 0 key = 10

1 2 3 4

j yA[j] = ∅ A[j+1] = 10

InsertionSort(A, n) {for i = 2 to n {

key = A[i]j i 1j = i - 1;while (j > 0) and (A[j] > key) {

A[j+1] = A[j]j j 1j = j - 1

}A[j+1] = key

}}}

26 Dr. M. G. Abbas Malik; COMSATS IIT Lahore

Page 27: Design and Analysis of Algorithms - 01 - sanlp.org and Analysis of Algorithms - 01.pdf · Course yPurpose: a thorough introduction to the design andPurpose: a thorough introduction

An Example: Insertion Sort10 30 40 20 i = 3 j = 0 key = 40

1 2 3 4

j yA[j] = ∅ A[j+1] = 10

InsertionSort(A, n) {for i = 2 to n {

key = A[i]j i 1j = i - 1;while (j > 0) and (A[j] > key) {

A[j+1] = A[j]j j 1j = j - 1

}A[j+1] = key

}}}

27 Dr. M. G. Abbas Malik; COMSATS IIT Lahore

Page 28: Design and Analysis of Algorithms - 01 - sanlp.org and Analysis of Algorithms - 01.pdf · Course yPurpose: a thorough introduction to the design andPurpose: a thorough introduction

An Example: Insertion Sort10 30 40 20 i = 3 j = 0 key = 40

1 2 3 4

j yA[j] = ∅ A[j+1] = 10

InsertionSort(A, n) {for i = 2 to n {

key = A[i]j i 1j = i - 1;while (j > 0) and (A[j] > key) {

A[j+1] = A[j]j j 1j = j - 1

}A[j+1] = key

}}}

28 Dr. M. G. Abbas Malik; COMSATS IIT Lahore

Page 29: Design and Analysis of Algorithms - 01 - sanlp.org and Analysis of Algorithms - 01.pdf · Course yPurpose: a thorough introduction to the design andPurpose: a thorough introduction

An Example: Insertion Sort10 30 40 20 i = 3 j = 2 key = 40

1 2 3 4

j yA[j] = 30 A[j+1] = 40

InsertionSort(A, n) {for i = 2 to n {

key = A[i]j i 1j = i - 1;while (j > 0) and (A[j] > key) {

A[j+1] = A[j]j j 1j = j - 1

}A[j+1] = key

}}}

29 Dr. M. G. Abbas Malik; COMSATS IIT Lahore

Page 30: Design and Analysis of Algorithms - 01 - sanlp.org and Analysis of Algorithms - 01.pdf · Course yPurpose: a thorough introduction to the design andPurpose: a thorough introduction

An Example: Insertion Sort10 30 40 20 i = 3 j = 2 key = 40

1 2 3 4

j yA[j] = 30 A[j+1] = 40

InsertionSort(A, n) {for i = 2 to n {

key = A[i]j i 1j = i - 1;while (j > 0) and (A[j] > key) {

A[j+1] = A[j]j j 1j = j - 1

}A[j+1] = key

}}}

30 Dr. M. G. Abbas Malik; COMSATS IIT Lahore

Page 31: Design and Analysis of Algorithms - 01 - sanlp.org and Analysis of Algorithms - 01.pdf · Course yPurpose: a thorough introduction to the design andPurpose: a thorough introduction

An Example: Insertion Sort10 30 40 20 i = 3 j = 2 key = 40

1 2 3 4

j yA[j] = 30 A[j+1] = 40

InsertionSort(A, n) {for i = 2 to n {

key = A[i]j i 1j = i - 1;while (j > 0) and (A[j] > key) {

A[j+1] = A[j]j j 1j = j - 1

}A[j+1] = key

}}}

31 Dr. M. G. Abbas Malik; COMSATS IIT Lahore

Page 32: Design and Analysis of Algorithms - 01 - sanlp.org and Analysis of Algorithms - 01.pdf · Course yPurpose: a thorough introduction to the design andPurpose: a thorough introduction

An Example: Insertion Sort10 30 40 20 i = 4 j = 2 key = 40

1 2 3 4

j yA[j] = 30 A[j+1] = 40

InsertionSort(A, n) {for i = 2 to n {

key = A[i]j i 1j = i - 1;while (j > 0) and (A[j] > key) {

A[j+1] = A[j]j j 1j = j - 1

}A[j+1] = key

}}}

32 Dr. M. G. Abbas Malik; COMSATS IIT Lahore

Page 33: Design and Analysis of Algorithms - 01 - sanlp.org and Analysis of Algorithms - 01.pdf · Course yPurpose: a thorough introduction to the design andPurpose: a thorough introduction

An Example: Insertion Sort10 30 40 20 i = 4 j = 2 key = 20

1 2 3 4

j yA[j] = 30 A[j+1] = 40

InsertionSort(A, n) {for i = 2 to n {

key = A[i]j i 1j = i - 1;while (j > 0) and (A[j] > key) {

A[j+1] = A[j]j j 1j = j - 1

}A[j+1] = key

}}}

33 Dr. M. G. Abbas Malik; COMSATS IIT Lahore

Page 34: Design and Analysis of Algorithms - 01 - sanlp.org and Analysis of Algorithms - 01.pdf · Course yPurpose: a thorough introduction to the design andPurpose: a thorough introduction

An Example: Insertion Sort10 30 40 20 i = 4 j = 2 key = 20

1 2 3 4

j yA[j] = 30 A[j+1] = 40

InsertionSort(A, n) {for i = 2 to n {

key = A[i]j i 1j = i - 1;while (j > 0) and (A[j] > key) {

A[j+1] = A[j]j j 1j = j - 1

}A[j+1] = key

}}}

34 Dr. M. G. Abbas Malik; COMSATS IIT Lahore

Page 35: Design and Analysis of Algorithms - 01 - sanlp.org and Analysis of Algorithms - 01.pdf · Course yPurpose: a thorough introduction to the design andPurpose: a thorough introduction

An Example: Insertion Sort10 30 40 20 i = 4 j = 3 key = 20

1 2 3 4

j yA[j] = 40 A[j+1] = 20

InsertionSort(A, n) {for i = 2 to n {

key = A[i]j i 1j = i - 1;while (j > 0) and (A[j] > key) {

A[j+1] = A[j]j j 1j = j - 1

}A[j+1] = key

}}}

35 Dr. M. G. Abbas Malik; COMSATS IIT Lahore

Page 36: Design and Analysis of Algorithms - 01 - sanlp.org and Analysis of Algorithms - 01.pdf · Course yPurpose: a thorough introduction to the design andPurpose: a thorough introduction

An Example: Insertion Sort10 30 40 20 i = 4 j = 3 key = 20

1 2 3 4

j yA[j] = 40 A[j+1] = 20

InsertionSort(A, n) {for i = 2 to n {

key = A[i]j i 1j = i - 1;while (j > 0) and (A[j] > key) {

A[j+1] = A[j]j j 1j = j - 1

}A[j+1] = key

}}}

36 Dr. M. G. Abbas Malik; COMSATS IIT Lahore

Page 37: Design and Analysis of Algorithms - 01 - sanlp.org and Analysis of Algorithms - 01.pdf · Course yPurpose: a thorough introduction to the design andPurpose: a thorough introduction

An Example: Insertion Sort10 30 40 40 i = 4 j = 3 key = 20

1 2 3 4

j yA[j] = 40 A[j+1] = 40

InsertionSort(A, n) {for i = 2 to n {

key = A[i]j i 1j = i - 1;while (j > 0) and (A[j] > key) {

A[j+1] = A[j]j j 1j = j - 1

}A[j+1] = key

}}}

37 Dr. M. G. Abbas Malik; COMSATS IIT Lahore

Page 38: Design and Analysis of Algorithms - 01 - sanlp.org and Analysis of Algorithms - 01.pdf · Course yPurpose: a thorough introduction to the design andPurpose: a thorough introduction

An Example: Insertion Sort10 30 40 40 i = 4 j = 3 key = 20

1 2 3 4

j yA[j] = 40 A[j+1] = 40

InsertionSort(A, n) {for i = 2 to n {

key = A[i]j i 1j = i - 1;while (j > 0) and (A[j] > key) {

A[j+1] = A[j]j j 1j = j - 1

}A[j+1] = key

}}}

38 Dr. M. G. Abbas Malik; COMSATS IIT Lahore

Page 39: Design and Analysis of Algorithms - 01 - sanlp.org and Analysis of Algorithms - 01.pdf · Course yPurpose: a thorough introduction to the design andPurpose: a thorough introduction

An Example: Insertion Sort10 30 40 40 i = 4 j = 2 key = 20

1 2 3 4

j yA[j] = 30 A[j+1] = 40

InsertionSort(A, n) {for i = 2 to n {

key = A[i]j i 1j = i - 1;while (j > 0) and (A[j] > key) {

A[j+1] = A[j]j j 1j = j - 1

}A[j+1] = key

}}}

39 Dr. M. G. Abbas Malik; COMSATS IIT Lahore

Page 40: Design and Analysis of Algorithms - 01 - sanlp.org and Analysis of Algorithms - 01.pdf · Course yPurpose: a thorough introduction to the design andPurpose: a thorough introduction

An Example: Insertion Sort10 30 40 40 i = 4 j = 2 key = 20

1 2 3 4

j yA[j] = 30 A[j+1] = 40

InsertionSort(A, n) {for i = 2 to n {

key = A[i]j i 1j = i - 1;while (j > 0) and (A[j] > key) {

A[j+1] = A[j]j j 1j = j - 1

}A[j+1] = key

}}}

40 Dr. M. G. Abbas Malik; COMSATS IIT Lahore

Page 41: Design and Analysis of Algorithms - 01 - sanlp.org and Analysis of Algorithms - 01.pdf · Course yPurpose: a thorough introduction to the design andPurpose: a thorough introduction

An Example: Insertion Sort10 30 30 40 i = 4 j = 2 key = 20

1 2 3 4

j yA[j] = 30 A[j+1] = 30

InsertionSort(A, n) {for i = 2 to n {

key = A[i]j i 1j = i - 1;while (j > 0) and (A[j] > key) {

A[j+1] = A[j]j j 1j = j - 1

}A[j+1] = key

}}}

41 Dr. M. G. Abbas Malik; COMSATS IIT Lahore

Page 42: Design and Analysis of Algorithms - 01 - sanlp.org and Analysis of Algorithms - 01.pdf · Course yPurpose: a thorough introduction to the design andPurpose: a thorough introduction

An Example: Insertion Sort10 30 30 40 i = 4 j = 2 key = 20

1 2 3 4

j yA[j] = 30 A[j+1] = 30

InsertionSort(A, n) {for i = 2 to n {

key = A[i]j i 1j = i - 1;while (j > 0) and (A[j] > key) {

A[j+1] = A[j]j j 1j = j - 1

}A[j+1] = key

}}}

42 Dr. M. G. Abbas Malik; COMSATS IIT Lahore

Page 43: Design and Analysis of Algorithms - 01 - sanlp.org and Analysis of Algorithms - 01.pdf · Course yPurpose: a thorough introduction to the design andPurpose: a thorough introduction

An Example: Insertion Sort10 30 30 40 i = 4 j = 1 key = 20

1 2 3 4

j yA[j] = 10 A[j+1] = 30

InsertionSort(A, n) {for i = 2 to n {

key = A[i]j i 1j = i - 1;while (j > 0) and (A[j] > key) {

A[j+1] = A[j]j j 1j = j - 1

}A[j+1] = key

}}}

43 Dr. M. G. Abbas Malik; COMSATS IIT Lahore

Page 44: Design and Analysis of Algorithms - 01 - sanlp.org and Analysis of Algorithms - 01.pdf · Course yPurpose: a thorough introduction to the design andPurpose: a thorough introduction

An Example: Insertion Sort10 30 30 40 i = 4 j = 1 key = 20

1 2 3 4

j yA[j] = 10 A[j+1] = 30

InsertionSort(A, n) {for i = 2 to n {

key = A[i]j i 1j = i - 1;while (j > 0) and (A[j] > key) {

A[j+1] = A[j]j j 1j = j - 1

}A[j+1] = key

}}}

44 Dr. M. G. Abbas Malik; COMSATS IIT Lahore

Page 45: Design and Analysis of Algorithms - 01 - sanlp.org and Analysis of Algorithms - 01.pdf · Course yPurpose: a thorough introduction to the design andPurpose: a thorough introduction

An Example: Insertion Sort10 20 30 40 i = 4 j = 1 key = 20

1 2 3 4

j yA[j] = 10 A[j+1] = 20

InsertionSort(A, n) {for i = 2 to n {

key = A[i]j i 1j = i - 1;while (j > 0) and (A[j] > key) {

A[j+1] = A[j]j j 1j = j - 1

}A[j+1] = key

}}}

45 Dr. M. G. Abbas Malik; COMSATS IIT Lahore

Page 46: Design and Analysis of Algorithms - 01 - sanlp.org and Analysis of Algorithms - 01.pdf · Course yPurpose: a thorough introduction to the design andPurpose: a thorough introduction

An Example: Insertion Sort10 20 30 40 i = 4 j = 1 key = 20

1 2 3 4

j yA[j] = 10 A[j+1] = 20

InsertionSort(A, n) {for i = 2 to n {

key = A[i]j i 1j = i - 1;while (j > 0) and (A[j] > key) {

A[j+1] = A[j]j j 1j = j - 1

}A[j+1] = key

}}}

Done!

46 Dr. M. G. Abbas Malik; COMSATS IIT Lahore

Page 47: Design and Analysis of Algorithms - 01 - sanlp.org and Analysis of Algorithms - 01.pdf · Course yPurpose: a thorough introduction to the design andPurpose: a thorough introduction

Animating Insertion SortCheck out the Animator a java applet at:Check out the Animator, a java applet at:

http://www.cs.hope.edu/~alganim/animator/Animathttp://www.cs.hope.edu/ alganim/animator/Animator.html

Try it out with random, ascending, and descending inputsdesce d g pu s

47 Dr. M. G. Abbas Malik; COMSATS IIT Lahore

Page 48: Design and Analysis of Algorithms - 01 - sanlp.org and Analysis of Algorithms - 01.pdf · Course yPurpose: a thorough introduction to the design andPurpose: a thorough introduction

Insertion SortInsertionSort(A n) {

What is the preconditionfor this loop?

InsertionSort(A, n) {for i = 2 to n {

key = A[i]j = i - 1;while (j > 0) and (A[j] > key) {

A[j+1] A[j]A[j+1] = A[j]j = j - 1

}}A[j+1] = key

}}

48 Dr. M. G. Abbas Malik; COMSATS IIT Lahore

Page 49: Design and Analysis of Algorithms - 01 - sanlp.org and Analysis of Algorithms - 01.pdf · Course yPurpose: a thorough introduction to the design andPurpose: a thorough introduction

Insertion SortInsertionSort(A n) {InsertionSort(A, n) {for i = 2 to n {

key = A[i]j = i - 1;while (j > 0) and (A[j] > key) {

A[j+1] A[j]A[j+1] = A[j]j = j - 1

}}A[j+1] = key

} How many times will}

How many times will this loop execute?

49 Dr. M. G. Abbas Malik; COMSATS IIT Lahore

Page 50: Design and Analysis of Algorithms - 01 - sanlp.org and Analysis of Algorithms - 01.pdf · Course yPurpose: a thorough introduction to the design andPurpose: a thorough introduction

Insertion SortStatement EffortStatement Effort

InsertionSort(A, n) {for i = 2 to n { c1n

key = A[i] c2(n-1)j = i - 1; c3(n-1)

while (j > 0) and (A[j] > key) { c Σtwhile (j > 0) and (A[j] > key) { c4Σti

A[j+1] = A[j] c5 Σ(ti-1)j = j - 1 c6 Σ(ti-1)}

A[j+1] = key c7(n-1)

}}}

50 Dr. M. G. Abbas Malik; COMSATS IIT Lahore

Page 51: Design and Analysis of Algorithms - 01 - sanlp.org and Analysis of Algorithms - 01.pdf · Course yPurpose: a thorough introduction to the design andPurpose: a thorough introduction

Analyzing Insertion SortT(n) = c1n + c2(n-1) + c3(n-1) + c4 Σ2≤ i ≤ nti + c5 Σ2≤ i ≤ n (ti-1) + c6 Σ2≤ i ≤ n (ti-1)+ c7(n-1)

What can T be?Best case -- inner loop body never executedy

ti = 1 T(n) is a linear functionWorst case -- inner loop body executed for all previous lelementsti = i T(n) is a quadratic function

51 Dr. M. G. Abbas Malik; COMSATS IIT Lahore