Premaster Course Algorithms 1 Chapter 2: Heapsort and ...€¦ · 15.04.2019 Chapter 2 1 Premaster...

38
15.04.2019 Chapter 2 1 Premaster Course Algorithms 1 Chapter 2: Heapsort and Quicksort Christian Scheideler SS 2019

Transcript of Premaster Course Algorithms 1 Chapter 2: Heapsort and ...€¦ · 15.04.2019 Chapter 2 1 Premaster...

Page 1: Premaster Course Algorithms 1 Chapter 2: Heapsort and ...€¦ · 15.04.2019 Chapter 2 1 Premaster Course Algorithms 1 Chapter 2: Heapsort and Quicksort Christian Scheideler SS 2019.

15.04.2019 Chapter 2 1

Premaster Course Algorithms 1

Chapter 2: Heapsort andQuicksort

Christian ScheidelerSS 2019

Page 2: Premaster Course Algorithms 1 Chapter 2: Heapsort and ...€¦ · 15.04.2019 Chapter 2 1 Premaster Course Algorithms 1 Chapter 2: Heapsort and Quicksort Christian Scheideler SS 2019.

HeapsortMotivation: Consider the following sorting algorithm

Input: Array AOutput: Numbers in A in ascending order

Max-Sort(A):for i←length(A) downto 2 do

m←Max-Search(A[1..i]) // returns index of maximumA[m]↔A[i]

Is there a data structure that allows us to find the maximum quicker than with Max-Search implemented in the same way as Min-Search in Chapter 1?→ priority queues

15.04.2019 Chapter 2 2

Page 3: Premaster Course Algorithms 1 Chapter 2: Heapsort and ...€¦ · 15.04.2019 Chapter 2 1 Premaster Course Algorithms 1 Chapter 2: Heapsort and Quicksort Christian Scheideler SS 2019.

15.04.2019 Chapter 2 3

Priority Queue

5 8

12

15

37

Page 4: Premaster Course Algorithms 1 Chapter 2: Heapsort and ...€¦ · 15.04.2019 Chapter 2 1 Premaster Course Algorithms 1 Chapter 2: Heapsort and Quicksort Christian Scheideler SS 2019.

15.04.2019 Chapter 2 4

Priority Queue

5 8

12

15

37

insert(10)

10

Page 5: Premaster Course Algorithms 1 Chapter 2: Heapsort and ...€¦ · 15.04.2019 Chapter 2 1 Premaster Course Algorithms 1 Chapter 2: Heapsort and Quicksort Christian Scheideler SS 2019.

15.04.2019 Chapter 2 5

Priority Queue

5 8

12

15

37

min() outputs 3 (minimal element)

10

Page 6: Premaster Course Algorithms 1 Chapter 2: Heapsort and ...€¦ · 15.04.2019 Chapter 2 1 Premaster Course Algorithms 1 Chapter 2: Heapsort and Quicksort Christian Scheideler SS 2019.

15.04.2019 Chapter 2 6

Priority Queue

5 8

12

15

37

deleteMin()

10

Page 7: Premaster Course Algorithms 1 Chapter 2: Heapsort and ...€¦ · 15.04.2019 Chapter 2 1 Premaster Course Algorithms 1 Chapter 2: Heapsort and Quicksort Christian Scheideler SS 2019.

15.04.2019 Chapter 2 7

Priority QueueM: set of elements in priority queueEvery element e identified by key(e).Operations:• M.build({e1,…,en}): M:={e1,…,en}• M.insert(e: Element): M:=M∪{e}• M.min: outputs e∈M with minimal key(e)• M.deleteMin: like M.min, but additionally

M:=M∖{e}, for that e with minimal key(e)

Page 8: Premaster Course Algorithms 1 Chapter 2: Heapsort and ...€¦ · 15.04.2019 Chapter 2 1 Premaster Course Algorithms 1 Chapter 2: Heapsort and Quicksort Christian Scheideler SS 2019.

15.04.2019 Chapter 2 8

Priority Queue• Priority Queue based on unsorted list:

– build({e1,…,en}): time O(n)– insert(e): O(1) – min, deleteMin: O(n)

• Priority Queue based on sorted array:– build({e1,…,en}): time O(n log n) (needed for sorting)– insert(e): O(n) (rearrange elements in array)– min, deleteMin: O(1)

Better structure needed than list or array!

Page 9: Premaster Course Algorithms 1 Chapter 2: Heapsort and ...€¦ · 15.04.2019 Chapter 2 1 Premaster Course Algorithms 1 Chapter 2: Heapsort and Quicksort Christian Scheideler SS 2019.

