CS 350 Algorithms and Complexity

Similar documents
CS 350 Algorithms and Complexity

CS 770G - Parallel Algorithms in Scientific Computing

CHAPTER 17 Amortized Analysis

Calculation of time complexity (3%)

2.3 Nilpotent endomorphisms

: Numerical Analysis Topic 2: Solution of Nonlinear Equations Lectures 5-11:

Dynamic Programming! CSE 417: Algorithms and Computational Complexity!

Design and Analysis of Algorithms

Introduction to Algorithms

Lecture 2 Solution of Nonlinear Equations ( Root Finding Problems )

Problem Set 9 Solutions

Exercises. 18 Algorithms

Math Review. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

= z 20 z n. (k 20) + 4 z k = 4

Common loop optimizations. Example to improve locality. Why Dependence Analysis. Data Dependence in Loops. Goal is to find best schedule:

and problem sheet 2

Errors for Linear Systems

Stanford University CS359G: Graph Partitioning and Expanders Handout 4 Luca Trevisan January 13, 2011

University of Washington Department of Chemistry Chemistry 453 Winter Quarter 2015

Attacks on RSA The Rabin Cryptosystem Semantic Security of RSA Cryptology, Tuesday, February 27th, 2007 Nils Andersen. Complexity Theoretic Reduction

Speeding up Computation of Scalar Multiplication in Elliptic Curve Cryptosystem

5 The Rational Canonical Form

Using T.O.M to Estimate Parameter of distributions that have not Single Exponential Family

LINEAR REGRESSION ANALYSIS. MODULE IX Lecture Multicollinearity

princeton univ. F 13 cos 521: Advanced Algorithm Design Lecture 3: Large deviations bounds and applications Lecturer: Sanjeev Arora

Multilayer Perceptrons and Backpropagation. Perceptrons. Recap: Perceptrons. Informatics 1 CG: Lecture 6. Mirella Lapata

Line Drawing and Clipping Week 1, Lecture 2

U.C. Berkeley CS294: Spectral Methods and Expanders Handout 8 Luca Trevisan February 17, 2016

Review of Taylor Series. Read Section 1.2

On the Best Case of Heapsort

A PROBABILITY-DRIVEN SEARCH ALGORITHM FOR SOLVING MULTI-OBJECTIVE OPTIMIZATION PROBLEMS

Lecture 3: Probability Distributions

Chapter Twelve. Integration. We now turn our attention to the idea of an integral in dimensions higher than one. Consider a real-valued function f : D

Chapter 8 SCALAR QUANTIZATION

x = , so that calculated

A 2D Bounded Linear Program (H,c) 2D Linear Programming

Example: (13320, 22140) =? Solution #1: The divisors of are 1, 2, 3, 4, 5, 6, 9, 10, 12, 15, 18, 20, 27, 30, 36, 41,

Introduction to Algorithms

ENTROPIC QUESTIONING

Algorithms And Programming I. Lecture 5 Quicksort

Chapter 3 Describing Data Using Numerical Measures

find (x): given element x, return the canonical element of the set containing x;

Weighted Voting Systems

CS 331 DESIGN AND ANALYSIS OF ALGORITHMS DYNAMIC PROGRAMMING. Dr. Daisy Tang

Hashing. Alexandra Stefan

Combining Constraint Programming and Integer Programming

Lecture 5 Decoding Binary BCH Codes

Outline and Reading. Dynamic Programming. Dynamic Programming revealed. Computing Fibonacci. The General Dynamic Programming Technique

Edge Isoperimetric Inequalities

Homework 9 Solutions. 1. (Exercises from the book, 6 th edition, 6.6, 1-3.) Determine the number of distinct orderings of the letters given:

Complex Numbers Alpha, Round 1 Test #123

Learning Theory: Lecture Notes

Appendix B: Resampling Algorithms

8.4 COMPLEX VECTOR SPACES AND INNER PRODUCTS

Finding Primitive Roots Pseudo-Deterministically

EEL 6266 Power System Operation and Control. Chapter 3 Economic Dispatch Using Dynamic Programming

Faster Searching by Elimination

Algorithms. Algorithms. Algorithms 2.2 M ERGESORT. mergesort bottom-up mergesort. sorting complexity divide-and-conquer

An efficient algorithm for multivariate Maclaurin Newton transformation

Finding Dense Subgraphs in G(n, 1/2)

VQ widely used in coding speech, image, and video

Min Cut, Fast Cut, Polynomial Identities

Retrieval Models: Language models

Statistical Inference. 2.3 Summary Statistics Measures of Center and Spread. parameters ( population characteristics )

A New Scrambling Evaluation Scheme based on Spatial Distribution Entropy and Centroid Difference of Bit-plane

Expected Value and Variance

U.C. Berkeley CS278: Computational Complexity Professor Luca Trevisan 2/21/2008. Notes for Lecture 8

CISE301: Numerical Methods Topic 2: Solution of Nonlinear Equations

2 More examples with details

ELEMENTARY SORTING ALGORITHMS

04 - Treaps. Dr. Alexander Souza

MERGESORT BBM ALGORITHMS DEPT. OF COMPUTER ENGINEERING ERKUT ERDEM. Mergesort. Feb. 27, 2014

Computing Correlated Equilibria in Multi-Player Games

College of Computer & Information Science Fall 2009 Northeastern University 20 October 2009

General theory of fuzzy connectedness segmentations: reconciliation of two tracks of FC theory

Tornado and Luby Transform Codes. Ashish Khisti Presentation October 22, 2003

Introduction to Information Theory, Data Compression,

Algorithms for factoring

2-Adic Complexity of a Sequence Obtained from a Periodic Binary Sequence by Either Inserting or Deleting k Symbols within One Period

VARIATION OF CONSTANT SUM CONSTRAINT FOR INTEGER MODEL WITH NON UNIFORM VARIABLES

Valuated Binary Tree: A New Approach in Study of Integers

A CLASS OF RECURSIVE SETS. Florentin Smarandache University of New Mexico 200 College Road Gallup, NM 87301, USA

arxiv:math/ v1 [math.co] 17 Sep 1998

A random variable is a function which associates a real number to each element of the sample space

Dynamic Programming. Preview. Dynamic Programming. Dynamic Programming. Dynamic Programming (Example: Fibonacci Sequence)

Hiding data in images by simple LSB substitution

THE SUMMATION NOTATION Ʃ

The Minimum Universal Cost Flow in an Infeasible Flow Network

= 1.23 m/s 2 [W] Required: t. Solution:!t = = 17 m/s [W]! m/s [W] (two extra digits carried) = 2.1 m/s [W]

Some Consequences. Example of Extended Euclidean Algorithm. The Fundamental Theorem of Arithmetic, II. Characterizing the GCD and LCM

LOW BIAS INTEGRATED PATH ESTIMATORS. James M. Calvin

p 1 c 2 + p 2 c 2 + p 3 c p m c 2

Outline. Communication. Bellman Ford Algorithm. Bellman Ford Example. Bellman Ford Shortest Path [1]

Notes on Frequency Estimation in Data Streams

Generalized Linear Methods

Chapter 5. Solution of System of Linear Equations. Module No. 6. Solution of Inconsistent and Ill Conditioned Systems

LECTURE 9 CANONICAL CORRELATION ANALYSIS

Self-complementing permutations of k-uniform hypergraphs

E Tail Inequalities. E.1 Markov s Inequality. Non-Lecture E: Tail Inequalities

The Selection Problem - Variable Size Decrease/Conquer (Practice with algorithm analysis)

Transcription:

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