Hanoi Towers Big Oh Recursion Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science.

Slides:



Advertisements
Similar presentations
Chapter Objectives To learn about recursive data structures and recursive methods for a LinkedList class To understand how to use recursion to solve the.
Advertisements

AE1APS Algorithmic Problem Solving John Drake
Towers of Hanoi
2.3 Recursion do automated demo of towers of hanoi before class
COSO 1030 Section 4 Recursion. What is about Towers of Hanoi Divide and Conquer Strategy Recursion and Induction Thinking Recursively Recursion Pitfalls.
COSC 2006 Data Structures I Recursion III
Representational Choices The Towers of Hanoi Problem.
Tirgul 2 Asymptotic Analysis. Motivation: Suppose you want to evaluate two programs according to their run-time for inputs of size n. The first has run-time.
Fall 2007CS 2251 Proof by Induction. Fall 2007CS 2252 Proof by Induction There are two forms of induction Standard Induction –Prove the theorem is true.
1 CSCD 300 Data Structures Recursion. 2 Proof by Induction Introduction only - topic will be covered in detail in CS 320 Prove: N   i = N ( N + 1.
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Recursion.
1 Recursion, Recurrences and Induction Supplementary Notes Prepared by Raymond Wong Presented by Raymond Wong.
The Tower of Hanoi
Data Structures Mohamed Mustaq Ahmed Chapter 2- Algorithms.
Towers of Hanoi. Introduction This problem is discussed in many maths texts, And in computer science an AI as an illustration of recursion and problem.
Recursion Jordi Cortadella Department of Computer Science.
General Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Linked Lists part 2 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
Review and Prepare for Test 3 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science.
By: Lokman Chan Recursive Algorithm Recursion Definition: A function that is define in terms of itself. Goal: Reduce the solution to.
Big Oh CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Bubble Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and.
Priority Queues, Trees, and Huffman Encoding CS 244 This presentation requires Audio Enabled Brent M. Dingle, Ph.D. Game Design and Development Program.
HelloWorld – GameOver CS 244 – Data Structures and Algorithms Department of Mathematics, Statistics, and Computer Science University of Wisconsin – Stout.
Makefiles, Geany CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
CS320n –Visual Programming Advanced Recursion (Slides 8-2) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.
1 CompSci 105 SS 2005 Principles of Computer Science Lecture 6: Recursion Lecturer: Santokh Singh Assignment 1 due tomorrow. Should have started working.
Searching CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Priority Queues and Sorting CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science.
Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
22C:19 Discrete Math Advanced Counting Fall 2010 Sukumar Ghosh.
Quicksort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer.
COMP 170 L2 L11: Recursion, Recurrence, and Induction l Objective n Recursion  A problem solving technique that reduces big problems into smaller ones.
Priority Queues and Heaps Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University.
Reading from a file, Sorting, and a little Searching Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics,
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Divide and Conquer Prudence Wong
Review of Recursion  a recursive method calls itself  to prevent infinite recursion you need to ensure that: 1. the method reaches a base case 2. each.
UNIT 17 Recursion: Towers of Hanoi.
CMPF144 FUNDAMENTALS OF COMPUTING THEORY Module 9: The Tower of Hanoi.
Dr. Naveed Riaz Design and Analysis of Algorithms 1 1 Formal Methods in Software Engineering Lecture # 27.
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Hanoi Towers part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
INDUCTION David Kauchak CS52 – Spring to-1 multiplexer control control_negate and_out1 input0 input1 and_out2 output.
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.
Aumm-e-hani munir Maria umer Rakhshanda batool Faiza.
Take the Test Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
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.
Lecture 11 Recursion. A recursive function is a function that calls itself either directly, or indirectly through another function; it is an alternative.
Mathematical Induction. The Principle of Mathematical Induction Let S n be a statement involving the positive integer n. If 1.S 1 is true, and 2.the truth.
Recursion CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Hubert Chan (Chapters 1.6, 1.7, 4.1)
MATH 224 – Discrete Mathematics
COMP108 Algorithmic Foundations Divide and Conquer
COMP108 Algorithmic Foundations Divide and Conquer
Recursion &Faster Sorting
Hubert Chan (Chapters 1.6, 1.7, 4.1)
Introduction to Computer Science - Alice
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
More Recursion.
Use mathematical induction to prove that the formula is true for all natural numbers m. {image} Choose the first step of the proof from the following:
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
More on Merge Sort CS 244 This presentation is not given in class
Jordi Cortadella Department of Computer Science
Notes 9.5 – Mathematical Induction
The Hanoi Tower Problem
CSE 373 Data Structures and Algorithms
Review and Prepare for Test 2
Tower of Hanoi Algorithm
Data Structures and Algorithms CS 244
Presentation transcript:

Hanoi Towers Big Oh Recursion Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin – Stout Based on the book: Data Structures and Algorithms in C++ (Goodrich, Tamassia, Mount)

First: Math Example

Moving On That was basic math version of an induction proof Next let’s apply it to Towers of Hanoi Problem

Towers of Hanoi 7 moves

Human Way while true: if n is odd: move disk1 one peg left (first peg wraps around to last peg) else: move disk1 one peg right (last peg wraps around to first peg) if done: break else: make the only legal move not involving disk1

Computer Way Tower of Hanoi & Big Oh Pseudocode : FUNCTION MoveTower(n, source,dest, spare): IF n == 1, THEN: move disk 1 from source to dest //base case ELSE: MoveTower(n - 1, source, spare, dest) // Step 1 move disk n from source to dest // Step 2 MoveTower(n - 1, spare, dest, source) // Step 3 END IF aside: 1 is smallest disk and n is the largest disk How did we get this code? we start in the “middle” See next slides

Tower of Hanoi & Big Oh Pseudocode : FUNCTION MoveTower(n, source,dest, spare): IF n == 1, THEN: move disk 1 from source to dest //base case ELSE: MoveTower(n - 1, source, spare, dest) // Step 1 move disk n from source to dest // Step 2 MoveTower(n - 1, spare, dest, source) // Step 3 END IF aside: 1 is smallest disk and n is the largest disk Notice Something At the start we have 3 disks (n=3) As of Move 3, shown below, We have moved the (n-1) tower i.e. the tower of 2 disks to the Middle Peg

Tower of Hanoi & Big Oh Pseudocode : FUNCTION MoveTower(n, source,dest, spare): IF n == 1, THEN: move disk 1 from source to dest //base case ELSE: MoveTower(n - 1, source, spare, dest) // Step 1 move disk n from source to dest // Step 2 MoveTower(n - 1, spare, dest, source) // Step 3 END IF aside: 1 is smallest disk and n is the largest disk Notice Something At the start we have 3 disks (n=3) As of Move 3, shown below, We have moved the (n-1) tower i.e. the tower of 2 disks to the Middle Peg On Move 4 we move the largest disk to the Rightmost Peg

Tower of Hanoi & Big Oh Pseudocode : FUNCTION MoveTower(n, source,dest, spare): IF n == 1, THEN: move disk 1 from source to dest //base case ELSE: MoveTower(n - 1, source, spare, dest) // Step 1 move disk n from source to dest // Step 2 MoveTower(n - 1, spare, dest, source) // Step 3 END IF aside: 1 is smallest disk and n is the largest disk Notice Something At the start we have 3 disks (n=3) As of Move 3, shown below, We have moved the (n-1) tower i.e. the tower of 2 disks to the Middle Peg On Move 4 we move the largest disk to the Rightmost Peg On Move 7 we have moved the (n-1) tower to the Rightmost Peg as well

Analysis

Base Case of 1 disk is easy to prove Claim: S(1) = 2^1 – 1 = 1 Proof: Move the one disk from the left peg to the right peg and done taking only 1 Move

Analysis

The End Or is it?