datenstrukturen und algorithmenexercise 1 fs 2020 1 schedule for today 1 exercise process 2...

79
Datenstrukturen und Algorithmen Exercise 1 FS 2020 1

Upload: others

Post on 01-Sep-2021

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Datenstrukturen und Algorithmen

Exercise 1

FS 2020

1

Page 2: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Schedule for today

1 Exercise Process

2 Repetition Theory

Aysmptotic Running Time

3 Programming Exercise

2

Page 3: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Process for the exercisesMon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun

handout

kick-off

handin

discussion

Ü Ü

3

Page 4: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Process for the exercisesMon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun

handout

kick-off

handin

discussion

Ü Ü

Thursday:

Handout of new exercise sheet (online per Code Expert).Submission of old exercise sheet (online per Code Expert).

Friday during exercise class:

Kick-off presentation of new exercise sheet.Discussion of old exercise sheet.Opportunity to ask questions about lecture and exercises.

3

Page 5: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

2. Repetition Theory

4

Page 6: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Warm-up

What is a problem?

What is an algorithm?

Ü well-defined computing procedure to compute output data from input data.

What is a program?

Ü Concrete implementation of an algorithm

5

Page 7: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Warm-up

What is a problem?What is an algorithm?

Ü well-defined computing procedure to compute output data from input data.

What is a program?

Ü Concrete implementation of an algorithm

5

Page 8: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Warm-up

What is a problem?What is an algorithm?

Ü well-defined computing procedure to compute output data from input data.

What is a program?

Ü Concrete implementation of an algorithm

5

Page 9: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Warm-up

What is a problem?What is an algorithm?

Ü well-defined computing procedure to compute output data from input data.

What is a program?

Ü Concrete implementation of an algorithm

5

Page 10: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Warm-up

What is a problem?What is an algorithm?

Ü well-defined computing procedure to compute output data from input data.

What is a program?

Ü Concrete implementation of an algorithm

5

Page 11: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Warm-up

Problem

Algorithm

solves a

Program

implements an

6

Page 12: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Warm-up

Problem

Algorithms

can be solved by multiple

Program

can be implemented in various ways

7

Page 13: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Efficiency

Problem Complexity Minimal (asymptotic) cost over all algo-rithms that solve the problem.

Algorithm Cost Number of elementary operationsProgram Computing time Measurable value on an actual machine.

Ü Estimation of cost or computing time depending on the input size,denoted by n.

8

Page 14: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Efficiency

Problem Complexity Minimal (asymptotic) cost over all algo-rithms that solve the problem.

Algorithm Cost Number of elementary operationsProgram Computing time Measurable value on an actual machine.

Ü Estimation of cost or computing time depending on the input size,denoted by n.

8

Page 15: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Asymptotic behavior

What are Ω(g(n)), Θ(g(n)), O(g(n))?

Ü Sets of functions!

Repetition, sets A,B:subset A ⊆ Bproper subset A Bintersection A ∩B

9

Page 16: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Asymptotic behavior

What are Ω(g(n)), Θ(g(n)), O(g(n))?Ü Sets of functions!

Repetition, sets A,B:subset A ⊆ Bproper subset A Bintersection A ∩B

9

Page 17: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Asymptotic behavior

What are Ω(g(n)), Θ(g(n)), O(g(n))?Ü Sets of functions!

Repetition, sets A,B:subset A ⊆ Bproper subset A Bintersection A ∩B

9

Page 18: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Asymptotic behavior

Given: function f : N→ R.

Definition:

O(g) = f : N→ R+|∃c > 0, n0 ∈ N|∀n ≥ n0 : f(n) ≤ c · g(n)

Ω(g) = f : N→ R+|∃c > 0, n0 ∈ N|∀n ≥ n0 : c · g(n) ≤ f(n)

Θ(g) = O(g) ∩ Ω(g)

10

Page 19: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Used slightly more seldomGiven: function f : N→ R.

Definition:

O(g) = f : N→ R+|∃c > 0, n0 ∈ N|∀n ≥ n0 : f(n) ≤ c · g(n)

o(g) = f : N→ R+|∀c > 0 ∃n0 ∈ N|∀n ≥ n0 : f(n) ≤ c · g(n)

Ω(g) = f : N→ R+|∃c > 0, n0 ∈ N|∀n ≥ n0 : c · g(n) ≤ f(n)

