Algorithms and Data Structures Conditional...

42
Algorithms and Data Structures Fabian Kuhn Lecture 2 Runtime Analysis, Sorting II Algorithms and Data Structures Conditional Course Fabian Kuhn Algorithms and Complexity

Transcript of Algorithms and Data Structures Conditional...

Page 1: Algorithms and Data Structures Conditional Courseac.informatik.uni-freiburg.de/teaching/ss_20/ad...Fabian Kuhn Algorithms and Data Structures So far: Number of basic operations is

Algorithms and Data StructuresFabian Kuhn

Lecture 2

Runtime Analysis, Sorting II

Algorithms and Data StructuresConditional Course

Fabian Kuhn

Algorithms and Complexity

Page 2: Algorithms and Data Structures Conditional Courseac.informatik.uni-freiburg.de/teaching/ss_20/ad...Fabian Kuhn Algorithms and Data Structures So far: Number of basic operations is

Algorithms and Data StructuresFabian Kuhn

โ€ข How can we analyze the runtime of an algorithm?โ€“ runtime is different on different computersโ€ฆ

โ€“ depends on compiler, programming language, etc.

โ€ข We need an abstract measure to express the runtime

โ€ข Idea: Count the number of (basic) operationsโ€“ instead of directly measuring the time

โ€“ the number of basic operations is independent of computer, compiler

โ€“ It is a good measure for the runtime if all basic operations require about thesame time.

2

Runtime Analysis I

Page 3: Algorithms and Data Structures Conditional Courseac.informatik.uni-freiburg.de/teaching/ss_20/ad...Fabian Kuhn Algorithms and Data Structures So far: Number of basic operations is

Algorithms and Data StructuresFabian Kuhn

What is a basic operation?

โ€ข Simple arithmetic operations / comparisonsโ€“ +, -, *, /, % (mod), <, >, ==, โ€ฆ

โ€ข One memory accessโ€“ reading or writing a variable

โ€“ not clear if this is really a basic operation?

โ€ข One function callโ€“ Of course only jumping to the function code

โ€ข Intuitively: one line of program code

โ€ข Better: one line of assembly language code

โ€ข Even better (?): one processor cycle

โ€ข We will see: It is only important that the number of basic opertionsis roughly proportional to the actual running time.

3

Basic Operations

Page 4: Algorithms and Data Structures Conditional Courseac.informatik.uni-freiburg.de/teaching/ss_20/ad...Fabian Kuhn Algorithms and Data Structures So far: Number of basic operations is

Algorithms and Data StructuresFabian Kuhn

RAM = Random Access Machine

โ€ข Standard model to analyze algorithms!

โ€ข Basic operations (as โ€œdefinedโ€) all require one time unit

โ€ข In particular, all memory accesses are equally expensive:

Each memory cell (1 machine word) can be read or written in 1 time unitโ€“ In particular ignores memory hierarchies

โ€“ In most cases, it is however a reasonable assumption

โ€ข There are alternative abstract models:โ€“ to explicitly capture memory hierarchies

โ€“ for huge data volumes (cf. big data)

โ€ข e.g.: streaming-models: memory has to be read sequentially

โ€“ for distributed / parallel architectures

โ€ข memory access can be local or over the networkโ€ฆ

4

RAM Model

Page 5: Algorithms and Data Structures Conditional Courseac.informatik.uni-freiburg.de/teaching/ss_20/ad...Fabian Kuhn Algorithms and Data Structures So far: Number of basic operations is

Algorithms and Data StructuresFabian Kuhn

So far: Number of basic operations is proportional to the runtime

โ€ข We can also achieve this without counting thebasic operations exactly!

Simplification 1: We only calculate an upper bound (or a lowerbound) on the number of basic operations

โ€“ such that the upper / lower bound is still proportional to the runtimeโ€ฆ

โ€ข No. of basic op. can depend on several properties of the inputโ€“ Size/length of input, but, e.g., for sorting also the ordering in the input

Simplification 2: Most important parameter is input size ๐‘›We always consider the runtime ๐‘‡(๐‘›) as a function of ๐‘›.

โ€“ And we ignore other properties of the input

5

Runtime analysis II

Page 6: Algorithms and Data Structures Conditional Courseac.informatik.uni-freiburg.de/teaching/ss_20/ad...Fabian Kuhn Algorithms and Data Structures So far: Number of basic operations is

Algorithms and Data StructuresFabian Kuhn

SelectionSort(A):

1: for i=0 to n-2 do

2: minIdx = i

3: for j=i to n-1 do

4: if A[j] < A[minIdx] then

5: minIdx = j

6: swap(A[i], A[minIdx])

6

Selection Sort: Analysis

#basic op. โ‰ค ๐‘ โ‹… #inner for loop iterations

๐‘ฅ(๐‘›)

โ‰ค ๐‘1

โ‰ค ๐‘2

โ‰ค ๐‘3

๐‘ฅ ๐‘› =

๐‘–=0

๐‘›โˆ’2

๐‘› โˆ’ ๐‘– =

โ„Ž=2

๐‘›

โ„Ž โ‰ค

โ„Ž=1

๐‘›

โ„Ž =๐‘› ๐‘› + 1

2โ‰ค ๐‘›2

Page 7: Algorithms and Data Structures Conditional Courseac.informatik.uni-freiburg.de/teaching/ss_20/ad...Fabian Kuhn Algorithms and Data Structures So far: Number of basic operations is

Algorithms and Data StructuresFabian Kuhn

SelectionSort(A):

1: for i=0 to n-2 do

2: minIdx = i

3: for j=i to n-1 do

4: if A[j] < A[minIdx] then

5: minIdx = j