15.04.2019 Chapter 2 9

Binary Heap

Idee: use binary tree instead of list

Preserve two invariants:• Form invariant:complete

binary tree up to lowest level

• Heap invariant: e1

e2 e3

key(e1)≤min{key(e2),key(e3)}

Page 10: Premaster Course Algorithms 1 Chapter 2: Heapsort and ...€¦ · 15.04.2019 Chapter 2 1 Premaster Course Algorithms 1 Chapter 2: Heapsort and Quicksort Christian Scheideler SS 2019.

15.04.2019 Chapter 2 10

Binary Heap

Example:

4

8 5

11 9 12 18

15 17Form invariant

Heap invariant

Page 11: Premaster Course Algorithms 1 Chapter 2: Heapsort and ...€¦ · 15.04.2019 Chapter 2 1 Premaster Course Algorithms 1 Chapter 2: Heapsort and Quicksort Christian Scheideler SS 2019.

15.04.2019 Chapter 2 11

Binary Heap

Representation of binary tree via array:

e1

e2 e3

e4 e5 e6 e7

e8 e9

e1 e2 e3 e4 e5 e6 e7 e8 e9e3

Page 12: Premaster Course Algorithms 1 Chapter 2: Heapsort and ...€¦ · 15.04.2019 Chapter 2 1 Premaster Course Algorithms 1 Chapter 2: Heapsort and Quicksort Christian Scheideler SS 2019.

15.04.2019 Chapter 2 12

Binary HeapRepresentation of binary tree via array:

