Informatik II Ubung 4 FS 2020 1 Program Today Survey - - PowerPoint PPT Presentation

informatik ii
SMART_READER_LITE
LIVE PREVIEW

Informatik II Ubung 4 FS 2020 1 Program Today Survey - - PowerPoint PPT Presentation

Informatik II Ubung 4 FS 2020 1 Program Today Survey Productive Failure Case Study 1 1 Feedback of last exercise 2 Repetition Theory 3 Analysis of programs Analysis of recurrences Divide & Conquer Sorting Algorithms 4 2 3.


slide-1
SLIDE 1

Informatik II

¨ Ubung 4

FS 2020

1

slide-2
SLIDE 2

Program Today

1

Survey Productive Failure Case Study 1

2

Feedback of last exercise

3

Repetition Theory Analysis of programs Analysis of recurrences

4

Divide & Conquer Sorting Algorithms

2

slide-3
SLIDE 3
  • 3. Repetition Theory

3

slide-4
SLIDE 4

Analysis

How many calls to f()?

for(unsigned i = 1; i <= n/3; i += 3) for(unsigned j = 1; j <= i; ++j) f();

4

slide-5
SLIDE 5

Analysis

How many calls to f()?

for(unsigned i = 1; i <= n/3; i += 3) for(unsigned j = 1; j <= i; ++j) f();

The code fragment implies Θ(n2) calls to f(): the outer loop is executed n/9 times and the inner loop contains i calls to f().

4

slide-6
SLIDE 6

Analysis

How many calls to f()?

for(unsigned i = 0; i < n; ++i) { for(unsigned j = 100; j∗j >= 1; −−j) f(); for(unsigned k = 1; k <= n; k ∗= 2) f(); }

5

slide-7
SLIDE 7

Analysis

How many calls to f()?

for(unsigned i = 0; i < n; ++i) { for(unsigned j = 100; j∗j >= 1; −−j) f(); for(unsigned k = 1; k <= n; k ∗= 2) f(); }

We can ignore the first inner loop because it contains only a constant number of calls to f()

5

slide-8
SLIDE 8

Analysis

How many calls to f()?

for(unsigned i = 0; i < n; ++i) { for(unsigned j = 100; j∗j >= 1; −−j) f(); for(unsigned k = 1; k <= n; k ∗= 2) f(); }

We can ignore the first inner loop because it contains only a constant number of calls to f() The second inner loop contains ⌊log2(n)⌋ + 1 calls to f(). Summing up yields Θ(n log(n)) calls.

5

slide-9
SLIDE 9

Analysis

How many calls to f() in g(n), depending on n > 0?

void g(int n){ if (n>1){ g(n/2); } else{ f(); } }

6

slide-10
SLIDE 10

Analysis

How many calls to f() in g(n), depending on n > 0?

void g(int n){ if (n>1){ g(n/2); } else{ f(); } }

There is only a single call to f() at the end of the recursion.

6

slide-11
SLIDE 11

Analysis

How many calls to f() in g(n), depending on n > 0?

void g(int n){ if (n>1){ g(n−1); } f(); }

7

slide-12
SLIDE 12

Analysis

How many calls to f() in g(n), depending on n > 0?

void g(int n){ if (n>1){ g(n−1); } f(); }

Recurrence

T(n) =

  • T(n − 1) + 1

n > 1 1 n = 1

7

slide-13
SLIDE 13

Analysis

How many calls to f() in g(n), depending on n > 0?

void g(int n){ if (n>1){ g(n−1); } f(); }

Recurrence

T(n) =

  • T(n − 1) + 1

n > 1 1 n = 1 ∈ Θ(n)

7

slide-14
SLIDE 14

Analysis

How many calls to f() in g(n), depending on n = 2k?

void g(int n){ if (n>1){ g(n/2); g(n/2); } else{ f(); } }

8

slide-15
SLIDE 15

Analysis

How many calls to f() in g(n), depending on n = 2k?

void g(int n){ if (n>1){ g(n/2); g(n/2); } else{ f(); } }

Recurrence

T(n) =

  • 2T(n/2)

n > 1 1 n = 1

8

slide-16
SLIDE 16

Analysis

How many calls to f() in g(n), depending on n = 2k?

void g(int n){ if (n>1){ g(n/2); g(n/2); } else{ f(); } }

