Some more Decrease-and-conquer Algorithms

Slides:



Advertisements
Similar presentations
CSC 421: Algorithm Design & Analysis
Advertisements

22C:19 Discrete Structures Trees Spring 2014 Sukumar Ghosh.
1 Today’s Material Medians & Order Statistics – Ch. 9.
Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort
Design & Analysis of Algorithms CS315
Lectures on Recursive Algorithms1 COMP 523: Advanced Algorithmic Techniques Lecturer: Dariusz Kowalski.
CSCE 3110 Data Structures & Algorithm Analysis
DIVIDE AND CONQUER APPROACH. General Method Works on the approach of dividing a given problem into smaller sub problems (ideally of same size).  Divide.
CSC 2300 Data Structures & Algorithms March 27, 2007 Chapter 7. Sorting.
Tirgul 8 Universal Hashing Remarks on Programming Exercise 1 Solution to question 2 in theoretical homework 2.
Tirgul 4 Order Statistics Heaps minimum/maximum Selection Overview
Using Reduction for the Game of NIM. At each turn, a player chooses one pile and removes some sticks. The player who takes the last stick wins. Problem:
Great Theoretical Ideas in Computer Science.
Great Theoretical Ideas in Computer Science.
MA/CSSE 473 Day 22 Gray Code More Decrease and Conquer Algorithms No class Day 22 in because of Abby's broken arm.
Ch. 8 & 9 – Linear Sorting and Order Statistics What do you trade for speed?
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))”
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.
Section 3.1: Proof Strategy Now that we have a fair amount of experience with proofs, we will start to prove more difficult theorems. Our experience so.
Order Statistics. Order statistics Given an input of n values and an integer i, we wish to find the i’th largest value. There are i-1 elements smaller.
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.
MA/CSSE 473 Day 14 Permutations wrap-up Subset generation (Horner’s method)
Discrete Structures Lecture 12: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.
Trees – Chapter 9 Slides courtesy of Dr. Michael P. Frank University of Florida Dept. of Computer & Information Science & Engineering.
MA/CSSE 473 Day 20 Questions before exam More decrease and Conquer.
Quicksort CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 7.
Asymptotic Behavior Algorithm : Design & Analysis [2]
Copyright © 2014 Curt Hill Algorithms From the Mathematical Perspective.
Benjamin Casey C S 329 E Spring  Variants played since ancient times; resemblance to Chinese “Jianshizi” or “picking stones”  Current name and.
MA/CSSE 473 Day 21 In , Day 21 was the exam.
MA/CSSE 473 Day 09 Modular Division Revisited Fermat's Little Theorem Primality Testing.
MA/CSSE 473 Day 14 Strassen's Algorithm: Matrix Multiplication Decrease and Conquer DFS.
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
Analysis of Algorithms CS 477/677
Linear Algebra Review.
Introductory Game Theory
Decrease-and-Conquer
MA/CSSE 473 Day 19 Sleepsort Finish Nim Josephus problem
Great Theoretical Ideas in Computer Science
CSC 421: Algorithm Design & Analysis
CSC 421: Algorithm Design & Analysis
Chapter 5 Decrease-and-Conquer
MA/CSSE 473 Day 17 Divide-and-conquer Convex Hull
CSC 421: Algorithm Design & Analysis
Decrease-and-Conquer
Chapter 5 Decrease-and-Conquer
Quick Sort (11.2) CSE 2011 Winter November 2018.
Decrease-and-Conquer
Ch 7: Quicksort Ming-Te Chi
Welcome to CIS 068 ! Lesson 9: Sorting CIS 068.
Decrease-and-Conquer
8/04/2009 Many thanks to David Sun for some of the included slides!
Topic: Divide and Conquer
Lial/Hungerford/Holcomb/Mullins: Mathematics with Applications 11e Finite Mathematics with Applications 11e Copyright ©2015 Pearson Education, Inc. All.
Decrease-and-Conquer
Chapter 5 Decrease-and-Conquer
Advanced Algorithms Analysis and Design
Linear-Time Sorting Algorithms
Systems of distinct representations
CSC 421: Algorithm Design & Analysis
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Algorithms CSCI 235, Spring 2019 Lecture 20 Order Statistics II
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
The Selection Problem.
CSC 421: Algorithm Design & Analysis
Divide and Conquer Merge sort and quick sort Binary search
Presentation transcript:

Some more Decrease-and-conquer Algorithms What questions do you have? Decrease by a constant factor Decrease by a variable amount Some more Decrease-and-conquer Algorithms