6: swap(A[i], A[minIdx])

7

Selection Sort: Analysis

โ‰ค ๐‘1

โ‰ค ๐‘2

โ‰ค ๐‘3

Runtime ๐‘ป ๐’ โ‰ค ๐’„ โ‹… ๐’๐Ÿ ๐‘ป ๐’ โ‰ฅ ๐’„๐Ÿโ€ฒ โ‹… ๐’๐Ÿ

๐‘‡(๐‘›)

โ‰ฅ ๐‘2โ€ฒ

#basic op. โ‰ค ๐‘ โ‹… #inner for loop iterations

๐‘ฅ ๐‘› โ‰ค ๐‘›2

Page 8: Algorithms and Data Structures Conditional Courseac.informatik.uni-freiburg.de/teaching/ss_20/ad...Fabian Kuhn Algorithms and Data Structures So far: Number of basic operations is

Algorithms and Data StructuresFabian Kuhn

๐‘ป(๐’): Number of basic operations of Selection Sort algorithms for arrays of length ๐‘›

Lemma: There is a constant ๐’„๐‘ผ > ๐ŸŽ, such that ๐‘ป ๐’ โ‰ค ๐’„๐‘ผ โ‹… ๐’๐Ÿ

Lemma: There is a constant ๐’„๐‘ณ > ๐ŸŽ, such that ๐‘ป ๐’ โ‰ฅ ๐’„๐‘ณ โ‹… ๐’๐Ÿ

8

Selection Sort: Analysis

Page 9: Algorithms and Data Structures Conditional Courseac.informatik.uni-freiburg.de/teaching/ss_20/ad...Fabian Kuhn Algorithms and Data Structures So far: Number of basic operations is

Algorithms and Data StructuresFabian Kuhn

Summary

โ€ข We can only obtain a value that is proportional to the runtime.

โ€ข However, we also do not want anything else:โ€“ Analysis should be independent of computer / compiler / etc.

โ€“ We want to have statements that are valid in 10 / 100 /โ€ฆ years

โ€ข We will always get statements of the following form:

There is a constant ๐ถ, such that

๐‘‡ ๐‘› โ‰ค ๐ถ โ‹… ๐‘“ ๐‘› or ๐‘‡ ๐‘› โ‰ฅ ๐ถ โ‹… ๐‘“(๐‘›)

โ€ข The Big-O notation allows to simplify / generalize this kind ofstatementsโ€ฆ

9

Runtime analysis

Page 10: Algorithms and Data Structures Conditional Courseac.informatik.uni-freiburg.de/teaching/ss_20/ad...Fabian Kuhn Algorithms and Data Structures So far: Number of basic operations is

Algorithms and Data StructuresFabian Kuhn

โ€ข Formalism to describe the asymptotic growth of functions.โ€“ For formal definitions: see next slideโ€ฆ

โ€ข There is a const. ๐ถ > 0, s. t. ๐‘‡ ๐‘› โ‰ค ๐ถ โ‹… ๐‘“(๐‘›) becomes:

๐‘‡ ๐‘› โˆˆ ๐‘‚(๐‘“ ๐‘› )

โ€ข There is a const. ๐ถ > 0, s. t. ๐‘‡ ๐‘› โ‰ฅ ๐ถ โ‹… ๐‘”(๐‘›) becomes:

๐‘‡ ๐‘› โˆˆ ฮฉ(๐‘” ๐‘› )

โ€ข For Selection Sort:

10

Big-O Notation

๐‘ป ๐’ โˆˆ ๐‘ถ ๐’๐Ÿ

๐‘ป ๐’ โˆˆ ๐›€ ๐’๐Ÿ๐‘ป ๐’ โˆˆ ๐šฏ ๐’๐Ÿ

Page 11: Algorithms and Data Structures Conditional Courseac.informatik.uni-freiburg.de/teaching/ss_20/ad...Fabian Kuhn Algorithms and Data Structures So far: Number of basic operations is

Algorithms and Data StructuresFabian Kuhn

๐‘ถ ๐’ˆ ๐’ โ‰” ๐’‡ ๐’ | โˆƒ๐’„, ๐’๐ŸŽ > ๐ŸŽ โˆ€๐’ โ‰ฅ ๐’๐ŸŽ โˆถ ๐’‡ ๐’ โ‰ค ๐’„ โ‹… ๐’ˆ(๐’)

โ€ข Function ๐‘“ ๐‘› โˆˆ ๐‘‚(๐‘” ๐‘› ), if there are constants ๐‘ and ๐‘›0 s. t. ๐‘“ ๐‘› โ‰ค ๐‘ โ‹… ๐‘”(๐‘›) for all ๐‘› โ‰ฅ ๐‘›0

๐›€ ๐’ˆ ๐’ โ‰” ๐’‡ ๐’ | โˆƒ๐’„, ๐’๐ŸŽ > ๐ŸŽ โˆ€๐’ โ‰ฅ ๐’๐ŸŽ โˆถ ๐’‡ ๐’ โ‰ฅ ๐’„ โ‹… ๐’ˆ(๐’)

โ€ข Function ๐‘“ ๐‘› โˆˆ ฮฉ(๐‘” ๐‘› ), if there are constants ๐‘ and ๐‘›0 s. t. ๐‘“ ๐‘› โ‰ฅ ๐‘ โ‹… ๐‘”(๐‘›) for all ๐‘› โ‰ฅ ๐‘›0

๐šฏ ๐’ˆ ๐’ โ‰” ๐‘ถ ๐’ˆ ๐’ โˆฉ ๐›€ ๐’ˆ ๐’

