23
CS60020: Foundations of Algorithm Design and Machine Learning Sourangshu Bhattacharya

algo1 - cse.iitkgp.ac.incse.iitkgp.ac.in/~sourangshu/coursefiles/FADML18S/algo1.pdf · Title: algo1 Created Date: 1/8/2018 2:54:08 AM

  • Upload
    others

  • View
    9

  • Download
    0

Embed Size (px)

Citation preview

Page 1: algo1 - cse.iitkgp.ac.incse.iitkgp.ac.in/~sourangshu/coursefiles/FADML18S/algo1.pdf · Title: algo1 Created Date: 1/8/2018 2:54:08 AM

CS60020:FoundationsofAlgorithmDesignandMachine

LearningSourangshuBhattacharya

Page 2: algo1 - cse.iitkgp.ac.incse.iitkgp.ac.in/~sourangshu/coursefiles/FADML18S/algo1.pdf · Title: algo1 Created Date: 1/8/2018 2:54:08 AM

Whystudyalgorithms andperformance?

• Algorithms help us to understand scalability.• Performance often draws the line between what

is feasible and what is impossible.• Algorithmic mathematics provides a language

for talking about program behavior.• Performance is the currency of computing.• The lessons of program performance generalize

to other computing resources.• Speed is fun!

Copyright © 2001-5 Erik D. Demaine and Charles E. LeisersonIntroduction to Algorithms L1.4

Page 3: algo1 - cse.iitkgp.ac.incse.iitkgp.ac.in/~sourangshu/coursefiles/FADML18S/algo1.pdf · Title: algo1 Created Date: 1/8/2018 2:54:08 AM

L1.5Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson

Introduction to Algorithms

Theproblemof sorting

Input: sequence áa1, a2, …, anñ of numbers.

Output: permutation áa'1, a'2, …, a'nñ such that a'1 £ a'2 £ … £ a'n .

Example:Input: 8 2 4 9 3 6

Output: 2 3 4 6 8 9

Page 4: algo1 - cse.iitkgp.ac.incse.iitkgp.ac.in/~sourangshu/coursefiles/FADML18S/algo1.pdf · Title: algo1 Created Date: 1/8/2018 2:54:08 AM

L1.6Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson

Introduction to Algorithms

Insertion sort⊳ A[1 . . n]INSERTION-SORT (A, n)

for j ← 2 to ndo key ← A[ j]

i ← j – 1while i > 0 and A[i] > key

do A[i+1] ← A[i] i ← i – 1

A[i+1] = key

“pseudocode”

Page 5: algo1 - cse.iitkgp.ac.incse.iitkgp.ac.in/~sourangshu/coursefiles/FADML18S/algo1.pdf · Title: algo1 Created Date: 1/8/2018 2:54:08 AM

L1.7Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson

Introduction to Algorithms

Insertion sort⊳ A[1 . . n]INSERTION-SORT (A, n)

for j ← 2 to ndo key ← A[ j]

i ← j – 1“pseudocode”

sorted

i j

keyA:

1

while i > 0 and A[i] > keydo A[i+1] ← A[i]

i ← i – 1A[i+1] = key

n

Page 6: algo1 - cse.iitkgp.ac.incse.iitkgp.ac.in/~sourangshu/coursefiles/FADML18S/algo1.pdf · Title: algo1 Created Date: 1/8/2018 2:54:08 AM

L1.8Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson

Introduction to Algorithms

Exampleofinsertion sort8 2 4 9 3 6

Page 7: algo1 - cse.iitkgp.ac.incse.iitkgp.ac.in/~sourangshu/coursefiles/FADML18S/algo1.pdf · Title: algo1 Created Date: 1/8/2018 2:54:08 AM

L1.9Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson

Introduction to Algorithms

Exampleofinsertion sort8 2 4 9 3 6

Page 8: algo1 - cse.iitkgp.ac.incse.iitkgp.ac.in/~sourangshu/coursefiles/FADML18S/algo1.pdf · Title: algo1 Created Date: 1/8/2018 2:54:08 AM

L1.10Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson

Introduction to Algorithms

Exampleofinsertion sort8 2 4 9 3 6

2 8 4 9 3 6

Page 9: algo1 - cse.iitkgp.ac.incse.iitkgp.ac.in/~sourangshu/coursefiles/FADML18S/algo1.pdf · Title: algo1 Created Date: 1/8/2018 2:54:08 AM

L1.11Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson

Introduction to Algorithms

Exampleofinsertion sort8 2 4 9 3 6

2 8 4 9 3 6

Page 10: algo1 - cse.iitkgp.ac.incse.iitkgp.ac.in/~sourangshu/coursefiles/FADML18S/algo1.pdf · Title: algo1 Created Date: 1/8/2018 2:54:08 AM

L1.12Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson

Introduction to Algorithms

Exampleofinsertion sort

8 2 4 9 3 6

2 8 4 9 3 6

2 4 8 9 3 6

Page 11: algo1 - cse.iitkgp.ac.incse.iitkgp.ac.in/~sourangshu/coursefiles/FADML18S/algo1.pdf · Title: algo1 Created Date: 1/8/2018 2:54:08 AM

L1.13Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson

Introduction to Algorithms

Exampleofinsertion sort

8 2 4 9 3 6

2 8 4 9 3 6

2 4 8 9 3 6

Page 12: algo1 - cse.iitkgp.ac.incse.iitkgp.ac.in/~sourangshu/coursefiles/FADML18S/algo1.pdf · Title: algo1 Created Date: 1/8/2018 2:54:08 AM

L1.14Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson

Introduction to Algorithms

Exampleofinsertion sort

8 2 4 9 3 6

2 8 4 9 3 6

2 4 8 9 3 6

2 4 8 9 3 6

