MA/CSSE 473 Day 20 Finish Josephus

Slides:



Advertisements
Similar presentations
Chapter 13. Red-Black Trees
Advertisements

LU Factorization.
CPSC 252 AVL Trees Page 1 AVL Trees Motivation: We have seen that when data is inserted into a BST in sorted order, the BST contains only one branch (it.
Transform and Conquer Chapter 6. Transform and Conquer Solve problem by transforming into: a more convenient instance of the same problem (instance simplification)
AVL Trees COL 106 Amit Kumar Shweta Agrawal Slide Courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
Transform & Conquer Replacing One Problem With Another Saadiq Moolla.
Chapter 6: Transform and Conquer
EECS 311: Chapter 4 Notes Chris Riesbeck EECS Northwestern.
Chapter 9.1 = LU Decomposition MATH 264 Linear Algebra.
MA/CSSE 473 Day 25 Transform and Conquer Student questions?
TCSS 343, version 1.1 Algorithms, Design and Analysis Transform and Conquer Algorithms Presorting HeapSort.
Tirgul 6 B-Trees – Another kind of balanced trees Problem set 1 - some solutions.
Chapter 10 Search Structures Instructors: C. Y. Tang and J. S. Roger Jang All the material are integrated from the textbook "Fundamentals of Data Structures.
Multivariate Linear Systems and Row Operations.
MA/CSSE 473 Day 28 Hashing review B-tree overview Dynamic Programming.
Copyright © 2011 Pearson, Inc. 7.3 Multivariate Linear Systems and Row Operations.
MA/CSSE 473 Day 20 Josephus problem Transform and conquer examples.
MA/CSSE 473 Day 22 Binary Heaps Heapsort Answers to student questions Exam 2 Tuesday, Nov 4 in class.
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 CSC 421: Algorithm Design Analysis Spring 2014 Transform & conquer  transform-and-conquer approach  presorting  balanced search trees, heaps  Horner's.
Balancing Binary Search Trees. Balanced Binary Search Trees A BST is perfectly balanced if, for every node, the difference between the number of nodes.
Computer Algorithms Submitted by: Rishi Jethwa Suvarna Angal.
MA/CSSE 473 Day 21 AVL Tree Maximum height 2-3 Trees Student questions?
Triangular Form and Gaussian Elimination Boldly on to Sec. 7.3a… HW: p odd.
Binary Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
Lecture 14 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
1 CSC 421: Algorithm Design Analysis Spring 2013 Transform & conquer  transform-and-conquer approach  presorting  balanced search trees, heaps  Horner's.
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
Chapter 11 Sorting Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and Mount.
CSC 421: Algorithm Design Analysis
MA/CSSE 473 Day 23 Review of Binary Heaps and Heapsort
COMP 53 – Week Fourteen Trees.
MA/CSSE 473 Day 19 Sleepsort Finish Nim Josephus problem
Linear Equations.
Balancing Binary Search Trees
CSIT 402 Data Structures II
Chapter 6 Transform-and-Conquer
CS202 - Fundamental Structures of Computer Science II
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
CSCI Trees and Red/Black Trees
MA/CSSE 473 Day 21 AVL Tree Maximum height 2-3 Trees
Chapter 8: Lesson 8.1 Matrices & Systems of Equations
AVL Trees "The voyage of discovery is not in seeking new landscapes but in having new eyes. " - Marcel Proust.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Red-Black Trees Motivations
Written Midterm Solutions
Chapter 6 Transform and Conquer.
Transform and Conquer This group of techniques solves a problem by a transformation to a simpler/more convenient instance [eg sorted] of the same problem.
CS202 - Fundamental Structures of Computer Science II
Transform and Conquer This group of techniques solves a problem by a transformation to a simpler/more convenient instance of the same problem (instance.
Multi-Way Search Trees
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
A Kind of Binary Tree Usually Stored in an Array
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
Tree Rotations and AVL Trees
Chapter 6: Transform and Conquer
Lecture No.20 Data Structures Dr. Sohail Aslam
Transform and Conquer This group of techniques solves a problem by a transformation to a simpler/more convenient instance of the same problem (instance.
Advanced Implementation of Tables
Transform and Conquer Transform and Conquer Transform and Conquer.
CS202 - Fundamental Structures of Computer Science II
Transform and Conquer Transform and Conquer Transform and Conquer.
CSC 143 Binary Search Trees.
AVL-Trees (Part 1).
BST Insert To insert an element, we essentially do a find( ). When we reach a NULL pointer, we create a new node there. void BST::insert(const Comp &
2-3 Trees Extended tree. Tree in which all empty subtrees are replaced by new nodes that are called external nodes. Original nodes are called internal.
CSE 373 Data Structures Lecture 8
Lecture 9: Intro to Trees
CS202 - Fundamental Structures of Computer Science II
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

MA/CSSE 473 Day 20 Finish Josephus Transform and conquer Gaussian Elimination LU-decomposition AVL Tree Maximum height 2-3 Trees Student questions?

Recap: Josephus Problem n people, numbered 1-n, are in a circle Count starts with 1 Every 2nd 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: J(2k) = 2*J(k) - 1 Solution if n is odd: J(2k+1) = 2*J(k) + 1 Use it to find J(2) … J(8) Clever solution: cyclic bit shift left n=8: First pass eliminates positions 2, 4, 6, 8. Second pass eliminates 3, 7. Third pass 5. 1 left n=7. First pass 2, 4, 6, 1. Then 5, 3 . 7 is left If n is even: We eliminate 2, 4, 6, … n on first round. This leaves us with the same problem for n/2 (except for renumbering) 1 1, 2 3, 35, etc. i2*i-1. Thus J(2k) = 2∙J(k)-1 If n is odd: We eliminate 2, 4, 6, … n-1, 1 on first round. This leaves us with the same problem for (n-1)/2 items (except for renumbering) 1 3, 2 5, 37, etc. i2*i+1. Thus J(2k+1) = 2∙J(k)+1 Cyclic bit shift: 11, 1001, 1111, 100001, 101011, 110101, 111111, 100001

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

Instance simplification: 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 course Why are these "transform and conquer"?

Instance Simplification: Gaussian Elimination (hopefully you saw basics in a DE or lin. Alg. course) 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 in my office. Ѳ(n3) [previous HW problem] Why is this "transform and conquer"?

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

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 value? 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 Multiples determinant by -1 Multiplies determinant by that constant No effect.

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: Splitting A up into L and U is an n3 operation. https://rosettacode.org/wiki/LU_decomposition Example, solving Ax = B is the same as solving LUx = B. let y = Ux. Solve Ly = B, easy because L is triangular. Then solve Ux = y, also easy because U is triangular. We won't do the details. It turns out that finding L and U are theta(n^3) – like G.E.. But once we have done it, doing the multiplication is theta(n^2) Helpful in situation where we want to solve several different equations: Ax = b, Ax = c, Ax=d, etc. The L and U are the same for all of the cases.

Review: Representation change: AVL Trees (what you should remember…) Named for authors of original paper, Adelson-Velskii and Landis (1962). An AVL tree is a height-balanced Binary Search Tree. A BST T is height balanced if T is empty, or if | height( TL ) - height( TR ) |  1, and TL and TR are both height-balanced. Show: Maximum height of an AVL tree with N nodes is (log N) How do we maintain balance after insertion? Exercise for later: Given a pointer to the root of an AVL tree with N nodes, find the height of the tree in log N time Details on balance codes and various rotations are in the CSSE 230 slides that are linked from the schedule page. Let's review that together Extra data to make this more efficient. Balance code in each node. This is an essential part of the AVL data structure.

Representation change: 2-3 trees Another approach to balanced trees Keeps all leaves on the same level Some non-leaf nodes have 2 keys and 3 subtrees Others are regular binary nodes. Easy to balance

2-3 tree insertion example More examples of insertion: http://www.cs.ucr.edu/cs14/cs14_06win/slides/2-3_trees_covered.pdf http://slady.net/java/bt/view.php?w=450&h=300 Add 10, 11, 12, … to the last tree

Efficiency of 2-3 tree insertion Upper and lower bounds on height of a tree with n elements? Worst case insertion and lookup times is proportional to the height of the tree. Upper bound: A full binary tree. So N ≥ 2h+1 -1. h ≤ lg(N+1) -1. Lower bound. Full ternary tree. Two items in each node N <= 2(1 + 3 + 32 + … + 3h) = 3h+1 -1. So h ≥ log3(N+1) -1

2-3 Tree insertion practice Insert 84 into this tree and show the resulting tree

2-3 Tree insertion practice Insert 84 into this tree and show the resulting tree