ω(g) = f : N→ R+|∀c > 0 ∃n0 ∈ N|∀n ≥ n0 : ·g(n) ≤ f(n)

f ∈ o(g): f grows much slower than g

f ∈ ω(g): f grows much faster than g11

Page 20: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Useful information for the exerciseTheorem

1 limn→∞f(n)g(n) = 0⇒ f ∈ O(g), O(f) ( O(g).

2 limn→∞f(n)g(n) = C > 0 (C constant)⇒ f ∈ Θ(g).

3f(n)g(n) →n→∞∞⇒ g ∈ O(f), O(g) ( O(f).

Example1 limn→∞

nn2 = 0⇒ n ∈ O(n2), O(n) ( O(n2).

2 limn→∞2nn = 2 > 0⇒ 2n ∈ Θ(n).

3n2

n →n→∞∞⇒ n ∈ O(n2), O(n) ( O(n2).

12

Page 21: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Useful information for the exerciseTheorem

1 limn→∞f(n)g(n) = 0⇒ f ∈ O(g), O(f) ( O(g).

2 limn→∞f(n)g(n) = C > 0 (C constant)⇒ f ∈ Θ(g).

3f(n)g(n) →n→∞∞⇒ g ∈ O(f), O(g) ( O(f).

Example1 limn→∞

nn2 = 0⇒ n ∈ O(n2), O(n) ( O(n2).

2 limn→∞2nn = 2 > 0⇒ 2n ∈ Θ(n).

3n2

n →n→∞∞⇒ n ∈ O(n2), O(n) ( O(n2).

12

Page 22: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Property

f1 ∈ O(g), f2 ∈ O(g)⇒ f1 + f2 ∈ O(g)

13

Page 23: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Examples

O(g) = f : N→ R| ∃c > 0,∃n0 ∈ N : ∀n ≥ n0 : 0 ≤ f(n) ≤ c · g(n)

f(n) f ∈ O(?) Example3n + 4

O(n) c = 4, n0 = 4

2n

O(n) c = 2, n0 = 0

n2 + 100n

O(n2) c = 2, n0 = 100

n +√n

O(n) c = 2, n0 = 1

14

Page 24: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Examples

O(g) = f : N→ R| ∃c > 0,∃n0 ∈ N : ∀n ≥ n0 : 0 ≤ f(n) ≤ c · g(n)

f(n) f ∈ O(?) Example3n + 4 O(n) c = 4, n0 = 42n

O(n) c = 2, n0 = 0

n2 + 100n

O(n2) c = 2, n0 = 100

n +√n

O(n) c = 2, n0 = 1

14

Page 25: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Examples

O(g) = f : N→ R| ∃c > 0,∃n0 ∈ N : ∀n ≥ n0 : 0 ≤ f(n) ≤ c · g(n)

f(n) f ∈ O(?) Example3n + 4 O(n) c = 4, n0 = 42n O(n) c = 2, n0 = 0n2 + 100n

O(n2) c = 2, n0 = 100

n +√n

O(n) c = 2, n0 = 1

14

Page 26: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Examples

O(g) = f : N→ R| ∃c > 0,∃n0 ∈ N : ∀n ≥ n0 : 0 ≤ f(n) ≤ c · g(n)

f(n) f ∈ O(?) Example3n + 4 O(n) c = 4, n0 = 42n O(n) c = 2, n0 = 0n2 + 100n O(n2) c = 2, n0 = 100n +√n

O(n) c = 2, n0 = 1

14

Page 27: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Examples

O(g) = f : N→ R| ∃c > 0,∃n0 ∈ N : ∀n ≥ n0 : 0 ≤ f(n) ≤ c · g(n)

f(n) f ∈ O(?) Example3n + 4 O(n) c = 4, n0 = 42n O(n) c = 2, n0 = 0n2 + 100n O(n2) c = 2, n0 = 100n +√n O(n) c = 2, n0 = 1

14

Page 28: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Examples

n ∈ O(n2) correct, but too imprecise:n ∈ O(n) and even n ∈ Θ(n).3n2 ∈ O(2n2) correct but uncommon:Omit constants: 3n2 ∈ O(n2).

2n2 ∈ O(n) is wrong: 2n2

n = 2n →n→∞

∞ !

O(n) ⊆ O(n2) is correctΘ(n) ⊆ Θ(n2) is wrong n 6∈ Ω(n2) ⊃ Θ(n2)

15

Page 29: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Examples

n ∈ O(n2)

correct, but too imprecise:n ∈ O(n) and even n ∈ Θ(n).

3n2 ∈ O(2n2) correct but uncommon:Omit constants: 3n2 ∈ O(n2).

2n2 ∈ O(n) is wrong: 2n2

n = 2n →n→∞

∞ !

O(n) ⊆ O(n2) is correctΘ(n) ⊆ Θ(n2) is wrong n 6∈ Ω(n2) ⊃ Θ(n2)

15

Page 30: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Examples

n ∈ O(n2) correct, but too imprecise:

n ∈ O(n) and even n ∈ Θ(n).

3n2 ∈ O(2n2) correct but uncommon:Omit constants: 3n2 ∈ O(n2).

2n2 ∈ O(n) is wrong: 2n2

n = 2n →n→∞

∞ !

O(n) ⊆ O(n2) is correctΘ(n) ⊆ Θ(n2) is wrong n 6∈ Ω(n2) ⊃ Θ(n2)

15

Page 31: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Examples

n ∈ O(n2) correct, but too imprecise:n ∈ O(n) and even n ∈ Θ(n).

3n2 ∈ O(2n2) correct but uncommon:Omit constants: 3n2 ∈ O(n2).

2n2 ∈ O(n) is wrong: 2n2

n = 2n →n→∞

∞ !

O(n) ⊆ O(n2) is correctΘ(n) ⊆ Θ(n2) is wrong n 6∈ Ω(n2) ⊃ Θ(n2)

15

Page 32: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Examples

n ∈ O(n2) correct, but too imprecise:n ∈ O(n) and even n ∈ Θ(n).

3n2 ∈ O(2n2) correct but uncommon:Omit constants: 3n2 ∈ O(n2).

2n2 ∈ O(n) is wrong: 2n2

n = 2n →n→∞

∞ !

O(n) ⊆ O(n2) is correctΘ(n) ⊆ Θ(n2) is wrong n 6∈ Ω(n2) ⊃ Θ(n2)

15

Page 33: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Examples

n ∈ O(n2) correct, but too imprecise:n ∈ O(n) and even n ∈ Θ(n).3n2 ∈ O(2n2)

correct but uncommon:Omit constants: 3n2 ∈ O(n2).

2n2 ∈ O(n) is wrong: 2n2

n = 2n →n→∞

∞ !

O(n) ⊆ O(n2) is correctΘ(n) ⊆ Θ(n2) is wrong n 6∈ Ω(n2) ⊃ Θ(n2)

15

Page 34: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Examples

n ∈ O(n2) correct, but too imprecise:n ∈ O(n) and even n ∈ Θ(n).3n2 ∈ O(2n2) correct but uncommon:

Omit constants: 3n2 ∈ O(n2).

2n2 ∈ O(n) is wrong: 2n2

n = 2n →n→∞

∞ !

O(n) ⊆ O(n2) is correctΘ(n) ⊆ Θ(n2) is wrong n 6∈ Ω(n2) ⊃ Θ(n2)

15

Page 35: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Examples

n ∈ O(n2) correct, but too imprecise:n ∈ O(n) and even n ∈ Θ(n).3n2 ∈ O(2n2) correct but uncommon:Omit constants: 3n2 ∈ O(n2).

2n2 ∈ O(n) is wrong: 2n2

n = 2n →n→∞

∞ !

O(n) ⊆ O(n2) is correctΘ(n) ⊆ Θ(n2) is wrong n 6∈ Ω(n2) ⊃ Θ(n2)

15

Page 36: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Examples

n ∈ O(n2) correct, but too imprecise:n ∈ O(n) and even n ∈ Θ(n).3n2 ∈ O(2n2) correct but uncommon:Omit constants: 3n2 ∈ O(n2).

2n2 ∈ O(n) is wrong: 2n2

n = 2n →n→∞

∞ !

O(n) ⊆ O(n2) is correctΘ(n) ⊆ Θ(n2) is wrong n 6∈ Ω(n2) ⊃ Θ(n2)

15

Page 37: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Examples

n ∈ O(n2) correct, but too imprecise:n ∈ O(n) and even n ∈ Θ(n).3n2 ∈ O(2n2) correct but uncommon:Omit constants: 3n2 ∈ O(n2).

2n2 ∈ O(n)

is wrong: 2n2

n = 2n →n→∞

∞ !

O(n) ⊆ O(n2) is correctΘ(n) ⊆ Θ(n2) is wrong n 6∈ Ω(n2) ⊃ Θ(n2)

15

Page 38: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Examples

n ∈ O(n2) correct, but too imprecise:n ∈ O(n) and even n ∈ Θ(n).3n2 ∈ O(2n2) correct but uncommon:Omit constants: 3n2 ∈ O(n2).

2n2 ∈ O(n) is wrong:

2n2

n = 2n →n→∞

∞ !

O(n) ⊆ O(n2) is correctΘ(n) ⊆ Θ(n2) is wrong n 6∈ Ω(n2) ⊃ Θ(n2)

15

Page 39: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Examples

n ∈ O(n2) correct, but too imprecise:n ∈ O(n) and even n ∈ Θ(n).3n2 ∈ O(2n2) correct but uncommon:Omit constants: 3n2 ∈ O(n2).

2n2 ∈ O(n) is wrong: 2n2

n = 2n →n→∞

∞ !

O(n) ⊆ O(n2) is correctΘ(n) ⊆ Θ(n2) is wrong n 6∈ Ω(n2) ⊃ Θ(n2)

15

Page 40: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Examples

n ∈ O(n2) correct, but too imprecise:n ∈ O(n) and even n ∈ Θ(n).3n2 ∈ O(2n2) correct but uncommon:Omit constants: 3n2 ∈ O(n2).

2n2 ∈ O(n) is wrong: 2n2

n = 2n →n→∞

∞ !

O(n) ⊆ O(n2) is correctΘ(n) ⊆ Θ(n2) is wrong n 6∈ Ω(n2) ⊃ Θ(n2)

15

Page 41: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Examples

n ∈ O(n2) correct, but too imprecise:n ∈ O(n) and even n ∈ Θ(n).3n2 ∈ O(2n2) correct but uncommon:Omit constants: 3n2 ∈ O(n2).

2n2 ∈ O(n) is wrong: 2n2

n = 2n →n→∞

∞ !

O(n) ⊆ O(n2)

is correct

Θ(n) ⊆ Θ(n2) is wrong n 6∈ Ω(n2) ⊃ Θ(n2)

15

Page 42: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Examples

n ∈ O(n2) correct, but too imprecise:n ∈ O(n) and even n ∈ Θ(n).3n2 ∈ O(2n2) correct but uncommon:Omit constants: 3n2 ∈ O(n2).

2n2 ∈ O(n) is wrong: 2n2

n = 2n →n→∞

∞ !

O(n) ⊆ O(n2) is correct

Θ(n) ⊆ Θ(n2) is wrong n 6∈ Ω(n2) ⊃ Θ(n2)

15

Page 43: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Examples

n ∈ O(n2) correct, but too imprecise:n ∈ O(n) and even n ∈ Θ(n).3n2 ∈ O(2n2) correct but uncommon:Omit constants: 3n2 ∈ O(n2).

2n2 ∈ O(n) is wrong: 2n2

n = 2n →n→∞

∞ !

O(n) ⊆ O(n2) is correct

Θ(n) ⊆ Θ(n2) is wrong n 6∈ Ω(n2) ⊃ Θ(n2)

15

Page 44: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Examples

n ∈ O(n2) correct, but too imprecise:n ∈ O(n) and even n ∈ Θ(n).3n2 ∈ O(2n2) correct but uncommon:Omit constants: 3n2 ∈ O(n2).

2n2 ∈ O(n) is wrong: 2n2

n = 2n →n→∞

∞ !

O(n) ⊆ O(n2) is correctΘ(n) ⊆ Θ(n2)

is wrong n 6∈ Ω(n2) ⊃ Θ(n2)

15

Page 45: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Examples

n ∈ O(n2) correct, but too imprecise:n ∈ O(n) and even n ∈ Θ(n).3n2 ∈ O(2n2) correct but uncommon:Omit constants: 3n2 ∈ O(n2).

2n2 ∈ O(n) is wrong: 2n2

n = 2n →n→∞

∞ !

O(n) ⊆ O(n2) is correctΘ(n) ⊆ Θ(n2) is wrong

n 6∈ Ω(n2) ⊃ Θ(n2)

15

Page 46: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Examples

n ∈ O(n2) correct, but too imprecise:n ∈ O(n) and even n ∈ Θ(n).3n2 ∈ O(2n2) correct but uncommon:Omit constants: 3n2 ∈ O(n2).

2n2 ∈ O(n) is wrong: 2n2

n = 2n →n→∞

∞ !

O(n) ⊆ O(n2) is correctΘ(n) ⊆ Θ(n2) is wrong n 6∈ Ω(n2) ⊃ Θ(n2)

15

Page 47: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Quiz

1 ∈ O(15) ?

3 better 1 ∈ O(1)2n + 1 ∈ Θ(n) ? 3√n ∈ O(n) ? 3√n ∈ Ω(n) ? 7

n ∈ Ω(√n) ? 3√

n /∈ Θ(n) ? 3O(√n) ⊂ O(n) ? 3

2n /∈ O(exp(n)) ? 7

16

Page 48: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Quiz

1 ∈ O(15) ? 3 better 1 ∈ O(1)

2n + 1 ∈ Θ(n) ? 3√n ∈ O(n) ? 3√n ∈ Ω(n) ? 7

n ∈ Ω(√n) ? 3√

n /∈ Θ(n) ? 3O(√n) ⊂ O(n) ? 3

2n /∈ O(exp(n)) ? 7

16

Page 49: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Quiz

1 ∈ O(15) ? 3 better 1 ∈ O(1)2n + 1 ∈ Θ(n) ?

3√n ∈ O(n) ? 3√n ∈ Ω(n) ? 7

n ∈ Ω(√n) ? 3√

n /∈ Θ(n) ? 3O(√n) ⊂ O(n) ? 3

2n /∈ O(exp(n)) ? 7

16

Page 50: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Quiz

1 ∈ O(15) ? 3 better 1 ∈ O(1)2n + 1 ∈ Θ(n) ? 3

√n ∈ O(n) ? 3√n ∈ Ω(n) ? 7

n ∈ Ω(√n) ? 3√

n /∈ Θ(n) ? 3O(√n) ⊂ O(n) ? 3

2n /∈ O(exp(n)) ? 7

16

Page 51: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Quiz

1 ∈ O(15) ? 3 better 1 ∈ O(1)2n + 1 ∈ Θ(n) ? 3√n ∈ O(n) ?

3√n ∈ Ω(n) ? 7

n ∈ Ω(√n) ? 3√

n /∈ Θ(n) ? 3O(√n) ⊂ O(n) ? 3

2n /∈ O(exp(n)) ? 7

16

Page 52: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Quiz

1 ∈ O(15) ? 3 better 1 ∈ O(1)2n + 1 ∈ Θ(n) ? 3√n ∈ O(n) ? 3

√n ∈ Ω(n) ? 7

n ∈ Ω(√n) ? 3√

n /∈ Θ(n) ? 3O(√n) ⊂ O(n) ? 3

2n /∈ O(exp(n)) ? 7

16

Page 53: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Quiz

1 ∈ O(15) ? 3 better 1 ∈ O(1)2n + 1 ∈ Θ(n) ? 3√n ∈ O(n) ? 3√n ∈ Ω(n) ?

7n ∈ Ω(

√n) ? 3√

n /∈ Θ(n) ? 3O(√n) ⊂ O(n) ? 3

2n /∈ O(exp(n)) ? 7

16

Page 54: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Quiz

1 ∈ O(15) ? 3 better 1 ∈ O(1)2n + 1 ∈ Θ(n) ? 3√n ∈ O(n) ? 3√n ∈ Ω(n) ? 7

n ∈ Ω(√n) ? 3√

n /∈ Θ(n) ? 3O(√n) ⊂ O(n) ? 3

2n /∈ O(exp(n)) ? 7

16

Page 55: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Quiz

1 ∈ O(15) ? 3 better 1 ∈ O(1)2n + 1 ∈ Θ(n) ? 3√n ∈ O(n) ? 3√n ∈ Ω(n) ? 7

n ∈ Ω(√n) ?

3√n /∈ Θ(n) ? 3O(√n) ⊂ O(n) ? 3

2n /∈ O(exp(n)) ? 7

16

Page 56: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Quiz

1 ∈ O(15) ? 3 better 1 ∈ O(1)2n + 1 ∈ Θ(n) ? 3√n ∈ O(n) ? 3√n ∈ Ω(n) ? 7

n ∈ Ω(√n) ? 3

√n /∈ Θ(n) ? 3O(√n) ⊂ O(n) ? 3

2n /∈ O(exp(n)) ? 7

16

Page 57: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Quiz

1 ∈ O(15) ? 3 better 1 ∈ O(1)2n + 1 ∈ Θ(n) ? 3√n ∈ O(n) ? 3√n ∈ Ω(n) ? 7

n ∈ Ω(√n) ? 3√

n /∈ Θ(n) ?

3O(√n) ⊂ O(n) ? 3

2n /∈ O(exp(n)) ? 7

16

Page 58: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Quiz

1 ∈ O(15) ? 3 better 1 ∈ O(1)2n + 1 ∈ Θ(n) ? 3√n ∈ O(n) ? 3√n ∈ Ω(n) ? 7

n ∈ Ω(√n) ? 3√

n /∈ Θ(n) ? 3

O(√n) ⊂ O(n) ? 3

2n /∈ O(exp(n)) ? 7

16

Page 59: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Quiz

1 ∈ O(15) ? 3 better 1 ∈ O(1)2n + 1 ∈ Θ(n) ? 3√n ∈ O(n) ? 3√n ∈ Ω(n) ? 7

n ∈ Ω(√n) ? 3√

n /∈ Θ(n) ? 3O(√n) ⊂ O(n) ?

32n /∈ O(exp(n)) ? 7

16

Page 60: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Quiz

1 ∈ O(15) ? 3 better 1 ∈ O(1)2n + 1 ∈ Θ(n) ? 3√n ∈ O(n) ? 3√n ∈ Ω(n) ? 7

n ∈ Ω(√n) ? 3√

n /∈ Θ(n) ? 3O(√n) ⊂ O(n) ? 3

2n /∈ O(exp(n)) ? 7

16

Page 61: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Quiz

1 ∈ O(15) ? 3 better 1 ∈ O(1)2n + 1 ∈ Θ(n) ? 3√n ∈ O(n) ? 3√n ∈ Ω(n) ? 7

n ∈ Ω(√n) ? 3√

n /∈ Θ(n) ? 3O(√n) ⊂ O(n) ? 3

2n /∈ O(exp(n)) ?

7

16

Page 62: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Quiz

1 ∈ O(15) ? 3 better 1 ∈ O(1)2n + 1 ∈ Θ(n) ? 3√n ∈ O(n) ? 3√n ∈ Ω(n) ? 7

n ∈ Ω(√n) ? 3√

n /∈ Θ(n) ? 3O(√n) ⊂ O(n) ? 3

2n /∈ O(exp(n)) ? 7

16

Page 63: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Quiz

1 ∈ O(15) ? 3 better 1 ∈ O(1)2n + 1 ∈ Θ(n) ? 3√n ∈ O(n) ? 3√n ∈ Ω(n) ? 7

n ∈ Ω(√n) ? 3√

n /∈ Θ(n) ? 3O(√n) ⊂ O(n) ? 3

2n /∈ O(exp(n)) ? 7

16

Page 64: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Quiz: A good strategy?

... Then I simply buy a new machine

If today I can solve a problem ofsize n, then with a 10 or 100 times faster machine I can solve ...1

Komplexität (speed ×10) (speed ×100)

log2 n

n→ n10 n→ n100

n

n→ 10 · n n→ 100 · n

n2

n→ 3.16 · n n→ 10 · n

2n

n→ n + 3.32 n→ n + 6.64

1

To see this, you set f(n′) = c · f(n) (c = 10 or c = 100) and solve for n′

17

Page 65: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Quiz: A good strategy?

... Then I simply buy a new machine If today I can solve a problem ofsize n, then with a 10 or 100 times faster machine I can solve ...1

Komplexität (speed ×10) (speed ×100)

log2 n

n→ n10 n→ n100

n

n→ 10 · n n→ 100 · n

n2

n→ 3.16 · n n→ 10 · n

2n

n→ n + 3.32 n→ n + 6.64

1

To see this, you set f(n′) = c · f(n) (c = 10 or c = 100) and solve for n′

17

Page 66: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Quiz: A good strategy?

... Then I simply buy a new machine If today I can solve a problem ofsize n, then with a 10 or 100 times faster machine I can solve ...1

Komplexität (speed ×10) (speed ×100)

log2 n n→ n10 n→ n100

n

n→ 10 · n n→ 100 · n

n2

n→ 3.16 · n n→ 10 · n

2n

n→ n + 3.32 n→ n + 6.64

1

To see this, you set f(n′) = c · f(n) (c = 10 or c = 100) and solve for n′

17

Page 67: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Quiz: A good strategy?

... Then I simply buy a new machine If today I can solve a problem ofsize n, then with a 10 or 100 times faster machine I can solve ...1

Komplexität (speed ×10) (speed ×100)

log2 n n→ n10 n→ n100

n n→ 10 · n n→ 100 · n

n2

n→ 3.16 · n n→ 10 · n

2n

n→ n + 3.32 n→ n + 6.64

1

To see this, you set f(n′) = c · f(n) (c = 10 or c = 100) and solve for n′

17

Page 68: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Quiz: A good strategy?

... Then I simply buy a new machine If today I can solve a problem ofsize n, then with a 10 or 100 times faster machine I can solve ...1

Komplexität (speed ×10) (speed ×100)

log2 n n→ n10 n→ n100

n n→ 10 · n n→ 100 · n

n2 n→ 3.16 · n n→ 10 · n

2n

n→ n + 3.32 n→ n + 6.64

1

To see this, you set f(n′) = c · f(n) (c = 10 or c = 100) and solve for n′

17

Page 69: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Quiz: A good strategy?

... Then I simply buy a new machine If today I can solve a problem ofsize n, then with a 10 or 100 times faster machine I can solve ...1

Komplexität (speed ×10) (speed ×100)

log2 n n→ n10 n→ n100

n n→ 10 · n n→ 100 · n

n2 n→ 3.16 · n n→ 10 · n

2n n→ n + 3.32 n→ n + 6.64

1To see this, you set f(n′) = c · f(n) (c = 10 or c = 100) and solve for n′

17

Page 70: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Asymptotic Running Times with Θ

void run(int n)for (int i = 1; i<n; ++i)

for (int j = 1; j<n; ++j)op();

How often is op() called?

18

Page 71: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Asymptotic Running Times with Θ

void run(int n)for (int i = 1; i<n; ++i)

for (int j = i; j<n; ++j)op();

How often is op() called?

19

Page 72: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Asymptotic Running Times with Θ

void run(int n)for (int i = 1; i<n; ++i)

op();for (int j = i; j<n; ++j)

op();

How often is op() called?

20

Page 73: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Asymptotic Running Times with Θ

void run(int n)for(int i = 1; i <= n; ++i)

for(int j = 1; j∗j <= n; ++j)for(int k = n; k >= 2; −−k)

op();

How often is op() called?

21

Page 74: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

3. Programming Exercise

22

Page 75: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Sum in sub-interval (naive algorithm)

Input: A sequence of n numbers (a0, a1, . . . , an−1) and a sub-intervalI = [x0, x1]

Output:∑x1

i=x0ai.

S ← 0for i ∈ x0, . . . , x1 doS ← S + ai

return S

IdeaUse the prefix sum to compute the sum of arbitrary sub-intervalswith constant complexityGeneralization to two dimensions.

23

Page 76: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Sum in sub-interval (naive algorithm)

Input: A sequence of n numbers (a0, a1, . . . , an−1) and a sub-intervalI = [x0, x1]

Output:∑x1

i=x0ai.

S ← 0for i ∈ x0, . . . , x1 doS ← S + ai

return S

IdeaUse the prefix sum to compute the sum of arbitrary sub-intervalswith constant complexityGeneralization to two dimensions.

23

Page 77: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Multidimensional vectors

Definitionstd::vector< std::vector<int> > my_vec( n_rows,

std::vector<int>(n_cols,init_value) );

Indexingmy_vec[row][col]

24

Page 78: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Classes

class Insurance // Definitionpublic: // public section

Insurance(double rate) rate_ = rate; // Construktordouble get_rate() return rate_; // member function

private: // private sectiondouble rate_; // data member

;

int main() Insurance insurance(2.);std::cout << insurance.get_rate();return 0;

25

Page 79: Datenstrukturen und AlgorithmenExercise 1 FS 2020 1 Schedule for today 1 Exercise Process 2 Repetition Theory Aysmptotic Running Time 3 Programming Exercise 2 Process for the exercises

Questions or Suggestions?

26