โ€ข Function ๐‘“ ๐‘› โˆˆ ฮ˜(๐‘” ๐‘› ), if there are constants ๐‘1, ๐‘2 and ๐‘›0 s. t. ๐‘1 โ‹… ๐‘” ๐‘› โ‰ค ๐‘“ ๐‘› โ‰ค ๐‘2 โ‹… ๐‘”(๐‘›) for all ๐‘› โ‰ฅ ๐‘›0, resp. if๐‘“ ๐‘› โˆˆ ๐‘‚(๐‘›) and ๐‘“ ๐‘› โˆˆ ฮฉ(๐‘›)

11

Big-O Notation : Definitions

Page 12: Algorithms and Data Structures Conditional Courseac.informatik.uni-freiburg.de/teaching/ss_20/ad...Fabian Kuhn Algorithms and Data Structures So far: Number of basic operations is

Algorithms and Data StructuresFabian Kuhn

o ๐’ˆ ๐’ โ‰” ๐’‡ ๐’ | โˆ€๐’„ > ๐ŸŽ โˆƒ๐’๐ŸŽ > ๐ŸŽ โˆ€๐’ โ‰ฅ ๐’๐ŸŽ โˆถ ๐’‡ ๐’ โ‰ค ๐’„ โ‹… ๐’ˆ(๐’)

โ€ข Function ๐‘“ ๐‘› โˆˆ ๐‘œ(๐‘” ๐‘› ), if for all constants ๐‘ > 0, we have ๐‘“ ๐‘› โ‰ค ๐‘ โ‹… ๐‘”(๐‘›) (for sufficiently large ๐‘›, indep. of ๐‘)

๐Ž ๐’ˆ ๐’ โ‰” ๐’‡ ๐’ | โˆ€๐’„ > ๐ŸŽ โˆƒ๐’๐ŸŽ > ๐ŸŽ โˆ€๐’ โ‰ฅ ๐’๐ŸŽ โˆถ ๐’‡ ๐’ โ‰ฅ ๐’„ โ‹… ๐’ˆ(๐’)

โ€ข Function ๐‘“ ๐‘› โˆˆ ๐œ”(๐‘” ๐‘› ), if for all constants ๐‘ > 0, we have ๐‘“ ๐‘› โ‰ฅ ๐‘ โ‹… ๐‘”(๐‘›) (for sufficiently large ๐‘›, indep. of ๐‘)

In particular:

๐‘“ ๐‘› โˆˆ ๐‘œ ๐‘” ๐‘› โŸน ๐‘“ ๐‘› โˆˆ ๐‘‚ ๐‘” ๐‘›

๐‘“ ๐‘› โˆˆ ๐œ” ๐‘” ๐‘› โŸน ๐‘“ ๐‘› โˆˆ ฮฉ ๐‘” ๐‘›

12

Big-O Notation : Definitions

Page 13: Algorithms and Data Structures Conditional Courseac.informatik.uni-freiburg.de/teaching/ss_20/ad...Fabian Kuhn Algorithms and Data Structures So far: Number of basic operations is

Algorithms and Data StructuresFabian Kuhn

๐’‡ ๐’ โˆˆ ๐‘ถ ๐’ˆ ๐’ :

โ€ข ๐‘“ ๐‘› " โ‰ค " ๐‘”(๐‘›), asymptoticallyโ€ฆ

โ€ข ๐‘“(๐‘›) asymptotically grows at most as fast as ๐‘”(๐‘›)

๐’‡ ๐’ โˆˆ ๐›€ ๐’ˆ ๐’ :

โ€ข ๐‘“ ๐‘› " โ‰ฅ " ๐‘”(๐‘›), asymptoticallyโ€ฆ

โ€ข ๐‘“(๐‘›) asymptotically grows at least as fast as ๐‘”(๐‘›)

๐’‡ ๐’ โˆˆ ๐šฏ ๐’ˆ ๐’ :

โ€ข ๐‘“ ๐‘› " = " ๐‘”(๐‘›), asymptoticallyโ€ฆ

โ€ข ๐‘“(๐‘›) asymptotically grows equally fast as ๐‘”(๐‘›)

13

Big-O Notation : Intuitively

Page 14: Algorithms and Data Structures Conditional Courseac.informatik.uni-freiburg.de/teaching/ss_20/ad...Fabian Kuhn Algorithms and Data Structures So far: Number of basic operations is

Algorithms and Data StructuresFabian Kuhn

๐’‡ ๐’ โˆˆ ๐’ ๐’ˆ ๐’ :

โ€ข ๐‘“ ๐‘› " < " ๐‘”(๐‘›), asymptoticallyโ€ฆ

โ€ข ๐‘“(๐‘›) asymptotically grows slower than ๐‘”(๐‘›)

๐’‡ ๐’ โˆˆ ๐Ž ๐’ˆ ๐’ :

โ€ข ๐‘“ ๐‘› " > " ๐‘”(๐‘›), asymptoticallyโ€ฆ

โ€ข ๐‘“(๐‘›) asymptotically grows faster than ๐‘”(๐‘›)

If ๐‘“(๐‘›) and ๐‘”(๐‘›) grow monotonically, we have:

๐‘“ ๐‘› โˆˆ ๐‘œ ๐‘” ๐‘› โŸบ ๐‘“ ๐‘› โˆ‰ ฮฉ ๐‘” ๐‘›

๐‘“ ๐‘› โˆˆ ๐œ” ๐‘” ๐‘› โŸบ ๐‘“ ๐‘› โˆ‰ ๐‘‚ ๐‘” ๐‘›

14

Big-O Notation : Intuitively

Page 15: Algorithms and Data Structures Conditional Courseac.informatik.uni-freiburg.de/teaching/ss_20/ad...Fabian Kuhn Algorithms and Data Structures So far: Number of basic operations is