Recurrence

T(n) =

  • 2T(n/2)

n > 1 1 n = 1 ∈ Θ(n)

8

slide-17
SLIDE 17

Analysis

How many calls to f() in g(n), depending on n = 2k?

void g(int n){ if (n>1){ g(n/2); g(n/2); } for (int i = 0; i<n; ++i){ f(); } }

9

slide-18
SLIDE 18

Analysis

How many calls to f() in g(n), depending on n = 2k?

void g(int n){ if (n>1){ g(n/2); g(n/2); } for (int i = 0; i<n; ++i){ f(); } }

Recurrence

T(n) =

  • 2T(n/2) + n

n > 1 1 n = 1

9

slide-19
SLIDE 19

Analysis

How many calls to f() in g(n), depending on n = 2k?

void g(int n){ if (n>1){ g(n/2); g(n/2); } for (int i = 0; i<n; ++i){ f(); } }

Recurrence

T(n) =

  • 2T(n/2) + n

n > 1 1 n = 1 ∈ Θ(n log n)

9

slide-20
SLIDE 20

Merge

1 4 7 9 16 2 3 10 11 12 1 2 3 4 7 9 10 11 12 16

10

slide-21
SLIDE 21

Merge

1 4 7 9 16 2 3 10 11 12 1 2 3 4 7 9 10 11 12 16

10

slide-22
SLIDE 22

Merge

1 4 7 9 16 2 3 10 11 12 1 2 3 4 7 9 10 11 12 16

10

slide-23
SLIDE 23

Merge

1 4 7 9 16 2 3 10 11 12 1 2 3 4 7 9 10 11 12 16

10

slide-24
SLIDE 24

Merge

1 4 7 9 16 2 3 10 11 12 1 2 3 4 7 9 10 11 12 16

10

slide-25
SLIDE 25

Merge

1 4 7 9 16 2 3 10 11 12 1 2 3 4 7 9 10 11 12 16

10

slide-26
SLIDE 26

Merge

1 4 7 9 16 2 3 10 11 12 1 2 3 4 7 9 10 11 12 16

10

slide-27
SLIDE 27

Merge

1 4 7 9 16 2 3 10 11 12 1 2 3 4 7 9 10 11 12 16

10

slide-28
SLIDE 28

Merge

1 4 7 9 16 2 3 10 11 12 1 2 3 4 7 9 10 11 12 16

10

slide-29
SLIDE 29

Merge

1 4 7 9 16 2 3 10 11 12 1 2 3 4 7 9 10 11 12 16

10

slide-30
SLIDE 30

Merge

1 4 7 9 16 2 3 10 11 12 1 2 3 4 7 9 10 11 12 16

10

slide-31
SLIDE 31

Algorithm Merge(A, l, m, r)

Input: Array A with length n, indexes 1 ≤ l ≤ m ≤ r ≤ n. A[l, . . . , m], A[m + 1, . . . , r] sorted Output: A[l, . . . , r] sorted

1 B ← new Array(r − l + 1) 2 i ← l; j ← m + 1; k ← 1 3 while i ≤ m and j ≤ r do 4

if A[i] ≤ A[j] then B[k] ← A[i]; i ← i + 1

5

else B[k] ← A[j]; j ← j + 1

6

k ← k + 1;

7 while i ≤ m do B[k] ← A[i]; i ← i + 1; k ← k + 1 8 while j ≤ r do B[k] ← A[j]; j ← j + 1; k ← k + 1 9 for k ← l to r do A[k] ← B[k − l + 1]

11

slide-32
SLIDE 32

Algorithm (recursive 2-way) Mergesort(A, l, r)

Input: Array A with length n. 1 ≤ l ≤ r ≤ n Output: A[l, . . . , r] sorted. if l < r then m ← ⌊(l + r)/2⌋ // middle position Mergesort(A, l, m) // sort lower half Mergesort(A, m + 1, r) // sort higher half Merge(A, l, m, r) // Merge subsequences

12

slide-33
SLIDE 33

Natural 2-way mergesort

5 6 2 4 8 3 9 7 1

slide-34
SLIDE 34

Natural 2-way mergesort

5 6 2 4 8 3 9 7 1

slide-35
SLIDE 35

Natural 2-way mergesort

