Introduction to. Algorithms. Lecture 10. Prof. Piotr Indyk

Similar documents
Introduction to. Algorithms. Lecture 10. Prof. Constantinos Daskalakis CLRS

Divide & conquer. Which works better for multi-cores: insertion sort or merge sort? Why?

Chapter 7: Sorting 7.1. Original

CS 758/858: Algorithms

A Lower Bound for Comparison Sort

CSE373: Data Structure & Algorithms Lecture 23: More Sorting and Other Classes of Algorithms. Nicki Dell Spring 2014

Fast Sorting and Pattern-Avoiding Permutations

CSE 573 Problem Set 1. Answers on 10/17/08

The Theory Behind the z/architecture Sort Assist Instructions

CSE 100: RED-BLACK TREES

DATA STRUCTURES USING C

CS3334 Data Structures Lecture 4: Bubble Sort & Insertion Sort. Chee Wei Tan

Lecture 20: Combinatorial Search (1997) Steven Skiena. skiena

CSE465, Spring 2009 March 16 1

MITOCW 7. Counting Sort, Radix Sort, Lower Bounds for Sorting

Econ 172A - Slides from Lecture 18

CS 473G: Combinatorial Algorithms, Fall 2005 Homework 0. I understand the Homework Instructions and FAQ.

MITOCW R7. Comparison Sort, Counting and Radix Sort

MITOCW ocw lec11

Sorting. Suppose behind each door (indicated below) there are numbers placed in a random order and I ask you to find the number 41.

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims. Lecture 17: Heaps and Priority Queues

Game Theory and Randomized Algorithms

Merge Sort. Note that the recursion bottoms out when the subarray has just one element, so that it is trivially sorted.

Algorithms and Data Structures CS 372. The Sorting Problem. Insertion Sort - Summary. Merge Sort. Input: Output:

Math 3012 Applied Combinatorics Lecture 2

CSE 332: Data Structures and Parallelism Games, Minimax, and Alpha-Beta Pruning. Playing Games. X s Turn. O s Turn. X s Turn.

MITOCW 6. AVL Trees, AVL Sort

CS188 Spring 2010 Section 3: Game Trees

Previous Lecture. How can computation sort data faster for you? Sorting Algorithms: Speed Comparison. Recursive Algorithms 10/31/11

Introduction to Spring 2009 Artificial Intelligence Final Exam

The tenure game. The tenure game. Winning strategies for the tenure game. Winning condition for the tenure game

COS 226 Algorithms and Data Structures Fall Midterm Exam

CS188 Spring 2010 Section 3: Game Trees

COS 226 Algorithms and Data Structures Fall Midterm Exam

PRIORITY QUEUES AND HEAPS

A Memory Efficient Anti-Collision Protocol to Identify Memoryless RFID Tags

PRIORITY QUEUES AND HEAPS. Lecture 19 CS2110 Spring 2014

Patterns and random permutations II

RBT Operations. The basic algorithm for inserting a node into an RBT is:

Mathematics (Project Maths)

Permutation Tableaux and the Dashed Permutation Pattern 32 1

Coding for Efficiency

Evacuation and a Geometric Construction for Fibonacci Tableaux

Design and Analysis of Algorithms Prof. Madhavan Mukund Chennai Mathematical Institute. Module 6 Lecture - 37 Divide and Conquer: Counting Inversions

Massachusetts Institute of Technology 6.042J/18.062J, Spring 04: Mathematics for Computer Science April 16 Prof. Albert R. Meyer and Dr.

mywbut.com Two agent games : alpha beta pruning

Pin-Permutations and Structure in Permutation Classes

UNIVERSITY of PENNSYLVANIA CIS 391/521: Fundamentals of AI Midterm 1, Spring 2010

Introduction to Algorithms / Algorithms I Lecturer: Michael Dinitz Topic: Algorithms and Game Theory Date: 12/4/14

Mathematics (Project Maths Phase 2)

Examination paper for TDT4120 Algorithms and Data Structures

Permutations with short monotone subsequences

What is a Sorting Function?