Algorithms and Data StructuresFabian Kuhn

The following definitions hold for monotonically growing functions

๐‘“ ๐‘› โˆˆ ๐‘‚ ๐‘” ๐‘› , lim๐‘›โ†’โˆž

๐‘“ ๐‘›

๐‘” ๐‘›< โˆž

๐‘“ ๐‘› โˆˆ ฮฉ ๐‘” ๐‘› , lim๐‘›โ†’โˆž

๐‘“ ๐‘›

๐‘” ๐‘›> 0

๐‘“ ๐‘› โˆˆ ฮ˜ ๐‘” ๐‘› , 0 < lim๐‘›โ†’โˆž

๐‘“ ๐‘›

๐‘” ๐‘›< โˆž

๐‘“ ๐‘› โˆˆ ๐‘œ ๐‘” ๐‘› , lim๐‘›โ†’โˆž

๐‘“ ๐‘›

๐‘” ๐‘›= 0

๐‘“ ๐‘› โˆˆ ๐œ” ๐‘” ๐‘› , lim๐‘›โ†’โˆž

๐‘“ ๐‘›

๐‘” ๐‘›= โˆž

15

Definition by Limits (simplified)

Page 16: Algorithms and Data Structures Conditional Courseac.informatik.uni-freiburg.de/teaching/ss_20/ad...Fabian Kuhn Algorithms and Data Structures So far: Number of basic operations is

Algorithms and Data StructuresFabian Kuhn

Writing Convention:

โ€ข ๐‘‚ ๐‘” ๐‘› , ฮฉ ๐‘” ๐‘› , โ€ฆ are sets (of functions)

โ€ข Correct way of writing (in principle): ๐‘“ ๐‘› โˆˆ ๐‘‚ ๐‘” ๐‘›

โ€ข Very common way of writing: ๐‘“ ๐‘› = ๐‘‚ ๐‘” ๐‘›

Examples:

โ€ข ๐‘‡ ๐‘› = ๐‘‚(๐‘›2) instead ๐‘‡ ๐‘› โˆˆ ๐‘‚ ๐‘›2

โ€ข ๐‘‡ ๐‘› = ฮฉ(๐‘›2) instead ๐‘‡ ๐‘› โˆˆ ฮฉ ๐‘›2

โ€ข ๐‘“ ๐‘› = ๐‘›2 + ๐‘‚(๐‘›) :

๐‘“ ๐‘› โˆˆ ๐‘” ๐‘› โˆถ โˆƒโ„Ž ๐‘› โˆˆ ๐‘‚ ๐‘› s. t. ๐‘” ๐‘› = ๐‘›2 + โ„Ž ๐‘›

โ€ข ๐‘Ž ๐‘› = 1 + ๐‘œ 1 โ‹… ๐‘ ๐‘›

16

Big-O Notation : Remarks

Page 17: Algorithms and Data Structures Conditional Courseac.informatik.uni-freiburg.de/teaching/ss_20/ad...Fabian Kuhn Algorithms and Data Structures So far: Number of basic operations is

Algorithms and Data StructuresFabian Kuhn

Writing Convention:

โ€ข ๐‘‚ ๐‘” ๐‘› , ฮฉ ๐‘” ๐‘› , โ€ฆ are sets (of functions)

โ€ข Correct way of writing (in principle): ๐‘“ ๐‘› โˆˆ ๐‘‚ ๐‘” ๐‘›

โ€ข Very common way of writing: ๐‘“ ๐‘› = ๐‘‚ ๐‘” ๐‘›

Asymptotic Behavior of General Limits:

โ€ข Same notation is used more generally, e.g., ๐‘“(๐‘ฅ) for ๐‘ฅ โ†’ 0

โ€ข E.g., Taylor approx.: ๐‘’๐‘ฅ = 1 + ๐‘ฅ + ๐‘‚(๐‘ฅ2), or ๐‘’๐‘ฅ = 1 + ๐‘ฅ + ๐‘œ ๐‘ฅ

Alternative Definition for ๐›€ ๐’ˆ ๐’ :

๐›€ ๐’ˆ ๐’ โ‰” ๐’‡ ๐’ | โˆƒ๐’„, ๐’๐ŸŽ > ๐ŸŽ โˆ€๐’ โ‰ฅ ๐’๐ŸŽ โˆถ ๐’‡ ๐’ โ‰ฅ ๐’„ โ‹… ๐’ˆ(๐’)

๐›€ ๐’ˆ ๐’ โ‰” ๐’‡ ๐’ | โˆƒ๐’„ > ๐ŸŽ โˆ€๐’๐ŸŽ > ๐ŸŽ โˆƒ๐’ โ‰ฅ ๐’๐ŸŽ โˆถ ๐’‡ ๐’ โ‰ฅ ๐’„ โ‹… ๐’ˆ(๐’)

โ€“ We will use the 1st definition

โ€“ The two definitions are only different for non-monotonic functions

17

Big-O Notation : Remarks

Page 18: Algorithms and Data Structures Conditional Courseac.informatik.uni-freiburg.de/teaching/ss_20/ad...Fabian Kuhn Algorithms and Data Structures So far: Number of basic operations is

Algorithms and Data StructuresFabian Kuhn

Selection Sort:

โ€ข Runtime ๐‘‡ ๐‘› , there are constants ๐‘1, ๐‘2 : ๐‘1๐‘›2 โ‰ค ๐‘‡ ๐‘› โ‰ค ๐‘2๐‘›

2

๐‘‡ ๐‘› โˆˆ ๐‘‚ ๐‘›2 , ๐‘‡ ๐‘› โˆˆ ฮฉ ๐‘›2 , ๐‘‡ ๐‘› โˆˆ ฮ˜ ๐‘›2