5 6 2 4 8 3 9 7 1 2 4 5 6 8 3 7 9 1

slide-36
SLIDE 36

Natural 2-way mergesort

5 6 2 4 8 3 9 7 1 2 4 5 6 8 3 7 9 1

slide-37
SLIDE 37

Natural 2-way mergesort

5 6 2 4 8 3 9 7 1 2 4 5 6 8 3 7 9 1 2 3 4 5 6 7 8 9 1

slide-38
SLIDE 38

Natural 2-way mergesort

5 6 2 4 8 3 9 7 1 2 4 5 6 8 3 7 9 1 2 3 4 5 6 7 8 9 1

slide-39
SLIDE 39

Natural 2-way mergesort

5 6 2 4 8 3 9 7 1 2 4 5 6 8 3 7 9 1 2 3 4 5 6 7 8 9 1 1 2 3 4 5 6 7 8 9

13

slide-40
SLIDE 40

Algorithm NaturalMergesort(A)

Input: Array A with length n > 0 Output: Array A sorted repeat r ← 0 while r < n do l ← r + 1 m ← l; while m < n and A[m + 1] ≥ A[m] do m ← m + 1 if m < n then r ← m + 1; while r < n and A[r + 1] ≥ A[r] do r ← r + 1 Merge(A, l, m, r); elser ← n until l = 1

14

slide-41
SLIDE 41

Algorithm Partition(A, l, r, p)

Input: Array A, that contains the pivot p in A[l, . . . , r] at least once. Output: Array A partitioned in [l, . . . , r] around p. Returns position of p. while l ≤ r do while A[l] < p do l ← l + 1 while A[r] > p do r ← r − 1 swap(A[l], A[r]) if A[l] = A[r] then l ← l + 1 return l-1

15

slide-42
SLIDE 42

Illustration Partition

Pivot = 4 5 6 2 8 4 1 3 3 6 2 8 4 1 5 3 1 2 8 4 6 5 3 1 2 4 8 6 5

l r

16

slide-43
SLIDE 43

Illustration Partition

Pivot = 4 5 6 2 8 4 1 3 3 6 2 8 4 1 5 3 1 2 8 4 6 5 3 1 2 4 8 6 5

l r

16

slide-44
SLIDE 44

Illustration Partition

Pivot = 4 5 6 2 8 4 1 3 3 6 2 8 4 1 5 3 1 2 8 4 6 5 3 1 2 4 8 6 5

l r

16

slide-45
SLIDE 45

Illustration Partition

Pivot = 4 5 6 2 8 4 1 3 3 6 2 8 4 1 5 3 1 2 8 4 6 5 3 1 2 4 8 6 5

l r

16

slide-46
SLIDE 46

Illustration Partition

Pivot = 4 5 6 2 8 4 1 3 3 6 2 8 4 1 5 3 1 2 8 4 6 5 3 1 2 4 8 6 5

l r

16

slide-47
SLIDE 47

Illustration Partition

Pivot = 4 5 6 2 8 4 1 3 3 6 2 8 4 1 5 3 1 2 8 4 6 5 3 1 2 4 8 6 5

l r

16

slide-48
SLIDE 48

Illustration Partition

Pivot = 4 5 6 2 8 4 1 3 3 6 2 8 4 1 5 3 1 2 8 4 6 5 3 1 2 4 8 6 5

l r

16

slide-49
SLIDE 49

Illustration Partition

Pivot = 4 5 6 2 8 4 1 3 3 6 2 8 4 1 5 3 1 2 8 4 6 5 3 1 2 4 8 6 5

l r

16

slide-50
SLIDE 50

Algorithm Quicksort(A, l, r)

Input: Array A with length n. 1 ≤ l ≤ r ≤ n. Output: Array A, sorted in A[l, . . . , r]. if l < r then Choose pivot p ∈ A[l, . . . , r] k ← Partition(A, l, r, p) Quicksort(A, l, k − 1) Quicksort(A, k + 1, r)

17

slide-51
SLIDE 51

Stable and in-situ sorting algorithms

Stabe sorting algorithms don’t change the relative position of two elements. 1

5 2 6 6 8 4

not stable

2 4 5 6 6 8

1Every sorting algorithm can be made stable by storing the array position together with the element. 18

slide-52
SLIDE 52

Stable and in-situ sorting algorithms

