CS 350 Algorthms and Complexty Wnter 2015 Lecture 8: Decrease & Conquer (contnued) Andrew P. Black Department of Computer Scence Portland State Unversty
Example: DFS traversal of undrected graph a b c d e f g h DFS traversal stack: DFS tree: 2
Decrease by a Constant Factor!bnary search and bsecton method ( 12.4)!exponentaton by squarng!multplcaton à la russe 3
Varable-sze decrease!eucld s algorthm!medan (or percentle) by partton!nm-lke games 4
Fndng the Medan!The Medan of an array of numbers s the mddle number, when sorted.!we can obvously fnd the medan by sortng the array, and then pckng the n k th element 2!How much work s that (n average case)? A. O(n) B. O(n lg n) C. O(n 2 ) D. somethng else 5
Medan n Lnear Tme?! Can we do better? " After all, sortng the whole array s more work than s needed to fnd the medan! Key nsght: generalze the problem! " Rather than seekng an algorthm for the th element, lets ok for the k th element, k [1..n] Suppose that we have a way of parttonng the array at element wth value p: How can ths help? l p p p r n 2 k 6
l r A = p p p!suppose that we are okng for the 10 th element, and: # A = 5 # Ap = 1 A Ap Ah " Then we can seek the 4 th element of Ah nstead! We have reduced the problem sze by a varable amount, n ths case A + Ap = 6 7
l r A = p p p A Ap Ah!Suppose that we are okng for the 10 th element, and: # A = 28 " Then we can seek the 10 th element of A nstead! We have reduced the problem sze by a varable amount, n ths case Ap + Ah 8
l r A = p p p!suppose that we are okng for the 8 th element, and: # A = 6 # Ap = 2 A Ap Ah " Then we can seek the 2 nd element of Ap nstead.! We have now solved the problem, because all the elements of Ap are p 9
Varable-sze decrease?!what s the connecton? " suppose that we have A[1:20] and are okng for the 7 th -smallest element: " run partton, fnd s = 9, say " Where do we ok for the 7 th -smallest element? A: A[1..20] B: A[1..8] C: A[1..9] D: A[10..20] 10
Varable-sze decrease?!what s the connecton? " suppose that we have A[1:20] and are okng for the 7 th -smallest element: " run partton, fnd s = 3, say " Where do we ok for the 7 th -smallest element? A: A[1..3] B: A[1..4] C: A[3..20] D: A[4..20] 11
What s the Effcency?!Dasgupta s analyss shows that: f we can do the partton n O(n) tme, then we can select the k th element n O(n) tme!how can we do partton n O(n) tme? Lomuto Partton Hoare Partton 12
Lomuto Partton!Whle algorthm s runnng: l s r p < p p?!invarant: " A[l] = p A[l+1.. s] < p A[s+1.. 1] p l s < r!establsh nvarant ntally: " p A[l]; s l; s+1 // makes < p nterval and p ntervals both empty 13
I don't lke Lumuto Partton l s r p < p p? 14
I don't lke Lumuto Partton!It does more swaps than necessary l s r p < p p? 14
I don't lke Lumuto Partton!It does more swaps than necessary l s r p < p p? " half of the swap s wasted!it confuses students! " Qucksort does not use the Lumuto Partton 14
Hoare Partton!Classc algorthm of computng!deveped n 1959, publshed n 1961.!Not only lnear, but pecularly effcent!!tony Hoare won the Turng Award for Qucksort, whch s based on ths algorthm... and some other thngs! 15
Partton: CACM (Vol 4) July 1961 ALGORITHM 63 PARTITION C. A. R. HOARE Eltt Brothers Ltd., Borehamwood, Hertfordshre, En procedure partton (A,M,N,I,J); value M,N; array A; nteger M,N,1,J; conunent I and J are output varables, and A s the array (w subscrpt bounds M:N) whch s operated upon by ths procedur Partton takes the value X of a random element of the array and rearranges the values of the elements of the array n such way that there exst ntegers I and J wth the folwng properte M _-< J < I =< NprovdedM < N A[R] =< XforM =< R _-< J A[R] = XforJ < R < I A[R] ~ Xfor I =< R ~ N The procedure uses an nteger procedure random (M,N) wh chooses equprobably a random nteger F between M and N, a also a procedure exchange, whch exchanges the values of ts tw parameters ; begn real X; nteger F; F := random (M,N); I:=M; J:=N; up: X := A[F]; for I : = I step 1 untl N do f X < A [I] then go to down; I:=N; down: forj := J step --1 untl M do f A[J]<X then go to change; J:=M; change: else f I < J then begn exchange (A[IL A[J]); I := I+ 1;J:= J - 1; go to up end f [ < F then begn exchange (A[IL A[F]) I:=I+l end else f F < J tllen begn exchange (A[F], A[J]) ; J:=J-1 end ; end partton 16
Partton: CACM (Vol 4) July 1961 ALGORITHM 63 PARTITION C. A. R. HOARE Eltt Brothers Ltd., Borehamwood, Hertfordshre, En procedure partton (A,M,N,I,J); value M,N; array A; nteger M,N,1,J; conunent I and J are output varables, and A s the array (w subscrpt bounds M:N) whch s operated upon by ths procedur Partton takes the value X of a random element of the array and rearranges the values of the elements of the array n such way that there exst ntegers I and J wth the folwng properte M _-< J < I =< NprovdedM < N A[R] =< XforM =< R _-< J A[R] = XforJ < R < I A[R] ~ Xfor I =< R ~ N The procedure uses an nteger procedure random (M,N) wh chooses equprobably a random nteger F between M and N, a also a procedure exchange, whch exchanges the values of ts tw parameters ; begn real X; nteger F; F := random (M,N); I:=M; J:=N; up: X := A[F]; for I : = I step 1 untl N do f X < A [I] then go to down; I:=N; down: forj := J step --1 untl M do f A[J]<X then go to change; J:=M; change: else f I < J then begn exchange (A[IL A[J]); I := I+ 1;J:= J - 1; go to up end f [ < F then begn exchange (A[IL A[F]) I:=I+l end else f F < J tllen begn exchange (A[F], A[J]) ; J:=J-1 end ; end partton 16
Partton: CACM (Vol 4) July 1961 ALGORITHM 63 PARTITION C. A. R. HOARE Eltt Brothers Ltd., Borehamwood, Hertfordshre, En procedure partton (A,M,N,I,J); value M,N; array A; nteger M,N,1,J; conunent I and J are output varables, and A s the array (w subscrpt bounds M:N) whch s operated upon by ths procedur Partton takes the value X of a random element of the array and rearranges the values of the elements of the array n such way that there exst ntegers I and J wth the folwng properte M _-< J < I =< NprovdedM < N A[R] =< XforM =< R _-< J A[R] = XforJ < R < I A[R] ~ Xfor I =< R ~ N The procedure uses an nteger procedure random (M,N) wh chooses equprobably a random nteger F between M and N, a also a procedure exchange, whch exchanges the values of ts tw parameters ; begn real X; nteger F; F := random (M,N); I:=M; J:=N; up: X := A[F]; for I : = I step 1 untl N do f X < A [I] then go to down; I:=N; down: forj := J step --1 untl M do f A[J]<X then go to change; J:=M; change: else f I < J then begn exchange (A[IL A[J]); I := I+ 1;J:= J - 1; go to up end f [ < F then begn exchange (A[IL A[F]) I:=I+l end else f F < J tllen begn exchange (A[F], A[J]) ; J:=J-1 end ; end partton 16
Partton: CACM (Vol 4) July 1961 ALGORITHM 63 PARTITION C. A. R. HOARE Eltt Brothers Ltd., Borehamwood, Hertfordshre, En procedure partton (A,M,N,I,J); value M,N; array A; nteger M,N,1,J; conunent I and J are output varables, and A s the array (w subscrpt bounds M:N) whch s operated upon by ths procedur Partton takes the value X of a random element of the array and rearranges the values of the elements of the array n such way that there exst ntegers I and J wth the folwng properte M _-< J < I =< NprovdedM < N A[R] =< XforM =< R _-< J A[R] = XforJ < R < I A[R] ~ Xfor I =< R ~ N The procedure uses an nteger procedure random (M,N) wh chooses equprobably a random nteger F between M and N, a also a procedure exchange, whch exchanges the values of ts tw parameters ; begn real X; nteger F; F := random (M,N); I:=M; J:=N; up: X := A[F]; for I : = I step 1 untl N do f X < A [I] then go to down; I:=N; down: forj := J step --1 untl M do f A[J]<X then go to change; J:=M; change: else f I < J then begn exchange (A[IL A[J]); I := I+ 1;J:= J - 1; go to up end f [ < F then begn exchange (A[IL A[F]) I:=I+l end else f F < J tllen begn exchange (A[F], A[J]) ; J:=J-1 end ; end partton 16
Partton: CACM (Vol 4) July 1961 ALGORITHM 63 PARTITION C. A. R. HOARE Eltt Brothers Ltd., Borehamwood, Hertfordshre, En procedure partton (A,M,N,I,J); value M,N; array A; nteger M,N,1,J; conunent I and J are output varables, and A s the array (w subscrpt bounds M:N) whch s operated upon by ths procedur Partton takes the value X of a random element of the array and rearranges the values of the elements of the array n such way that there exst ntegers I and J wth the folwng properte M _-< J < I =< NprovdedM < N A[R] =< XforM =< R _-< J A[R] = XforJ < R < I A[R] ~ Xfor I =< R ~ N The procedure uses an nteger procedure random (M,N) wh chooses equprobably a random nteger F between M and N, a also a procedure exchange, whch exchanges the values of ts tw parameters ; begn real X; nteger F; F := random (M,N); I:=M; J:=N; up: X := A[F]; for I : = I step 1 untl N do f X < A [I] then go to down; I:=N; down: forj := J step --1 untl M do f A[J]<X then go to change; J:=M; change: else f I < J then begn exchange (A[IL A[J]); I := I+ 1;J:= J - 1; go to up end f [ < F then begn exchange (A[IL A[F]) I:=I+l end else f F < J tllen begn exchange (A[F], A[J]) ; J:=J-1 end ; end partton Important features: random pvot double-ended search works n place two outputs 16
Hoare Partton method partton(a,, h) { def pvotindex = randombetween()and(h) def pvot = A[pvotIndex] var := 1 var := h+1 whle { do { := + 1 } whle { ( <= h).andalso {A[] <= pvot} } do { := 1 } whle { ( >= ).andalso {A[] >= pvot} } < } do { exchange(a,, ) } f ( < pvotindex) then { exchange(a,, pvotindex) ; := + 1 } elsef ( > pvotindex) then { exchange(a, pvotindex, ) ; := 1 } lst.wth(, ) } 17
Before partton begns: p h 7
Before partton begns: p h Leave elements that are already n the rght place: h 7
Before partton begns: p h Leave elements that are already n the rght place: h Now a[] p a[], so swap a[] and a[]: h 7
Before partton begns: p h Leave elements that are already n the rght place: h Now a[] p a[], so swap a[] and a[]: p h 7
Before partton begns: p h Leave elements that are already n the rght place: h Now a[] p a[], so swap a[] and a[]: p p h 7
Before partton begns: p h Leave elements that are already n the rght place: h Now a[] p a[], so swap a[] and a[]: p And contnue... p h h 7
when do we stop? And contnue... h untl and cross! p p h 19
when do we stop? And contnue... h untl and cross! p p h s ths possble? h 19
when do we stop? And contnue... h untl and cross! p p h s ths possble? p h 19
when do we stop? And contnue... h untl and cross! p p h s ths possble? p p h 19
when do we stop? And contnue... h untl and cross! p p h s ths possble? p p p h 19
when do we stop? And contnue... h untl and cross! p p h s ths possble? p p p p h 19
12 Cons! Ths problem s orgnally stated as: " You have a balance scale and 12 cons, 1 of whch s counterfet. The counterfet wegh less or more than the other cons. Can you determne the counterfet n 3 weghtngs, and tell f t s heaver or lghter?! A harder and more general problem s: " For some gven n > 1, there are (3 n 3)/2 cons, 1 of whch s counterfet. The counterfet wegh less or more than the other cons. Can you state a pror n weghtng experments wth a balance, wth whch you determne the counterfet con, and tell f t s heaver or lghter? 20
Problem: Bnary Inserton Sort Bnary nserton sort uses bnary search to fnd an approprate poston to nsert A[] among the prevously sorted A[0]... A[ 1]. In your work-groups, determne the worst-case effcency class of ths algorthm. Hnt: The order of growth of the worst-case number of key comparsons made by bnary nserton sort can be obtaned from formulas n Secton 4.1 and Appendx A. For ths algorthm, however, a key comparson s not the operaton that determnes the algorthm s effcency class. Whch operaton does? 21
Problem: Gray Code Use the decrease-by-one technque (Algorthm BRGC) to generate the bnary reflected Gray code for n = 4. 22
Problem: Gray Code Use the decrease-by-one technque (Algorthm BRGC) to generate the bnary reflected Gray code for n = 4. 110 111 10 11 100 101 010 011 0 1 00 01 000 001 n = 1 n = 2 n = 3 22
Problem: Gray Code Algorthm! Trace the folwng algorthm for generatng the Bnary Gray Code of order 4. Start wth code = 0000 output code for = 1 to 15 do: b poston of least sgnfcant 1 n bnary rep of code code XOR (bt b) output code 23
Nm!1 ple of n chps!players take turns removng 1 k m chps!the player removng the last chp wns m = 4 24
Nm!1 ple of n chps!players take turns removng 1 k m chps!the player removng the last chp wns m = 4 24
Nm!1 ple of n chps!players take turns removng 1 k m chps!the player removng the last chp wns m = 4 24
Nm!1 ple of n chps!players take turns removng 1 k m chps!the player removng the last chp wns m = 4 25
Nm!1 ple of n chps!players take turns removng 1 k m chps!the player removng the last chp wns m = 4 25
Nm!1 ple of n chps!players take turns removng 1 k m chps!the player removng the last chp wns m = 4 25
Multplcaton à la russe n m = ( n 2 2m f n s even n 1 2 2m + m f n s odd 26
You try t!!multply 37 67 n m 37 67 18 134 + 67 27
You try t!!multply 37 67 n m 37 67 18 134 + 67 27
You try t!!multply 37 67 n m 37 67 18 134 + 67 27
You try t!!multply 37 67 n m 37 67 18 134 + 67 27