Page 13: algo1 - cse.iitkgp.ac.incse.iitkgp.ac.in/~sourangshu/coursefiles/FADML18S/algo1.pdf · Title: algo1 Created Date: 1/8/2018 2:54:08 AM

L1.15Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson

Introduction to Algorithms

Exampleofinsertion sort

8 2 4 9 3 6

2 8 4 9 3 6

2 4 8 9 3 6

2 4 8 9 3 6

Page 14: algo1 - cse.iitkgp.ac.incse.iitkgp.ac.in/~sourangshu/coursefiles/FADML18S/algo1.pdf · Title: algo1 Created Date: 1/8/2018 2:54:08 AM

L1.16Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson

Introduction to Algorithms

Exampleofinsertion sort

8 2 4 9 3 6

2 8 4 9 3 6

2 4 8 9 3 6

2 4 8 9 3 6

2 3 4 8 9 6

Page 15: algo1 - cse.iitkgp.ac.incse.iitkgp.ac.in/~sourangshu/coursefiles/FADML18S/algo1.pdf · Title: algo1 Created Date: 1/8/2018 2:54:08 AM

L1.17Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson

Introduction to Algorithms

Exampleofinsertion sort

8 2 4 9 3 6

2 8 4 9 3 6

2 4 8 9 3 6

2 4 8 9 3 6

2 3 4 8 9 6

Page 16: algo1 - cse.iitkgp.ac.incse.iitkgp.ac.in/~sourangshu/coursefiles/FADML18S/algo1.pdf · Title: algo1 Created Date: 1/8/2018 2:54:08 AM

L1.18Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson

Introduction to Algorithms

Exampleofinsertion sort

8 2 4 9 3 6

2 8 4 9 3 6

2 4 8 9 3 6

2 4 8 9 3 6

2 3 4 8 9 6

2 3 4 6 8 9 done

Page 17: algo1 - cse.iitkgp.ac.incse.iitkgp.ac.in/~sourangshu/coursefiles/FADML18S/algo1.pdf · Title: algo1 Created Date: 1/8/2018 2:54:08 AM

L1.19Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson

Introduction to Algorithms

Running time

• The running time depends on the input: an already sorted sequence is easier to sort.

• Parameterize the running time by the size ofthe input, since short sequences are easier tosort than long ones.

• Generally, we seek upper bounds on therunning time, because everybody likes aguarantee.

Page 18: algo1 - cse.iitkgp.ac.incse.iitkgp.ac.in/~sourangshu/coursefiles/FADML18S/algo1.pdf · Title: algo1 Created Date: 1/8/2018 2:54:08 AM

L1.20

Kindsof analysesWorst-case: (usually)

• T(n) = maximum time of algorithm on any input of size n.

Average-case: (sometimes)• T(n) = expected time of algorithm

over all inputs of size n.• Need assumption of statistical

distribution of inputs.Best-case: (bogus)

• Cheat with a slow algorithm that works fast on some input.

Copyright © 2001-5 Erik D. Demaine and Charles E. LeisersonIntroduction to Algorithms

Page 19: algo1 - cse.iitkgp.ac.incse.iitkgp.ac.in/~sourangshu/coursefiles/FADML18S/algo1.pdf · Title: algo1 Created Date: 1/8/2018 2:54:08 AM

Machine-independent time

What is insertion sort’s worst-case time?• It depends on the speed of our computer:

• relative speed (on the same machine),• absolute speed (on different machines).

BIG IDEA:• Ignore machine-dependent constants.• Look at growth of T(n) as n → ∞ .

“Asymptotic Analysis”Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson

Introduction to Algorithms L1.21

Page 20: algo1 - cse.iitkgp.ac.incse.iitkgp.ac.in/~sourangshu/coursefiles/FADML18S/algo1.pdf · Title: algo1 Created Date: 1/8/2018 2:54:08 AM

L1.22Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson

Introduction to Algorithms

Q-notation

Math:Q(g(n)) = { f (n) : there exist positive constants c1, c2, and

n0 such that 0 £ c1 g(n) £ f (n) £ c2 g(n) for all n ³ n0}

Engineering:• Drop low-order terms; ignore leading constants.• Example: 3n3 + 90n2 – 5n + 6046 =Q(n3)

Page 21: algo1 - cse.iitkgp.ac.incse.iitkgp.ac.in/~sourangshu/coursefiles/FADML18S/algo1.pdf · Title: algo1 Created Date: 1/8/2018 2:54:08 AM

Copyright © 2001-5 Erik D. Demaine and Charles E. LeisersonIntroduction to Algorithms L1.23

Asymptotic performance

n

T(n)

n0

• Real-world design situations often call for a careful balancing of engineering objectives.

• Asymptotic analysis is a useful tool to help to structure our thinking.

When n gets large enough, a Q(n2) algorithmalways beats a Q(n3) algorithm.

• We shouldn’t ignore asymptotically slower algorithms, however.

Page 22: algo1 - cse.iitkgp.ac.incse.iitkgp.ac.in/~sourangshu/coursefiles/FADML18S/algo1.pdf · Title: algo1 Created Date: 1/8/2018 2:54:08 AM

Introduction to Algorithms L1.24

Insertionsort analysisWorst case: Input reverse sorted.

nT (n) =åQ( j) =Q(n2)

n

[arithmetic series]j=2

Average case: All permutations equally likely.

T (n) =åQ( j / 2) =Q(n2)j=2

Is insertion sort a fast sorting algorithm?• Moderately so, for small n.• Not at all, for large n.

Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson

Page 23: algo1 - cse.iitkgp.ac.incse.iitkgp.ac.in/~sourangshu/coursefiles/FADML18S/algo1.pdf · Title: algo1 Created Date: 1/8/2018 2:54:08 AM

Analysis