Notes for Recitation 3

MITOCW R3. Document Distance, Insertion and Merge Sort

Topic 23 Red Black Trees

Section Marks Agents / 8. Search / 10. Games / 13. Logic / 15. Total / 46

Study Unit. Auxiliary Views. This sneak preview of your study material has been prepared in advance of the book's actual online release.

and 6.855J. Network Simplex Animations

Game-Playing & Adversarial Search Alpha-Beta Pruning, etc.

Chapter 1 An Introduction to Computer Science. INVITATION TO Computer Science 1

Permutation Tableaux and the Dashed Permutation Pattern 32 1

Programming Abstractions

UMBC 671 Midterm Exam 19 October 2009

Game-playing AIs: Games and Adversarial Search FINAL SET (w/ pruning study examples) AIMA

University of Connecticut Department of Mathematics

CS 787: Advanced Algorithms Homework 1

Problem A. Worst Locations

A New network multiplier using modified high order encoder and optimized hybrid adder in CMOS technology

CS188 Spring 2014 Section 3: Games

Hypercube Networks-III

MATH 200 FINAL EXAM PEYAM RYAN TABRIZIAN

Greedy Algorithms. Kleinberg and Tardos, Chapter 4

CS/COE 1501

Finite Mathematical Structures A

COUNTING AND PROBABILITY

Permutations. Example 1. Lecture Notes #2 June 28, Will Monroe CS 109 Combinatorics

Module 3 Greedy Strategy

Twin Binary Sequences: A Nonredundant Representation for General Nonslicing Floorplan

CS100: DISCRETE STRUCTURES. Lecture 8 Counting - CH6

: Principles of Automated Reasoning and Decision Making Midterm

Mathematics (Project Maths Phase 2)

1 Permutations. Example 1. Lecture #2 Sept 26, Chris Piech CS 109 Combinatorics

CSE 100: BST AVERAGE CASE AND HUFFMAN CODES

Bit Reversal Broadcast Scheduling for Ad Hoc Systems

1 This work was partially supported by NSF Grant No. CCR , and by the URI International Engineering Program.

Last update: March 9, Game playing. CMSC 421, Chapter 6. CMSC 421, Chapter 6 1

Sudoku Mock Test 5. Instruction Booklet. 28 th December, IST (GMT ) 975 points + Time Bonus. Organized by. Logic Masters: India

Lecture 6: Latin Squares and the n-queens Problem

Collectives Pattern CS 472 Concurrent & Parallel Programming University of Evansville

EK307 Lab 3 Spring Lab Assignment 3 Logic Gates

Homework Assignment #1

Syllabus Cosines Sampled Signals. Lecture 1: Cosines. ECE 401: Signal and Image Analysis. University of Illinois 1/19/2017

6.004 Computation Structures Spring 2009

Huffman Coding - A Greedy Algorithm. Slides based on Kevin Wayne / Pearson-Addison Wesley

1 Permutations. 1.1 Example 1. Lisa Yan CS 109 Combinatorics. Lecture Notes #2 June 27, 2018

Introduction. BME208 Logic Circuits Yalçın İŞLER

Algorithms. Abstract. We describe a simple construction of a family of permutations with a certain pseudo-random

Resistive Circuits. Lab 2: Resistive Circuits ELECTRICAL ENGINEERING 42/43/100 INTRODUCTION TO MICROELECTRONIC CIRCUITS

LAB 1: Familiarity with Laboratory Equipment (_/10)

Transcription:

6.006- Introduction to Algorithms Lecture 10 Prof. Piotr Indyk

