Definition A transition system S is a pair of the form. where C is the set of configurations and T C C is a relation, the

Size: px
Start display at page:

Download "Definition A transition system S is a pair of the form. where C is the set of configurations and T C C is a relation, the"

Transcription

1 Transition system Definition A transition system S is a pair of the form = S T) (C, where C is the set of configurations and T C C is a relation, the transition Λ relation. Algorithms and Data Structures Chapter 1: Transition Systems 1

2 Sequences generated by a transition system Definition Let S = (C, T) be a transition system. S generates a set of sequences, S(S), defined as follows: 1. the finite sequence c 0, c 1,...,c n (for n 0) belongs to S(S) if (ι) c 0 C 2 (ιι) for all i with» 1» i n: (c i 1, c i 2 T ) 2. the infinite sequence c 0, c 1,...,c n,... belongs to S(S) if (ι) c 0 C 2 (ιι) for all i 1: (c i 1, c i 2 T ) Λ Algorithms and Data Structures Chapter 1: Transition Systems 2

3 Processes generated by a transition system Definition Let S = (C, T) be a transition system. The set of processes generated by S, written P (S), is the subset of S(S) containing 1. all infinite sequences of S(S) 2. all finite sequences c 0, c 1,...,c n (n 0) of S(S) for which it holds that there is no c 2 C with (c n, c) 2 T. The final configuration of a finite process is called a dead configuration. Λ Algorithms and Data Structures Chapter 1: Transition Systems 3

4 Football Football b] j»» 2 Ng 2 + b] Λ» b] + b]» Λ b] +» + b] b] Λ Λ + b] +» b] Λ b] b]» + Λ b] b] Λ» b] + + 1] Λ b]» b] + 1] b]» + Λ + Λ b] b] Transition system Configurations: f[t, X, a, 0 t 90, X fa, B, Rg, a, b [t, A, a, [t 2, B, a, if t 88 [t, A, a, [t 2, B, a 1, if t 88 [t, A, a, [t 1, B, a, if t 89 [t, A, a, [t 1, B, a 1, if t 89 [90, A, a, [90, R, a, [t, B, a, [t 2, A, a, if t 88 [t, B, a, [t 2, A, a, b if t 88 [t, B, a, [t 1, A, a, if t 89 [t, B, a, [t 1, A, a, b if t 89 [90, B, a, [90, R, a, Algorithms and Data Structures Chapter 1: Transition Systems 4

5 Induction principle Induction principle Let P(0), P(1),...,P(n),... bestatements. If P(0) a) is true b) for all n 0 it holds P(n) that implies + P(n 1), P(n) then is true for all n 0. Algorithms and Data Structures Chapter 1: Transition Systems 5

6 Invariance principle Invariance principle for transition systems Let S = (C, T) be a transition system and let c 0 2 C be a configuration. If I(c) is a statement about the configurations of the system, the following holds. If a) I(c 0 ) is true b) for all (c, c 0 ) 2 T it holds that I(c) implies I(c 0 ) then I(c) is true for any configuration c that occurs in a sequence starting with c 0. Algorithms and Data Structures Chapter 1: Transition Systems 6

7 Termination principle Termination principle for transition systems Let S = (C, T) be a transition system and let µ : C! N be a function. If for all (c, c 0 ) 2 T it holds that µ(c) > µ(c 0 ) then all processes in P (S) are finite. Algorithms and Data Structures Chapter 1: Transition Systems 7

8 Nim Nim Bg n] Λ 2] n] Λ 1] n] Λ 2] 1] n] Λ Transition system Configurations: fa, N [A, [B, n if n 2 [A, [B, n if n 1 [B, [A, n if n 2 [B, [A, n if n 1 Algorithms and Data Structures Chapter 1: Transition Systems 8

9 Towers of Hanoi Transition system Hanoi(n) Configurations: f[a, B, C] j fa, B, Cg a partition of f1,..., ngg [A,B,C] Λ [An frg,b[ frg,c] if (r = min A) ^ (r < min B) [A, B, C] Λ [A n frg, B, C [ frg] if (r = min A) ^ (r < min C) [A, B, C] Λ [A [ frg, B n frg, C] if (r = min B) ^ (r < min A) [A, B, C] Λ [A, B n frg, C [ frg] if (r = min B) ^ (r < min C) [A, B, C] Λ [A [ frg, B, C n frg] if (r = min C) ^ (r < min A) [A, B, C] Λ [A, B [ frg, C n frg] if (r = min C) ^ (r < min B) Algorithms and Data Structures Chapter 1: Transition Systems 9

10 Euclid s algorithm Transition Euclid system Configurations: n] j f[m, m, 1g n n] Λ [m, [m n] n, if > m n [m, n] Λ [m, n m] if m < n Algorithms and Data Structures Chapter 1: Transition Systems 10

11 Expressions Transition Expressions system Configurations: f0, 1, +, E, T, )g (, αeβ Λ αtβ Λ Λ αeβ αt+eβ Λ αtβ α0β Λ αtβ α1β Λ αtβ α(e)β Algorithms and Data Structures Chapter 1: Transition Systems 11

12 Expressions (context-free) Transition Expressions system Configurations: f0, 1, +, E, T, )g (, E Λ T, T+E Λ Λ T 0, (E) 1, Algorithms and Data Structures Chapter 1: Transition Systems 12

13 Graph coloring Transition GraphColoring system Configurations: Danish graphs Λ Λ if there are no pink nodes Λ Algorithms and Data Structures Chapter 1: Transition Systems 13

14 Red-black tree Definition A red-black tree is binary search tree in which all internal nodes are colored either red or black, in which the leaves are black, and Invariant I 2 Each red node has a black parent. Invariant I 3 There is the same number of black nodes on all paths from the root to a leaf Λ Algorithms and Data Structures Chapter 1: Transition Systems 14

15 Insertion Illegitimate red node: x Invariant I2 : Each legitimate red node has a black parent. 0 Algorithms and Data Structures Chapter 1: Transition Systems 15

16 Insertion: transitions 1 and 2 The illegitimate node is the root of the tree: x x 1 2 Λ 1 2 The illegitimate node has a black father: A A x Λ 3 x Algorithms and Data Structures Chapter 1: Transition Systems 16

17 Insertion: transitions 3.1 and 3.2 The illegitimate node has a red father and a red uncle: A A B C B C Λ x x A A B C B C Λ 1 x x Algorithms and Data Structures Chapter 1: Transition Systems 17

18 Insertion: transitions 4.1 and 4.2 The illegitimate node has a red father and a black uncle: x B A C Λ B x A C x B A C Λ x B A C Algorithms and Data Structures Chapter 1: Transition Systems 18

19 Deletion Illegitimate black node: Invariant I 0 1 The tree satisfies I 1 if we remove the illegitimate node. Invariant I 0 2 Each red node has a legitimate black father. Algorithms and Data Structures Chapter 1: Transition Systems 19

20 Deletion: transition 1 The illegitimate node is the root: Λ Algorithms and Data Structures Chapter 1: Transition Systems 20

21 Deletion: transitions 2 and 3 The illegitimate node has a red father and a red closer nephew: 1 A C 2 3 B 4 Λ A C B The illegitimate node has a red father and a black closer nephew: A B B 1 C 4 Λ A 4 1 C Algorithms and Data Structures Chapter 1: Transition Systems 21

22 Deletion: transitions 4.1 and 4.2 The illegitimate node has a black father, a black sibling and one red nephew: 1 1 A C 2 3 A 2 B B 4 C 3 4 Λ Λ A C B A B C Algorithms and Data Structures Chapter 1: Transition Systems 22

23 Deletion: transition 5 The illegitimate node has a black father, a black sibling and two black nephews 1 A C 2 3 B 4 D 5 Λ 1 A B C D 5 Algorithms and Data Structures Chapter 1: Transition Systems 23

24 Deletion: transition 6 The illegitimate node has a black father and a red sibling: B A A B Λ Algorithms and Data Structures Chapter 1: Transition Systems 24

25 The transition system Commands Transition system Commands Configurations: f[c, σ] j C a command-sequence, σ a stateg [x ψ e; C 0,σ] Λ [C 0,σhx:ei] b then C [if 1 else else C 2 ; C 0,σ] Λ [C 1 ; C 0,σ] σ(b) = true if σ(b) = false if b then C [if 1 else else C 2 ; C 0,σ] Λ [C 2 ; C 0,σ] b do [ do C; C 0,σ] Λ [C; b do do C; C 0,σ] if σ(b) = true b do [ do C; C 0,σ] Λ [C 0,σ] if σ(b) = false Algorithms and Data Structures Chapter 2: Algorithm Theory 25

26 Euclid s algorithm n) = ) 6= do Algorithm Euclid(m, Input : m, n 1 Output : r gcd(m 0, n 0 Method m n : if do m > n then if ψ m m n then else r ψ m n ψ n m; Algorithms and Data Structures Chapter 2: Algorithm Theory 26

27 Execution of Euclid(72,45) Step# Action State# m n r m<>n m > n m < m-n m<>n m > n n < n-m m<>n m > n m < m-n m<>n m > n n < n-m m<>n r < m

28 Correctness A( ) Algorithm Input : In Output : Out Method : C Definition The A algorithm is correct if any process for the transition Commands system starting in a configuration [C,σ], where σ satisfies In, is finite and ends with a configuration σ satisfying Out. 0 Λ Algorithms and Data Structures Chapter 2: Algorithm Theory 27

29 Decorations b do [fig do C; C 0,σ] Λ [C; fig b do do C; C 0,σ] if σ(b) = true. Definition An assertion U of a decorated algorithm is valid for a process if for all configurations of the [fugc,σ] form in the process, the assertion U is satisfied by the state Λ σ. Algorithms and Data Structures Chapter 2: Algorithm Theory 28

30 Euclid s algorithm (decorated) n) = ) 6= do Algorithm Euclid(m, Input : m, n 1 Output : r gcd(m 0, n 0 Method m n fig : if do m > n then if ψ m m n then else r ψ m n ψ n m; I : gcd(m, n) = gcd(m 0, n 0 ), Algorithms and Data Structures Chapter 2: Algorithm Theory 29

31 Validity A( ) Algorithm Input : In Output : Out Method : C Definition The A algorithm is valid if all its assertions are valid for all processes starting in a [C,σ] configuration where σ satisfies Λ In. Algorithms and Data Structures Chapter 2: Algorithm Theory 30

32 Proof-burdens fugcfvg For any state σ, ifσsatisfies U and the execution of C in σ leads to σ 0 (ie. [C,σ] Λ Λ σ 0 ), then σ 0 must satisfy V. Algorithms and Data Structures Chapter 2: Algorithm Theory 31

33 Proof principle for simple proof-burdens Proof principle for simple proof-burdens Let C = c 1 ; ;c k be a sequence of assignments and let x 1,...,x n be the variables of the algorithm. Suppose that C executed in σ leads to σ 0. Then the proofburden fugcfvg is proved by proving the implication U(x 1,...,x n ) ) V(x 0 1,...,x 0 n ) where x1 n are the values of the variables in σ 0 functions of their values in σ. 0,...,x0 expressed as Algorithms and Data Structures Chapter 2: Algorithm Theory 32

34 Proof principle for compound proof-burdens Proof principle for compound proof-burdens A proof-burden of the form fugc 1 ; C 2 fvg gives rise to the proof-burdens fugc 1 fwg and fwgc 2 fvg. A proof-burden of the fugif form the proof-burdens b then C if 1 else fu ^ bgc 1 fvg and fu ^ :bgc 2 fvg. A proof-burden of the fug form proof-burdens b do ) U I (basis) ^ bgcfig fi (invariance) ^ I ) :b V (conclusion). else C 2 fvg gives rise to do CfVg gives rise to the Algorithms and Data Structures Chapter 2: Algorithm Theory 33

35 Termination A( ) Algorithm Input : In Output : Out Method : C Definition We say A that terminates if any process starting in a configuration [C,σ], where σ satisfies In, is Λ finite. Algorithms and Data Structures Chapter 2: Algorithm Theory 34

36 Termination principle Termination principle for algorithms Let x 1,...,x n be the variables of an algoritm A. A terminates if for every loop in its method with I as valid invariant, there exists an integer-valued function µ(x 1,...,x n ) satisfying a) I ) µ(x 1,...,x n ) 0 fig b do C b) I ^ b ) µ(x 1,...,x n ) > µ(x 0 1,...,x0 n ) 0 where x 0 1,...,x0 n are the values of the variables after an iteration expressed as functions of their values before. Algorithms and Data Structures Chapter 2: Algorithm Theory 35

37 Extended version of Euclid s algorithm n) = )) ^ = )) ψ ψ Algorithm ExtendedEuclid(m, Input : m, n 1 Output : (r gcd(m 0, n 0 (s lcm(m 0, n 0 Method : p m; q n; m > n then if ψ m m n; ψ p + p q if then fig do m 6= n do n ψ n m; q ψ q + p; r ψ m; s ψ (p + q)=2 else I : (mq + np = 2m 0 n 0 ) ^ (gcd(m, n) = gcd(m 0, n 0 )). Algorithms and Data Structures Chapter 2: Algorithm Theory 36

38 Factorial Factorial(n) = ψ Algorithm Input : n 0 Output : r n 0! Method : r 1; n 6= 0 do ψ r Λ r n; ψ n n 1; fig do I : (r = n 0!=n!) ^ (n 0 n 0). Algorithms and Data Structures Chapter 2: Algorithm Theory 37

39 Power sum Algorithm n) PowerSum(x, Input : 6= 0) ^ (x 0) (n Constants: x, n Output : = ± r i=0 n xi Method : ψ r 1; ψ m 0; m 6= n do ψ r Λ r + x 1; ψ m + m 1; fig do I : (r = ± m i=0x i ) ^ (n m 0). Algorithms and Data Structures Chapter 2: Algorithm Theory 38

40 Finding maximum in an array Algorithm ArrayMax(A) true = ψ ψ Input : Constants: A Output : r max A Method : r 1; i 0; fig do i 6= jaj do if r < A[i] then r ψ A[i]; ψ i + i 1 I : (0» i» jaj) ^ (r = max A[0..i]) µ(a, i, r) = jaj i Algorithms and Data Structures Chapter 2: Algorithm Theory 39

41 Time complexity ) Algorithm A(x 1,...,x n Input : In Output : Out Method : C Definition The time complexity of A is the function TJAK taking a state σ satisfying In to the length of the process starting at [C,σ]. Λ We can regard TJAK as a function of the input parameters x 1,...,x n Algorithms and Data Structures Chapter 2: Algorithm Theory 40

42 Time complexity for ArrayMax Steps ψ r 1; ψ i 0; 2 i 6= jaj do jaj + 1 fig if r < A[i] then r ψ A[i]; between jaj and 2jAj ψ i + i jaj 1 Total number of steps: 3jAj + 3» TJArrayMaxK(A)» 4jAj + 3 More precisely, TJArrayMaxK(A) equals 3jAj + 3 plus the number of times A[i] is strictly larger than max A[0..i], with i running through the indices 0,..., jaj 1. Algorithms and Data Structures Chapter 2: Algorithm Theory 41

43 Time complexity for Euclid if then m ψ m n then if m > n then do m 6= n do else n ψ n m m n T m n T m n T m n T m T n Algorithms and Data Structures Chapter 2: Algorithm Theory 42

44 Worst-case time complexity ) Algorithm A(x 1,...,x n Input : In Output : Out Method : C Definition Let size be a function mapping states satisfying In to non-negative integers. The worst-case time complexity A of is the function mapping n 0tothe maximum TJAK(σ) of for states σ (satisfying In) size(σ) = with Λ n. We can regard size as a function of the input parameters x 1,...,x n, and write TJArrayMaxK(A) = 4jAj O(jAj) Algorithms and Data Structures Chapter 2: Algorithm Theory 43

45 Exponentiation in linear time Algorithm p) LinExp(x, Input : p 0 Constants: x, p Output : = r x p Method : ψ r 1; ψ q p; q > 0 do ψ r Λ r x; ψ q q 1 fig do I : (rx q = x p ) ^ (q 0) µ(x, p, r, q) = q Algorithms and Data Structures Chapter 3: Fundamental algorithms 44

46 Exponentiation in logarithmic time Algorithm p) LogExp(x, Input : p 0 Constants: x, p Output : = r x p Method : ψ r 1; ψ q p; ψ h x; q even then if ψ q q=2; ψ h Λ h h if then fig do q > 0 do else q ψ q 1; r ψ r Λ h I : (rh q = x p ) ^ (q 0) µ(x, p, r, q, h) = q Algorithms and Data Structures Chapter 3: Fundamental algorithms 45

47 Scanning A i I : (0» i» jaj) ^ I 0 µ(a, i,...) = jaj i fi init fl; i ψ 0; fig do i 6= jaj do update fl; i ψ i + 1; fi end fl fi Algorithms and Data Structures Chapter 3: Fundamental algorithms 46

48 Linear search Algorithm s) LinearSearch(A, Input true : Constants: A, s Output :» (0» jaj) ^ r 62 A[0..r]) ^ (s = jaj _ A[r] = s) (r Method : ψ r 0; (r 6= jaj) ^ (A[r] 6= s) do ψ r + r 1; fig do I : (0» r» jaj) ^ (s 62 A[0..r]) µ(a, s, r) = jaj r Algorithms and Data Structures Chapter 3: Fundamental algorithms 47

49 Binary search: specification and invariant Algorithm s) BinarySearch(A, Input : A sorted Constants: A, s Output :» (0» jaj) ^ (A[0..r] < s) ^ r» A[r..jAj]) (s A low high low ψ 0; high ψ jaj; fig do low 6= high do narrow the gap fl; fi ψ r low Algorithms and Data Structures Chapter 3: Fundamental algorithms 48

50 Binary search: the algorithm Algorithm s) BinarySearch(A, Input : A sorted Constants: A, s Output :» (0» jaj) ^ (A[0..r] < s) ^ r» A[r..jAj]) (s Method : ψ low 0; ψ high jaj; low 6= high do ψ m + (low high)=2; fig if A[m] < s then do then low ψ m + 1 else r ψ low high ψ m; I : (0» low» high» jaj) ^ (A[0..low] < s) ^ (s» A[high..jAj]) µ(a, s, low, high, m, r) = high low Algorithms and Data Structures Chapter 3: Fundamental algorithms 49

51 Insertion sort InsertionSort(A) Algorithm Input true : ) ^ sorted) ψ Output : (A perm A 0 (A Method : i 0; i 6= jaj do ψ j i; fig fjg do do ( j 6= 0) ^ (A[ j 1] > A[ j]) do j 1] $ A[ j]; A[ ψ j j 1; ψ i + i 1 I : (A perm A 0 ^ (0» i» jaj) ^ (A[0..i] sorted) ) J : (A perm A 0 ^ (0» j» i < jaj) ^ (A[0..j],A[ j..i + 1] sorted) ) µ I (A, j) = jaj i, i µ J (A, j) = i, j Algorithms and Data Structures Chapter 3: Fundamental algorithms 50

52 Merge sort Algorithm MergeSort(A) Input Output Method : true : (A perm A 0 ) ^ (A sorted) if : then B ψ A[0..jAj=2]; C ψ A[jAj=2..jAj]; MergeSort(B); MergeSort(C); fwgmerge(a, B, C) then if jaj > 1 then Algorithm Merge(A, C) B, Input (jaj = jbj + jcj) ^ : (B, sorted) C Constants: B, C Output : (A BC) ^ perm sorted) (A W : (A = A 0 ) ^ (B perm A 1 ) ^ (C perm A 2 ) ^ (B, C sorted) = jaj µ(a) Algorithms and Data Structures Chapter 3: Fundamental algorithms 51

53 Merge sort: time complexity, assuming n a power of 2 level 0 level 1 level 2 level 3 level 4 Time spent at level i (for 0» i» log n): 2 i n=2 i = n time units. Algorithms and Data Structures Chapter 3: Fundamental algorithms 52

54 Merge: specification Algorithm Merge(A, C) B, Input (jaj = jbj + jcj) ^ : (B, sorted) C Constants: B, C Output : (A BC) ^ perm sorted) (A So, with jaj = 11 and we should obtain B = [1, 1, 4, 7, 8] and C = [1, 2, 2, 3, 4, 7] A = [1, 1, 1, 2, 2, 3, 4, 4, 7, 7, 8]. Algorithms and Data Structures Chapter 3: Fundamental algorithms 53

55 Merge: scanning invariant A k=i+j B i C j I (jaj = jbj + jcj) ^ :» (0» jbj) ^ i» (0» jcj) ^ j + j] (A[0..i B[0..i]C[0..j]) ^ perm + j] sorted) (A[0..i fi init fl; fig do i + j 6= jaj do update fl; fi end fl fi Algorithms and Data Structures Chapter 3: Fundamental algorithms 54

56 Merge: the algorithm Algorithm Merge(A, C) B, Input (jaj = jbj + jcj) ^ : (B, sorted) C Constants: B, C Output : (A BC) ^ perm sorted) (A Method : ψ i 0; ψ j 0; (i < jbj) ^ ( j = jcj _ B[i]» C[ j]) then if + j] ψ A[i B[i]; ψ i + i 1 if then fig do i + j 6= jaj do else A[i + j] ψ C[ j]; j ψ j + 1 I (jaj = jbj + jcj) ^ :» (0» jbj) ^ i» (0» jcj) ^ j + j] (A[0..i B[0..i]C[0..j]) ^ perm + j] sorted) (A[0..i µ(a, B, C, i, j) = jaj (i + j) Algorithms and Data Structures Chapter 3: Fundamental algorithms 55

57 Dutch flag: specification A w b Algorithm s) DutchFlag(A, Input true : Constants: s Output : (A perm A 0 ^ (0» w» b» jaj) ^ ) (A[0..w] < s) ^ (A[w..b] = s) ^ (A[b..jAj] > s) Algorithms and Data Structures Chapter 3: Fundamental algorithms 56

58 I : (A perm A 0 ^ (0» w» b» i» jaj) ^ ) < s) ^ (A[w..b] = s) ^ (A[b..i] > s) (A[0..w] Dutch flag: scanning invariant A w b i fi init fl; i ψ 0; fig do i 6= jaj do update fl; i ψ i + 1; fi end fl fi Algorithms and Data Structures Chapter 3: Fundamental algorithms 57

59 Dutch flag: basis ftruegfi init fl; i ψ 0fIg A w=b=i fi init fl = w ψ 0; b ψ 0 Algorithms and Data Structures Chapter 3: Fundamental algorithms 58

60 Dutch flag: invariance fi ^ i 6= jajgfi update fl; i ψ i + 1fIg fi update fl = if A[i] < s then if fi red fl else if A[i] = s then fi white fl else fi blue fl 1: ^ fi 6= jaj) ^ (A[i] < s)gfi red (i fl; ψ i + 1fIg i 2: ^ fi 6= jaj) ^ (A[i] = s)gfi white (i fl; ψ i + 1fIg i 3: ^ fi 6= jaj) ^ (A[i] > s)gfi blue (i fl; ψ i + 1fIg i Algorithms and Data Structures Chapter 3: Fundamental algorithms 59

61

62 Dutch flag: invariance, white fi ^ (i 6= jaj) ^ (A[i] = s)gfi white fl; i ψ i + 1fIg A w b i fi white fl = A[i] $ A[b]; b ψ b + 1 Algorithms and Data Structures Chapter 3: Fundamental algorithms 61

63 Dutch flag: invariance, blue fi ^ (i 6= jaj) ^ (A[i] > s)gfi blue fl; i ψ i + 1fIg A w b i fi blue fl = λ Algorithms and Data Structures Chapter 3: Fundamental algorithms 62

64 Dutch flag: conclusion fi ^ (i = jaj)gfi end flfoutg A w b i fi end fl = λ Algorithms and Data Structures Chapter 3: Fundamental algorithms 63

65

66 Dutch flag: from low to high A low w b high Algorithm DutchFlag(A, low, s) high, Input :» 0» low» jaj high Constants: low, high, A[0..low], A[high..jAj], s Output (A[low..high] : perm A 0 ^ [low..high])» (low» w» high) ^ b (A[low..w] < s) ^ (A[w..b] = s) ^ (A[b..high] > s) Algorithms and Data Structures Chapter 3: Fundamental algorithms 65

67 Quick sort Algorithm QuickSort(A, high) low, Input :» 0» low» jaj high Constants: low, high, A[high..jAj] A[0..low], Output (A[low..high] : perm A 0 ^ (A[low..high] sorted) [low..high]) Method if : > then 1 ψ r random(low, high); b) ψ (w, DutchFlag(A, low, high, A[r]); QuickSort(A, low, w); QuickSort(A, high) b, µ(a, low, high) = high low Algorithms and Data Structures Chapter 3: Fundamental algorithms 66

68 Quick select Algorithm QuickSelect(A, low, k) high, Input :» (0» low < k» jaj) high Constants: low, high, A[0..low], A[high..jAj], k Output (A[low..high] : perm A 0 ^ [low..high]) Method» A[k]» A[k..high]) (A[low..k] : ψ r random(low, high); b) ψ (w, DutchFlag(A, low, high, A[r]); then QuickSelect(A, low, w, k) if then if k < w then then QuickSelect(A, b, high, k) else if then if k b then µ(a, low, high) = high low Algorithms and Data Structures Chapter 3: Fundamental algorithms 67

69 Maximal subsum MaxSubsum(A) Algorithm Input true : = ms(a) fi init ψ Constants: A Output : r Method : fl; i 0; fig do i 6= jaj do update fl; i ψ i + 1; fi end fl fi I : (0» i» jaj) ^ (r = ms(a[0..i])) ^ Algorithms and Data Structures Chapter 3: Fundamental algorithms 68

70 Maximal subsum: maintaining the invariant ms(a[0..i + 1]) is the maximum of the maximal subsum obtained by not using A[i], and the maximal subsum obtained by using A[i] So, we remember also the maximal right subsum mrs(a[0..i]) I : (0» i» jaj) ^ (r = ms(a[0..i])) ^ (h = mrs(a[0..i])) Algorithms and Data Structures Chapter 3: Fundamental algorithms 69

71 Maximal subsum: the algorithm MaxSubsum(A) Algorithm Input true : = ms(a) ψ ψ ψ Constants: A Output : m Method : r 0; h 0; i 0; i 6= jaj do ψ h + maxfh A[i],0g; ψ r maxfr, hg; ψ i + i 1 fig do µ(a, r, h, i) = jaj i Algorithms and Data Structures Chapter 3: Fundamental algorithms 70

72 Longest monotone sequence LLMS(A) Algorithm Input true : Constants: A Output : = llms(a) r Method fi init : fl; ψ i 0; fig do i 6= jaj do update fl; i ψ i + 1; fi end fl fi I : (0» i» jaj) ^ (r = llms(a[0..i])) ^ Algorithms and Data Structures Chapter 3: Fundamental algorithms 71

73 I :» (0» jaj) ^ i = llms(a[0..i])) ^ (r = ms (B[l] l for 0» l» jaj) (A[0..i]) Longest monotone sequence: maintaining the invariant In the i th iteration of the loop, B[l] for 1» l» jaj must contain the necessary information about a monotone sequence from A[0..i] of length l whose last element is minimal (if there is no sequence of length l,wejust record that). ms l (A[0..i]) = 8 >< >: if = 1 l 0 minimal last element in a monotone sequence of if such exists A[0..i] length l from 1 otherwise Algorithms and Data Structures Chapter 3: Fundamental algorithms 72

74 Longest monotone sequence: the algorithm LLMS(A) Algorithm Input true : Constants: A Output : = llms(a) r Method : ψ [jaj + B B[0] ψ 1:1]; 1; ψ r 0; ψ i 0; i 6= jaj do ψ BinarySearch l (B, A[i]); 0 fig do B[l] ψ A[i]; r ψ maxfr, lg; i ψ i + 1 µ(a, B, r, i) = jaj i BinarySearch Algorithm (B, 0 s) Input : B sorted Constants: B, s Output :» (0» jbj) ^ (B[0..l]» s) ^ l < B[l..jBj]) (s Algorithms and Data Structures Chapter 3: Fundamental algorithms 73

Definition A transition system S is a pair of the form. where C is the set of configurations and T C C is a relation, the

Definition A transition system S is a pair of the form. where C is the set of configurations and T C C is a relation, the Transition system Definition 1.3.1 transition system S is a pair of the form = S T) (, where is the set of configurations and T is a relation, the transition relation. lgorithms and Data Structures hapter

More information

Department of Computer Science Aarhus University. Algorithms and Data Structures. Transition systems

Department of Computer Science Aarhus University. Algorithms and Data Structures. Transition systems Department of Computer Science Aarhus University Algorithms and Data Structures Transition systems Mikkel Nygaard Erik Meineche Schmidt February 2014 Contents 1 Transition systems 1 1.1 Introduction............................

More information

Week 5: Quicksort, Lower bound, Greedy

Week 5: Quicksort, Lower bound, Greedy Week 5: Quicksort, Lower bound, Greedy Agenda: Quicksort: Average case Lower bound for sorting Greedy method 1 Week 5: Quicksort Recall Quicksort: The ideas: Pick one key Compare to others: partition into

More information

CS156: The Calculus of Computation

CS156: The Calculus of Computation Page 1 of 61 CS156: The Calculus of Computation Zohar Manna Winter 2010 Chapter 5: Program Correctness: Mechanics Page 2 of 61 Program A: LinearSearch with function specification @pre 0 l u < a @post rv

More information

Problem. Problem Given a dictionary and a word. Which page (if any) contains the given word? 3 / 26

Problem. Problem Given a dictionary and a word. Which page (if any) contains the given word? 3 / 26 Binary Search Introduction Problem Problem Given a dictionary and a word. Which page (if any) contains the given word? 3 / 26 Strategy 1: Random Search Randomly select a page until the page containing

More information

Advanced Analysis of Algorithms - Midterm (Solutions)

Advanced Analysis of Algorithms - Midterm (Solutions) Advanced Analysis of Algorithms - Midterm (Solutions) K. Subramani LCSEE, West Virginia University, Morgantown, WV {ksmani@csee.wvu.edu} 1 Problems 1. Solve the following recurrence using substitution:

More information

Lecture 4. Quicksort

Lecture 4. Quicksort Lecture 4. Quicksort 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 2000-2018 Networking

More information

Divide and conquer. Philip II of Macedon

Divide and conquer. Philip II of Macedon Divide and conquer Philip II of Macedon Divide and conquer 1) Divide your problem into subproblems 2) Solve the subproblems recursively, that is, run the same algorithm on the subproblems (when the subproblems

More information

Sorting Algorithms. We have already seen: Selection-sort Insertion-sort Heap-sort. We will see: Bubble-sort Merge-sort Quick-sort

Sorting Algorithms. We have already seen: Selection-sort Insertion-sort Heap-sort. We will see: Bubble-sort Merge-sort Quick-sort Sorting Algorithms We have already seen: Selection-sort Insertion-sort Heap-sort We will see: Bubble-sort Merge-sort Quick-sort We will show that: O(n log n) is optimal for comparison based sorting. Bubble-Sort

More information

Solutions. Problem 1: Suppose a polynomial in n of degree d has the form

Solutions. Problem 1: Suppose a polynomial in n of degree d has the form Assignment 1 1. Problem 3-1 on p. 57 2. Problem 3-2 on p. 58 3. Problem 4-5 on p. 86 4. Problem 6-1 on p. 142 5. Problem 7-4 on p. 162 6. Prove correctness (including halting) of SelectionSort (use loop

More information

Lecture 5: Loop Invariants and Insertion-sort

Lecture 5: Loop Invariants and Insertion-sort Lecture 5: Loop Invariants and Insertion-sort COMS10007 - Algorithms Dr. Christian Konrad 11.01.2019 Dr. Christian Konrad Lecture 5: Loop Invariants and Insertion-sort 1 / 12 Proofs by Induction Structure

More information

5. Program Correctness: Mechanics

5. Program Correctness: Mechanics 5. Program Correctness: Mechanics Program A: LinearSearch with function specification @pre 0 l u < a @post rv i. l i u a[i] = e bool LinearSearch(int[] a, int l, int u, int e) { for @ (int i := l; i u;

More information

CSCE 222 Discrete Structures for Computing

CSCE 222 Discrete Structures for Computing CSCE 222 Discrete Structures for Computing Algorithms Dr. Philip C. Ritchey Introduction An algorithm is a finite sequence of precise instructions for performing a computation or for solving a problem.

More information

Data selection. Lower complexity bound for sorting

Data selection. Lower complexity bound for sorting Data selection. Lower complexity bound for sorting Lecturer: Georgy Gimel farb COMPSCI 220 Algorithms and Data Structures 1 / 12 1 Data selection: Quickselect 2 Lower complexity bound for sorting 3 The

More information

Administrivia. COMP9020 Lecture 7 Session 2, 2017 Induction and Recursion. Lecture 6 recap. Lecture 6 recap

Administrivia. COMP9020 Lecture 7 Session 2, 2017 Induction and Recursion. Lecture 6 recap. Lecture 6 recap Administrivia COMP9020 Lecture 7 Session 2, 2017 Induction and Recursion Guidelines for good mathematical writing Assignment 1 Solutions now available; marks available soon Assignment 2 available on Saturday,

More information

Input Decidable Language -- Program Halts on all Input Encoding of Input -- Natural Numbers Encoded in Binary or Decimal, Not Unary

Input Decidable Language -- Program Halts on all Input Encoding of Input -- Natural Numbers Encoded in Binary or Decimal, Not Unary Complexity Analysis Complexity Theory Input Decidable Language -- Program Halts on all Input Encoding of Input -- Natural Numbers Encoded in Binary or Decimal, Not Unary Output TRUE or FALSE Time and Space

More information

Algorithms And Programming I. Lecture 5 Quicksort

Algorithms And Programming I. Lecture 5 Quicksort Algorithms And Programming I Lecture 5 Quicksort Quick Sort Partition set into two using randomly chosen pivot 88 31 25 52 14 98 62 30 23 79 14 31 2530 23 52 88 62 98 79 Quick Sort 14 31 2530 23 52 88

More information

Quicksort (CLRS 7) We previously saw how the divide-and-conquer technique can be used to design sorting algorithm Merge-sort

Quicksort (CLRS 7) We previously saw how the divide-and-conquer technique can be used to design sorting algorithm Merge-sort Quicksort (CLRS 7) We previously saw how the divide-and-conquer technique can be used to design sorting algorithm Merge-sort Partition n elements array A into two subarrays of n/2 elements each Sort the

More information

CS483 Design and Analysis of Algorithms

CS483 Design and Analysis of Algorithms CS483 Design and Analysis of Algorithms Chapter 2 Divide and Conquer Algorithms Instructor: Fei Li lifei@cs.gmu.edu with subject: CS483 Office hours: Room 5326, Engineering Building, Thursday 4:30pm -

More information

Module 1: Analyzing the Efficiency of Algorithms

Module 1: Analyzing the Efficiency of Algorithms Module 1: Analyzing the Efficiency of Algorithms Dr. Natarajan Meghanathan Professor of Computer Science Jackson State University Jackson, MS 39217 E-mail: natarajan.meghanathan@jsums.edu What is an Algorithm?

More information

COMP Analysis of Algorithms & Data Structures

COMP Analysis of Algorithms & Data Structures COMP 3170 - Analysis of Algorithms & Data Structures Shahin Kamali Lecture 4 - Jan. 10, 2018 CLRS 1.1, 1.2, 2.2, 3.1, 4.3, 4.5 University of Manitoba Picture is from the cover of the textbook CLRS. 1 /

More information

Discrete Math in Computer Science Solutions to Practice Problems for Midterm 2

Discrete Math in Computer Science Solutions to Practice Problems for Midterm 2 Discrete Math in Computer Science Solutions to Practice Problems for Midterm 2 CS 30, Fall 2018 by Professor Prasad Jayanti Problems 1. Let g(0) = 2, g(1) = 1, and g(n) = 2g(n 1) + g(n 2) whenever n 2.

More information

COMP Analysis of Algorithms & Data Structures

COMP Analysis of Algorithms & Data Structures COMP 3170 - Analysis of Algorithms & Data Structures Shahin Kamali Lecture 4 - Jan. 14, 2019 CLRS 1.1, 1.2, 2.2, 3.1, 4.3, 4.5 University of Manitoba Picture is from the cover of the textbook CLRS. COMP

More information

NOTE: You have 2 hours, please plan your time. Problems are not ordered by difficulty.

NOTE: You have 2 hours, please plan your time. Problems are not ordered by difficulty. EXAM 2 solutions (COT3100, Sitharam, Spring 2017) NAME:last first: UF-ID Section NOTE: You have 2 hours, please plan your time. Problems are not ordered by difficulty. (1) Are the following functions one-to-one

More information

Lecture 2. Fundamentals of the Analysis of Algorithm Efficiency

Lecture 2. Fundamentals of the Analysis of Algorithm Efficiency Lecture 2 Fundamentals of the Analysis of Algorithm Efficiency 1 Lecture Contents 1. Analysis Framework 2. Asymptotic Notations and Basic Efficiency Classes 3. Mathematical Analysis of Nonrecursive Algorithms

More information

Randomized Sorting Algorithms Quick sort can be converted to a randomized algorithm by picking the pivot element randomly. In this case we can show th

Randomized Sorting Algorithms Quick sort can be converted to a randomized algorithm by picking the pivot element randomly. In this case we can show th CSE 3500 Algorithms and Complexity Fall 2016 Lecture 10: September 29, 2016 Quick sort: Average Run Time In the last lecture we started analyzing the expected run time of quick sort. Let X = k 1, k 2,...,

More information

CS Data Structures and Algorithm Analysis

CS Data Structures and Algorithm Analysis CS 483 - Data Structures and Algorithm Analysis Lecture VII: Chapter 6, part 2 R. Paul Wiegand George Mason University, Department of Computer Science March 22, 2006 Outline 1 Balanced Trees 2 Heaps &

More information

Maximum flow problem (part I)

Maximum flow problem (part I) Maximum flow problem (part I) Combinatorial Optimization Giovanni Righini Università degli Studi di Milano Definitions A flow network is a digraph D = (N,A) with two particular nodes s and t acting as

More information

Divide and Conquer Algorithms. CSE 101: Design and Analysis of Algorithms Lecture 14

Divide and Conquer Algorithms. CSE 101: Design and Analysis of Algorithms Lecture 14 Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 14 CSE 101: Design and analysis of algorithms Divide and conquer algorithms Reading: Sections 2.3 and 2.4 Homework 6 will

More information

Algorithms, CSE, OSU Quicksort. Instructor: Anastasios Sidiropoulos

Algorithms, CSE, OSU Quicksort. Instructor: Anastasios Sidiropoulos 6331 - Algorithms, CSE, OSU Quicksort Instructor: Anastasios Sidiropoulos Sorting Given an array of integers A[1... n], rearrange its elements so that A[1] A[2]... A[n]. Quicksort Quicksort(A, p, r) if

More information

Quicksort. Where Average and Worst Case Differ. S.V. N. (vishy) Vishwanathan. University of California, Santa Cruz

Quicksort. Where Average and Worst Case Differ. S.V. N. (vishy) Vishwanathan. University of California, Santa Cruz Quicksort Where Average and Worst Case Differ S.V. N. (vishy) Vishwanathan University of California, Santa Cruz vishy@ucsc.edu February 1, 2016 S.V. N. Vishwanathan (UCSC) CMPS101 1 / 28 Basic Idea Outline

More information

Review Of Topics. Review: Induction

Review Of Topics. Review: Induction Review Of Topics Asymptotic notation Solving recurrences Sorting algorithms Insertion sort Merge sort Heap sort Quick sort Counting sort Radix sort Medians/order statistics Randomized algorithm Worst-case

More information

Analysis of Algorithms CMPSC 565

Analysis of Algorithms CMPSC 565 Analysis of Algorithms CMPSC 565 LECTURES 38-39 Randomized Algorithms II Quickselect Quicksort Running time Adam Smith L1.1 Types of randomized analysis Average-case analysis : Assume data is distributed

More information

Outline. 1 Introduction. 3 Quicksort. 4 Analysis. 5 References. Idea. 1 Choose an element x and reorder the array as follows:

Outline. 1 Introduction. 3 Quicksort. 4 Analysis. 5 References. Idea. 1 Choose an element x and reorder the array as follows: Outline Computer Science 331 Quicksort Mike Jacobson Department of Computer Science University of Calgary Lecture #28 1 Introduction 2 Randomized 3 Quicksort Deterministic Quicksort Randomized Quicksort

More information

Analysis of Algorithms

Analysis of Algorithms September 29, 2017 Analysis of Algorithms CS 141, Fall 2017 1 Analysis of Algorithms: Issues Correctness/Optimality Running time ( time complexity ) Memory requirements ( space complexity ) Power I/O utilization

More information

data structures and algorithms lecture 2

data structures and algorithms lecture 2 data structures and algorithms 2018 09 06 lecture 2 recall: insertion sort Algorithm insertionsort(a, n): for j := 2 to n do key := A[j] i := j 1 while i 1 and A[i] > key do A[i + 1] := A[i] i := i 1 A[i

More information

Complexity Theory VU , SS The Polynomial Hierarchy. Reinhard Pichler

Complexity Theory VU , SS The Polynomial Hierarchy. Reinhard Pichler Complexity Theory Complexity Theory VU 181.142, SS 2018 6. The Polynomial Hierarchy Reinhard Pichler Institut für Informationssysteme Arbeitsbereich DBAI Technische Universität Wien 15 May, 2018 Reinhard

More information

Outline. Complexity Theory EXACT TSP. The Class DP. Definition. Problem EXACT TSP. Complexity of EXACT TSP. Proposition VU 181.

Outline. Complexity Theory EXACT TSP. The Class DP. Definition. Problem EXACT TSP. Complexity of EXACT TSP. Proposition VU 181. Complexity Theory Complexity Theory Outline Complexity Theory VU 181.142, SS 2018 6. The Polynomial Hierarchy Reinhard Pichler Institut für Informationssysteme Arbeitsbereich DBAI Technische Universität

More information

Problem One: Order Relations i. What three properties does a binary relation have to have to be a partial order?

Problem One: Order Relations i. What three properties does a binary relation have to have to be a partial order? CS103 Handout 16 Fall 2011 November 4, 2011 Extra Practice Problems Many of you have expressed interest in additional practice problems to review the material from the first four weeks of CS103. This handout

More information

MIT Loop Optimizations. Martin Rinard

MIT Loop Optimizations. Martin Rinard MIT 6.035 Loop Optimizations Martin Rinard Loop Optimizations Important because lots of computation occurs in loops We will study two optimizations Loop-invariant code motion Induction variable elimination

More information

CS 4407 Algorithms Lecture 2: Iterative and Divide and Conquer Algorithms

CS 4407 Algorithms Lecture 2: Iterative and Divide and Conquer Algorithms CS 4407 Algorithms Lecture 2: Iterative and Divide and Conquer Algorithms Prof. Gregory Provan Department of Computer Science University College Cork 1 Lecture Outline CS 4407, Algorithms Growth Functions

More information

Algorithm Analysis Recurrence Relation. Chung-Ang University, Jaesung Lee

Algorithm Analysis Recurrence Relation. Chung-Ang University, Jaesung Lee Algorithm Analysis Recurrence Relation Chung-Ang University, Jaesung Lee Recursion 2 Recursion 3 Recursion in Real-world Fibonacci sequence = + Initial conditions: = 0 and = 1. = + = + = + 0, 1, 1, 2,

More information

Fundamental Algorithms

Fundamental Algorithms Chapter 2: Sorting, Winter 2018/19 1 Fundamental Algorithms Chapter 2: Sorting Jan Křetínský Winter 2018/19 Chapter 2: Sorting, Winter 2018/19 2 Part I Simple Sorts Chapter 2: Sorting, Winter 2018/19 3

More information

Fundamental Algorithms

Fundamental Algorithms Fundamental Algorithms Chapter 2: Sorting Harald Räcke Winter 2015/16 Chapter 2: Sorting, Winter 2015/16 1 Part I Simple Sorts Chapter 2: Sorting, Winter 2015/16 2 The Sorting Problem Definition Sorting

More information

Introduction to Divide and Conquer

Introduction to Divide and Conquer Introduction to Divide and Conquer Sorting with O(n log n) comparisons and integer multiplication faster than O(n 2 ) Periklis A. Papakonstantinou York University Consider a problem that admits a straightforward

More information

Analysis of Algorithm Efficiency. Dr. Yingwu Zhu

Analysis of Algorithm Efficiency. Dr. Yingwu Zhu Analysis of Algorithm Efficiency Dr. Yingwu Zhu Measure Algorithm Efficiency Time efficiency How fast the algorithm runs; amount of time required to accomplish the task Our focus! Space efficiency Amount

More information

CS 350 Algorithms and Complexity

CS 350 Algorithms and Complexity CS 350 Algorithms and Complexity Winter 2019 Lecture 15: Limitations of Algorithmic Power Introduction to complexity theory Andrew P. Black Department of Computer Science Portland State University Lower

More information

CS 350 Algorithms and Complexity

CS 350 Algorithms and Complexity 1 CS 350 Algorithms and Complexity Fall 2015 Lecture 15: Limitations of Algorithmic Power Introduction to complexity theory Andrew P. Black Department of Computer Science Portland State University Lower

More information

Quick Tour of Linear Algebra and Graph Theory

Quick Tour of Linear Algebra and Graph Theory Quick Tour of Linear Algebra and Graph Theory CS224w: Social and Information Network Analysis Fall 2012 Yu Wayne Wu Based on Borja Pelato s version in Fall 2011 Matrices and Vectors Matrix: A rectangular

More information

Analysis of Algorithms I: Asymptotic Notation, Induction, and MergeSort

Analysis of Algorithms I: Asymptotic Notation, Induction, and MergeSort Analysis of Algorithms I: Asymptotic Notation, Induction, and MergeSort Xi Chen Columbia University We continue with two more asymptotic notation: o( ) and ω( ). Let f (n) and g(n) are functions that map

More information

1 Quick Sort LECTURE 7. OHSU/OGI (Winter 2009) ANALYSIS AND DESIGN OF ALGORITHMS

1 Quick Sort LECTURE 7. OHSU/OGI (Winter 2009) ANALYSIS AND DESIGN OF ALGORITHMS OHSU/OGI (Winter 2009) CS532 ANALYSIS AND DESIGN OF ALGORITHMS LECTURE 7 1 Quick Sort QuickSort 1 is a classic example of divide and conquer. The hard work is to rearrange the elements of the array A[1..n]

More information

Algorithms. Quicksort. Slide credit: David Luebke (Virginia)

Algorithms. Quicksort. Slide credit: David Luebke (Virginia) 1 Algorithms Quicksort Slide credit: David Luebke (Virginia) Sorting revisited We have seen algorithms for sorting: INSERTION-SORT, MERGESORT More generally: given a sequence of items Each item has a characteristic

More information

CS 470/570 Divide-and-Conquer. Format of Divide-and-Conquer algorithms: Master Recurrence Theorem (simpler version)

CS 470/570 Divide-and-Conquer. Format of Divide-and-Conquer algorithms: Master Recurrence Theorem (simpler version) CS 470/570 Divide-and-Conquer Format of Divide-and-Conquer algorithms: Divide: Split the array or list into smaller pieces Conquer: Solve the same problem recursively on smaller pieces Combine: Build the

More information

Outline. 1 Merging. 2 Merge Sort. 3 Complexity of Sorting. 4 Merge Sort and Other Sorts 2 / 10

Outline. 1 Merging. 2 Merge Sort. 3 Complexity of Sorting. 4 Merge Sort and Other Sorts 2 / 10 Merge Sort 1 / 10 Outline 1 Merging 2 Merge Sort 3 Complexity of Sorting 4 Merge Sort and Other Sorts 2 / 10 Merging Merge sort is based on a simple operation known as merging: combining two ordered arrays

More information

Reductions, Recursion and Divide and Conquer

Reductions, Recursion and Divide and Conquer CS/ECE 374: Algorithms & Models of Computation, Fall 2018 Reductions, Recursion and Divide and Conquer Lecture 10 October 2, 2018 Chandra Chekuri (UIUC) CS/ECE 374 1 Fall 2018 1 / 56 Part I Brief Intro

More information

Definition: A binary relation R from a set A to a set B is a subset R A B. Example:

Definition: A binary relation R from a set A to a set B is a subset R A B. Example: Chapter 9 1 Binary Relations Definition: A binary relation R from a set A to a set B is a subset R A B. Example: Let A = {0,1,2} and B = {a,b} {(0, a), (0, b), (1,a), (2, b)} is a relation from A to B.

More information

7.11 A proof involving composition Variation in terminology... 88

7.11 A proof involving composition Variation in terminology... 88 Contents Preface xi 1 Math review 1 1.1 Some sets............................. 1 1.2 Pairs of reals........................... 3 1.3 Exponentials and logs...................... 4 1.4 Some handy functions......................

More information

CSC 8301 Design & Analysis of Algorithms: Lower Bounds

CSC 8301 Design & Analysis of Algorithms: Lower Bounds CSC 8301 Design & Analysis of Algorithms: Lower Bounds Professor Henry Carter Fall 2016 Recap Iterative improvement algorithms take a feasible solution and iteratively improve it until optimized Simplex

More information

Mergesort and Recurrences (CLRS 2.3, 4.4)

Mergesort and Recurrences (CLRS 2.3, 4.4) Mergesort and Recurrences (CLRS 2.3, 4.4) We saw a couple of O(n 2 ) algorithms for sorting. Today we ll see a different approach that runs in O(n lg n) and uses one of the most powerful techniques for

More information

Lecture 9. Greedy Algorithm

Lecture 9. Greedy Algorithm Lecture 9. Greedy Algorithm 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 2000-2018

More information

Search Trees. Chapter 10. CSE 2011 Prof. J. Elder Last Updated: :52 AM

Search Trees. Chapter 10. CSE 2011 Prof. J. Elder Last Updated: :52 AM Search Trees Chapter 1 < 6 2 > 1 4 = 8 9-1 - Outline Ø Binary Search Trees Ø AVL Trees Ø Splay Trees - 2 - Outline Ø Binary Search Trees Ø AVL Trees Ø Splay Trees - 3 - Binary Search Trees Ø A binary search

More information

Lecture 4 October 18th

Lecture 4 October 18th Directed and undirected graphical models Fall 2017 Lecture 4 October 18th Lecturer: Guillaume Obozinski Scribe: In this lecture, we will assume that all random variables are discrete, to keep notations

More information

Analysis of Algorithms. Randomizing Quicksort

Analysis of Algorithms. Randomizing Quicksort Analysis of Algorithms Randomizing Quicksort Randomizing Quicksort Randomly permute the elements of the input array before sorting OR... modify the PARTITION procedure At each step of the algorithm we

More information

Average Case Analysis. October 11, 2011

Average Case Analysis. October 11, 2011 Average Case Analysis October 11, 2011 Worst-case analysis Worst-case analysis gives an upper bound for the running time of a single execution of an algorithm with a worst-case input and worst-case random

More information

CSE 431/531: Analysis of Algorithms. Dynamic Programming. Lecturer: Shi Li. Department of Computer Science and Engineering University at Buffalo

CSE 431/531: Analysis of Algorithms. Dynamic Programming. Lecturer: Shi Li. Department of Computer Science and Engineering University at Buffalo CSE 431/531: Analysis of Algorithms Dynamic Programming Lecturer: Shi Li Department of Computer Science and Engineering University at Buffalo Paradigms for Designing Algorithms Greedy algorithm Make a

More information

Computer Science 385 Analysis of Algorithms Siena College Spring Topic Notes: Limitations of Algorithms

Computer Science 385 Analysis of Algorithms Siena College Spring Topic Notes: Limitations of Algorithms Computer Science 385 Analysis of Algorithms Siena College Spring 2011 Topic Notes: Limitations of Algorithms We conclude with a discussion of the limitations of the power of algorithms. That is, what kinds

More information

Limitations of Algorithm Power

Limitations of Algorithm Power Limitations of Algorithm Power Objectives We now move into the third and final major theme for this course. 1. Tools for analyzing algorithms. 2. Design strategies for designing algorithms. 3. Identifying

More information

Heaps Induction. Heaps. Heaps. Tirgul 6

Heaps Induction. Heaps. Heaps. Tirgul 6 Tirgul 6 Induction A binary heap is a nearly complete binary tree stored in an array object In a max heap, the value of each node that of its children (In a min heap, the value of each node that of its

More information

Lecture 6 September 21, 2016

Lecture 6 September 21, 2016 ICS 643: Advanced Parallel Algorithms Fall 2016 Lecture 6 September 21, 2016 Prof. Nodari Sitchinava Scribe: Tiffany Eulalio 1 Overview In the last lecture, we wrote a non-recursive summation program and

More information

INF2220: algorithms and data structures Series 1

INF2220: algorithms and data structures Series 1 Universitetet i Oslo Institutt for Informatikk I. Yu, D. Karabeg INF2220: algorithms and data structures Series 1 Topic Function growth & estimation of running time, trees (Exercises with hints for solution)

More information

Divide-and-conquer: Order Statistics. Curs: Fall 2017

Divide-and-conquer: Order Statistics. Curs: Fall 2017 Divide-and-conquer: Order Statistics Curs: Fall 2017 The divide-and-conquer strategy. 1. Break the problem into smaller subproblems, 2. recursively solve each problem, 3. appropriately combine their answers.

More information

Problem 5. Use mathematical induction to show that when n is an exact power of two, the solution of the recurrence

Problem 5. Use mathematical induction to show that when n is an exact power of two, the solution of the recurrence A. V. Gerbessiotis CS 610-102 Spring 2014 PS 1 Jan 27, 2014 No points For the remainder of the course Give an algorithm means: describe an algorithm, show that it works as claimed, analyze its worst-case

More information

Fundamental Algorithms

Fundamental Algorithms Fundamental Algorithms Chapter 5: Searching Michael Bader Winter 2014/15 Chapter 5: Searching, Winter 2014/15 1 Searching Definition (Search Problem) Input: a sequence or set A of n elements (objects)

More information

Computing Connected Components Given a graph G = (V; E) compute the connected components of G. Connected-Components(G) 1 for each vertex v 2 V [G] 2 d

Computing Connected Components Given a graph G = (V; E) compute the connected components of G. Connected-Components(G) 1 for each vertex v 2 V [G] 2 d Data Structures for Disjoint Sets Maintain a Dynamic collection of disjoint sets. Each set has a unique representative (an arbitrary member of the set). x. Make-Set(x) - Create a new set with one member

More information

Divide and Conquer Algorithms

Divide and Conquer Algorithms Divide and Conquer Algorithms Antonio Carzaniga Faculty of Informatics University of Lugano October 3, 2007 c 2005, 2006 Antonio Carzaniga 1 Merging Searching Sorting Multiplying Computing the median Outline

More information

CSE101: Design and Analysis of Algorithms. Ragesh Jaiswal, CSE, UCSD

CSE101: Design and Analysis of Algorithms. Ragesh Jaiswal, CSE, UCSD Greedy s Greedy s Shortest path Claim 2: Let S be a subset of vertices containing s such that we know the shortest path length l(s, u) from s to any vertex in u S. Let e = (u, v) be an edge such that 1

More information

CS1802 Week 11: Algorithms, Sums, Series, Induction

CS1802 Week 11: Algorithms, Sums, Series, Induction CS180 Discrete Structures Recitation Fall 017 Nov 11 - November 17, 017 CS180 Week 11: Algorithms, Sums, Series, Induction 1 Markov chain i. Boston has days which are either sunny or rainy and can be modeled

More information

Chapter 5 Data Structures Algorithm Theory WS 2017/18 Fabian Kuhn

Chapter 5 Data Structures Algorithm Theory WS 2017/18 Fabian Kuhn Chapter 5 Data Structures Algorithm Theory WS 2017/18 Fabian Kuhn Priority Queue / Heap Stores (key,data) pairs (like dictionary) But, different set of operations: Initialize-Heap: creates new empty heap

More information

CSE548, AMS542: Analysis of Algorithms, Fall 2017 Date: Oct 26. Homework #2. ( Due: Nov 8 )

CSE548, AMS542: Analysis of Algorithms, Fall 2017 Date: Oct 26. Homework #2. ( Due: Nov 8 ) CSE548, AMS542: Analysis of Algorithms, Fall 2017 Date: Oct 26 Homework #2 ( Due: Nov 8 ) Task 1. [ 80 Points ] Average Case Analysis of Median-of-3 Quicksort Consider the median-of-3 quicksort algorithm

More information

Induction and Recursion

Induction and Recursion . All rights reserved. Authorized only for instructor use in the classroom. No reproduction or further distribution permitted without the prior written consent of McGraw-Hill Education. Induction and Recursion

More information

Dictionary: an abstract data type

Dictionary: an abstract data type 2-3 Trees 1 Dictionary: an abstract data type A container that maps keys to values Dictionary operations Insert Search Delete Several possible implementations Balanced search trees Hash tables 2 2-3 trees

More information

On shredders and vertex connectivity augmentation

On shredders and vertex connectivity augmentation On shredders and vertex connectivity augmentation Gilad Liberman The Open University of Israel giladliberman@gmail.com Zeev Nutov The Open University of Israel nutov@openu.ac.il Abstract We consider the

More information

25. Minimum Spanning Trees

25. Minimum Spanning Trees 695 25. Minimum Spanning Trees Motivation, Greedy, Algorithm Kruskal, General Rules, ADT Union-Find, Algorithm Jarnik, Prim, Dijkstra, Fibonacci Heaps [Ottman/Widmayer, Kap. 9.6, 6.2, 6.1, Cormen et al,

More information

Bin Sort. Sorting integers in Range [1,...,n] Add all elements to table and then

Bin Sort. Sorting integers in Range [1,...,n] Add all elements to table and then Sorting1 Bin Sort Sorting integers in Range [1,...,n] Add all elements to table and then Retrieve in order 1,2,3,...,n Stable Sorting Method (repeated elements will end up in their original order) Numbers

More information

25. Minimum Spanning Trees

25. Minimum Spanning Trees Problem Given: Undirected, weighted, connected graph G = (V, E, c). 5. Minimum Spanning Trees Motivation, Greedy, Algorithm Kruskal, General Rules, ADT Union-Find, Algorithm Jarnik, Prim, Dijkstra, Fibonacci

More information

Disjoint Sets : ADT. Disjoint-Set : Find-Set

Disjoint Sets : ADT. Disjoint-Set : Find-Set Disjoint Sets : ADT Disjoint-Set Forests Make-Set( x ) Union( s, s ) Find-Set( x ) : π( ) : π( ) : ν( log * n ) amortized cost 6 9 {,, 9,, 6} set # {} set # 7 5 {5, 7, } set # Disjoint-Set : Find-Set Disjoint

More information

Hoare Logic (I): Axiomatic Semantics and Program Correctness

Hoare Logic (I): Axiomatic Semantics and Program Correctness Hoare Logic (I): Axiomatic Semantics and Program Correctness (Based on [Apt and Olderog 1991; Gries 1981; Hoare 1969; Kleymann 1999; Sethi 199]) Yih-Kuen Tsay Dept. of Information Management National Taiwan

More information

Data Structures and Algorithms

Data Structures and Algorithms Data Structures and Algorithms Spring 2017-2018 Outline 1 Sorting Algorithms (contd.) Outline Sorting Algorithms (contd.) 1 Sorting Algorithms (contd.) Analysis of Quicksort Time to sort array of length

More information

i=1 i B[i] B[i] + A[i, j]; c n for j n downto i + 1 do c n i=1 (n i) C[i] C[i] + A[i, j]; c n

i=1 i B[i] B[i] + A[i, j]; c n for j n downto i + 1 do c n i=1 (n i) C[i] C[i] + A[i, j]; c n Fundamental Algorithms Homework #1 Set on June 25, 2009 Due on July 2, 2009 Problem 1. [15 pts] Analyze the worst-case time complexity of the following algorithms,and give tight bounds using the Theta

More information

CS Data Structures and Algorithm Analysis

CS Data Structures and Algorithm Analysis CS 483 - Data Structures and Algorithm Analysis Lecture II: Chapter 2 R. Paul Wiegand George Mason University, Department of Computer Science February 1, 2006 Outline 1 Analysis Framework 2 Asymptotic

More information

Generating p-extremal graphs

Generating p-extremal graphs Generating p-extremal graphs Derrick Stolee Department of Mathematics Department of Computer Science University of Nebraska Lincoln s-dstolee1@math.unl.edu August 2, 2011 Abstract Let f(n, p be the maximum

More information

Dynamic Programming. Reading: CLRS Chapter 15 & Section CSE 2331 Algorithms Steve Lai

Dynamic Programming. Reading: CLRS Chapter 15 & Section CSE 2331 Algorithms Steve Lai Dynamic Programming Reading: CLRS Chapter 5 & Section 25.2 CSE 233 Algorithms Steve Lai Optimization Problems Problems that can be solved by dynamic programming are typically optimization problems. Optimization

More information

Decision Procedures. Jochen Hoenicke. Software Engineering Albert-Ludwigs-University Freiburg. Winter Term 2016/17

Decision Procedures. Jochen Hoenicke. Software Engineering Albert-Ludwigs-University Freiburg. Winter Term 2016/17 Decision Procedures Jochen Hoenicke Software Engineering Albert-Ludwigs-University Freiburg Winter Term 2016/17 Jochen Hoenicke (Software Engineering) Decision Procedures Winter Term 2016/17 1 / 436 Program

More information

Chapter 8 The Disjoint Sets Class

Chapter 8 The Disjoint Sets Class Chapter 8 The Disjoint Sets Class 2 Introduction equivalence problem can be solved fairly simply simple data structure each function requires only a few lines of code two operations: and can be implemented

More information

Analysis of Algorithms. Outline 1 Introduction Basic Definitions Ordered Trees. Fibonacci Heaps. Andres Mendez-Vazquez. October 29, Notes.

Analysis of Algorithms. Outline 1 Introduction Basic Definitions Ordered Trees. Fibonacci Heaps. Andres Mendez-Vazquez. October 29, Notes. Analysis of Algorithms Fibonacci Heaps Andres Mendez-Vazquez October 29, 2015 1 / 119 Outline 1 Introduction Basic Definitions Ordered Trees 2 Binomial Trees Example 3 Fibonacci Heap Operations Fibonacci

More information

Chapter 11. Approximation Algorithms. Slides by Kevin Wayne Pearson-Addison Wesley. All rights reserved.

Chapter 11. Approximation Algorithms. Slides by Kevin Wayne Pearson-Addison Wesley. All rights reserved. Chapter 11 Approximation Algorithms Slides by Kevin Wayne. Copyright @ 2005 Pearson-Addison Wesley. All rights reserved. 1 Approximation Algorithms Q. Suppose I need to solve an NP-hard problem. What should

More information

Divide and Conquer Strategy

Divide and Conquer Strategy Divide and Conquer Strategy Algorithm design is more an art, less so a science. There are a few useful strategies, but no guarantee to succeed. We will discuss: Divide and Conquer, Greedy, Dynamic Programming.

More information

Algorithms and Their Complexity

Algorithms and Their Complexity CSCE 222 Discrete Structures for Computing David Kebo Houngninou Algorithms and Their Complexity Chapter 3 Algorithm An algorithm is a finite sequence of steps that solves a problem. Computational complexity

More information

Physical Mapping. Restriction Mapping. Lecture 12. A physical map of a DNA tells the location of precisely defined sequences along the molecule.

Physical Mapping. Restriction Mapping. Lecture 12. A physical map of a DNA tells the location of precisely defined sequences along the molecule. Computat onal iology Lecture 12 Physical Mapping physical map of a DN tells the location of precisely defined sequences along the molecule. Restriction mapping: mapping of restriction sites of a cutting

More information

Inf 2B: Sorting, MergeSort and Divide-and-Conquer

Inf 2B: Sorting, MergeSort and Divide-and-Conquer Inf 2B: Sorting, MergeSort and Divide-and-Conquer Lecture 7 of ADS thread Kyriakos Kalorkoti School of Informatics University of Edinburgh The Sorting Problem Input: Task: Array A of items with comparable

More information