Download presentation
Presentation is loading. Please wait.
Published byBrittney Hubbard Modified over 9 years ago
1
MA/CSSE 473 Day 23 Transform and Conquer
2
MA/CSSE 473 Day 23 Scores on HW 7 were very high (class average was 93%). Good job! (Score was not included in midterm grade) HW 8 is due tonight –with a "grace day" that extends until Thursday at 11:59 PM. HW 9 due next Tuesday, Oct 16 HW 10 due Friday, Oct 19. HW 11 due Monday, Oct 22. Exam 2 Friday, Nov 2. Student Questions Divide and Conquer leftovers Transform and conquer –Many of the examples should be review. On exam problems, or anything else?
3
DECREASE AND CONQUER A few leftover examples
4
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, …
5
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. From each winning position, only winning moves are shown (bold arrows). Generalization: The player moving first wins iff n is not a multiple of m+1; –The winning strategy is to always take n mod (m+1)) chips if possible. Q1
6
Multi-Pile Nim There are multiple piles of chips. Two players take turns by removing from any single pile at least 1 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 that 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? Q2-4
7
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.
8
Multi-Pile Nim Proof Notation: –Let x 1, …,x n be the sizes of the piles before a move, and y 1, …,y n be the sizes of the piles after that move. –Let s = x 1 … x n, and t = y 1 … y n. Observe: If the chips were removed from pile k, then x i = y i for all i k, and x k > y k. Lemma 1: t = s x k y k. 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. (do this one with a partner) Q5-11
9
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 http://en.wikipedia.org/wiki/Josephus
10
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
11
Transform and Conquer Algorithms Transform a problem to a simpler instance of the same problem – instance simplification Transformation to a different representation of the same instance – representation change Transformation to an instance of a different problem that we know how to solve – problem reduction Q1
12
Presorting an Array The following problems are simplified by pre- sorting the array: –Search (can do Binary or Interpolation search) –Determine whether the array contains duplicates –Find the median of the array –Find the mode of the elements of the array The most frequently-occurring element –A related problem: Anagrams In a large collection of words, find words that are anagrams of each other How can pre-sorting help? Sort the letters of each word –Interval union problem from early part of PLC Q2-3
13
Gaussian Elimination (review of MA221) [and whichever new-numbered course does it] Solve a system of n linear equations in n unknowns –Represent the system by an augmented coefficient matrix –Transform the matrix to triangular matrix by a combination of the following solution-preserving elementary operations: exchange two rows multiply a row by a nonzero constant replace a row by that row plus or minus a constant multiple of a different row –Look at the algorithm and analysis on pp 207-208; if you can't understand them, ask at some point. Hopefully you saw this in your D.E. class. –Ѳ(n 3 ) [previous HW] Q4-5
14
Other Applications of G.E. Matrix inverse –Augment a square matrix by the identity matrix –Perform elementary operations until the original matrix is the identity. –The "augmented part" will be the inverse –More details and an example at http://en.wikipedia.org/wiki/Gauss- Jordan_elimination http://en.wikipedia.org/wiki/Gauss- Jordan_elimination
15
Other Applications of G.E. Determinant calculation –Calculation of the determinant of a triangular matrix is easy What effect does each of the elementary operations have on the determinant? –exchange two rows –multiply a row by a nonzero constant –replace a row by that row plus or minus a constant multiple of a different row Do these operations until you get a triangular matrix Keep track of the operations' cumulative effect on the determinant
16
LU Decomposition This can speed up all three applications of Gaussian Elimination Write the matrix A as a product of a Lower Triangular matrix L and an upper Triangular matrix U. Example: Q6
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.