Quiz Rules Do not open this quiz booklet until directed to do so. Read all the instructions on this page When the quiz begins, write your name on every page of this quiz booklet. You have 120 minutes to earn 120 points. Do not spend too much time on any one problem. Read them all through first, and attack them in the order that allows you to make the most progress. This quiz booklet contains X pages, including this one. Two extra sheets of scratch paper are attached. Please detach them before turning in your quiz at the end of the exam period. This quiz is closed book. You may use one 8 1 11 or A4 crib sheets (both sides). No calculators or programmable devices are permitted. No cell phones or other communications devices are permitted. Write your solutions in the space provided. If you need more space, write on the back of the sheet containing the problem. Pages may be separated for grading. Do not waste time and paper re-deriving facts that we have studied. It is sufficient to cite known results. Show your work, as partial credit will be given. You will be graded not only on the correctness of your answer, but also on the clarity with which you express it. Be neat. *At participating locations only. Do not use while sleeping. Read label before using. Details inside.harmful if swallowed.

Menu: sorting ctd. Show that Θ(n lg n) is the best possible running time for a sorting algorithm. Design an algorithm that sorts in Θ(n) time.??? Hint: maybe the models are different?

Comparison sort All the sorting algorithms we have seen so far are comparison sorts: only use comparisons to determine the relative order of elements. E.g., insertion sort, merge sort, heapsort. The best running time that we ve seen for comparison sorting is O(n lg n). Is O(n lg n) the best we can do via comparisons? Decision trees can help us answer this question.

Decision-tree A recipe for sorting n numbers a 1, a 2,, a n 1:2 - Nodes are suggested 2:3 comparisons: i:j means 123 1:3 compare a i to a j, for i, j {1, 2,, n}. 132 312 1:3 213 2:3 231 321 - Branching direction depends on outcome of comparisons. - Leaves are labeled with permutations corresponding to the outcome of the sorting.

Decision-tree example Sort a 1, a 2, a 3 = 9, 4, 6 : 2:3 1:2 9 4 1:3 123 1:3 213 2:3 132 312 231 321 Each internal node is labeled i:j for i, j {1, 2,, n}. The left subtree shows subsequent comparisons if a i a j. The right subtree shows subsequent comparisons if a i a j.

Decision-tree example Sort a 1, a 2, a 3 = 9, 4, 6 : 2:3 1:2 1:3 9 6 123 1:3 132 312 213 2:3 231 321 Each internal node is labeled i:j for i, j {1, 2,, n}. The left subtree shows subsequent comparisons if a i a j. The right subtree shows subsequent comparisons if a i a j.

Decision-tree example Sort a 1, a 2, a 3 = 9, 4, 6 : 2:3 1:2 1:3 123 1:3 213 4 6 2:3 132 312 231 321 Each internal node is labeled i:j for i, j {1, 2,, n}. The left subtree shows subsequent comparisons if a i a j. The right subtree shows subsequent comparisons if a i a j.

Decision-tree example Sort a 1, a 2, a 3 = 9, 4, 6 : 2:3 1:2 1:3 123 1:3 213 2:3 132 312 231 321 4 6 9 Each leaf contains a permutation π(1), π(2),, π(n) to indicate that the ordering a π(1) a π(2) a π(n) has been established.

Decision-tree model A decision tree can model the execution of any comparison sort: One tree for each input size n. A path from the root to the leaves of the tree represents a trace of comparisons that the algorithm may perform. The running time of the algorithm = the length of the path taken. Worst-case running time = height of tree.

Lower bound for decisiontree sorting Theorem. Any decision tree that can sort n elements must have height Ω(n lg n). Proof The tree must contain n! leaves, since there are n! possible permutations A height-h binary tree has 2 h leaves Thus 2 h n! h lg(n!) (lg is mono. increasing) lg ((n/e) n ) (Stirling s formula) = n lg n n lg e = Ω(n lg n).

Sorting in linear time Counting sort: No comparisons between elements. Input: A[1.. n], where A[ j] {1, 2,, k}. Output: B[1.. n], a sorted permutation of A (implicitly, we also determine the permutation π transforming A into B) Auxiliary storage: C[1.. k].

Counting sort for i 1 to k do C[i] 0 for j 1 to n do C[A[ j]] C[A[ j]] + 1 for i 2 to k do C[i] C[i] + C[i 1] for j n downto 1 do B[C[A[ j]]] A[ j] C[A[ j]] C[A[ j]] 1 store in C the frequencies of the different keys in A i.e. C[i] = {key = i} now C contains the cumulative frequencies of different keys in A, i.e. C[i] = {key i} using cumulative frequencies build sorted permutation