Shell's sort – A quick recap Insertion Sort on Steroids Shell's sort – A quick recap

Shell's Sort We use the following gaps: 7, then 3, then 1 (last gap must always be 1): Next, do the same thing for the next group of 7ths

Shell's sort 2

Shell's sort 3 Why bother, if we are going to do a regular insertion sort at the end anyway? Analysis? Why would this be an inferior gap sequence? 36, 12, 3, 1 https://www.youtube.com/watch?v=CmPA7zE8mx0 No one has been able to analyze it yet, thought it works fast in practice if gap sequence is chosen well.

Code from Weiss book

More Decrease and Conquer examples

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 11 13 5 26 2 52 1 104 143 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?

Decrease by a variable amount Search in a Binary Search Tree Interpolation Search See Levitin, pp190-191 Also Weiss, Section 5.6.3 And class slides from Session 12 (Winter, 2017) Mention the Wilkes-Barre phone book.

Median finding Find the kth smallest element of an (unordered) list of n elements Start with quicksort's partition method Informal analysis QuickSelect(A[l..r], k): pos = partition(A) // use A[l] as pivot If pos == k, return A[k] Else if pos > k return QuickSelect(A[l, pos-1], k) Else return QuickSelect(A[pos+1, r], k-pos-1) Analysis: Best, constant, worst N^2. Average turns out to be linear, but analysis is complicated.

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, … If it's your turn and there are zero chips left, you have already lost. If 1, 2, 3, … m, you can take them all and win. What about m+1? m+2?

Graph of One-Pile Nim with m = 4 Vertex numbers indicate n, the number of chips in the pile. The losing positions for the current player are circled. Only winning moves from a winning position are shown. Generalization: The player who moves 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.

Multi-Pile Nim There are multiple piles of chips. Two players take turns by removing from any single pile at least one and at most all of that pile's chips. (The number of chips taken can vary from move to move) The winner is the player who takes the last chip. What is the winning strategy for 2-pile Nim? For the general case, consider the "Nim sum", x  y, which is the integer obtained by bitwise XOR of corresponding bits of two non-negative integers x and y. What is 6  3? 2-piles: Take enough chips form larger pile so that n1 = n2. 6  3: 1 1 0 0 1 1 ------ 1 0 1 = 5

Multi-Pile Nim Strategy Solution by C.L. Bouton: The first player has a winning strategy iff the nim sum of the "pile counts" is not zero. Let's prove it. Note that  is commutative and associative. Also note that for any non-negative integer k, kk is zero.

Multi-Pile Nim Proof Notation: Let x1, … ,xn be the sizes of the piles before a move, and y1, … ,yn be the sizes of the piles after that move. Let s = x1  …  xn, and t = y1  …  yn. Observe: If the chips were removed from pile k, then xi = yi for all ik, and xk > yk . Lemma 1: t = s  xk  yk . Lemma 2: If s = 0, then t  0. Lemma 3: If s  0, it is possible to make a move such that t=0. [after proof, do an example]. Proof of the strategy is then a simple induction. (It's a HW problem) Example: 3 piles, containing 7, 13, and 8 chips. Lemma 1 proof: t = 0 ⊕ t = s ⊕ s ⊕ t = s ⊕ (x1 ⊕ ... ⊕ xn) ⊕ (y1 ⊕ ... ⊕ yn) = s ⊕ (x1 ⊕ y1) ⊕ ... ⊕ (xn ⊕ yn) = s ⊕ 0 ⊕ ... ⊕ 0 ⊕ (xk ⊕ yk) ⊕ 0 ⊕ ... ⊕ 0 = = s ⊕ xk ⊕ yk Lemma 2 proof: If there is no possible move, then the lemma is vacuously true (and the first player loses the normal play game by definition). Otherwise, any move in heap k will produce t = xk ⊕ yk from (*). This number is nonzero, since xk ≠ yk. Lemma 3 proof: Let d be the position of the leftmost (most significant) nonzero bit in the binary representation of s, and choose k such that the dth bit of xk is also nonzero. (Such a k must exist, since otherwise the dth bit of s would be 0.) Then letting yk = s ⊕ xk, we claim that yk < xk: all bits to the left of d are the same in xk and yk, bit d decreases from 1 to 0 (decreasing the value by 2d), and any change in the remaining bits will amount to at most 2d−1. The first player can thus make a move by taking xk − yk objects from heap k, then t = s ⊕ xk ⊕ yk (by (Lemma 1)) = s ⊕ xk ⊕ (s ⊕ xk) = 0.