Stabe sorting algorithms don’t change the relative position of two elements. 1

5 2 6 6 8 4

not stable

2 4 5 6 6 8 5 2 6 6 8 4

stable

2 4 5 6 6 8

1Every sorting algorithm can be made stable by storing the array position together with the element. 18

slide-53
SLIDE 53

Stable and in-situ sorting algorithms

Stabe sorting algorithms don’t change the relative position of two elements. 1

5 2 6 6 8 4

not stable

2 4 5 6 6 8 5 2 6 6 8 4

stable

2 4 5 6 6 8

In-situ algorithms require only a constant amount of additional

  • memory. (Mergesort is not in-situ)

1Every sorting algorithm can be made stable by storing the array position together with the element. 18

slide-54
SLIDE 54

Quiz: Sorting and Running Times

Algorithm Comparisons Swaps average worst average worst Bubble Sort

19

slide-55
SLIDE 55

Quiz: Sorting and Running Times

Algorithm Comparisons Swaps average worst average worst Bubble Sort

Θ(n2) Θ(n2)

19

slide-56
SLIDE 56

Quiz: Sorting and Running Times

Algorithm Comparisons Swaps average worst average worst Bubble Sort

Θ(n2) Θ(n2) Θ(n2) Θ(n2)

Selection

19

slide-57
SLIDE 57

Quiz: Sorting and Running Times

Algorithm Comparisons Swaps average worst average worst Bubble Sort

Θ(n2) Θ(n2) Θ(n2) Θ(n2)

Selection

Θ(n2) Θ(n2)

19

slide-58
SLIDE 58

Quiz: Sorting and Running Times

Algorithm Comparisons Swaps average worst average worst Bubble Sort

Θ(n2) Θ(n2) Θ(n2) Θ(n2)

Selection

Θ(n2) Θ(n2) Θ(n) Θ(n)

Insertion

19

slide-59
SLIDE 59

Quiz: Sorting and Running Times

Algorithm Comparisons Swaps average worst average worst Bubble Sort

Θ(n2) Θ(n2) Θ(n2) Θ(n2)

Selection

Θ(n2) Θ(n2) Θ(n) Θ(n)

Insertion

Θ(n log n) Θ(n log n)

19

slide-60
SLIDE 60

Quiz: Sorting and Running Times

Algorithm Comparisons Swaps average worst average worst Bubble Sort

Θ(n2) Θ(n2) Θ(n2) Θ(n2)

Selection

Θ(n2) Θ(n2) Θ(n) Θ(n)

Insertion

Θ(n log n) Θ(n log n) Θ(n2) Θ(n2)

Quicksort

19

slide-61
SLIDE 61

Quiz: Sorting and Running Times

Algorithm Comparisons Swaps average worst average worst Bubble Sort

Θ(n2) Θ(n2) Θ(n2) Θ(n2)

Selection

Θ(n2) Θ(n2) Θ(n) Θ(n)

Insertion

Θ(n log n) Θ(n log n) Θ(n2) Θ(n2)

Quicksort

Θ(n log n) Θ(n2)

19

slide-62
SLIDE 62

Quiz: Sorting and Running Times

Algorithm Comparisons Swaps average worst average worst Bubble Sort

Θ(n2) Θ(n2) Θ(n2) Θ(n2)

Selection

Θ(n2) Θ(n2) Θ(n) Θ(n)

Insertion

Θ(n log n) Θ(n log n) Θ(n2) Θ(n2)

Quicksort

Θ(n log n) Θ(n2) Θ(n log n) Θ(n log n)*

Mergesort

Θ(n log n) Θ(n log n)

19

slide-63
SLIDE 63

Quiz: Sorting and Running Times

Algorithm Comparisons Swaps average worst average worst Bubble Sort

Θ(n2) Θ(n2) Θ(n2) Θ(n2)

Selection

Θ(n2) Θ(n2) Θ(n) Θ(n)

Insertion

Θ(n log n) Θ(n log n) Θ(n2) Θ(n2)

Quicksort

Θ(n log n) Θ(n2) Θ(n log n) Θ(n log n)*

Mergesort

Θ(n log n) Θ(n log n) Θ(n log n) Θ(n log n)

* non-trivial (and not derived in class)

19

slide-64
SLIDE 64

Questions / Suggestions?

20