Counting-sort example 5 A: 4 1 3 4 3 C: one index for each possible key stored in A B:

Loop 1: initialization 5 A: 4 1 3 4 3 C: 0 0 0 0 B: for i 1 to k do C[i] 0

Loop 2: count frequencies 5 A: 4 1 3 4 3 C: 0 0 0 1 B: for j 1 to n do C[A[ j]] C[A[ j]] + 1 C[i] = {key = i}

Loop 2: count frequencies 5 A: 4 1 3 4 3 C: 1 0 0 1 B: for j 1 to n do C[A[ j]] C[A[ j]] + 1 C[i] = {key = i}

Loop 2: count frequencies 5 A: 4 1 3 4 3 C: 1 0 1 1 B: for j 1 to n do C[A[ j]] C[A[ j]] + 1 C[i] = {key = i}

Loop 2: count frequencies 5 A: 4 1 3 4 3 C: 1 0 1 2 B: for j 1 to n do C[A[ j]] C[A[ j]] + 1 C[i] = {key = i}

Loop 2: count frequencies 5 A: 4 1 3 4 3 C: 1 0 2 2 B: for j 1 to n do C[A[ j]] C[A[ j]] + 1 C[i] = {key = i}

Loop 2: count frequencies 5 A: 4 1 3 4 3 C: 1 0 2 2 B: for j 1 to n do C[A[ j]] C[A[ j]] + 1 C[i] = {key = i}

5 A: 4 1 3 4 3 C: 1 0 2 2 B: Walk through frequency array an place the appropriate number of each key in output array

A parenthesis: a quick finish 5 A: 4 1 3 4 3 C: 1 0 2 2 B: 1

A parenthesis: a quick finish 5 A: 4 1 3 4 3 C: 1 0 2 2 B: 1

A parenthesis: a quick finish 5 A: 4 1 3 4 3 C: 1 0 2 2 B: 1 3 3

A parenthesis: a quick finish 5 A: 4 1 3 4 3 C: 1 0 2 2 B: 1 3 3 4 4 B is sorted! but no permutation π

Loop 2: count frequencies 5 A: 4 1 3 4 3 C: 1 0 2 2 B: for j 1 to n do C[A[ j]] C[A[ j]] + 1 C[i] = {key = i}

Loop 3: cumulative frequencies 5 A: 4 1 3 4 3 C: 1 0 2 2 B: C': 1 1 2 2 for i 2 to k do C[i] C[i] + C[i 1] C[i] = {key i}

Loop 3: cumulative frequencies 5 A: 4 1 3 4 3 C: 1 0 2 2 B: C': 1 1 3 2 for i 2 to k do C[i] C[i] + C[i 1] C[i] = {key i}

Loop 3: cumulative frequencies 5 A: 4 1 3 4 3 C: 1 0 2 2 B: C': 1 1 3 5 for i 2 to k do C[i] C[i] + C[i 1] C[i] = {key i}

Loop 4: permute elements of A 5 A: 4 1 3 4 3 C: 1 1 3 5 B: for j n downto 1 do B[C[A[ j]]] A[ j] C[A[ j]] C[A[ j]] 1

Loop 4: permute elements of A 5 A: 4 1 3 4 3 C: 1 1 3 5 B: There are exactly 3 elements A[5]; so where should I place A[5]? for j n downto 1 do B[C[A[ j]]] A[ j] C[A[ j]] C[A[ j]] 1

Loop 4: permute elements of A 5 A: 4 1 3 4 3 C: 1 1 3 5 B: 3 Used-up one 3; update counter. for j n downto 1 do B[C[A[ j]]] A[ j] C[A[ j]] C[A[ j]] 1

Loop 4: permute elements of A 5 A: 4 1 3 4 3 C: 1 1 2 5 B: 3 for j n downto 1 do B[C[A[ j]]] A[ j] C[A[ j]] C[A[ j]] 1