โ€ข ๐‘‡(๐‘›) grows more than linear in ๐‘›: ๐‘‡ ๐‘› โˆˆ ๐œ”(๐‘›)

Further examples:

โ€ข ๐‘“ ๐‘› = 10๐‘›3, ๐‘” ๐‘› = ฮค๐‘›3 1000 :

โ€ข ๐‘“ ๐‘› = ๐‘’๐‘›, ๐‘” ๐‘› = ๐‘›100 :

โ€ข ๐‘“ ๐‘› = ฮค๐‘› log2 ๐‘› , ๐‘” ๐‘› = ๐‘› :

โ€ข ๐‘“ ๐‘› = ๐‘› ฮค1 256, ๐‘” ๐‘› = 10 ln ๐‘› :

โ€ข ๐‘“ ๐‘› = log10 ๐‘› , ๐‘” ๐‘› = log2 ๐‘› :

โ€ข ๐‘“ ๐‘› = ๐‘› ๐‘›, ๐‘” ๐‘› = 2๐‘› :

18

Big-O Notation : Examples

lim๐‘›โ†’โˆž

๐‘’๐‘›

๐‘›100โ†’ โˆž

๐‘“ ๐‘›

๐‘” ๐‘›=

๐‘›

log2 ๐‘›

=2 ฮค๐‘ก 2

๐‘ก

๐‘“ ๐‘› โˆˆ ฮ˜ ๐‘” ๐‘›

๐‘“ ๐‘› โˆˆ ๐œ” ๐‘” ๐‘›

๐‘“ ๐‘› โˆˆ ๐œ” ๐‘” ๐‘›

๐‘“ ๐‘› โˆˆ ๐œ” ๐‘” ๐‘›

๐‘“ ๐‘› โˆˆ ฮ˜ ๐‘” ๐‘›

๐‘“ ๐‘› โˆˆ o ๐‘” ๐‘›

log ๐‘› ๐‘› = ๐‘› โ‹… log ๐‘› , log 2๐‘› = ๐‘›

log10 ๐‘› =log2 ๐‘›

log2 10

Page 19: Algorithms and Data Structures Conditional Courseac.informatik.uni-freiburg.de/teaching/ss_20/ad...Fabian Kuhn Algorithms and Data Structures So far: Number of basic operations is

Algorithms and Data StructuresFabian Kuhn

InsertionSort(A):

1: for i = 1 to n-1 do

2: // prefix A[1..i] is already sorted

3: pos = i

4: while (pos > 0) and (A[pos] < A[pos-1]) do

5: swap(A[pos], A[posโ€“1])

6: pos = pos - 1

19

Analysis Insertion Sort

Page 20: Algorithms and Data Structures Conditional Courseac.informatik.uni-freiburg.de/teaching/ss_20/ad...Fabian Kuhn Algorithms and Data Structures So far: Number of basic operations is

Algorithms and Data StructuresFabian Kuhn

Worst Case Analysis

โ€ข Analyze runtime ๐‘‡(๐‘›) for a worst possible input of size ๐‘›

โ€ข Important / standard way of analyzing algorithms

Best Case Analyse

โ€ข Analyze runtime ๐‘‡(๐‘›) for a best possible input of size ๐‘›

โ€ข Usually not very interestingโ€ฆ

Average Case Analyse

โ€ข Analyze runtime ๐‘‡(๐‘›) for a typical input of size ๐‘›

โ€ข Problem: what is a typical input?โ€“ Standard approach: use a random input

โ€“ Not clear, how close real inputs and random inputs areโ€ฆ

โ€“ Possible alternative: smoothed analysis (we will not look at this)

20

Worst case, best case, average case

Page 21: Algorithms and Data Structures Conditional Courseac.informatik.uni-freiburg.de/teaching/ss_20/ad...Fabian Kuhn Algorithms and Data Structures So far: Number of basic operations is

Algorithms and Data StructuresFabian Kuhn

Quadratic = 2x as large input 4x as long runtimeโ€“ For large ๐‘›, this already seems to grow quite fastโ€ฆ

Example calculation:

โ€ข Assume that the number of basic operations ๐‘‡ ๐‘› = ๐‘›2

โ€ข Additionally, assume there is 1 basic operation per processor cycle

โ€ข For a 1Ghz processor, we get 1 ns per basic operation

21

How good is quadratic runtime?

Input size ๐’ 4 bytes per number Runtime ๐‘ป(๐’)

103 numbers โ‰ˆ 4KB 103โ‹…2 โ‹… 10โˆ’9 s = 1 ms

106 numbers โ‰ˆ 4MB 106โ‹…2 โ‹… 10โˆ’9 s = 16.7 min

109 numbers โ‰ˆ 4GB 109โ‹…2 โ‹… 10โˆ’9 s = 31.7 years

too slow for large problems!

Page 22: Algorithms and Data Structures Conditional Courseac.informatik.uni-freiburg.de/teaching/ss_20/ad...Fabian Kuhn Algorithms and Data Structures So far: Number of basic operations is

Algorithms and Data StructuresFabian Kuhn

โ€ข Divide is trivial cost ๐‘‚(1)

โ€ข Recursive sorting: We will look at this...

โ€ข Merge: We will look at this first...

22

Analysis Merge Sort

Divide

Sort recursively(by using mergesort)

Merge

Page 23: Algorithms and Data Structures Conditional Courseac.informatik.uni-freiburg.de/teaching/ss_20/ad...Fabian Kuhn Algorithms and Data Structures So far: Number of basic operations is

Algorithms and Data StructuresFabian Kuhn