• H: Array [1..N] of Element (N ≥ #elements n)• Children of e in H[i]: in H[2i], H[2i+1]• Form invariant: H[1],…,H[n] occupied• Heap invariant: for all i∈{2,…,n},

key(H[i])≥key(H[⌊i/2⌋])

e1 e2 e3 e4 e5 e6 e7 e8 e9e3

Page 13: Premaster Course Algorithms 1 Chapter 2: Heapsort and ...€¦ · 15.04.2019 Chapter 2 1 Premaster Course Algorithms 1 Chapter 2: Heapsort and Quicksort Christian Scheideler SS 2019.

15.04.2019 Chapter 2 13

Binary HeapRepresentation of binary tree via array:

insert(e):• Form invariant: n:=n+1; H[n]:=e• Heap invariant: as long as e is in H[k] with

k>1 and key(e)<key(H[⌊k/2⌋]), switch ewith parent

e1 e2 e3 e4 e5 e6 e7 e8 e9e3 e10

Page 14: Premaster Course Algorithms 1 Chapter 2: Heapsort and ...€¦ · 15.04.2019 Chapter 2 1 Premaster Course Algorithms 1 Chapter 2: Heapsort and Quicksort Christian Scheideler SS 2019.

15.04.2019 Chapter 2 14

Insert Operationinsert(e: Element):

n:=n+1; H[n]:=eheapifyUp(n)

heapifyUp(i: Integer):while i>1 and key(H[i])<key(H[⌊i/2⌋]) do

H[i] ↔ H[⌊i/2⌋]i:=⌊i/2⌋

Runtime: O(log n)

Page 15: Premaster Course Algorithms 1 Chapter 2: Heapsort and ...€¦ · 15.04.2019 Chapter 2 1 Premaster Course Algorithms 1 Chapter 2: Heapsort and Quicksort Christian Scheideler SS 2019.

15.04.2019 Chapter 2 15

Insert Operation - Correctness

3

5 8

10 9 12 15

11 18

Invariant: H[k] is minimal w.r.t. subtree of H[k]

: nodes that may violate invariant

3

5 8

10 9 12 15

11 18 4

Page 16: Premaster Course Algorithms 1 Chapter 2: Heapsort and ...€¦ · 15.04.2019 Chapter 2 1 Premaster Course Algorithms 1 Chapter 2: Heapsort and Quicksort Christian Scheideler SS 2019.

15.04.2019 Chapter 2 16

Insert Operation - Correctness

3

5 8

10 9 12 15

11 18 4

3

5 8

10 4 12 15

11 18 9

Invariant: H[k] is minimal w.r.t. subtree of H[k]

: nodes that may violate invariant

Page 17: Premaster Course Algorithms 1 Chapter 2: Heapsort and ...€¦ · 15.04.2019 Chapter 2 1 Premaster Course Algorithms 1 Chapter 2: Heapsort and Quicksort Christian Scheideler SS 2019.

15.04.2019 Chapter 2 17

Insert Operation - Correctness

3

5 8

10 4 12 15

11 18 9

3

4 8

10 5 12 15

11 18 9

Invariant: H[k] is minimal w.r.t. subtree of H[k]

: nodes that may violate invariant

Page 18: Premaster Course Algorithms 1 Chapter 2: Heapsort and ...€¦ · 15.04.2019 Chapter 2 1 Premaster Course Algorithms 1 Chapter 2: Heapsort and Quicksort Christian Scheideler SS 2019.

15.04.2019 Chapter 2 18

Insert Operation - Correctness

3

4 8

10 5 12 15

11 18 9

3

4 8

10 5 12 15

11 18 9

Invariant: H[k] is minimal w.r.t. subtree of H[k]

: nodes that may violate invariant

Page 19: Premaster Course Algorithms 1 Chapter 2: Heapsort and ...€¦ · 15.04.2019 Chapter 2 1 Premaster Course Algorithms 1 Chapter 2: Heapsort and Quicksort Christian Scheideler SS 2019.

15.04.2019 Chapter 2 19

Binary Heap

deleteMin:• Form invariant: H[1]:=H[n]; n:=n-1• Heap invariant: start with e in H[1].

Switch e with the child with minimum key until H[k]≤min{H[2k],H[2k+1]} for the current position k of e or e is in a leaf

e1 e2 e3 e4 e5 e6 e7 e8 e9e3

Page 20: Premaster Course Algorithms 1 Chapter 2: Heapsort and ...€¦ · 15.04.2019 Chapter 2 1 Premaster Course Algorithms 1 Chapter 2: Heapsort and Quicksort Christian Scheideler SS 2019.

15.04.2019 Chapter 2 20

Binary HeapdeleteMin():

e:=H[1]; H[1]:=H[n]; n:=n-1heapifyDown(1)return e

heapifyDown(i: Integer):while 2i≤n do // i is not a leaf position

if 2i+1>n then m:=2i // m: pos. of the minimum childelse

if key(H[2i])<key(H[2i+1]) then m:=2ielse m:=2i+1

if key(H[i])≤key(H[m]) then return // heap inv. holdsH[i] ↔ H[m]; i:=m

Runtime: O(log n)

Page 21: Premaster Course Algorithms 1 Chapter 2: Heapsort and ...€¦ · 15.04.2019 Chapter 2 1 Premaster Course Algorithms 1 Chapter 2: Heapsort and Quicksort Christian Scheideler SS 2019.

15.04.2019 Chapter 2 21

deleteMin Operation - Correctness

3

5 8

10 9 12 15

11 18

5 8

10 9 12 15

11

18

Invariant: H[k] is minimal w.r.t. subtree of H[k]

: nodes that may violate invariant

Page 22: Premaster Course Algorithms 1 Chapter 2: Heapsort and ...€¦ · 15.04.2019 Chapter 2 1 Premaster Course Algorithms 1 Chapter 2: Heapsort and Quicksort Christian Scheideler SS 2019.

15.04.2019 Chapter 2 22

deleteMin Operation - Correctness

5 8

10 9 12 15

11

5

8

10 9 12 15

11

18

18

Invariant: H[k] is minimal w.r.t. subtree of H[k]

: nodes that may violate invariant

Page 23: Premaster Course Algorithms 1 Chapter 2: Heapsort and ...€¦ · 15.04.2019 Chapter 2 1 Premaster Course Algorithms 1 Chapter 2: Heapsort and Quicksort Christian Scheideler SS 2019.

15.04.2019 Chapter 2 23

deleteMin Operation - Correctness

5

8

10 9 12 15

11

5

8

10

9

12 15

11

18

18

Invariant: H[k] is minimal w.r.t. subtree of H[k]

: nodes that may violate invariant

Page 24: Premaster Course Algorithms 1 Chapter 2: Heapsort and ...€¦ · 15.04.2019 Chapter 2 1 Premaster Course Algorithms 1 Chapter 2: Heapsort and Quicksort Christian Scheideler SS 2019.

15.04.2019 Chapter 2 24

Binary Heapbuild({e1,…,en}):• Naive implementation: via n insert(e) operations.

Runtime O(n log n)• Better implementation:

build({e1,…,en}):for i:=⌊n/2⌋ downto 1 do

heapifyDown(i)

• Runtime (with k=⌈log n⌉):O(∑0≤l<k 2l(k-l)) = O(2k ∑j≥1 j/2j) = O(n)

runtime of heapifyDown for level lnumber of nodes in level l

Page 25: Premaster Course Algorithms 1 Chapter 2: Heapsort and ...€¦ · 15.04.2019 Chapter 2 1 Premaster Course Algorithms 1 Chapter 2: Heapsort and Quicksort Christian Scheideler SS 2019.

15.04.2019 Chapter 2 25

Binary Heap

Call HeapifyDown(i) for i=⌊n/2⌋ down to 1:

Invariant: ∀j>i: H[j] min w.r.t. subtree of H[j]

invariantviolated

Page 26: Premaster Course Algorithms 1 Chapter 2: Heapsort and ...€¦ · 15.04.2019 Chapter 2 1 Premaster Course Algorithms 1 Chapter 2: Heapsort and Quicksort Christian Scheideler SS 2019.

15.04.2019 Chapter 2 26

Binary Heap

Runtime:• build({e1,…,en}): O(n)• insert(e): O(log n)• min: O(1)• deleteMin: O(log n)

Page 27: Premaster Course Algorithms 1 Chapter 2: Heapsort and ...€¦ · 15.04.2019 Chapter 2 1 Premaster Course Algorithms 1 Chapter 2: Heapsort and Quicksort Christian Scheideler SS 2019.

HeapsortInput: Array AOutput: numbers in A in ascending order

Heapsort(A):Build-Max-Heap(A) // like build, but for max-heapfor i←length(A) downto 2 do

A[i]:=DeleteMax(A) // A[i] ← maximum in A[1..i]

Correctness: follows from correctness of Build-Max-Heap(A) and DeleteMax(A)

15.04.2019 Chapter 2 27

Page 28: Premaster Course Algorithms 1 Chapter 2: Heapsort and ...€¦ · 15.04.2019 Chapter 2 1 Premaster Course Algorithms 1 Chapter 2: Heapsort and Quicksort Christian Scheideler SS 2019.

Illustration of Heapsort

15.04.2019 Chapter 2 28

116

14 10

8 7

42 1

9 3

2 3

4 5 6 7

8 9 10

114

8 10

4 7

12 16

9 3

2 3

4 5 6 7

8 9

14 10 8 7 9 3 2 4 116

14 8 10 4 7 9 3 2 1 16

Page 29: Premaster Course Algorithms 1 Chapter 2: Heapsort and ...€¦ · 15.04.2019 Chapter 2 1 Premaster Course Algorithms 1 Chapter 2: Heapsort and Quicksort Christian Scheideler SS 2019.

Illustration of Heapsort

15.04.2019 Chapter 2 29

114

8 10

4 7

12 16

9 3

2 3

4 5 6 7

8 9

10 8 9 4 7 9 3 2 14 16

110

8 9

4 7

142 16

1 3

2 3

4 5 6 7

8 9 10

14 8 10 4 7 9 3 2 1 16

Page 30: Premaster Course Algorithms 1 Chapter 2: Heapsort and ...€¦ · 15.04.2019 Chapter 2 1 Premaster Course Algorithms 1 Chapter 2: Heapsort and Quicksort Christian Scheideler SS 2019.

Illustration of Heapsort

15.04.2019 Chapter 2 30

110

8 9

4 7

142 16

1 3

2 3

4 5 6 7

8 9

9 8 3 4 7 1 2 10 14 16

19

8 3

4 7

1410 16

1 2

2 3

4 5 6 7

8 9 10

10 8 9 4 7 9 3 2 14 16

Page 31: Premaster Course Algorithms 1 Chapter 2: Heapsort and ...€¦ · 15.04.2019 Chapter 2 1 Premaster Course Algorithms 1 Chapter 2: Heapsort and Quicksort Christian Scheideler SS 2019.

runtime:O(n)Σi=2

n (O(1)+T(I))O(log n)

O(n log n)

Runtime of HeapsortTheorem 2.1: Heapsort has a runtime of O(n log n). Proof:

Heapsort(A):Build-Max-Heap(A)

for i←length(A) downto 2 do A[i]←DeleteMax(A)

15.04.2019 Chapter 2 31

Page 32: Premaster Course Algorithms 1 Chapter 2: Heapsort and ...€¦ · 15.04.2019 Chapter 2 1 Premaster Course Algorithms 1 Chapter 2: Heapsort and Quicksort Christian Scheideler SS 2019.

QuicksortAlgorithm Quicksort(A,p,r):1. If p<r then2. q:=Partition(A,p,r)3. Quicksort(A,p,q-1)4. Quicksort(A,q+1,r)

Quicksort on array A called with: Quicksort(A,1,length(A))

15.04.2019 Chapter 2 32

Page 33: Premaster Course Algorithms 1 Chapter 2: Heapsort and ...€¦ · 15.04.2019 Chapter 2 1 Premaster Course Algorithms 1 Chapter 2: Heapsort and Quicksort Christian Scheideler SS 2019.

QuicksortAlgorithm Partition(A,p,r):1. x:=A[r] // x: pivot element,2. i:=p-1 // x used for comparisons3. for j:=p to r-1 do4. if A[j]≤x then5. i:=i+16. A[i]↔A[j]7. A[i+1]↔A[r]8. return i+1

15.04.2019 Chapter 2 33

Page 34: Premaster Course Algorithms 1 Chapter 2: Heapsort and ...€¦ · 15.04.2019 Chapter 2 1 Premaster Course Algorithms 1 Chapter 2: Heapsort and Quicksort Christian Scheideler SS 2019.

Quicksort

15.04.2019 Chapter 2 34

Algorithm Partition(A,p,r):1. x:=A[r]2. i:=p-13. for j:=p to r-1 do4. if A[j]≤x then5. i:=i+16. A[i]↔A[j]7. A[i+1]↔A[r]8. return i+1

2 8 7 1 3 5 6 4

rp,ji

2 8 7 1 3 5 6 4

rp,i j

2 8 7 1 3 5 6 4

rp,i j

2 8 7 1 3 5 6 4

rp,i j

Page 35: Premaster Course Algorithms 1 Chapter 2: Heapsort and ...€¦ · 15.04.2019 Chapter 2 1 Premaster Course Algorithms 1 Chapter 2: Heapsort and Quicksort Christian Scheideler SS 2019.

Quicksort

15.04.2019 Chapter 2 35

Algorithm Partition(A,p,r):1. x:=A[r]2. i:=p-13. for j:=p to r-1 do4. if A[j]≤x then5. i:=i+16. A[i]↔A[j]7. A[i+1]↔A[r]8. return i+1

2 1 7 8 3 5 6 4

rp ji

2 1 3 8 7 5 6 4

rp ji

2 1 3 8 7 5 6 4

jip r

2 1 3 8 7 5 6 4

ip r

2 1 3 4 7 5 6 8

ip r

Page 36: Premaster Course Algorithms 1 Chapter 2: Heapsort and ...€¦ · 15.04.2019 Chapter 2 1 Premaster Course Algorithms 1 Chapter 2: Heapsort and Quicksort Christian Scheideler SS 2019.

Runtime of QuicksortTheorem 2.2: Quicksort has a worst-case runtime ofΘ(n2).Proof:• Suppose that the elements in A are already stored

in ascending (resp. descending) order. Then theindex returned by Partition(A,p,r) will always be r(resp. p).

• This will cause the recursions to be highlyunbalanced ( T(n) = T(1)+T(n-1) + c⋅n ).

15.04.2019 Chapter 2 36

1 2 3 4 5 6 7 8

rp

Page 37: Premaster Course Algorithms 1 Chapter 2: Heapsort and ...€¦ · 15.04.2019 Chapter 2 1 Premaster Course Algorithms 1 Chapter 2: Heapsort and Quicksort Christian Scheideler SS 2019.

Randomized QuicksortTheorem 2.3: Suppose that the pivot x in Parition(A,p,r) ischosen uniformly at random from A[p..r]. Then the expectedruntime of Quicksort is O(n log n).Proof:• W.l.o.g. we assume that A=(a1,…,an) is a permutation of

{1,…,n}.• We define the binary random variable Xi,j to be 1 if and only if i

and j are compared.• Altogether, the number of comparisons is Σi<j Xi,j. This

dominates the runtime of Quicksort, so it remains to computeE[Σi<j Xi,j] = Σi<j E[Xi,j].

• It holds that E[Xi,j]=Pr[Xi,j=1]=2/(j-i+1). (Proof: whiteboard, or see book). Inserting that into the sum gives the theorem.

15.04.2019 Chapter 2 37

Page 38: Premaster Course Algorithms 1 Chapter 2: Heapsort and ...€¦ · 15.04.2019 Chapter 2 1 Premaster Course Algorithms 1 Chapter 2: Heapsort and Quicksort Christian Scheideler SS 2019.

15.04.2019 Chapter 2 38

Next Chapter

Topic: elementary data structures