MA/CSSE 473 Day 22 Gray Code More Decrease and Conquer Algorithms No class Day 22 in 20310 because of Abby's broken arm.

Slides:



Advertisements
Similar presentations
Sorting in Linear Time Introduction to Algorithms Sorting in Linear Time CSE 680 Prof. Roger Crawfis.
Advertisements

Introduction to Algorithms Quicksort
CSE Lecture 3 – Algorithms I
CSC 421: Algorithm Design & Analysis
Techniques for Dealing with Hard Problems Backtrack: –Systematically enumerates all potential solutions by continually trying to extend a partial solution.
Chapter 5 Decrease and Conquer. Homework 7 hw7 (due 3/17) hw7 (due 3/17) –page 127 question 5 –page 132 questions 5 and 6 –page 137 questions 5 and 6.
MA/CSSE 473 Day 13 Divide and Conquer Closest Points Convex Hull Answer Quiz Question 1.
Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 4 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Chapter 5: Decrease and Conquer
Chapter 4 Decrease-and-Conquer. Decrease-and-Conquer 1. Reduce problem instance to smaller instance of the same problem 2. Solve smaller instance 3. Extend.
Design & Analysis of Algorithms CS315
Decrease-and-Conquer
CS4413 Divide-and-Conquer
Quicksort
Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2. Solve smaller instances.
Design and Analysis of Algorithms - Chapter 41 Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two.
- ABHRA DASGUPTA Solving Adhoc and Math related problems.
MA/CSSE 473 Day 03 Asymptotics A Closer Look at Arithmetic With another student, try to write a precise, formal definition of “t(n) is in O(g(n))”
MA/CSSE 473 Day 20 Josephus problem Transform and conquer examples.
A few leftover Decrease-and-conquer Algorithms
MA/CSSE 473 Day 23 Transform and Conquer. MA/CSSE 473 Day 23 Scores on HW 7 were very high (class average was 93%). Good job! (Score was not included.
1 Decrease-and-Conquer 1. Reduce problem instance to smaller instance of the same problem 2. Solve smaller instance 3. Extend solution of smaller instance.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 4 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
The Selection Problem. 2 Median and Order Statistics In this section, we will study algorithms for finding the i th smallest element in a set of n elements.
Chapter 5 Decrease-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
MA/CSSE 473 Day 14 Permutations wrap-up Subset generation (Horner’s method)
MA/CSSE 473 Day 18 Permutations by lexicographic order number.
1 Joe Meehean.  Problem arrange comparable items in list into sorted order  Most sorting algorithms involve comparing item values  We assume items.
CSC 211 Data Structures Lecture 13
MA/CSSE 473 Day 23 Student questions Space-time tradeoffs Hash tables review String search algorithms intro.
MA/CSSE 473 Day 20 Questions before exam More decrease and Conquer.
MA/CSSE 473 Day 02 Some Numeric Algorithms and their Analysis.
Introduction to Algorithms Rabie A. Ramadan rabieramadan.org 7.
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
Quicksort CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Sorting.
Week 13 - Friday.  What did we talk about last time?  Sorting  Insertion sort  Merge sort  Started quicksort.
Asymptotic Behavior Algorithm : Design & Analysis [2]
Sorting and selection – Part 2 Prof. Noah Snavely CS1114
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Week 13 - Wednesday.  What did we talk about last time?  NP-completeness.
MA/CSSE 473 Day 19 Subset Generation. MA/CSSE 473 Day 19 HW 7 due today Exam 1 Thursday There will be significant time for your questions in tomorrow's.
MA/CSSE 473 Day 21 In , Day 21 was the exam.
MA/CSSE 473 Day 14 Strassen's Algorithm: Matrix Multiplication Decrease and Conquer DFS.
CS6045: Advanced Algorithms Sorting Algorithms. Sorting So Far Insertion sort: –Easy to code –Fast on small inputs (less than ~50 elements) –Fast on nearly-sorted.
MA/CSSE 473 Day 11 Knuth interview Amortization (growable Array) Brute Force Examples.
MA/CSSE 473 Day 20 Finish Josephus
MA/CSSE 473 Day 12 Interpolation Search Insertion Sort quick review
Some more Decrease-and-conquer Algorithms
Decrease-and-Conquer
MA/CSSE 473 Day 19 Sleepsort Finish Nim Josephus problem
CSC 421: Algorithm Design & Analysis
CSC 421: Algorithm Design & Analysis
Chapter 5 Decrease-and-Conquer
MA/CSSE 473 Day 16 Answers to your questions Divide and Conquer
CSC 421: Algorithm Design & Analysis
Sorting by Tammy Bailey
Decrease-and-Conquer
Chapter 5 Decrease-and-Conquer
Decrease-and-Conquer
Decrease-and-Conquer
Chapter 5 Decrease-and-Conquer
CSC 421: Algorithm Design & Analysis
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
CSC 421: Algorithm Design & Analysis
Presentation transcript:

MA/CSSE 473 Day 22 Gray Code More Decrease and Conquer Algorithms No class Day 22 in because of Abby's broken arm.

MA/CSSE 473 Day 22 You can copy your answers for quiz questions 1-2 from yesterday’s quiz. HW 9 is due Tuesday. Its "late day" period will extend until Friday at noon. Don't forget the Convex Hull implementation problem. It is due 24 days after I assigned it (October 21), and 11 of those days have already passed. HW 10 due Tuesday, Oct 19 HW 11 Due Friday, Oct 22. Yes, there really will be 3 things due that week! Student Questions Gray Code More Decrease and Conquer algorithms

Generate all Subsets of {a 0, …, a n-1 } Decrease by one (yesterday): –Generate S n-1, the collection of the 2 n-1 subsets of {a 0, …, a n-2 } –Then S n = S n-1  { s  {a n-1 } : s  S n-1 } Each subset of {a 0, …, a n-1 } corresponds to a bit string of length n, where the i th bit is 1 iff a i is in the subset –Generate binary codes in numeric order (yesterday) –Gray code

Recap: Another approach: Each subset of {a 0, …, a n-1 } corresponds to an bit string of length n, where the i th bit is 1 if and only if a i is in the subset def allSubsets(s): n = len(s) subsets=[] for i in range(2**n): subset = [] current = i for j in range (n): if current % 2 == 1: subset += [s[j]] current /= 2 subsets += [subset] return subsets Output: [[], [0],[1], [0, 1], [2], [0, 2], [1, 2], [0, 1, 2]]

Recap: Gray Code Named for Frank Gray The term can refer to any sequence of the binary codes for n bits in which only one bit changes in each transitions. Usually refers to binary reflected code. Non-binary-reflected example: 000, 010, 011, 001, 101, 111, 110, 100 A Gray code can be represented by its transition sequence: which bit changes each time In the above example: 1, 0, 1, 2, 1, 0, 1 In terms of subsets, the transition sequence tells which element to add or remove from one subset to get the next subset Gray code applications. Wikipedia has a decent overview. Q1

Generating a Transition Sequence Transition sequences for Binary Reflected Code T 1 = 0 T n+1 = T n, n, T n reversed What are T 2, T 3, T 4 T n is always a palindrome, so T n+1 = T n, n, T n Q2 Yesterday's quiz question 3 was rather silly. The property is obvious.

Direct Recursive Gray Code Generation G 0 is empty To get G i+1 from G i : –Start with G i and G i reversed. –Prepend 0 to each code in the first half, and 1 to each code in the second half. Example: G 2  G 3 –{00, 01, 11, 10} {10, 11, 01, 00}  –{000, 001, 011, 010, 110, 111, 101, 100}

Iteratively Generate Gray Code We add a parity bit, p. Set all bits (including p) to 0. Q3 * Based on Knuth, Volume 4, Fascicle 2, page 6.

Quote of the Day There are 10^11 stars in the galaxy. That used to be a huge number. But it's only a hundred billion. It's less than the national deficit! We used to call them astronomical numbers. Now we should call them economical numbers. - Richard FeynmanRichard Feynman

OTHER DECREASE-AND-CONQUER ALGORITHMS Decrease by a constant factor Decrease by a variable amount

Decrease by a Constant Factor Examples that we have already seen: –Binary Search –Exponentiation (ordinary and modular) by repeated squaring –Multiplication à la Russe (The Dasgupta book that I often used for the first part of the course calls it "European" instead of "Russian") Example Then strike out any rows whose first number is even, and add up the remaining numbers in the second column.

Fake Coin Problem We have n coins All but one have the same weight One is lighter We have a balance scale with two pans. All it will tell us is whether the two sides have equal weight, or which side is heavier What is the minimum number of weighings that will guarantee that we find the fake coin? Decrease by factor of two. Q4

Decrease by a variable amount Search in a Binary Search Tree Interpolation Search –See Levitin, pp –Also Weiss, Section 5.6.3

Interpolation Search Searches a sorted array similar to binary search but estimates location of the search key in A[l..r] by using its value v. Specifically, the values of the array’s elements are assumed to grow linearly from A[l] to A[r] Location of v is estimated as the x-coordinate of the point on the straight line through (l, A[l]) and (r, A[r]) whose y-coordinate is v: x = l +  (v - A[l])(r - l)/(A[r] – A[l] )  Q5

Interpolation Search Runtime Average case  (lg lg n) Worst:  (n) What can lead to worst-case behavior? Social Security numbers of US residents Phone book (Wilkes-Barre) CSSE department employees, Q6

Median finding Find the k th element of an (unordered) list of n elements Start with quicksort's partition method Informal analysis

One Pile Nim There is a pile of n chips. Two players take turns by removing from the pile at least 1 and at most m chips. (The number of chips taken can vary from move to move.) The winner is the player that takes the last chip. Who wins the game – the player moving first or second, if both players make the best moves possible? It’s a good idea to analyze this and similar games “backwards”, i.e., starting with n = 0, 1, 2, …

Graph of One-Pile Nim with m = 4 Vertex numbers indicate n, the number of chips in the pile. The losing position for the player to move are circled. Only winning moves from a winning position are shown (in bold). Generalization: The player moving first wins iff n is not a multiple of 5 (more generally, m+1); –The winning move is to take n mod 5 (n mod (m+1)) chips on every move.

Josephus problem - background Flavius Josephus was a Jewish general and historian who lived and wrote in the 1 st century AD Much of what we know about 1 st century life in Israel (and the beginnings of Christianity) before and after the Roman destruction of the Jewish temple in 70 AD comes from his writings The "Josephus problem" is based on an odd suicide pact that he describes –He and his men stood in a circle and counted off –Every other person (or every third person, accounts vary) was killed –The last person was supposed to kill himself –He must have been the next-to-last person! –When it got down to two people, he persuaded the other person that they should surrender instead

Josephus Problem n people, numbered 1-n, are in a circle Count starts with 1 Every 2 nd person is eliminated The last person left, J(n), is the winner Examples: n=8, n=7 J(1) = 1 Solution if n is even Solution if n is odd Use it to find J(2) … J(8) Clever solution: cyclic bit shift left