Introduction to Algorithms 6.046J/18.401J/SMA5503

Similar documents
8.5. Solving Equations II. Goal Solve equations by balancing.

MST 121: Supplementary resource material for Chapter A1, Sequences

SPH4U Transmission of Waves in One and Two Dimensions LoRusso

CS Lecture 5. Vidroha debroy. Material adapted courtesy of Prof. Xiangnan Kong and Prof. Carolina Ruiz at Worcester Polytechnic Institute

The structure of the Fibonacci numbers in the modular ring Z 5

University of California, Los Angeles Department of Statistics. Measures of central tendency and variation Data display

operate regenerator top without boiling aq. amine solution.

1. Write down the ideal gas law and define all its variable and parameters. 2. Calculate the values and units of the ideal gas law constant R.

Hypothesis testing: ANOVA Test of the equality of means among c groups. Flow-chart

Representing polynominals with DFT (Discrete Fourier Transform) and FFT (Fast Fourier Transform) Arne Andersson

THE EFFECTS OF COUPLED INDUCTORS IN PARALLEL INTERLEAVED BUCK CONVERTERS

A SECOND SOLUTION FOR THE RHIND PAPYRUS UNIT FRACTION DECOMPOSITIONS

Maximizing. Potential in the 4x100 Relay. 4x100 - Entire Race. A Review of Relay Basics. NFHS Relay Zone Measurements

Decision Trees. Nicholas Ruozzi University of Texas at Dallas. Based on the slides of Vibhav Gogate and David Sontag

Algorithms and Data Structures

ICC WORLD TWENTY ( WORLD CUP-2014 )- A CASE STUDY

Headfirst Entry - Diving and Sliding

number in a data set adds (or subtracts) that value to measures of center but does not affect measures of spread.

Available online at ScienceDirect. Procedia Engineering 113 (2015 )

Equipment. Rackets are fragile. Handle the shuttlecocks carefully and only by their rubber tips.

Analytical and numerical prediction of water sorptivity in cement-based materials

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING, THE UNIVERSITY OF NEW MEXICO ECE-238L: Computer Logic Design Fall Notes - Chapter 6.

THE LATENT DEMAND METHOD

DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2017)

Influences of Teaching Styles and Motor Educability on Learning Outcomes of Volleyball

Series 600 Accessories

SYMMETRY AND VARIABILITY OF VERTICAL GROUND REACTION FORCE AND CENTER OF PRESSURE IN ABLE-BODIED GAIT

Outline. Changing needs in Urban Traffic. Introduction The starting point Basic principles Analysis Facts Changes Context Solutions Key messages

Brian Hayes. A reprint from. American Scientist. the magazine of Sigma Xi, the Scientific Research Society

Comparison of Mathematical Approximation Methods for Mine Ventilation Network Analysis

Intuitive Understanding of Throughput-Delay Tradeoff in Ad hoc Wireless Networks

Intersleek Pro. Divers Manual. Our World is Water CONTENTS

GFC NIST MASS FLOW CONTROLLERS. Typical Stainless Steel GFC Mass Flow Controller. Design Features. General Description. Principles of Operation

GFC NIST MASS FLOW CONTROLLERS. Typical Stainless Steel GFC Mass Flow Controller. Design Features. General Description. Principles of Operation

Parsers. Introduction to Computational Linguistics: Parsing Algorithms. Ambiguity refresher. CFG refresher. Example: search space for top-down parser

Dynamic Programming: The Matrix Chain Algorithm

Basic Gas Spring Theory

n UL Listed and FM Approved for n Solenoid control n Quick pressure relief valve 73Q n Pressure sustaining & reducing valve 723

Catenary Analysis and Calculation Method of Track Rope of Cargo Cableway with Multiple Loads

Seated valves (PN 16) VF 2-2-way valve, flange VF 3-3-way valve, flange

DAMAGE ASSESSMENT OF FIBRE ROPES FOR OFFSHORE MOORING

Footwork is the foundation for a skilled basketball player, involving moves

The Real Thing?: Representing the Bullfight and Spain in Death in the Afternoon by Peter Messent

Introductory Rules of Officiating Small Sided Games Under 6 &Under 8 HANDBOOK

Rochester YMCA Swim Lessons Schedule

Emma B. Howe YMCA Swim Lessons Schedule

PERFORMANCE TEAM EVALUATION IN 2008 BEIJING OLYMPIC GAMES

Polynomial functions have graphs that are smooth and continuous. c) Use your knowledge of quadratic functions to sketch the graph.

Wondering where to start?

Wondering where to start?

Hastings YMCA Swim Lessons Schedule

Wondering where to start?

Sequential parimutuel games