Loop 4: permute elements of A 5 A: 4 1 3 4 3 C: 1 1 2 5 B: 3 for j n downto 1 do B[C[A[ j]]] A[ j] C[A[ j]] C[A[ j]] 1

Loop 4: permute elements of A 5 A: 4 1 3 4 3 C: 1 1 2 5 B: 3 There are exactly 5 elements A[4], so where should I place A[4]? for j n downto 1 do B[C[A[ j]]] A[ j] C[A[ j]] C[A[ j]] 1

Loop 4: permute elements of A 5 A: 4 1 3 4 3 C: 1 1 2 4 B: 3 4 for j n downto 1 do B[C[A[ j]]] A[ j] C[A[ j]] C[A[ j]] 1

Loop 4: permute elements of A 5 A: 4 1 3 4 3 C: 1 1 2 4 B: 3 4 for j n downto 1 do B[C[A[ j]]] A[ j] C[A[ j]] C[A[ j]] 1

Loop 4: permute elements of A 5 A: 4 1 3 4 3 C: 1 1 2 4 B: 3 4 for j n downto 1 do B[C[A[ j]]] A[ j] C[A[ j]] C[A[ j]] 1

Loop 4: permute elements of A 5 A: 4 1 3 4 3 C: 1 1 1 4 B: 3 3 4 for j n downto 1 do B[C[A[ j]]] A[ j] C[A[ j]] C[A[ j]] 1

Loop 4: permute elements of A 5 A: 4 1 3 4 3 C: 1 1 1 4 B: 3 3 4 for j n downto 1 do B[C[A[ j]]] A[ j] C[A[ j]] C[A[ j]] 1

Loop 4: permute elements of A 5 A: 4 1 3 4 3 C: 1 1 1 4 B: 3 3 4 for j n downto 1 do B[C[A[ j]]] A[ j] C[A[ j]] C[A[ j]] 1

Loop 4: permute elements of A 5 A: 4 1 3 4 3 C: 0 1 1 4 B: 1 3 3 4 for j n downto 1 do B[C[A[ j]]] A[ j] C[A[ j]] C[A[ j]] 1

Loop 4: permute elements of A 5 A: 4 1 3 4 3 C: 0 1 1 4 B: 1 3 3 4 for j n downto 1 do B[C[A[ j]]] A[ j] C[A[ j]] C[A[ j]] 1

Loop 4: permute elements of A 5 A: 4 1 3 4 3 C: 0 1 1 4 B: 1 3 3 4 for j n downto 1 do B[C[A[ j]]] A[ j] C[A[ j]] C[A[ j]] 1

Loop 4: permute elements of A 5 A: 4 1 3 4 3 C: 0 1 1 3 B: 1 3 3 4 4 for j n downto 1 do B[C[A[ j]]] A[ j] C[A[ j]] C[A[ j]] 1

Analysis Θ(k) Θ(n) Θ(k) Θ(n) Θ(n + k) for i 1 to k do C[i] 0 for j 1 to n do C[A[ j]] C[A[ j]] + 1 for i 2 to k do C[i] C[i] + C[i 1] for j n downto 1 do B[C[A[ j]]] A[ j] C[A[ j]] C[A[ j]] 1

Running time If k = O(n), then counting sort takes Θ(n) time. But, sorting takes Ω(n lg n) time! Where s the fallacy? Answer: Comparison sorting takes Ω(n lg n) time. Counting sort is not a comparison sort. In fact, not a single comparison between elements occurs!

Stable sorting Counting sort is a stable sort: it preserves the input order among equal elements. A: 4 1 3 4 3 B: 1 3 3 4 4

Radix sort Origin: Herman Hollerith s card-sorting machine for the 1890 U.S. Census. (See Appendix.) Digit-by-digit sort. Hollerith s original (bad) idea: sort on mostsignificant digit first. Good idea: Sort on least-significant digit first with auxiliary stable sort.

