Recurrences and Divide-and-Conquer

Size: px
Start display at page:

Download "Recurrences and Divide-and-Conquer"

Transcription

1 Recurrences and Divide-and-Conquer Frits Vaandrager Institute for Computing and Information Sciences 16th November 2017 Frits Vaandrager 16th November 2017 Lecture 9 1 / 35

2 Algorithm Design Strategies We can classify algorithms in terms of the strategy they use to solve a problem: Incremental (e.g. insertion sort) Divide and conquer (e.g. merge sort and quicksort) Greedy (e.g. Dijkstra s algorithm) Dynamic programming (e.g. all-pairs-shortest-path) Randomized algorithms (e.g. randomized quicksort) Frits Vaandrager 16th November 2017 Lecture 9 2 / 35

3 Divide and Conquer The concept refers to a strategy that breaks up existing power structures, and especially prevents smaller power groups from linking up, causing rivalries and fomenting discord among the people. Frits Vaandrager 16th November 2017 Lecture 9 3 / 35

4 Divide and Conquer Frits Vaandrager 16th November 2017 Lecture 9 4 / 35

5 Divide and Conquer Algorithm design strategy: 1 Divide a problem into n sub-problems (smaller instances of the original problem) 2 Conquer the sub-problems trivial for small sizes use the same strategy, otherwise 3 Combine solutions of sub-problems into a solution of the original problem Implementation is typically recursive Frits Vaandrager 16th November 2017 Lecture 9 5 / 35

6 Multiply and Conquer Frits Vaandrager 16th November 2017 Lecture 9 6 / 35

7 Multiply and Conquer Frits Vaandrager 16th November 2017 Lecture 9 7 / 35

8 Multiply and Conquer Thus far there is no multiply and conquer algorithm design strategy... Frits Vaandrager 16th November 2017 Lecture 9 8 / 35

9 Multiply and Conquer Thus far there is no multiply and conquer algorithm design strategy... However, Kuratsuba used divide and conquer to develop a fast multiplication algorithm with complexity O(n lg 3 ) O(n )! Frits Vaandrager 16th November 2017 Lecture 9 8 / 35