Capacity of Shared-Short Lanes at Unsignalised Intersections

Wondering where to start?

Chapter 9, Part B Hypothesis Tests

The Analysis of Bullwhip Effect in Supply Chain Based on Strategic Alliance

STRUCTURAL FILL, SONOTUBES, CONCRETE, GRAVEL, CLEAR SILICONE, ENGINEERED WOOD FIBRE (E.W.F.), GEO-TEXTILE, SOIL, PLANTS & MULCH ARE NOT SUPPLIED.

Patrick Boston (Leeds University) and Mark Chapman (Edinburgh University)

Load Calculation and Design of Roller Crowning of Truck Hub Bearing

Precautions for Total Hip Replacement Patients Only

A Comparison of MOEA/D, NSGA II and SPEA2 Algorithms

St. Paul Midway YMCA Swim Lessons Schedule

Heap Sort. Lecture 35. Robb T. Koether. Hampden-Sydney College. Mon, Apr 25, 2016

Minor Road, Wood Pole without Crossarm. Minor Road, Pole without Crossarm. Arterial Road, Steel or Wood Pole with Crossarm

"The twisting movement of any hoof should, for physiological reasons, not be hindered by Shoeing." (Lungwitz 1884)

WATER VALVES REPLACEMENT PROJECT

Welcome to the world of the Rube Goldberg!

(612)

A Genetic Program for Breeding Racing Pigeons

Wondering where to start?

10 th International Congress of the International Maritime Association of the Mediterranean (IMAM), Rethymno, Crete, May 2002

West St Paul YMCA Swim Lessons Schedule

Kentucky SCL National Core Indicators Data

Eagan YMCA Swim Lessons Schedule

Andover YMCA Swim Lessons Schedule

STRUCTURAL FILL, SONOTUBES, CONCRETE, GRAVEL, CLEAR SILICONE, ENGINEERED WOOD FIBRE (E.W.F.), GEO-TEXTILE, SOIL, PLANTS & MULCH ARE NOT SUPPLIED.

Policy sensitivity analysis of Karachi commuters

Real time lane departure warning system based on principal component analysis of grayscale distribution and risk evaluation model

The new name for... Mines Rescue Service

IRS ISSUES PROPOSED REGULATIONS FOR COMPARATIVE EFFECTIVENESS RESEARCH FEES

TRANSIT. WATS Transit Routes. Regular Fixed Route Service

Product Features Product Benefits Application

Chap 2. Statistical Properties and Spectra of Sea Waves

HYDRAULIC MOTORS MM APPLICATION CONTENTS GENERAL MOTORS

WhisperFit EZ Ventilation Fans

GENETICS 101 GLOSSARY

This report presents an assessment of existing and future parking & traffic requirements, for the site based on the current development proposal.

EQUIPEX NAOS WP5 : Deep oxygen floats in the North- Atlantic

1. (a) (i) 0.049, 0.39, 0.4, 0.409, B1 for correct order. (b) B1 for (a) B1 for (b) B1 for 0.08.

Extensible Detection and Indexing of Highlight Events in Broadcasted Sports Video

1 Main Board. 24 dots characters: 48 Columns

1973, 1975, A.w. Argue, B. Hillaby, and CD. Shepard

securing your safety

DFC NIST DIGITAL MASS FLOW CONTROLLERS. DFC with optional LCD readout PROG RS485. Programmable Mass Flow Controller with Digital Signal Processing

-H- Note. Flow control valve. Key features

SPEED OF SOUND MEASUREMENTS IN GAS-MIXTURES AT VARYING COMPOSITION USING AN ULTRASONIC GAS FLOW METER WITH SILICON BASED TRANSDUCERS

A Data Envelopment Analysis Evaluation and Financial Resources Reallocation for Brazilian Olympic Sports

A multivariate approach for defining fishing tactics from commercial catch and effort data

SIDDHARTH INSTITUTE OF ENGINEERING & TECHNOLOGY :: PUTTUR (AUTONOMOUS) Siddharth Nagar, Narayanavanam Road QUESTION BANK (DESCRIPTIVE)

Transcription:

Itroductio to Algorithms 6.046J/8.40J/SMA5503 Lecture 4 Prof. Charles E. Leiserso

Quicsort Proposed by C.A.R. Hoare i 962. Divide-ad-coquer algorithm. Sorts i place lie isertio sort, but ot lie merge sort. Very practical with tuig. Day 6 Itroductio to Algorithms L4.2

Divide ad coquer Quicsort a -elemet array:. Divide: Partitio the array ito two subarrays aroud a pivot x such that elemets i lower subarray x elemets i upper subarray. x xx x 2. Coquer: Recursively sort the two subarrays. 3. Combie: Trivial. Key: Liear-time partitioig subroutie. Day 6 Itroductio to Algorithms L4.3