MergeSortRecursive(A, start, end, tmp) // sort A[start..end-1]โ‹ฎ

5: pos = start; i = start; j = middle

6: while (pos < end) do

7: if (i < middle) and (A[i] < A[j]) then

8: tmp[pos] = A[i]; pos++; i++

9: else

10: tmp[pos] = A[j]; pos++; j++

11: for i = start to end-1 do A[i] = tmp[i]

23

Analysis Merge Step

Page 24: Algorithms and Data Structures Conditional Courseac.informatik.uni-freiburg.de/teaching/ss_20/ad...Fabian Kuhn Algorithms and Data Structures So far: Number of basic operations is

Algorithms and Data StructuresFabian Kuhn

Runtime ๐‘‡(๐‘›) consists of:

โ€ข Divide and Merge: ๐‘‚ ๐‘›

โ€ข 2 recursive calls to sort โŒˆ ฮค๐‘› 2โŒ‰ and ฮค๐‘› 2 elements

Recursive formulation of ๐‘ป(๐’):

โ€ข There is a constant ๐‘ > 0, s. t.

๐‘‡ ๐‘› โ‰ค ๐‘‡๐‘›

2+ ๐‘‡

๐‘›

2+ ๐‘ โ‹… ๐‘›, T 1 โ‰ค ๐‘

โ€ข We simplify a bit and ignore all the rounding:

๐‘ป ๐’ โ‰ค ๐Ÿ โ‹… ๐‘ป๐’

๐Ÿ+ ๐’ƒ โ‹… ๐’, ๐‘ป ๐Ÿ โ‰ค ๐’ƒ

24

Analysis Merge Sort

Page 25: Algorithms and Data Structures Conditional Courseac.informatik.uni-freiburg.de/teaching/ss_20/ad...Fabian Kuhn Algorithms and Data Structures So far: Number of basic operations is

Algorithms and Data StructuresFabian Kuhn

๐‘ป ๐’ โ‰ค ๐Ÿ โ‹… ๐‘ป๐’

๐Ÿ+ ๐’ƒ โ‹… ๐’, ๐‘ป ๐Ÿ โ‰ค ๐’ƒ

Letโ€™s just try and see what we getโ€ฆ

25

Analysis Merge Sort

Page 26: Algorithms and Data Structures Conditional Courseac.informatik.uni-freiburg.de/teaching/ss_20/ad...Fabian Kuhn Algorithms and Data Structures So far: Number of basic operations is

Algorithms and Data StructuresFabian Kuhn

Recursive equation: ๐‘‡ ๐‘› โ‰ค 2 โ‹… ๐‘‡๐‘›

2+ ๐‘ โ‹… ๐‘›, ๐‘‡ 1 โ‰ค ๐‘

Guess: ๐‘‡ ๐‘› โ‰ค ๐‘ โ‹… ๐‘› โ‹… 1 + log2 ๐‘›

Proof by induction:

26

Analysis Merge Sort

Page 27: Algorithms and Data Structures Conditional Courseac.informatik.uni-freiburg.de/teaching/ss_20/ad...Fabian Kuhn Algorithms and Data Structures So far: Number of basic operations is

Algorithms and Data StructuresFabian Kuhn

Recursive equation: ๐‘‡ ๐‘› โ‰ค 2 โ‹… ๐‘‡๐‘›

2+ ๐‘ โ‹… ๐‘›, ๐‘‡ 1 โ‰ค ๐‘

Consider the recursion tree:

27

Alternative Analysis of Merge Sort

Page 28: Algorithms and Data Structures Conditional Courseac.informatik.uni-freiburg.de/teaching/ss_20/ad...Fabian Kuhn Algorithms and Data Structures So far: Number of basic operations is

Algorithms and Data StructuresFabian Kuhn 28

Merge Sort Measurements

Page 29: Algorithms and Data Structures Conditional Courseac.informatik.uni-freiburg.de/teaching/ss_20/ad...Fabian Kuhn Algorithms and Data Structures So far: Number of basic operations is

Algorithms and Data StructuresFabian Kuhn 29

Merge Sort Measurements

Page 30: Algorithms and Data Structures Conditional Courseac.informatik.uni-freiburg.de/teaching/ss_20/ad...Fabian Kuhn Algorithms and Data Structures So far: Number of basic operations is

Algorithms and Data StructuresFabian Kuhn

The runtime of Merge Sort is ๐‘ป ๐’ โˆˆ ๐‘ถ(๐’ โ‹… ๐ฅ๐จ๐ ๐’).

โ€ข grows almost linearly with the input size ๐‘›โ€ฆ

How good is this?

โ€ข Example calculation:โ€“ Again assume that 1 basic operation = 1 ns

โ€“ We will be a bit more conservative than before and assume that๐‘‡ ๐‘› = 10 โ‹… ๐‘› log ๐‘›

30

Summary Analysis Merge Sort

Input size ๐’ 4 byte numbers Runtime ๐‘ป ๐’ = ๐Ÿ๐ŸŽ โ‹… ๐’ ๐ฅ๐จ๐  ๐’ ๐’๐Ÿ

210 โ‰ˆ 103numbers โ‰ˆ 4KB 10 โ‹… 10 โ‹… 210 โ‹… 10โˆ’9 s โ‰ˆ 0.1 ms 1 ms

220 โ‰ˆ 106 numbers โ‰ˆ 4MB 10 โ‹… 20 โ‹… 220 โ‹… 10โˆ’9 s โ‰ˆ 0.2 s 16.7 min

230 โ‰ˆ 109 numbers โ‰ˆ 4GB 10 โ‹… 30 โ‹… 230 โ‹… 10โˆ’9 s โ‰ˆ 5.4 min 31.7 years

