MA/CSSE 473 Day 15 Return Exam Student questions Towers of Hanoi

Slides:



Advertisements
Similar presentations
Algorithm Design Techniques
Advertisements

Recursion and Induction
Recurrences. What is a Recurrence Relation? A system of equations giving the value of a function from numbers to numbers in terms of the value of the.
ITEC200 – Week07 Recursion. 2 Learning Objectives – Week07 Recursion (Ch 07) Students can: Design recursive algorithms to solve.
Recursion. Idea: Some problems can be broken down into smaller versions of the same problem Example: n! 1*2*3*…*(n-1)*n n*factorial of (n-1)
Chapter 10 Recursion Instructor: alkar/demirer. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-2 Recursive Function recursive functionThe.
Recursion. 2 CMPS 12B, UC Santa Cruz Solving problems by recursion How can you solve a complex problem? Devise a complex solution Break the complex problem.
Recursion and Induction Themes –Recursion –Recurrence Definitions –Recursive Relations –Induction (prove properties of recursive programs and objects defined.
Recursively Defined Sequences Lecture 35 Section 8.1 Wed, Mar 23, 2005.
MA/CSSE 473 Day 13 Permutation Generation. MA/CSSE 473 Day 13 HW 6 due Monday, HW 7 next Thursday, Student Questions Tuesday’s exam Permutation generation.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved ADT Implementation:
MA/CSSE 473 Day 12 Insertion Sort quick review DFS, BFS Topological Sort.
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
MA/CSSE 473 Day 17 Permutations by lexicographic order number.
Module #20 - Recurrences 1 Ch. 7 Recurrence Relations Rosen 6 th ed., §§7.1.
MA/CSSE 473 Day 15 BFS Topological Sort Combinatorial Object Generation Intro.
MA/CSSE 473 Day 14 Permutations wrap-up Subset generation (Horner’s method)
MA/CSSE 473 Day 18 Permutations by lexicographic order number.
5.6 Generating Permutations and Combinations Generating Permutations Many different algorithms have been developed to generate the n! permutations of this.
Complexity A decidable problem is computationally solvable. But what resources are needed to solve the problem? –How much time will it require? –How much.
By: Lokman Chan Recursive Algorithm Recursion Definition: A function that is define in terms of itself. Goal: Reduce the solution to.
Lecture 5 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
COMP 170 L2 L11: Recursion, Recurrence, and Induction l Objective n Recursion  A problem solving technique that reduces big problems into smaller ones.
Recursion and Induction Themes –Recursion –Recurrence Definitions –Recursive Relations –Induction (prove properties of recursive programs and objects defined.
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 16 Combinatorial Object Generation Permutations.
chap10 Chapter 10 Recursion chap10 2 Recursive Function recursive function The recursive function is a kind of function that calls.
Recursion Chapter What is recursion? Recursion occurs when a method calls itself, either directly or indirectly. Used to solve difficult, repetitive.
1 In this puzzle, the player begins with n disks of decreasing diameter placed one on top of the other on one of three pegs of the game board. The player.
Recursively Defined Sequences Lecture 40 Section 8.1 Wed, Apr 11, 2007.
1 Towers of Hanoi Three pegs, one with n disks of decreasing diameter; two other pegs are empty Task: move all disks to the third peg under the following.
1 ADT Implementation: Recursion, Algorithm Analysis Chapter 10.
Lecture 11 Recursion. A recursive function is a function that calls itself either directly, or indirectly through another function; it is an alternative.
1 Data Structures CSCI 132, Spring 2016 Notes 16 Tail Recursion.
1 Dr. Chow-Sing LinRecursion - CH 10 Problem Solving and Program Design in C Chapter 9 Recursion Chow-Sing Lin.
Tower of Hanoi problem: Move the pile of rings from one peg to another
Chapter Topics Chapter 16 discusses the following main topics:
Generating Permutations and Subsets
Hamilton cycles in highly symmetric graphs
Recursion what is it? how to build recursive algorithms
Chapter 10 Recursion Instructor: Yuksel / Demirer.
Chapter 17 Recursion.
MA/CSSE 473 Day 13 Finish Topological Sort Permutation Generation
MA/CSSE 473 Day 16 Answers to your questions Divide and Conquer
MA/CSSE 473 Day 10 Data Encryption RSA.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
More Recursion.
Data Structures Recursion CIS265/506: Chapter 06 - Recursion.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Data Structures & Algorithms
Calculate n! Multiply my number by the factorial of the rest of the numbers. if( num > 2 ) return num * factorialR2( num - 1 ); else return 2;
Fundamentals of Programming
Sorting … and Insertion Sort.
Tower of Hanoi problem: Move the pile of rings from one peg to another
Chapter 17 Recursion.
Recall Some Algorithms And Algorithm Analysis Lots of Data Structures
3. Brute Force Selection sort Brute-Force string matching
Recall Brute Force as a Problem Solving Technique
Recursion: The Mirrors
Tower of Hanoi Txt: 10.4 and p. 478 & SOL: DM.10
Recursion and Induction
Recursion.
3. Brute Force Selection sort Brute-Force string matching
Recursion and Induction
Dr. Sampath Jayarathna Cal Poly Pomona
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
Tower of Hanoi problem: Move the pile of rings from one peg to another
Recursively Defined Sequences
3. Brute Force Selection sort Brute-Force string matching
Recursion: The Mirrors
Presentation transcript:

MA/CSSE 473 Day 15 Return Exam Student questions Towers of Hanoi Subsets Ordered Permutations Take Knuth Fascicle to class.

MA/CSSE 473 Day 13 Student Questions on exam or anything else Towers of Hanoi Subset generation – Gray code Permutations and order Pass around attendance sheet.

Towers of Hanoi Move all disks from peg A to peg B One at a time Never place larger disk on top of a smaller disk Demo Code Recurrence and solution

Recurrence for number of moves, Towers of Hanoi code Recurrence for number of moves, and its solution?

First generate Sn-1, the collection of all subsets of {0, …, N-2} Subset generation Goal: generate all subsets of {0, 1, 2, …, N-1} Bottom-up (decrease-by-one) approach First generate Sn-1, the collection of all subsets of {0, …, N-2} Then Sn = Sn-1  { s  {N-1} : sSn-1}

Subset generation Numeric approach: Each subset of {0, …, N-1} corresponds to an bit string of length N where the ith bit is 1 iff i is in the subset. So each subset can be represented by N bits. A simple loop generates them all in "numeric" order.

Subset generation Minimal change algorithm: flip exactly one bit each time we generate the next subset. Most common minimal-change approach: Binary-reflected Gray code. See the links in the announcements page and the schedule page. Transition sequences: which bit to flip 0 010 0102010 010201030102010

Gray Code and Hamiltonian Cycles A Hamiltonian cycle in an undirected graph is … Hypercubes (from Wikipedia): Binary-reflected Gray Code is a Hamiltonian Cycle of a Hypercube:

Recap: Permutations and Order number permutation 0123 12 2013 1 0132 13 2031 2 0213 14 2103 3 0231 15 2130 4 0312 16 2301 5 0321 17 2310 6 1023 18 3012 7 1032 19 3021 8 1203 20 3102 9 1230 21 3120 10 1302 22 3201 11 1320 23 3210 Given a permutation of 0, 1, …, n-1, can we directly find the next permutation in the lexicographic sequence? Given a permutation of 0..n-1, can we determine its permutation sequence number? Discuss these, with examples: Given n and i, can we directly generate the ith permutation of 0, …, n-1?

Discovery time (with two partners) Which permutation follows each of these in lexicographic order? 183647520 471638520 Try to write an algorithm for generating the next permutation, with only the current permutation as input. If the lexicographic permutations of the numbers [0, 1, 2, 3, 4, 5] are numbered starting with 0, what is the number of the permutation 14032? General form? How to calculate efficiency? In the lexicographic ordering of permutations of [0, 1, 2, 3, 4, 5], which permutation is number 541? How to calculate efficiently? After 183647520: 183650247 After 471638520: 471650238 Algorithm: Scan from right, find first place where p[i] < p[i+1]. find j, position of smallest among numbers in p[i+1]…p[n-1] that are greater than p[i]. exchange p[i] and p[j], then reverse p[i+1] ..p[n-1]