Partitioig subroutie PARTITIONA, p, q A[p.. q] x A[p] pivot A[p] i p for j p + to q do if A[ j] x the i i + exchage A[i] A[ j] exchage A[p] A[i] retur i Ivariat: Ruig time O for elemets. xx x x?? p i j q Day 6 Itroductio to Algorithms L4.4

Example of partitioig 66 0 3 55 88 33 22 i j Day 6 Itroductio to Algorithms L4.5

Example of partitioig 66 0 3 55 88 33 22 i j Day 6 Itroductio to Algorithms L4.6

Example of partitioig 66 0 3 55 88 33 22 i j Day 6 Itroductio to Algorithms L4.7

Example of partitioig 66 0 3 55 88 33 22 66 55 3 0 88 33 22 i j Day 6 Itroductio to Algorithms L4.8

Example of partitioig 66 0 3 55 88 33 22 66 55 3 0 88 33 22 i j Day 6 Itroductio to Algorithms L4.9

Example of partitioig 66 0 3 55 88 33 22 66 55 3 0 88 33 22 i j Day 6 Itroductio to Algorithms L4.0

Example of partitioig 66 0 3 55 88 33 22 66 55 3 0 88 33 22 66 55 33 0 88 3 22 i j Day 6 Itroductio to Algorithms L4.

Example of partitioig 66 0 3 55 88 33 22 66 55 3 0 88 33 22 66 55 33 0 88 3 22 i j Day 6 Itroductio to Algorithms L4.2

Example of partitioig 66 0 3 55 88 33 22 66 55 3 0 88 33 22 66 55 33 0 88 3 22 66 55 33 22 88 3 0 i j Day 6 Itroductio to Algorithms L4.3

Example of partitioig 66 0 3 55 88 33 22 66 55 3 0 88 33 22 66 55 33 0 88 3 22 66 55 33 22 88 3 0 i j Day 6 Itroductio to Algorithms L4.4

Example of partitioig 66 0 3 55 88 33 22 66 55 3 0 88 33 22 66 55 33 0 88 3 22 66 55 33 22 88 3 0 i j Day 6 Itroductio to Algorithms L4.5

Example of partitioig 66 0 3 55 88 33 22 66 55 3 0 88 33 22 66 55 33 0 88 3 22 66 55 33 22 88 3 0 22 55 33 66 88 3 0 i Day 6 Itroductio to Algorithms L4.6

Pseudocode for quicsort QUICKSORTA, p, r if p < r the q PARTITIONA, p, r QUICKSORTA, p, q QUICKSORTA, q+, r Iitial call: QUICKSORTA,, Day 6 Itroductio to Algorithms L4.7

Aalysis of quicsort Assume all iput elemets are distict. I practice, there are better partitioig algorithms for whe duplicate iput elemets may exist. Let T worst-case ruig time o a array of elemets. Day 6 Itroductio to Algorithms L4.8

Worst-case of quicsort Iput sorted or reverse sorted. Partitio aroud mi or max elemet. Oe side of partitio always has o elemets. T T 0 + T Θ + T T Θ 2 + Θ + Θ + Θ arithmetic series Day 6 Itroductio to Algorithms L4.9

Worst-case recursio tree T T0 + T + c Day 6 Itroductio to Algorithms L4.20

Worst-case recursio tree T T T0 + T + c Day 6 Itroductio to Algorithms L4.2

Worst-case recursio tree T T0 + T + c c T0 T Day 6 Itroductio to Algorithms L4.22

Worst-case recursio tree T T0 + T + c c T0 c T0 T 2 Day 6 Itroductio to Algorithms L4.23

Worst-case recursio tree T T0 + T + c c T0 c T0 c 2 T0 O Θ Day 6 Itroductio to Algorithms L4.24

Worst-case recursio tree T T0 + T + c c T0 c T0 c 2 T0 Θ O Θ Θ 2 Day 6 Itroductio to Algorithms L4.25

h Worst-case recursio tree c Θ c T T0 + T + c Θ c 2 Θ O Θ Θ Θ 2 T Θ + Θ 2 Θ 2 Day 6 Itroductio to Algorithms L4.26

Best-case aalysis For ituitio oly! If we re lucy, PARTITION splits the array evely: T 2T/2 + Θ Θ lg same as merge sort What if the split is always 9 :? 0 0 9 + T + Θ T T 0 0 What is the solutio to this recurrece? Day 6 Itroductio to Algorithms L4.27

Aalysis of almost-best case T Day 6 Itroductio to Algorithms L4.28