Operation of radix sort 3 2 9 4 5 7 6 5 7 8 3 9 4 3 6 7 2 0 3 5 5 7 2 0 3 5 5 4 3 6 4 5 7 6 5 7 3 2 9 8 3 9 7 2 0 3 2 9 4 3 6 8 3 9 3 5 5 4 5 7 6 5 7 3 2 9 3 5 5 4 3 6 4 5 7 6 5 7 7 2 0 8 3 9

Additional material (not covered in Quiz 1)

Correctness of radix sort Induction on digit position Assume that the numbers are sorted by their low-order t 1 digits. Sort on digit t Two numbers that differ in digit t are correctly sorted. Two numbers equal in digit t are put in the same order as the input correct order. 7 2 0 3 2 9 4 3 6 8 3 9 3 5 5 4 5 7 6 5 7 3 2 9 3 5 5 4 3 6 4 5 7 6 5 7 7 2 0 8 3 9

Runtime Analysis of radix sort Assume counting sort is the auxiliary stable sort. Sort n computer words of b bits each. Each word can be viewed as having b/r base-2 r digits. 8 8 8 8 Example: 32-bit word If each b-bit word is broken into r-bit pieces, each pass of counting sort takes Θ(n + 2 r ) time. Setting r=log n gives Θ(n) time per pass, or Θ(n b/log n) total

Appendix: Punched-card technology Herman Hollerith (1860-1929) Punched cards Hollerith s tabulating system Operation of the sorter Origin of radix sort Modern IBM card Web resources on punched-card technology Return to last slide viewed.

Herman Hollerith (1860-1929) The 1880 U.S. Census took almost 10 years to process. While a lecturer at MIT, Hollerith prototyped punched-card technology. His machines, including a card sorter, allowed the 1890 census total to be reported in 6 weeks. He founded the Tabulating Machine Company in 1911, which merged with other companies in 1924 to form International Business Machines.

Punched cards Punched card = data record. Hole = value. Algorithm = machine + human operator. Replica of punch card from the 1900 U.S. census. [Howells 2000]

Hollerith s tabulating system Figure from [Howells 2000]. Pantograph card punch Hand-press reader Dial counters Sorting box

Operation of the sorter An operator inserts a card into the press. Pins on the press reach through the punched holes to make electrical contact with mercuryfilled cups beneath the card. Whenever a particular digit value is punched, the lid of the corresponding sorting bin lifts. The operator deposits the card Hollerith Tabulator, Pantograph, Press, and Sorter into the bin and closes the lid. When all cards have been processed, the front panel is opened, and the cards are collected in order, yielding one pass of a stable sort.

Origin of radix sort Hollerith s original 1889 patent alludes to a mostsignificant-digit-first radix sort: The most complicated combinations can readily be counted with comparatively few counters or relays by first assorting the cards according to the first items entering into the combinations, then reassorting each group according to the second item entering into the combination, and so on, and finally counting on a few counters the last item of the combination for each group of cards. Least-significant-digit-first radix sort seems to be a folk invention originated by machine operators.

Modern IBM card One character per column. Produced by the WWW Virtual Punch-Card Server. So, that s why text windows have 80 columns! Int 3/8/11

Web resources on punchedcard technology Doug Jones s punched card index Biography of Herman Hollerith The 1890 U.S. Census Early history of IBM Pictures of Hollerith s inventions Hollerith s patent application (borrowed from Gordon Bell s CyberMuseum) Impact of punched cards on U.S. history Int 3/8/11

Correctness of radix sort Induction on digit position Assume that the numbers are sorted by their low-order t 1 digits. Sort on digit t 7 2 0 3 2 9 4 3 6 8 3 9 3 5 5 4 5 7 6 5 7 3 2 9 3 5 5 4 3 6 4 5 7 6 5 7 7 2 0 8 3 9

Correctness of radix sort Induction on digit position Assume that the numbers are sorted by their low-order t 1 digits. Sort on digit t Two numbers that differ in digit t are correctly sorted. 7 2 0 3 2 9 4 3 6 8 3 9 3 5 5 4 5 7 6 5 7 3 2 9 3 5 5 4 3 6 4 5 7 6 5 7 7 2 0 8 3 9