Presentation is loading. Please wait.

Presentation is loading. Please wait.

Divide and Conquer Pasi Fränti 2.11.2016.

Similar presentations


Presentation on theme: "Divide and Conquer Pasi Fränti 2.11.2016."— Presentation transcript:

1 Divide and Conquer Pasi Fränti

2 Divide and Conquer Divide to sub-problems Solve the sub-problems
Conquer the solutions By recursion!

3 Stupid example StupidExample(N) IF N=0 THEN RETURN O(1) ELSE
FOR i←1 TO N DO N WRITE(‘x’); O(1) StupidExample(N-1); T(N-1) END;

4 Analysis by substitution method
Repeat until T(0) k+… → …+k k=N

5 Sorting algorithm Recursive “through the bones”
Sort(A[i, j]) q  FindMin(A[i, j]); Swap(A[i],A[q]) Sort(A[i+1, j]); FindMin(A[i, j]) q  FindMin(A[i+1, j]); IF A[i]<A[q] THEN RETURN i ELSE RETURN q;

6 Master Theorem Arithmetic case
Time complexity function: Solution:

7 Master Theorem Proof for arithmetic case
Substitution:

8 Master Theorem Proof for arithmetic case
N/c terms

9 Master Theorem Proof for arithmetic case
Arithmetic sum

10 Master Theorem Proof for arithmetic case
Dominating term

11 Quicksort Quicksort(A[i, j]) IF i < j THEN O(1)
k ← Partition(A[i, j]); O(N) Quicksort(A[i, k]); T(N/2) Quicksort(A[k+1, j]); T(N/2) ELSE RETURN;

12 Partition algorithm Choose (any) element as pivot-value p.
Arrange all x≤p to the left side of list. Arrange all x>p to the right side. Time complexity O(N). 7 3 11 2 20 13 6 1 10 14 5 8 p=7 x≤7 x>7 7 3 2 6 1 5 11 20 13 10 14 8

13 R increases when needed
Partition algorithm x≤p x>p unprocessed Partition(A[i, j]) pivot = A[j]; r = i-1; FOR k = i TO j-1 DO IF (A[k] ≤ pivot) THEN r = r + 1; SWAP(A[r], A[k]); SWAP(A[r+1], A[j]); RETURN r+1; ∙∙∙ r ∙∙∙ k FOR increases k R increases when needed Swap done when more space needed to the left side

14 Selection in linear time Find the kth smallest
Selection(A[i, j], k) IF i=j THEN RETURN A[i]; ELSE q ← Partition(A, i, j); size ← (q-i)+1; IF k ≤ size THEN Selection(A[i, q], k); ELSE Selection(A[q+1, j], k-size);

15 Master Theorem Geometric case
Time complexity function: Solution:

16 Master Theorem Proof of geometric case
p steps Extract

17 Master Theorem Proof of geometric case
Geometric sum Geometric sum a>1

18 Master Theorem Proof of geometric case
=1 Geometric sum Log N terms N

19 Master Theorem Proof of geometric case
<1 …summation…

20 Merge sort Main algorithm
MergeSort(A[i, j]) IF i<j THEN k := (i+j)/2; O(1) MergeSort (A[i, k]); T(N/2) MergeSort(A[k+1, j]); T(N/2) Merge(A[i, j], k); c∙N

21 Merge sort Merge step Merge(A[i, j], k) { l←i; m←k+1; t←i;
WHILE (l ≤ k) OR (m ≤ j) IF l>k THEN B[t]←A[m]; m←m+1; ELSEIF m>j THEN B[t]←A[l]; l←l+1; ELSEIF A[l]<A[m] THEN B[t]←A[m]; m←m+1; ELSE B[t]←A[l]; l←l+1; t←t+1; FOR t ← i TO j DO A[t]←B[t]; }

22 Merge sort Time complexity
Since we have b=c  O(N log N)

23 Merge sort Substitution method

24 Karatsuba-Ofman multiplication Multiplication of two n-digit numbers
School book algorithm:

25 Karatsuba-Ofman multiplication Straightforward divide-and-conquer
One n-digit number divided into two n/2-digit numbers (most and least significant) 3624=36∙102+24 2345=23∙102+45 Multiplication reformulated:

26 Karatsuba-Ofman multiplication Time complexity analysis

27 Karatsuba-Ofman multiplication Divide-and-Conquer revised
+ 6 summations and 1.5 multiplications by kn

28

29 Working space


Download ppt "Divide and Conquer Pasi Fränti 2.11.2016."

Similar presentations


Ads by Google