Aalysis of almost-best case c T 9 0 T 0 Day 6 Itroductio to Algorithms L4.29

Aalysis of almost-best case c c 0 9 c 0 9 9 8 T 00 T 00 T 00 T 00 Day 6 Itroductio to Algorithms L4.30

Aalysis of almost-best case 00 c c 0 c 9 c 0 log 0/9 9 c 9 c 8 c 00 00 00 c c c Θ O leaves Θ Day 6 Itroductio to Algorithms L4.3

Aalysis of almost-best case 00 c c 0 c 9 c 0 log 0 log 0/9 9 c 9 c 8 c 00 00 00 c c c Θ Θ lg Lucy! O leaves c log 0 Θ T c log 0/9 + Ο Day 6 Itroductio to Algorithms L4.32

More ituitio Suppose we alterate lucy, ulucy, lucy, ulucy, lucy,. L 2U/2 + Θ lucy U L + Θ ulucy Solvig: L 2L/2 + Θ/2 + Θ 2L/2 + Θ Θ lg Lucy! How ca we mae sure we are usually lucy? Day 6 Itroductio to Algorithms L4.33

Radomized quicsort IDEA: Partitio aroud a radom elemet. Ruig time is idepedet of the iput order. No assumptios eed to be made about the iput distributio. No specific iput elicits the worst-case behavior. The worst case is determied oly by the output of a radom-umber geerator. Day 6 Itroductio to Algorithms L4.34

Radomized quicsort aalysis Let T the radom variable for the ruig time of radomized quicsort o a iput of size, assumig radom umbers are idepedet. For 0,,,, defie the idicator radom variable X if PARTITION geerates a : split, 0 otherwise. E[X ] Pr{X } /, sice all splits are equally liely, assumig elemets are distict. Day 6 Itroductio to Algorithms L4.35

Aalysis cotiued T T0 + T + Θ if 0: split, T + T 2 + Θ if : 2 split, M T + T0 + Θ if : 0 split, 0 X T + T + Θ. Day 6 Itroductio to Algorithms L4.36

Calculatig expectatio E[ T ] E 0 X T + T + Θ Tae expectatios of both sides. Day 6 Itroductio to Algorithms L4.37

Day 6 Itroductio to Algorithms L4.38 Calculatig expectatio [ ] + Θ + + Θ + 0 0 ] [ T T X E T T X E E T Liearity of expectatio.

Day 6 Itroductio to Algorithms L4.39 Calculatig expectatio [ ] [ ] [ ] + Θ + + Θ + + Θ + 0 0 0 ] [ T E T X E T T X E T T X E E T Idepedece of X from other radom choices.

Day 6 Itroductio to Algorithms L4.40 Calculatig expectatio [ ] [ ] [ ] [ ] [ ] Θ + + + Θ + + Θ + + Θ + 0 0 0 0 0 0 ] [ E T E T T E T X E T T X E T T X E E T Liearity of expectatio; E[X ] /.

Day 6 Itroductio to Algorithms L4.4 Calculatig expectatio [ ] [ ] [ ] [ ] [ ] [ ] 2 ] [ 0 0 0 0 0 0 E T E T E T T E T X E T T X E T T X E E T + Θ Θ + + + Θ + + Θ + + Θ + Summatios have idetical terms.

Hairy recurrece [ ] 2 E T 2 [ ] + Θ E T The 0, terms ca be absorbed i the Θ. Prove: E[T] alg for costat a > 0. Choose a large eough so that alg domiates E[T] for sufficietly small 2. Use fact: lg 2 lg 2 2 2 Day 6 Itroductio to Algorithms L4.42 8 exercise.

Substitutio method 2 [ ] a lg + Θ E T 2 Substitute iductive hypothesis. Day 6 Itroductio to Algorithms L4.43

Substitutio method [ ] E T 2 Use fact. 2a 2 2 a lg 2 lg + Θ 8 2 + Θ Day 6 Itroductio to Algorithms L4.44

Substitutio method [ ] E T 2 2a 2 2 a lg lg + Θ a lg a Θ 4 Express as desired residual. 2 8 2 + Θ Day 6 Itroductio to Algorithms L4.45

Substitutio method [ ] E T 2 2a 2 a lg a lg 2 a, a 4 + Θ + Θ Day 6 Itroductio to Algorithms L4.46 lg 2 lg 8 2 Θ if a is chose large eough so that a/4 domiates the Θ.

Quicsort i practice Quicsort is a great geeral-purpose sortig algorithm. Quicsort is typically over twice as fast as merge sort. Quicsort ca beefit substatially from code tuig. Quicsort behaves well eve with cachig ad virtual memory. Day 6 Itroductio to Algorithms L4.47