240 โ‰ˆ 1012numbers โ‰ˆ 4TB 10 โ‹… 40 โ‹… 240 โ‹… 10โˆ’9 s โ‰ˆ 122 h > 107 years

Page 31: Algorithms and Data Structures Conditional Courseac.informatik.uni-freiburg.de/teaching/ss_20/ad...Fabian Kuhn Algorithms and Data Structures So far: Number of basic operations is

Algorithms and Data StructuresFabian Kuhn

โ€ข Runtime depends on how we choose the pivots

โ€ข Runtime to sort array of size ๐‘› if pivot partitions array into parts of sizes ๐œ†๐‘› and 1 โˆ’ ๐œ† ๐‘›:

๐‘ป ๐’ = ๐‘ป ๐€๐’ + ๐‘ป ๐Ÿ โˆ’ ๐€ ๐’ + "๐…๐ข๐ง๐ ๐ฉ๐ข๐ฏ๐จ๐ญ + ๐ƒ๐ข๐ฏ๐ข๐๐žโ€œ

โ€ข Divide:โ€“ We iterate over the array from both sides, ๐‘‚ 1 cost per step Time to partition array of length ๐‘›: ๐‘‚(๐‘›)

31

Quick Sort : Analysis

๐‘ฅ

Divide

Sort recursively(by using quicksort)

Page 32: Algorithms and Data Structures Conditional Courseac.informatik.uni-freiburg.de/teaching/ss_20/ad...Fabian Kuhn Algorithms and Data Structures So far: Number of basic operations is

Algorithms and Data StructuresFabian Kuhn

If we can also find a pivot in time ๐‘‚(๐‘›) such that such that the array is partitioned into parts of sizes ๐œ†๐‘› and 1 โˆ’ ๐œ† ๐‘›:

โ€ข There is a constant ๐‘ > 0, s. t.

๐‘‡ ๐‘› โ‰ค ๐‘‡ ๐œ†๐‘› + ๐‘‡ 1 โˆ’ ๐œ† ๐‘› + ๐‘ โ‹… ๐‘›, ๐‘‡ 1 โ‰ค ๐‘

Extreme case I) ๐œ† = ฮค1 2 (best case):

๐‘‡ ๐‘› โ‰ค 2๐‘‡๐‘›

2+ ๐‘๐‘›, ๐‘‡ 1 โ‰ค ๐‘

โ€ข As for Merge Sort: ๐‘‡ ๐‘› โˆˆ ๐‘‚ ๐‘› log ๐‘›

Extreme case II) ๐œ†๐‘› = 1, 1 โˆ’ ๐œ† ๐‘› = ๐‘› โˆ’ 1 (worst case):

๐‘‡ ๐‘› = ๐‘‡ ๐‘› โˆ’ 1 + ๐‘๐‘›, ๐‘‡ 1 โ‰ค ๐‘

32

Quick Sort : Analysis

Page 33: Algorithms and Data Structures Conditional Courseac.informatik.uni-freiburg.de/teaching/ss_20/ad...Fabian Kuhn Algorithms and Data Structures So far: Number of basic operations is

Algorithms and Data StructuresFabian Kuhn

Extreme case II) ๐œ†๐‘› = 1, 1 โˆ’ ๐œ† ๐‘› = ๐‘› โˆ’ 1 (worst case):

๐‘‡ ๐‘› = ๐‘‡ ๐‘› โˆ’ 1 + ๐‘๐‘›, ๐‘‡ 1 โ‰ค ๐‘

In this case, we obtain ๐‘‡ ๐‘› โˆˆ ฮ˜ ๐‘›2 :

33

Quick Sort : Worst Case Analysis

Page 34: Algorithms and Data Structures Conditional Courseac.informatik.uni-freiburg.de/teaching/ss_20/ad...Fabian Kuhn Algorithms and Data Structures So far: Number of basic operations is

Algorithms and Data StructuresFabian Kuhn

Partition For Random Pivot:

โ€ข Runtime ๐‘ป ๐’ = ๐‘ถ(๐’ ๐ฅ๐จ๐ ๐’) for all inputsโ€“ but only in Erwartungswert and with very high probability

Intuition:

โ€ข With probability ฮค1 2, we get parts of size โ‰ฅ ฮค๐‘› 4, s. t.

๐‘‡ ๐‘› โ‰ค ๐‘‡๐‘›

4+ ๐‘‡

3๐‘›

4+ ๐‘๐‘›

34

Quick Sort With a Random Pivot

Page 35: Algorithms and Data Structures Conditional Courseac.informatik.uni-freiburg.de/teaching/ss_20/ad...Fabian Kuhn Algorithms and Data Structures So far: Number of basic operations is

Algorithms and Data StructuresFabian Kuhn

Partition For Random Pivot:

โ€ข Runtime ๐‘ป ๐’ = ๐‘ถ(๐’ ๐ฅ๐จ๐ ๐’) for all inputsโ€“ but only in Erwartungswert and with very high probability

Analysis:

โ€ข We will not do this hereโ€“ see, e.g., Cormen et al. or the algorithm theory lecture

โ€ข Possible approach: write recursion in terms of expected values

๐”ผ ๐‘‡ ๐‘› โ‰ค ๐”ผ ๐‘‡ ๐‘๐ฟ + ๐‘‡ ๐‘› โˆ’ ๐‘๐ฟ + ๐‘๐‘›

35

Quick Sort With a Random Pivot

Page 36: Algorithms and Data Structures Conditional Courseac.informatik.uni-freiburg.de/teaching/ss_20/ad...Fabian Kuhn Algorithms and Data Structures So far: Number of basic operations is