10 Algorithm invented by John von Neumann in 1945 that uses a divide and conquer strategy: Divide vector in two vectors of similar size Conquer: recursively sort the two vectors (trivial for... Frits Vaandrager 16th November 2017 Lecture 9 9 / 35

11 Algorithm invented by John von Neumann in 1945 that uses a divide and conquer strategy: Divide vector in two vectors of similar size Conquer: recursively sort the two vectors (trivial for... size 1) Combine two ordered vectors into a new ordered vector. Auxiliary merge function: 1 The merge function gets as input two ordered sequences A[p...q] and A[q+1...r] 2 As output it returns the sequence A[p...r] ordered Frits Vaandrager 16th November 2017 Lecture 9 9 / 35

12 void merge_sort(int A[], int p, int r) { if (p < r) { /* base case? */ q = (p+r)/2; /* Divide */ merge_sort(a,p,q); /* Conquer */ merge_sort(a,q+1,r); /* Conquer */ merge(a,p,q,r); /* Combine */ } } Frits Vaandrager 16th November 2017 Lecture 9 10 / 35

13 void merge_sort(int A[], int p, int r) { if (p < r) { /* base case? */ q = (p+r)/2; /* Divide */ merge_sort(a,p,q); /* Conquer */ merge_sort(a,q+1,r); /* Conquer */ merge(a,p,q,r); /* Combine */ } } Question: What is the dimension of each sequence in divide step? Frits Vaandrager 16th November 2017 Lecture 9 10 / 35

14 void merge_sort(int A[], int p, int r) { if (p < r) { /* base case? */ q = (p+r)/2; /* Divide */ merge_sort(a,p,q); /* Conquer */ merge_sort(a,q+1,r); /* Conquer */ merge(a,p,q,r); /* Combine */ } } Question: What is the dimension of each sequence in divide step? Initial call on an array with n elements: merge sort(a,1,n). Frits Vaandrager 16th November 2017 Lecture 9 10 / 35

15 Example I Frits Vaandrager 16th November 2017 Lecture 9 11 / 35

16 Example II Array with number of elements not a multiple of 2 Frits Vaandrager 16th November 2017 Lecture 9 12 / 35

17 Remains to do... merge procedure Input: Array A and indices p, q, r such that p q < r Subarrays A[p..q] and A[q + 1..r] are sorted. By restrictions on p, q, r, neither subarray is empty. Output: The two subarrays are merged into a single sorted subarray in A[p..r]. Frits Vaandrager 16th November 2017 Lecture 9 13 / 35

18 Merge Idea behind merging: Think of two piles of cards. Each pile is sorted and placed face-up on a table with the smallest cards on top. We will merge these into a single sorted pile, face-down on the table. A basic step: Choose the smaller of the two top cards. Remove it from its pile, thereby exposing a new top card. Place the chosen card face-down onto the output pile. Repeatedly perform basic steps until one input pile is empty. Once one input pile empties, just take the remaining input pile and place it face-down onto the output pile. Frits Vaandrager 16th November 2017 Lecture 9 14 / 35

19 Merge Idea behind merging: Think of two piles of cards. Each pile is sorted and placed face-up on a table with the smallest cards on top. We will merge these into a single sorted pile, face-down on the table. A basic step: Choose the smaller of the two top cards. Remove it from its pile, thereby exposing a new top card. Place the chosen card face-down onto the output pile. Repeatedly perform basic steps until one input pile is empty. Once one input pile empties, just take the remaining input pile and place it face-down onto the output pile. What is the complexity of this algorithm? Frits Vaandrager 16th November 2017 Lecture 9 14 / 35

20 Merge Each basic step should take constant time, since we check just the two top cards. There are n basic steps, since each basic step removes one card from the input piles, and we started with n cards in the input piles. Therefore, this procedure should take O(n) time. Frits Vaandrager 16th November 2017 Lecture 9 15 / 35

21 Merge procedure void merge(int A[], int p, int q, int r) { int L[MAX], R[MAX]; int n1 = q-p+1; /* length of array A[p..q] */ int n2 = r-q; /* length of array A[q+1..r] */ for (i=1 ; i<=n1 ; i++) L[i] = A[p+i-1]; /* array LEFT */ for (j=1 ; j<=n2 ; j++) R[j] = A[q+j]; /* array RIGHT */ L[n1+1] = MAXINT; R[n2+1] = MAXINT; /* sentinels */ Frits Vaandrager 16th November 2017 Lecture 9 16 / 35

22 Merge procedure void merge(int A[], int p, int q, int r) { int L[MAX], R[MAX]; int n1 = q-p+1; /* length of array A[p..q] */ int n2 = r-q; /* length of array A[q+1..r] */ for (i=1 ; i<=n1 ; i++) L[i] = A[p+i-1]; /* array LEFT */ for (j=1 ; j<=n2 ; j++) R[j] = A[q+j]; /* array RIGHT */ L[n1+1] = MAXINT; R[n2+1] = MAXINT; /* sentinels */ what are the sentinels good for? Frits Vaandrager 16th November 2017 Lecture 9 16 / 35

23 Merge procedure void merge(int A[], int p, int q, int r) { int L[MAX], R[MAX]; int n1 = q-p+1; /* length of array A[p..q] */ int n2 = r-q; /* length of array A[q+1..r] */ for (i=1 ; i<=n1 ; i++) L[i] = A[p+i-1]; /* array LEFT */ for (j=1 ; j<=n2 ; j++) R[j] = A[q+j]; /* array RIGHT */ L[n1+1] = MAXINT; R[n2+1] = MAXINT; /* sentinels */ } i = 1; j = 1; for (k=p ; k<=r ; k++) if (L[i] <= R[j]) { /* put the next smallest A[k] = L[i]; i++; element in the array */ } else { A[k] = R[j]; j++; } Frits Vaandrager 16th November 2017 Lecture 9 16 / 35

24 Merge procedure void merge(int A[], int p, int q, int r) { int L[MAX], R[MAX]; int n1 = q-p+1; /* length of array A[p..q] */ int n2 = r-q; /* length of array A[q+1..r] */ for (i=1 ; i<=n1 ; i++) L[i] = A[p+i-1]; /* array LEFT */ for (j=1 ; j<=n2 ; j++) R[j] = A[q+j]; /* array RIGHT */ L[n1+1] = MAXINT; R[n2+1] = MAXINT; /* sentinels */ } i = 1; j = 1; for (k=p ; k<=r ; k++) if (L[i] <= R[j]) { /* put the next smallest A[k] = L[i]; i++; element in the array */ } else { A[k] = R[j]; j++; } merge executes in Θ(n) time, where n = r p + 1 Frits Vaandrager 16th November 2017 Lecture 9 16 / 35

25 Example merge(a,9,12,16) Frits Vaandrager 16th November 2017 Lecture 9 17 / 35

26 Example c d merge(a,9,12,16) Frits Vaandrager 16th November 2017 Lecture 9 18 / 35

27 Complexity analysis Algorithm with a recursive call: running time can usually be described as a recurrence. Frits Vaandrager 16th November 2017 Lecture 9 19 / 35

28 Complexity analysis Algorithm with a recursive call: running time can usually be described as a recurrence. How does that work? Frits Vaandrager 16th November 2017 Lecture 9 19 / 35

29 Complexity analysis Algorithm with a recursive call: running time can usually be described as a recurrence. How does that work? Let s for a moment assume that the input size is a power of 2. In each division step the sub-arrays have exactly size n/2. Frits Vaandrager 16th November 2017 Lecture 9 19 / 35

30 Complexity analysis Algorithm with a recursive call: running time can usually be described as a recurrence. How does that work? Let s for a moment assume that the input size is a power of 2. In each division step the sub-arrays have exactly size n/2. T (n): time of execution (in the worst case) for a input of size n; If n = 1 then T (n) is constant: T (n) Θ(1). Frits Vaandrager 16th November 2017 Lecture 9 19 / 35

31 Complexity analysis Otherwise: 1 Divide computing middle of vector is done in constant time: Θ(1). 2 Conquer two problems of size n/2 are solved: 2T (n/2). 3 Compose merge function executes in linear time: Θ(n) Frits Vaandrager 16th November 2017 Lecture 9 20 / 35

32 Complexity analysis Otherwise: 1 Divide computing middle of vector is done in constant time: Θ(1). 2 Conquer two problems of size n/2 are solved: 2T (n/2). 3 Compose merge function executes in linear time: Θ(n) Hence: T (n) = { Θ(1) if n = 1 Θ(1) + 2T (n/2) + Θ(n) if n > 1 Frits Vaandrager 16th November 2017 Lecture 9 20 / 35

33 Complexity analysis Otherwise: 1 Divide computing middle of vector is done in constant time: Θ(1). 2 Conquer two problems of size n/2 are solved: 2T (n/2). 3 Compose merge function executes in linear time: Θ(n) Hence: T (n) = { Θ(1) if n = 1 Θ(1) + 2T (n/2) + Θ(n) if n > 1 and now we want to conclude that T (n) Θ(??). Frits Vaandrager 16th November 2017 Lecture 9 20 / 35

34 Merge vs Insertion sort Running ahead: we will show that T (n) Θ(n lg n). Frits Vaandrager 16th November 2017 Lecture 9 21 / 35

35 Merge vs Insertion sort Running ahead: we will show that T (n) Θ(n lg n). Cf. Insertion sort is Θ(n 2 ) and quicksort is O(n 2 ). Frits Vaandrager 16th November 2017 Lecture 9 21 / 35

36 Merge vs Insertion sort Running ahead: we will show that T (n) Θ(n lg n). Cf. Insertion sort is Θ(n 2 ) and quicksort is O(n 2 ). Frits Vaandrager 16th November 2017 Lecture 9 21 / 35

37 Recurrence relations Going back: How did we get n lg n for merge sort? Frits Vaandrager 16th November 2017 Lecture 9 22 / 35

38 Recurrence relations Going back: How did we get n lg n for merge sort? We had { Θ(1) if n = 1 T (n) = Θ(1) + 2T (n/2) + Θ(n) if n > 1 Frits Vaandrager 16th November 2017 Lecture 9 22 / 35

39 Recurrence relations Going back: How did we get n lg n for merge sort? We had { Θ(1) if n = 1 T (n) = Θ(1) + 2T (n/2) + Θ(n) if n > 1 Let us rewrite this as T (n) = { c if n = 1 2T (n/2) + cn if n > 1 Frits Vaandrager 16th November 2017 Lecture 9 22 / 35

40 Recurrence relations Going back: How did we get n lg n for merge sort? We had { Θ(1) if n = 1 T (n) = Θ(1) + 2T (n/2) + Θ(n) if n > 1 Let us rewrite this as T (n) = { c if n = 1 2T (n/2) + cn if n > 1 where c is the largest constant representing both the time required to solve problems of dimension 1 as well as the time per array element of the divide and combine steps. Frits Vaandrager 16th November 2017 Lecture 9 22 / 35

41 Recurrence relations c d For original problem we have cost cn plus cost of sub-problems: Frits Vaandrager 16th November 2017 Lecture 9 23 / 35

42 Recurrence relations c d For original problem we have cost cn plus cost of sub-problems: For each size-n/2 subproblem we have cost cn/2, plus two sub-problems, each costing T (n/4): Frits Vaandrager 16th November 2017 Lecture 9 23 / 35

43 Recurrence relations c d Continue expanding until the problem sizes get down to 1: Each level has cost cn Frits Vaandrager 16th November 2017 Lecture 9 24 / 35

44 Recurrence relations c d Continue expanding until the problem sizes get down to 1: Each level has cost cn There are lg n + 1 levels (height is lg n; proof by induction) Frits Vaandrager 16th November 2017 Lecture 9 24 / 35

45 Recurrence relations c d Continue expanding until the problem sizes get down to 1: Each level has cost cn There are lg n + 1 levels (height is lg n; proof by induction) Total cost is sum of costs at each level: cn(lg n + 1) = cn lg n + cn Frits Vaandrager 16th November 2017 Lecture 9 24 / 35

46 Recurrence relations c d Continue expanding until the problem sizes get down to 1: Each level has cost cn There are lg n + 1 levels (height is lg n; proof by induction) Total cost is sum of costs at each level: cn(lg n + 1) = cn lg n + cn Ignore low-order term: merge sort is in Θ(n lg n) Frits Vaandrager 16th November 2017 Lecture 9 24 / 35

47 How to solve recurrences? First, a technicality: we assumed length of the array was a power of 2. If that s not case, the right formula for the recurrence is: { Θ(1) if n = 1 T (n) = T ( n/2 ) + T ( n/2 ) + Θ(n) if n > 1 Frits Vaandrager 16th November 2017 Lecture 9 25 / 35

48 How to solve recurrences? First, a technicality: we assumed length of the array was a power of 2. If that s not case, the right formula for the recurrence is: { Θ(1) if n = 1 T (n) = T ( n/2 ) + T ( n/2 ) + Θ(n) if n > 1 The solution of a recurrence can be verified using the substitution method, which allows us to prove that indeed the recurrence above has solution T (n) Θ(n lg n) Frits Vaandrager 16th November 2017 Lecture 9 25 / 35

49 Substitution method 1 Guess solution 2 Use induction to find constants and show that solution works Example: Consider the following recurrence { 1 if n = 1 T (n) = 2T (n/2) + n if n > 1 1 Guess: T (n) = n lg n + n (usually done by looking at the recursion tree). 2 Induction: Basis: n = 1 n lg n + n = 1 = T (n) Frits Vaandrager 16th November 2017 Lecture 9 26 / 35

50 Substitution method Inductive step: Inductive hypothesis is that T (k) = k lg k + k for all k < n. We willl use this inductive hypothesis for T (n/2). T (n) = 2T (n/2) + n Frits Vaandrager 16th November 2017 Lecture 9 27 / 35

51 Substitution method Inductive step: Inductive hypothesis is that T (k) = k lg k + k for all k < n. We willl use this inductive hypothesis for T (n/2). T (n) = 2T (n/2) + n = 2(n/2 lg n/2 + n/2) + n induction hyp. Frits Vaandrager 16th November 2017 Lecture 9 27 / 35

52 Substitution method Inductive step: Inductive hypothesis is that T (k) = k lg k + k for all k < n. We willl use this inductive hypothesis for T (n/2). T (n) = 2T (n/2) + n = 2(n/2 lg n/2 + n/2) + n induction hyp. = n lg n/2 + n + n Frits Vaandrager 16th November 2017 Lecture 9 27 / 35

53 Substitution method Inductive step: Inductive hypothesis is that T (k) = k lg k + k for all k < n. We willl use this inductive hypothesis for T (n/2). T (n) = 2T (n/2) + n = 2(n/2 lg n/2 + n/2) + n induction hyp. = n lg n/2 + n + n = n(lg n lg 2) + n + n Frits Vaandrager 16th November 2017 Lecture 9 27 / 35

54 Substitution method Inductive step: Inductive hypothesis is that T (k) = k lg k + k for all k < n. We willl use this inductive hypothesis for T (n/2). T (n) = 2T (n/2) + n = 2(n/2 lg n/2 + n/2) + n induction hyp. = n lg n/2 + n + n = n(lg n lg 2) + n + n = n lg n n + n + n Frits Vaandrager 16th November 2017 Lecture 9 27 / 35

55 Substitution method Inductive step: Inductive hypothesis is that T (k) = k lg k + k for all k < n. We willl use this inductive hypothesis for T (n/2). T (n) = 2T (n/2) + n = 2(n/2 lg n/2 + n/2) + n induction hyp. = n lg n/2 + n + n = n(lg n lg 2) + n + n = n lg n n + n + n = n lg n + n Frits Vaandrager 16th November 2017 Lecture 9 27 / 35

56 Substitution method Generally, we use asymptotic notation: We would write e.g. T (n) = 2T (n/2) + Θ(n). We assume T (n) = O(1) for sufficiently small n. We express the solution by asymptotic notation: T (n) = Θ(n lg n) (or T (n) Θ(n lg n)). We do not worry about boundary cases, nor do we show base cases in the substitution proof. Frits Vaandrager 16th November 2017 Lecture 9 28 / 35

57 Substitution method T (n) is always constant for any constant n. Since we are ultimately interested in an asymptotic solution to a recurrence, it will always be possible to choose base cases that work. When we want an asymptotic solution to a recurrence, we do not worry about the base cases in our proofs. When we want an exact solution, then we have to deal with base cases. For the substitution method: Name the constant in the additive term. Show the upper (O) and lower (Ω) bounds separately. Might need to use different constants for each. Frits Vaandrager 16th November 2017 Lecture 9 29 / 35

58 Another example Take the recurrence T (n) = { 1 if n = 1 2T ( n/2 ) + n if n > 1 We seem to run into a problem if we want to show that T (n) cn lg n: T (1) c1 lg 1 = 0 FALSE! Frits Vaandrager 16th November 2017 Lecture 9 30 / 35

59 Another example Take the recurrence T (n) = { 1 if n = 1 2T ( n/2 ) + n if n > 1 We seem to run into a problem if we want to show that T (n) cn lg n: T (1) c1 lg 1 = 0 FALSE! However, recall that the O-notation only requires that we show that there exist c, n 0 > 0 for which T (n) cn lg n, for all n n 0. Frits Vaandrager 16th November 2017 Lecture 9 30 / 35

60 Another example Hence we just have to replace the base case n = 1 by something else... We take it step by step: let s look at n 0 = 2 Enough to choose c 2. T (2) = 4 c2 lg 2 = 2c Frits Vaandrager 16th November 2017 Lecture 9 31 / 35

61 Another example Hence we just have to replace the base case n = 1 by something else... We take it step by step: let s look at n 0 = 2 T (2) = 4 c2 lg 2 = 2c Enough to choose c 2. But now careful: T (2) = 2T 2/2 + 2 = 2T (1) + 2 = 4 T (3) = 2T 3/2 + 3 = 2T (1) + 3 = 5 T (4) = 2T 4/2 + 4 = 2T (2) + 4 = 12 T (5) = 2T 5/2 + 5 = 2T (2) + 5 = 13 T (6) = 2T 6/2 + 6 = 2T (3) + 6 = 16 Because both T (2) and T (3) depend on T (1) we cannot simply replace the base case n = 1 by n = 2, but we need both n = 2 and n = 3. Frits Vaandrager 16th November 2017 Lecture 9 31 / 35

62 Another example Hence we just have to replace the base case n = 1 by something else... We take it step by step: let s look at n 0 = 2 T (2) = 4 c2 lg 2 = 2c Enough to choose c 2. But now careful: T (2) = 2T 2/2 + 2 = 2T (1) + 2 = 4 T (3) = 2T 3/2 + 3 = 2T (1) + 3 = 5 T (4) = 2T 4/2 + 4 = 2T (2) + 4 = 12 T (5) = 2T 5/2 + 5 = 2T (2) + 5 = 13 T (6) = 2T 6/2 + 6 = 2T (3) + 6 = 16 Because both T (2) and T (3) depend on T (1) we cannot simply replace the base case n = 1 by n = 2, but we need both n = 2 and n = 3. For n = 3, we have T (3) = 5 c3 lg 3 = 4.8c, which also holds for c 2. Frits Vaandrager 16th November 2017 Lecture 9 31 / 35

63 Induction step T (n) = 2T n/2 + n Frits Vaandrager 16th November 2017 Lecture 9 32 / 35

64 Induction step T (n) = 2T n/2 + n 2(c n/2 lg( n/2 )) + n induction hyp. Frits Vaandrager 16th November 2017 Lecture 9 32 / 35

65 Induction step T (n) = 2T n/2 + n 2(c n/2 lg( n/2 )) + n induction hyp. cn lg n/2 + n Frits Vaandrager 16th November 2017 Lecture 9 32 / 35

66 Induction step T (n) = 2T n/2 + n 2(c n/2 lg( n/2 )) + n induction hyp. cn lg n/2 + n = cn(lg n lg 2) + n Frits Vaandrager 16th November 2017 Lecture 9 32 / 35

67 Induction step T (n) = 2T n/2 + n 2(c n/2 lg( n/2 )) + n induction hyp. cn lg n/2 + n = cn(lg n lg 2) + n = cn lg n cn + n Frits Vaandrager 16th November 2017 Lecture 9 32 / 35

68 Induction step T (n) = 2T n/2 + n 2(c n/2 lg( n/2 )) + n induction hyp. cn lg n/2 + n = cn(lg n lg 2) + n = cn lg n cn + n cn lg n Frits Vaandrager 16th November 2017 Lecture 9 32 / 35

69 Master method Provides a cookbook for solving recurrences of the form T (n) = at (n/b) + f (n), Discussed/proved in the bachelor course Complexity. Frits Vaandrager 16th November 2017 Lecture 9 33 / 35

70 Design an efficient algorithm for the following problem: 1 Input: A collection C of n banking cards 2 Output: Are there more than n/2 equivalent cards, that is, cards that correspond to the same account? 3 Only allowed operation on cards is equivalence test, which determines whether two cards are equivalent Frits Vaandrager 16th November 2017 Lecture 9 34 / 35

71 Algorithm Let C be nonempty set of cards. We define procedure P(C) which returns false if there is no card in C that is equivalent to more than n/2 cards (including itself), and returns yes if there is such a card. In the latter case P also returns a card c C and a number k > n/2 such that c is equivalent to k cards in C (including itself). 1 If n = 1 then P(C) returns yes together with card c and number 1. 2 If n > 1 then we divide C into two subsets C 1 and C 2 with sizes n/2 and n/2, respectively. 3 First we run P(C 1 ). If P(C 1 ) returns true and a card c 1 that is equivalent to k 1 cards in C 1, then we test, for each of the cards in C 2, whether or not it is equivalent to c 1 and record the number l 1 of cards in C 2 for which this is the case. If k 1 + l 1 > n/2 then P(C) returns true with card c 1 and number k 1 + l 1. 4 Otherwise we run P(C 2 ). If P(C 2 ) returns true and a card c 2 that is equivalent to k 2 cards in C 2, then we test, for each of the cards in C 1, whether it is equivalent to c 2 and record the number l 2 of cards in C 1 for which this is the case. If k 2 + l 2 > n/2 then P(C) returns true with card c 2 and number k 2 + l 2. 5 Otherwise, P(C) returns false. Frits Vaandrager 16th November 2017 Lecture 9 35 / 35

The divide-and-conquer paradigm involves three steps at each level of the recursion: Divide the problem into a number of subproblems.

The divide-and-conquer paradigm involves three steps at each level of the recursion: Divide the problem into a number of subproblems. 2.3 Designing algorithms There are many ways to design algorithms. Insertion sort uses an incremental approach: having sorted the subarray A[1 j - 1], we insert the single element A[j] into its proper

More information

Lecture 2: Getting Started

Lecture 2: Getting Started Lecture 2: Getting Started Insertion Sort Our first algorithm is Insertion Sort Solves the sorting problem Input: A sequence of n numbers a 1, a 2,..., a n. Output: A permutation (reordering) a 1, a 2,...,

More information

Plotting run-time graphically. Plotting run-time graphically. CS241 Algorithmics - week 1 review. Prefix Averages - Algorithm #1

Plotting run-time graphically. Plotting run-time graphically. CS241 Algorithmics - week 1 review. Prefix Averages - Algorithm #1 CS241 - week 1 review Special classes of algorithms: logarithmic: O(log n) linear: O(n) quadratic: O(n 2 ) polynomial: O(n k ), k 1 exponential: O(a n ), a > 1 Classifying algorithms is generally done

More information

Algorithms - Ch2 Sorting

Algorithms - Ch2 Sorting Algorithms - Ch2 Sorting (courtesy of Prof.Pecelli with some changes from Prof. Daniels) 1/28/2015 91.404 - Algorithms 1 Algorithm Description Algorithm Description: -Pseudocode see conventions on p. 20-22

More information

EECS 2011M: Fundamentals of Data Structures

EECS 2011M: Fundamentals of Data Structures M: Fundamentals of Data Structures Instructor: Suprakash Datta Office : LAS 3043 Course page: http://www.eecs.yorku.ca/course/2011m Also on Moodle Note: Some slides in this lecture are adopted from James

More information

Advanced Algorithms and Data Structures

Advanced Algorithms and Data Structures Advanced Algorithms and Data Structures Prof. Tapio Elomaa Course Basics A new 7 credit unit course Replaces OHJ-2156 Analysis of Algorithms We take things a bit further than OHJ-2156 We will assume familiarity

More information

Divide and Conquer. Algorithm D-and-C(n: input size)

Divide and Conquer. Algorithm D-and-C(n: input size) Divide and Conquer Algorithm D-and-C(n: input size) if n n 0 /* small size problem*/ Solve problem without futher sub-division; else Divide into m sub-problems; Conquer the sub-problems by solving them

More information

COMP250: Loop invariants

COMP250: Loop invariants COMP250: Loop invariants Jérôme Waldispühl School of Computer Science McGill University Based on (CRLS, 2009) & slides from (Sora,2015) Algorithm specification An algorithm is described by: Input data

More information

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 5. The Towers of Hanoi. Divide and Conquer

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 5. The Towers of Hanoi. Divide and Conquer Taking Stock IE170: Algorithms in Systems Engineering: Lecture 5 Jeff Linderoth Department of Industrial and Systems Engineering Lehigh University January 24, 2007 Last Time In-Place, Out-of-Place Count

More information

ECS122A Lecture Notes on Algorithm Design and Analysis

ECS122A Lecture Notes on Algorithm Design and Analysis ECS122A Lecture Notes on Algorithm Design and Analysis Winter 2018 http://www.cs.ucdavis.edu/ bai/ecs122a Professor Zhaojun Bai 1 / 12 Overview I. Introduction and getting started II. Growth of functions

More information

Sorting & Growth of Functions

Sorting & Growth of Functions Sorting & Growth of Functions CSci 588: Data Structures, Algorithms and Software Design Introduction to Algorithms, Cormen et al., Chapter 3 All material not from online sources or text copyright Travis

More information

Back to Sorting More efficient sorting algorithms

Back to Sorting More efficient sorting algorithms Back to Sorting More efficient sorting algorithms Merge Sort Strategy break problem into smaller subproblems recursively solve subproblems combine solutions to answer Called divide-and-conquer we used

More information

e-pg PATHSHALA- Computer Science Design and Analysis of Algorithms Module 10

e-pg PATHSHALA- Computer Science Design and Analysis of Algorithms Module 10 e-pg PATHSHALA- Computer Science Design and Analysis of Algorithms Module 10 Component-I (B) Description of Module Items Subject Name Computer Science Description of Module Paper Name Module Name/Title

More information

Dm507 project 1. Emil Sjulstok Rasmussen Hold: M1

Dm507 project 1. Emil Sjulstok Rasmussen Hold: M1 Dm507 project 1 Emil Sjulstok Rasmussen - 050391 Hold: M1 Problem 1 In assignment 1 I have used an ArrayList to store the int s from the text file, while in assignment 2 I have just used an int array.

More information

Divide & Conquer. 2. Conquer the sub-problems by solving them recursively. 1. Divide the problem into number of sub-problems

Divide & Conquer. 2. Conquer the sub-problems by solving them recursively. 1. Divide the problem into number of sub-problems Divide & Conquer Divide & Conquer The Divide & Conquer approach breaks down the problem into multiple smaller sub-problems, solves the sub-problems recursively, then combines the solutions of the sub-problems

More information

Divide and Conquer. Design and Analysis of Algorithms Andrei Bulatov

Divide and Conquer. Design and Analysis of Algorithms Andrei Bulatov Divide and Conquer Design and Analysis of Algorithms Andrei Bulatov Algorithms Divide and Conquer 4-2 Divide and Conquer, MergeSort Recursive algorithms: Call themselves on subproblem Divide and Conquer

More information

The divide and conquer strategy has three basic parts. For a given problem of size n,

The divide and conquer strategy has three basic parts. For a given problem of size n, 1 Divide & Conquer One strategy for designing efficient algorithms is the divide and conquer approach, which is also called, more simply, a recursive approach. The analysis of recursive algorithms often

More information

Analyzing Complexity of Lists

Analyzing Complexity of Lists Analyzing Complexity of Lists Operation Sorted Array Sorted Linked List Unsorted Array Unsorted Linked List Search( L, x ) O(logn) O( n ) O( n ) O( n ) Insert( L, x ) O(logn) O( n ) + O( 1 ) O( 1 ) + O(

More information

Jana Kosecka. Linear Time Sorting, Median, Order Statistics. Many slides here are based on E. Demaine, D. Luebke slides

Jana Kosecka. Linear Time Sorting, Median, Order Statistics. Many slides here are based on E. Demaine, D. Luebke slides Jana Kosecka Linear Time Sorting, Median, Order Statistics Many slides here are based on E. Demaine, D. Luebke slides Insertion sort: Easy to code Fast on small inputs (less than ~50 elements) Fast on

More information

Computer Science 385 Analysis of Algorithms Siena College Spring Topic Notes: Divide and Conquer

Computer Science 385 Analysis of Algorithms Siena College Spring Topic Notes: Divide and Conquer Computer Science 385 Analysis of Algorithms Siena College Spring 2011 Topic Notes: Divide and Conquer Divide and-conquer is a very common and very powerful algorithm design technique. The general idea:

More information

6/12/2013. Introduction to Algorithms (2 nd edition) Overview. The Sorting Problem. Chapter 2: Getting Started. by Cormen, Leiserson, Rivest & Stein

6/12/2013. Introduction to Algorithms (2 nd edition) Overview. The Sorting Problem. Chapter 2: Getting Started. by Cormen, Leiserson, Rivest & Stein Introduction to Algorithms (2 nd edition) by Cormen, Leiserson, Rivest & Stein Chapter 2: Getting Started (slides enhanced by N. Adlai A. DePano) Overview Aims to familiarize us with framework used throughout

More information

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics 1 Sorting 1.1 Problem Statement You are given a sequence of n numbers < a 1, a 2,..., a n >. You need to

More information

II (Sorting and) Order Statistics

II (Sorting and) Order Statistics II (Sorting and) Order Statistics Heapsort Quicksort Sorting in Linear Time Medians and Order Statistics 8 Sorting in Linear Time The sorting algorithms introduced thus far are comparison sorts Any comparison

More information

Another Sorting Algorithm

Another Sorting Algorithm 1 Another Sorting Algorithm What was the running time of insertion sort? Can we do better? 2 Designing Algorithms Many ways to design an algorithm: o Incremental: o Divide and Conquer: 3 Divide and Conquer

More information

Algorithms + Data Structures Programs

Algorithms + Data Structures Programs Introduction Algorithms Method for solving problems suitable for computer implementation Generally independent of computer hardware characteristics Possibly suitable for many different programming languages

More information

Lecture 5: Sorting Part A

Lecture 5: Sorting Part A Lecture 5: Sorting Part A Heapsort Running time O(n lg n), like merge sort Sorts in place (as insertion sort), only constant number of array elements are stored outside the input array at any time Combines

More information

CS 303 Design and Analysis of Algorithms

CS 303 Design and Analysis of Algorithms Mid-term CS 303 Design and Analysis of Algorithms Review For Midterm Dong Xu (Based on class note of David Luebke) 12:55-1:55pm, Friday, March 19 Close book Bring your calculator 30% of your final score

More information

CS473 - Algorithms I

CS473 - Algorithms I CS473 - Algorithms I Lecture 4 The Divide-and-Conquer Design Paradigm View in slide-show mode 1 Reminder: Merge Sort Input array A sort this half sort this half Divide Conquer merge two sorted halves Combine

More information

Deterministic and Randomized Quicksort. Andreas Klappenecker

Deterministic and Randomized Quicksort. Andreas Klappenecker Deterministic and Randomized Quicksort Andreas Klappenecker Overview Deterministic Quicksort Modify Quicksort to obtain better asymptotic bound Linear-time median algorithm Randomized Quicksort Deterministic

More information

DATA STRUCTURES AND ALGORITHMS

DATA STRUCTURES AND ALGORITHMS DATA STRUCTURES AND ALGORITHMS Fast sorting algorithms Shellsort, Mergesort, Quicksort Summary of the previous lecture Why sorting is needed? Examples from everyday life What are the basic operations in

More information

Merge Sort. Algorithm Analysis. November 15, 2017 Hassan Khosravi / Geoffrey Tien 1

Merge Sort. Algorithm Analysis. November 15, 2017 Hassan Khosravi / Geoffrey Tien 1 Merge Sort Algorithm Analysis November 15, 2017 Hassan Khosravi / Geoffrey Tien 1 The story thus far... CPSC 259 topics up to this point Priority queue Abstract data types Stack Queue Dictionary Tools

More information

DIVIDE & CONQUER. Problem of size n. Solution to sub problem 1

DIVIDE & CONQUER. Problem of size n. Solution to sub problem 1 DIVIDE & CONQUER Definition: Divide & conquer is a general algorithm design strategy with a general plan as follows: 1. DIVIDE: A problem s instance is divided into several smaller instances of the same

More information

Data Structures and Algorithms CSE 465

Data Structures and Algorithms CSE 465 Data Structures and Algorithms CSE 465 LECTURE 4 More Divide and Conquer Binary Search Exponentiation Multiplication Sofya Raskhodnikova and Adam Smith Review questions How long does Merge Sort take on

More information

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University CS 112 Introduction to Computing II Wayne Snyder Department Boston University Today Recursive Sorting Methods and their Complexity: Mergesort Conclusions on sorting algorithms and complexity Next Time:

More information

4. Sorting and Order-Statistics

4. Sorting and Order-Statistics 4. Sorting and Order-Statistics 4. Sorting and Order-Statistics The sorting problem consists in the following : Input : a sequence of n elements (a 1, a 2,..., a n ). Output : a permutation (a 1, a 2,...,

More information

MergeSort. Algorithm : Design & Analysis [5]

MergeSort. Algorithm : Design & Analysis [5] MergeSort Algorithm : Design & Analysis [5] In the last class Insertion sort Analysis of insertion sorting algorithm Lower bound of local comparison based sorting algorithm General pattern of divide-and-conquer

More information

Scribe: Sam Keller (2015), Seth Hildick-Smith (2016), G. Valiant (2017) Date: January 25, 2017

Scribe: Sam Keller (2015), Seth Hildick-Smith (2016), G. Valiant (2017) Date: January 25, 2017 CS 6, Lecture 5 Quicksort Scribe: Sam Keller (05), Seth Hildick-Smith (06), G. Valiant (07) Date: January 5, 07 Introduction Today we ll study another sorting algorithm. Quicksort was invented in 959 by

More information

Fundamental problem in computing science. putting a collection of items in order. Often used as part of another algorithm

Fundamental problem in computing science. putting a collection of items in order. Often used as part of another algorithm cmpt-225 Sorting Sorting Fundamental problem in computing science putting a collection of items in order Often used as part of another algorithm e.g. sort a list, then do many binary searches e.g. looking

More information

Lecture 3. Recurrences / Heapsort

Lecture 3. Recurrences / Heapsort Lecture 3. Recurrences / Heapsort T. H. Cormen, C. E. Leiserson and R. L. Rivest Introduction to Algorithms, 3rd Edition, MIT Press, 2009 Sungkyunkwan University Hyunseung Choo choo@skku.edu Copyright

More information

CS 506, Sect 002 Homework 5 Dr. David Nassimi Foundations of CS Due: Week 11, Mon. Apr. 7 Spring 2014

CS 506, Sect 002 Homework 5 Dr. David Nassimi Foundations of CS Due: Week 11, Mon. Apr. 7 Spring 2014 CS 506, Sect 002 Homework 5 Dr. David Nassimi Foundations of CS Due: Week 11, Mon. Apr. 7 Spring 2014 Study: Chapter 4 Analysis of Algorithms, Recursive Algorithms, and Recurrence Equations 1. Prove the

More information

CPSC 320 Midterm 2 Thursday March 13th, 2014

CPSC 320 Midterm 2 Thursday March 13th, 2014 CPSC 320 Midterm 2 Thursday March 13th, 2014 [12] 1. Answer each question with True or False, and then justify your answer briefly. [2] (a) The Master theorem can be applied to the recurrence relation

More information

Design and Analysis of Algorithms

Design and Analysis of Algorithms Design and Analysis of Algorithms CSE 5311 Lecture 8 Sorting in Linear Time Junzhou Huang, Ph.D. Department of Computer Science and Engineering CSE5311 Design and Analysis of Algorithms 1 Sorting So Far

More information

COE428 Lecture Notes Week 1 (Week of January 9, 2017)

COE428 Lecture Notes Week 1 (Week of January 9, 2017) COE428 Lecture Notes: Week 1 1 of 10 COE428 Lecture Notes Week 1 (Week of January 9, 2017) Table of Contents COE428 Lecture Notes Week 1 (Week of January 9, 2017)...1 Announcements...1 Topics...1 Informal

More information

Midterm solutions. n f 3 (n) = 3

Midterm solutions. n f 3 (n) = 3 Introduction to Computer Science 1, SE361 DGIST April 20, 2016 Professors Min-Soo Kim and Taesup Moon Midterm solutions Midterm solutions The midterm is a 1.5 hour exam (4:30pm 6:00pm). This is a closed

More information

Introduction to Computer Algorithms Lecture Notes (undergraduate CS470 course) Grzegorz Malewicz

Introduction to Computer Algorithms Lecture Notes (undergraduate CS470 course) Grzegorz Malewicz Introduction to Computer Algorithms Lecture Notes (undergraduate CS470 course) taught by Grzegorz Malewicz using the text Cormen, Leiserson, Rivest, Stein: Introduction to Algorithms (nd Edition). MIT

More information

Introduction. e.g., the item could be an entire block of information about a student, while the search key might be only the student's name

Introduction. e.g., the item could be an entire block of information about a student, while the search key might be only the student's name Chapter 7 Sorting 2 Introduction sorting fundamental task in data management well-studied problem in computer science basic problem given an of items where each item contains a key, rearrange the items

More information

Analysis of Algorithms - Quicksort -

Analysis of Algorithms - Quicksort - Analysis of Algorithms - Quicksort - Andreas Ermedahl MRTC (Mälardalens Real-Time Reseach Center) andreas.ermedahl@mdh.se Autumn 2003 Quicksort Proposed by C.A.R. Hoare in 962. Divide- and- conquer algorithm

More information

Algorithms in Systems Engineering ISE 172. Lecture 12. Dr. Ted Ralphs

Algorithms in Systems Engineering ISE 172. Lecture 12. Dr. Ted Ralphs Algorithms in Systems Engineering ISE 172 Lecture 12 Dr. Ted Ralphs ISE 172 Lecture 12 1 References for Today s Lecture Required reading Chapter 6 References CLRS Chapter 7 D.E. Knuth, The Art of Computer

More information

Lecture 8: Mergesort / Quicksort Steven Skiena

Lecture 8: Mergesort / Quicksort Steven Skiena Lecture 8: Mergesort / Quicksort Steven Skiena Department of Computer Science State University of New York Stony Brook, NY 11794 4400 http://www.cs.stonybrook.edu/ skiena Problem of the Day Give an efficient

More information

CSC236H Lecture 5. October 17, 2018

CSC236H Lecture 5. October 17, 2018 CSC236H Lecture 5 October 17, 2018 Runtime of recursive programs def fact1(n): if n == 1: return 1 else: return n * fact1(n-1) (a) Base case: T (1) = c (constant amount of work) (b) Recursive call: T

More information

Sorting. Sorting in Arrays. SelectionSort. SelectionSort. Binary search works great, but how do we create a sorted array in the first place?

Sorting. Sorting in Arrays. SelectionSort. SelectionSort. Binary search works great, but how do we create a sorted array in the first place? Sorting Binary search works great, but how do we create a sorted array in the first place? Sorting in Arrays Sorting algorithms: Selection sort: O(n 2 ) time Merge sort: O(nlog 2 (n)) time Quicksort: O(n

More information

CSC Design and Analysis of Algorithms

CSC Design and Analysis of Algorithms CSC 8301- Design and Analysis of Algorithms Lecture 6 Divide and Conquer Algorithm Design Technique Divide-and-Conquer The most-well known algorithm design strategy: 1. Divide a problem instance into two

More information

Problem. Input: An array A = (A[1],..., A[n]) with length n. Output: a permutation A of A, that is sorted: A [i] A [j] for all. 1 i j n.

Problem. Input: An array A = (A[1],..., A[n]) with length n. Output: a permutation A of A, that is sorted: A [i] A [j] for all. 1 i j n. Problem 5. Sorting Simple Sorting, Quicksort, Mergesort Input: An array A = (A[1],..., A[n]) with length n. Output: a permutation A of A, that is sorted: A [i] A [j] for all 1 i j n. 98 99 Selection Sort

More information

CSC Design and Analysis of Algorithms. Lecture 6. Divide and Conquer Algorithm Design Technique. Divide-and-Conquer

CSC Design and Analysis of Algorithms. Lecture 6. Divide and Conquer Algorithm Design Technique. Divide-and-Conquer CSC 8301- Design and Analysis of Algorithms Lecture 6 Divide and Conquer Algorithm Design Technique Divide-and-Conquer The most-well known algorithm design strategy: 1. Divide a problem instance into two

More information

Scientific Computing. Algorithm Analysis

Scientific Computing. Algorithm Analysis ECE257 Numerical Methods and Scientific Computing Algorithm Analysis Today s s class: Introduction to algorithm analysis Growth of functions Introduction What is an algorithm? A sequence of computation

More information

Algorithms and Data Structures (INF1) Lecture 7/15 Hua Lu

Algorithms and Data Structures (INF1) Lecture 7/15 Hua Lu Algorithms and Data Structures (INF1) Lecture 7/15 Hua Lu Department of Computer Science Aalborg University Fall 2007 This Lecture Merge sort Quick sort Radix sort Summary We will see more complex techniques

More information

CS 561, Lecture 1. Jared Saia University of New Mexico

CS 561, Lecture 1. Jared Saia University of New Mexico CS 561, Lecture 1 Jared Saia University of New Mexico Quicksort Based on divide and conquer strategy Worst case is Θ(n 2 ) Expected running time is Θ(n log n) An In-place sorting algorithm Almost always

More information

Lecture 9: Sorting Algorithms

Lecture 9: Sorting Algorithms Lecture 9: Sorting Algorithms Bo Tang @ SUSTech, Spring 2018 Sorting problem Sorting Problem Input: an array A[1..n] with n integers Output: a sorted array A (in ascending order) Problem is: sort A[1..n]

More information

CIS 121 Data Structures and Algorithms with Java Spring Code Snippets and Recurrences Monday, January 29/Tuesday, January 30

CIS 121 Data Structures and Algorithms with Java Spring Code Snippets and Recurrences Monday, January 29/Tuesday, January 30 CIS 11 Data Structures and Algorithms with Java Spring 018 Code Snippets and Recurrences Monday, January 9/Tuesday, January 30 Learning Goals Practice solving recurrences and proving asymptotic bounds

More information

Fundamental mathematical techniques reviewed: Mathematical induction Recursion. Typically taught in courses such as Calculus and Discrete Mathematics.

Fundamental mathematical techniques reviewed: Mathematical induction Recursion. Typically taught in courses such as Calculus and Discrete Mathematics. Fundamental mathematical techniques reviewed: Mathematical induction Recursion Typically taught in courses such as Calculus and Discrete Mathematics. Techniques introduced: Divide-and-Conquer Algorithms

More information

Data Structures and Algorithms CMPSC 465

Data Structures and Algorithms CMPSC 465 Data Structures and Algorithms CMPSC 465 LECTURES 7-8 More Divide and Conquer Multiplication Adam Smith S. Raskhodnikova and A. Smith; based on slides by E. Demaine and C. Leiserson John McCarthy (1927

More information

Chapter 4. Divide-and-Conquer. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

Chapter 4. Divide-and-Conquer. Copyright 2007 Pearson Addison-Wesley. All rights reserved. Chapter 4 Divide-and-Conquer Copyright 2007 Pearson Addison-Wesley. All rights reserved. Divide-and-Conquer The most-well known algorithm design strategy: 2. Divide instance of problem into two or more

More information

17/05/2018. Outline. Outline. Divide and Conquer. Control Abstraction for Divide &Conquer. Outline. Module 2: Divide and Conquer

17/05/2018. Outline. Outline. Divide and Conquer. Control Abstraction for Divide &Conquer. Outline. Module 2: Divide and Conquer Module 2: Divide and Conquer Divide and Conquer Control Abstraction for Divide &Conquer 1 Recurrence equation for Divide and Conquer: If the size of problem p is n and the sizes of the k sub problems are

More information

CSE 3101: Introduction to the Design and Analysis of Algorithms. Office hours: Wed 4-6 pm (CSEB 3043), or by appointment.

CSE 3101: Introduction to the Design and Analysis of Algorithms. Office hours: Wed 4-6 pm (CSEB 3043), or by appointment. CSE 3101: Introduction to the Design and Analysis of Algorithms Instructor: Suprakash Datta (datta[at]cse.yorku.ca) ext 77875 Lectures: Tues, BC 215, 7 10 PM Office hours: Wed 4-6 pm (CSEB 3043), or by

More information

Lecture 15 : Review DRAFT

Lecture 15 : Review DRAFT CS/Math 240: Introduction to Discrete Mathematics 3/10/2011 Lecture 15 : Review Instructor: Dieter van Melkebeek Scribe: Dalibor Zelený DRAFT Today slectureservesasareviewofthematerialthatwillappearonyoursecondmidtermexam.

More information

Sorting. Task Description. Selection Sort. Should we worry about speed?

Sorting. Task Description. Selection Sort. Should we worry about speed? Sorting Should we worry about speed? Task Description We have an array of n values in any order We need to have the array sorted in ascending or descending order of values 2 Selection Sort Select the smallest

More information

Scan and Quicksort. 1 Scan. 1.1 Contraction CSE341T 09/20/2017. Lecture 7

Scan and Quicksort. 1 Scan. 1.1 Contraction CSE341T 09/20/2017. Lecture 7 CSE341T 09/20/2017 Lecture 7 Scan and Quicksort 1 Scan Scan is a very useful primitive for parallel programming. We will use it all the time in this class. First, lets start by thinking about what other

More information

(Refer Slide Time: 01.26)

(Refer Slide Time: 01.26) Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi Lecture # 22 Why Sorting? Today we are going to be looking at sorting.

More information

Lecture 6: Divide-and-Conquer

Lecture 6: Divide-and-Conquer Lecture 6: Divide-and-Conquer COSC242: Algorithms and Data Structures Brendan McCane Department of Computer Science, University of Otago Types of Algorithms In COSC242, we will be looking at 3 general

More information

CS302 Topic: Algorithm Analysis #2. Thursday, Sept. 21, 2006

CS302 Topic: Algorithm Analysis #2. Thursday, Sept. 21, 2006 CS302 Topic: Algorithm Analysis #2 Thursday, Sept. 21, 2006 Analysis of Algorithms The theoretical study of computer program performance and resource usage What s also important (besides performance/resource

More information

Divide and Conquer Algorithms

Divide and Conquer Algorithms CSE341T 09/13/2017 Lecture 5 Divide and Conquer Algorithms We have already seen a couple of divide and conquer algorithms in this lecture. The reduce algorithm and the algorithm to copy elements of the

More information

1 Probabilistic analysis and randomized algorithms

1 Probabilistic analysis and randomized algorithms 1 Probabilistic analysis and randomized algorithms Consider the problem of hiring an office assistant. We interview candidates on a rolling basis, and at any given point we want to hire the best candidate

More information

Selection (deterministic & randomized): finding the median in linear time

Selection (deterministic & randomized): finding the median in linear time Lecture 4 Selection (deterministic & randomized): finding the median in linear time 4.1 Overview Given an unsorted array, how quickly can one find the median element? Can one do it more quickly than bysorting?

More information

Divide and Conquer Algorithms

Divide and Conquer Algorithms Divide and Conquer Algorithms T. M. Murali February 19, 2009 Divide and Conquer Break up a problem into several parts. Solve each part recursively. Solve base cases by brute force. Efficiently combine

More information

11/8/2016. Chapter 7 Sorting. Introduction. Introduction. Sorting Algorithms. Sorting. Sorting

11/8/2016. Chapter 7 Sorting. Introduction. Introduction. Sorting Algorithms. Sorting. Sorting Introduction Chapter 7 Sorting sorting fundamental task in data management well-studied problem in computer science basic problem given an array of items where each item contains a key, rearrange the items

More information

Lecture Notes for Chapter 2: Getting Started

Lecture Notes for Chapter 2: Getting Started Instant download and all chapters Instructor's Manual Introduction To Algorithms 2nd Edition Thomas H. Cormen, Clara Lee, Erica Lin https://testbankdata.com/download/instructors-manual-introduction-algorithms-2ndedition-thomas-h-cormen-clara-lee-erica-lin/

More information

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS Department of Computer Science University of Babylon LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS By Faculty of Science for Women( SCIW), University of Babylon, Iraq Samaher@uobabylon.edu.iq

More information

Analysis of Algorithms - Medians and order statistic -

Analysis of Algorithms - Medians and order statistic - Analysis of Algorithms - Medians and order statistic - Andreas Ermedahl MRTC (Mälardalens Real-Time Reseach Center) andreas.ermedahl@mdh.se Autumn 2003 Order statistics Select the i:th smallest element

More information

UNIT 1 BASICS OF AN ALGORITHM

UNIT 1 BASICS OF AN ALGORITHM UNIT 1 BASICS OF AN ALGORITHM Basics of an Algorithm Structure Page Nos. 1.0 Introduction 5 1.1 Objectives 6 1.2. Analysis and Complexity of Algorithms 6 1.3 Basic Technique for Design of Efficient Algorithms

More information

Your favorite blog : (popularly known as VIJAY JOTANI S BLOG..now in facebook.join ON FB VIJAY

Your favorite blog :  (popularly known as VIJAY JOTANI S BLOG..now in facebook.join ON FB VIJAY Course Code : BCS-042 Course Title : Introduction to Algorithm Design Assignment Number : BCA(IV)-042/Assign/14-15 Maximum Marks : 80 Weightage : 25% Last Date of Submission : 15th October, 2014 (For July

More information

Biostatistics 615/815 Lecture 5: Divide and Conquer Algorithms Sorting Algorithms

Biostatistics 615/815 Lecture 5: Divide and Conquer Algorithms Sorting Algorithms Biostatistics 615/815 Lecture 5: Algorithms Algorithms Hyun Min Kang September 20th, 2011 Hyun Min Kang Biostatistics 615/815 - Lecture 5 September 20th, 2011 1 / 30 Recap - An example C++ class #include

More information

Sorting. There exist sorting algorithms which have shown to be more efficient in practice.

Sorting. There exist sorting algorithms which have shown to be more efficient in practice. Sorting Next to storing and retrieving data, sorting of data is one of the more common algorithmic tasks, with many different ways to perform it. Whenever we perform a web search and/or view statistics

More information

Divide-and-Conquer CSE 680

Divide-and-Conquer CSE 680 Divide-and-Conquer CSE 680 1 Introduction Given an instance x of a problem, the divide-and-conquer method works as follows. function DAQ(x) if x is sufficiently small or simple then solve it directly else

More information

University of Waterloo Department of Electrical and Computer Engineering ECE250 Algorithms and Data Structures Fall 2017

University of Waterloo Department of Electrical and Computer Engineering ECE250 Algorithms and Data Structures Fall 2017 University of Waterloo Department of Electrical and Computer Engineering ECE250 Algorithms and Data Structures Fall 207 Midterm Examination Instructor: Dr. Ladan Tahvildari, PEng, SMIEEE Date: Wednesday,

More information

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University 0/6/6 CS Introduction to Computing II Wayne Snyder Department Boston University Today Conclusions on Iterative Sorting: Complexity of Insertion Sort Recursive Sorting Methods and their Complexity: Mergesort

More information

Analysis of Sorting Algorithms. Imagine you have a few thousand dollars in your safe in all different denominations. The

Analysis of Sorting Algorithms. Imagine you have a few thousand dollars in your safe in all different denominations. The Laskowski 1 Bob Laskowski Professor Shana Watters CSC 320 25 April 2016 Analysis of Sorting Algorithms Introduction Imagine you have a few thousand dollars in your safe in all different denominations.

More information

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: April 1, 2015

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: April 1, 2015 CS161, Lecture 2 MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: April 1, 2015 1 Introduction Today, we will introduce a fundamental algorithm design paradigm, Divide-And-Conquer,

More information

CPSC W2 Midterm #2 Sample Solutions

CPSC W2 Midterm #2 Sample Solutions CPSC 320 2014W2 Midterm #2 Sample Solutions March 13, 2015 1 Canopticon [8 marks] Classify each of the following recurrences (assumed to have base cases of T (1) = T (0) = 1) into one of the three cases

More information

Advanced Algorithms and Data Structures

Advanced Algorithms and Data Structures Advanced Algorithms and Data Structures Prof. Tapio Elomaa tapio.elomaa@tut.fi Course Prerequisites A seven credit unit course Replaced OHJ-2156 Analysis of Algorithms We take things a bit further than

More information

CSC 505, Spring 2005 Week 6 Lectures page 1 of 9

CSC 505, Spring 2005 Week 6 Lectures page 1 of 9 CSC 505, Spring 2005 Week 6 Lectures page 1 of 9 Objectives: learn general strategies for problems about order statistics learn how to find the median (or k-th largest) in linear average-case number of

More information

Data Structures and Algorithms Running time, Divide and Conquer January 25, 2018

Data Structures and Algorithms Running time, Divide and Conquer January 25, 2018 Data Structures and Algorithms Running time, Divide and Conquer January 5, 018 Consider the problem of computing n for any non-negative integer n. similar looking algorithms to solve this problem. Below

More information

COMP2012H Spring 2014 Dekai Wu. Sorting. (more on sorting algorithms: mergesort, quicksort, heapsort)

COMP2012H Spring 2014 Dekai Wu. Sorting. (more on sorting algorithms: mergesort, quicksort, heapsort) COMP2012H Spring 2014 Dekai Wu Sorting (more on sorting algorithms: mergesort, quicksort, heapsort) Merge Sort Recursive sorting strategy. Let s look at merge(.. ) first. COMP2012H (Sorting) 2 COMP2012H

More information

COMP Analysis of Algorithms & Data Structures

COMP Analysis of Algorithms & Data Structures COMP 3170 - Analysis of Algorithms & Data Structures Shahin Kamali Lecture 7 - Jan. 17, 2018 CLRS 7.1, 7-4, 9.1, 9.3 University of Manitoba COMP 3170 - Analysis of Algorithms & Data Structures 1 / 11 QuickSelect

More information

Analyze the obvious algorithm, 5 points Here is the most obvious algorithm for this problem: (LastLargerElement[A[1..n]:

Analyze the obvious algorithm, 5 points Here is the most obvious algorithm for this problem: (LastLargerElement[A[1..n]: CSE 101 Homework 1 Background (Order and Recurrence Relations), correctness proofs, time analysis, and speeding up algorithms with restructuring, preprocessing and data structures. Due Thursday, April

More information

15 150: Principles of Functional Programming Sorting Integer Lists

15 150: Principles of Functional Programming Sorting Integer Lists 15 150: Principles of Functional Programming Sorting Integer Lists Michael Erdmann Spring 2018 1 Background Let s define what we mean for a list of integers to be sorted, by reference to datatypes and

More information

2 Proposed Implementation. 1 Introduction. Abstract. 2.1 Pseudocode of the Proposed Merge Procedure

2 Proposed Implementation. 1 Introduction. Abstract. 2.1 Pseudocode of the Proposed Merge Procedure Enhanced Merge Sort Using Simplified Transferrable Auxiliary Space Zirou Qiu, Ziping Liu, Xuesong Zhang Department of Computer Science Southeast Missouri State University Cape Girardeau, MO 63701 zqiu1s@semo.edu,

More information

CMSC 451: Divide and Conquer

CMSC 451: Divide and Conquer CMSC 451: Divide and Conquer Slides By: Carl Kingsford Department of Computer Science University of Maryland, College Park Based on Sections 5.1 5.3 of Algorithm Design by Kleinberg & Tardos. Greedy Recap

More information

Divide and Conquer 4-0

Divide and Conquer 4-0 Divide and Conquer 4-0 Divide-and-Conquer The most-well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2. Solve smaller instances recursively 3. Obtain

More information

Sankalchand Patel College of Engineering - Visnagar Department of Computer Engineering and Information Technology. Assignment

Sankalchand Patel College of Engineering - Visnagar Department of Computer Engineering and Information Technology. Assignment Class: V - CE Sankalchand Patel College of Engineering - Visnagar Department of Computer Engineering and Information Technology Sub: Design and Analysis of Algorithms Analysis of Algorithm: Assignment

More information

CSC236 Week 5. Larry Zhang

CSC236 Week 5. Larry Zhang CSC236 Week 5 Larry Zhang 1 Logistics Test 1 after lecture Location : IB110 (Last names A-S), IB 150 (Last names T-Z) Length of test: 50 minutes If you do really well... 2 Recap We learned two types of

More information