Algorithms and Data StructuresFabian Kuhn

Task: Sort sequence ๐‘Ž1, ๐‘Ž2, โ€ฆ , ๐‘Ž๐‘›โ€ข Goal: lower bound (worst-case) runtime

Comparison-based sorting algorithms

โ€ข Comparisons are the only allowed way to determine the relative order between elements

โ€ข Hence, the only thing that can influence the sequence of elements in the final sorted sequence are comparisons of the kind

๐‘Ž๐‘– = ๐‘Ž๐‘— , ๐‘Ž๐‘– โ‰ค ๐‘Ž๐‘— , ๐‘Ž๐‘– < ๐‘Ž๐‘— , ๐‘Ž๐‘– โ‰ฅ ๐‘Ž๐‘— , ๐‘Ž๐‘– > ๐‘Ž๐‘—

โ€ข If we assume that the elements are pair-wise distinct, we only need comparisons of the form ๐‘Ž๐‘– โ‰ค ๐‘Ž๐‘—

โ€ข 1 comparison = 1 basic operation

36

Sorting Lower Bound

Page 37: Algorithms and Data Structures Conditional Courseac.informatik.uni-freiburg.de/teaching/ss_20/ad...Fabian Kuhn Algorithms and Data Structures So far: Number of basic operations is

Algorithms and Data StructuresFabian Kuhn

Alternative View

โ€ข Every program (for a deterministic, comp.-based sorting alg.) can be brought into a form where every if/while/โ€ฆ-condition is of the following form:

๐ข๐Ÿ ๐‘Ž๐‘– โ‰ค ๐‘Ž๐‘— ๐ญ๐ก๐ž๐ง โ€ฆ

โ€ข In each execution of an algorithm, the results of these comparisons induce a sequence of T/F (true/false) values:

๐“๐…๐…๐“๐“๐“๐…๐“๐…๐…๐“๐“๐…๐…๐…๐…๐…๐“๐…๐“๐“๐“โ€ฆ

โ€ข This sequence uniquely determines how the values of the array are rearranged (permuted) by the algorithm.

โ€ข Different inputs with the same values therefore must lead todifferent T/F sequences.

37

Comparison-Based Sorting Algorithms

Page 38: Algorithms and Data Structures Conditional Courseac.informatik.uni-freiburg.de/teaching/ss_20/ad...Fabian Kuhn Algorithms and Data Structures So far: Number of basic operations is

Algorithms and Data StructuresFabian Kuhn

Execution tree:

38

Comparison-Based Sorting Algorithms

Page 39: Algorithms and Data Structures Conditional Courseac.informatik.uni-freiburg.de/teaching/ss_20/ad...Fabian Kuhn Algorithms and Data Structures So far: Number of basic operations is

Algorithms and Data StructuresFabian Kuhn

โ€ข In comparison-based sorting algorithms, the execution depends on the initial ordering of the values in the inputs, but it does not depend on the actual values.โ€“ We restrict to cases where the values are all distinct.

โ€ข W.l.o.g. we can assume that we have to sort the numbers 1,โ€ฆ , ๐‘›.

โ€ข Different inputs have to be handled differently.

โ€ข Different inputs result in different T/F sequences

โ€ข Runtime of an execution โ‰ฅ length of the resulting T/F sequence

โ€ข Worst-Case runtime โ‰ฅ Length of longest T/F sequence:โ€“ We want a lower bound

โ€“ Count no. of possible inputs we need at least as many T/F sequences...

39

Comp.-Based Sorting: Lower Bound

Page 40: Algorithms and Data Structures Conditional Courseac.informatik.uni-freiburg.de/teaching/ss_20/ad...Fabian Kuhn Algorithms and Data Structures So far: Number of basic operations is

Algorithms and Data StructuresFabian Kuhn

Number of possible inputs (input orderings):

Number of T/F sequences of length โ‰ค ๐‘˜:

Theorem: Every comparison-based sorting algorithm requiresฮฉ ๐‘› โ‹… log ๐‘› comparisons in the worst case.

40

Comp.-Based Sorting: Lower Bound

Page 41: Algorithms and Data Structures Conditional Courseac.informatik.uni-freiburg.de/teaching/ss_20/ad...Fabian Kuhn Algorithms and Data Structures So far: Number of basic operations is

Algorithms and Data StructuresFabian Kuhn

โ€ข Not possible with comparison-based algorithmsโ€“ Lower bound also holds for randomized algorithms...

โ€ข Sometimes, we can be fasterโ€“ If we can exploit special properties of the input

โ€ข Example: Sort ๐‘› numbers ๐‘Ž๐‘– โˆˆ 0,1 :

1. Count number of zeroes and ones in time ๐‘‚(๐‘›)

2. Write solution to array in time ๐‘‚ ๐‘›

41

Sorting in Linear Time

Page 42: Algorithms and Data Structures Conditional Courseac.informatik.uni-freiburg.de/teaching/ss_20/ad...Fabian Kuhn Algorithms and Data Structures So far: Number of basic operations is

Algorithms and Data StructuresFabian Kuhn

Task:

โ€ข Sort integer array ๐ด of length ๐‘›

โ€ข We know that for all ๐‘– โˆˆ {0,โ€ฆ , ๐‘›}, ๐ด ๐‘– โˆˆ {0,โ€ฆ , ๐‘˜}

Algorithm:

1: counts = new int[k+1] // new int array of length k

2: for i = 0 to k do counts[i] = 0

3: for i = 0 to n-1 do counts[A[i]]++

4: i = 0;

5: for j = 0 to k do

6: for l = 1 to counts[j] do

7: A[i] = j; i++

42

Counting Sort