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

Slides:



Advertisements
Similar presentations
You have been given a mission and a code. Use the code to complete the mission and you will save the world from obliteration…
Advertisements

Advanced Piloting Cruise Plot.
Chapter 1 The Study of Body Function Image PowerPoint
1 Copyright © 2010, Elsevier Inc. All rights Reserved Fig 2.1 Chapter 2.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Title Subtitle.
Determine Eligibility Chapter 4. Determine Eligibility 4-2 Objectives Search for Customer on database Enter application signed date and eligibility determination.
My Alphabet Book abcdefghijklm nopqrstuvwxyz.
DIVIDING INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
FACTORING ax2 + bx + c Think “unfoil” Work down, Show all steps.
Addition Facts
Lecture 15 Linked Lists part 2
© 2006 Pearson Education Chapter 8: Recursion Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis, William.
Tower of Hanoi Tower of Hanoi is a mathematical puzzle invented by a French Mathematician Edouard Lucas in The game starts by having few discs stacked.
CS1010: Programming Methodology
Chapter 17 Recursion.
Data Structures: A Pseudocode Approach with C
Data Structures Using C++
ABC Technology Project
1 Undirected Breadth First Search F A BCG DE H 2 F A BCG DE H Queue: A get Undiscovered Fringe Finished Active 0 distance from A visit(A)
VOORBLAD.
Review Pseudo Code Basic elements of Pseudo code
1 Breadth First Search s s Undiscovered Discovered Finished Queue: s Top of queue 2 1 Shortest path from s.
“Start-to-End” Simulations Imaging of Single Molecules at the European XFEL Igor Zagorodnov S2E Meeting DESY 10. February 2014.
Squares and Square Root WALK. Solve each problem REVIEW:
YO-YO Leader Election Lijie Wang
© 2012 National Heart Foundation of Australia. Slide 2.
Lets play bingo!!. Calculate: MEAN Calculate: MEDIAN
Copyright © 2013 by John Wiley & Sons. All rights reserved. HOW TO CREATE LINKED LISTS FROM SCRATCH CHAPTER Slides by Rick Giles 16 Only Linked List Part.
Understanding Generalist Practice, 5e, Kirst-Ashman/Hull
Chapter 5 Test Review Sections 5-1 through 5-4.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 7: Recursion Java Software Structures: Designing and Using.
GG Consulting, LLC I-SUITE. Source: TEA SHARS Frequently asked questions 2.
Addition 1’s to 20.
25 seconds left…...
Chapter 8: Recursion Java Software Solutions
Januar MDMDFSSMDMDFSSS
Week 1.
Binary Search Trees Ravi Chugh March 28, Review: Linked Lists Goal: Program that keeps track of friends Problem: Arrays have fixed length Solution:
We will resume in: 25 Minutes.
Depth-First and Breadth-First Search CS 5010 Program Design Paradigms “Bootcamp” Lesson 9.2 TexPoint fonts used in EMF. Read the TexPoint manual before.
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
Starting Out with Java: From Control Structures through Objects
CS203 Lecture 15.
A SMALL TRUTH TO MAKE LIFE 100%
PSSA Preparation.
Immunobiology: The Immune System in Health & Disease Sixth Edition
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Essential Cell Biology
How Cells Obtain Energy from Food
Immunobiology: The Immune System in Health & Disease Sixth Edition
Towers of Hanoi
Traktor- og motorlære Kapitel 1 1 Kopiering forbudt.
Recursion Chapter 5.
Recursion Chapter 7. Spring 2010CS 2252 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn how.
Fall 2007CS 2251 Recursion Chapter 7. Fall 2007CS 2252 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method.
Recursion Chapter 7.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Recursion Chapter 5.
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
Recursion Topic 5.
Java Software Structures: John Lewis & Joseph Chase
7.4 Problem Solving with Recursion
Chapter 12 Recursion (methods calling themselves)
Presentation transcript:

Chapter Objectives To learn about recursive data structures and recursive methods for a LinkedList class To understand how to use recursion to solve the Towers of Hanoi problem To understand how to use recursion to process two- dimensional images To learn how to apply backtracking to solve search problems such as finding a path through a maze CS340 1

Recursive Data Structures CS340 2

Recursive Data Structures Data structures that are defined recursively – with another version of itself as a component Linked lists and trees Recursive methods provide a natural mechanism for processing recursive data structures CS340 3

Recursive Definition of a Linked List Each node of a linked list References another linked list That references another linked list … The last node references an empty list CS340 4

Class LinkedListRec LinkedListRec implements several list operations using recursive methods public class LinkedListRec { private Node head; // inner class Node here // (from chapter 2) } CS340 5

Recursive size Method CS340 6

Recursive toString Method CS340 7

Recursive replace Method 8

Recursive add Method 9

Recursive remove Method CS340 10

Recursive remove Method (cont.) CS340 11

Problem Solving with Recursion CS340 12

Simplified Towers of Hanoi Move the three disks to a different peg, maintaining their order (largest disk on bottom, smallest on top, etc.) Only the top disk on a peg can be moved to another peg A larger disk cannot be placed on top of a smaller disk CS340 13

Towers of Hanoi CS340 14

Algorithm for Towers of Hanoi CS Solution to Two-Disk Problem: Move Three Disks from Peg L to Peg R 1. Move the top two disks from peg L to peg M. 2. Move the bottom disk from peg L to peg R. 3. Move the top two disks from peg M to peg R.

Algorithm for Towers of Hanoi (cont.) CS Solution to Two-Disk Problem: Move Top Two Disks from Peg M to Peg R 1. Move the top disk from peg M to peg L. 2. Move the bottom disk from peg M to peg R. 3. Move the top disk from peg L to peg R.

Algorithm for Towers of Hanoi (cont.) CS Solution to Four-Disk Problem: Move Four Disks from Peg L to Peg R 1. Move the top three disks from peg L to peg M. 2. Move the bottom disk from peg L to peg R. 3. Move the top three disks from peg M to peg R.

Recursive Algorithm for Towers of Hanoi CS if n is 1 move disk 1 (the smallest disk) from the starting peg to the destination peg else move the top n – 1 disks from the starting peg to the temporary peg (neither starting nor destination peg) move disk n (the disk at the bottom) from the starting peg to the destination peg move the top n – 1 disks from the temporary peg to the destination peg

Recursive pseudo code for towers of Hanoi problem FUNCTION MoveTower(disk, source, dest, spare): IF disk == 0, THEN: move disk from source to dest ELSE: MoveTower(disk - 1, source, spare, dest) move disk from source to dest MoveTower(disk - 1, spare, dest, source) END IF CS340 19

Recursive Algorithm for Towers of Hanoi (cont.) CS340 20

Backtracking CS340 21

Backtracking Implementing a systematic trial and error search for a solution Ex. find a path through a maze Backtracking is a systematic, nonrepetitive approach to trying alternative paths and eliminating them if they dont work CS340 22

Backtracking (cont.) Recursion allows you to implement backtracking Each activation frame is used to remember the choice that was made at that particular decision point CS340 23

Finding a Path through a Maze Problem Use backtracking to find a display the path through a maze From each point in a maze, you can move to the next cell in a horizontal or vertical direction, if the cell is not blocked CS340 24

Finding a Path through a Maze (cont.) Analysis The maze will consist of a grid of colored cells The starting point is at the top left corner (0,0) The exit point is at the bottom right corner (getNCols() – 1, getNRow -1) All cells on the path will be BACKGROUND color All cells that represent barriers will be ABNORMAL color Cells that we have visited will be TEMPORARY color If we find a path, all cells on the path will be set to PATH color CS340 25

Recursive Algorithm for Finding Maze Path CS if the current cell is outside the maze return false (you are out of bounds) else if the current cell is part of the barrier or has already been visited return false (you are off the path or in a cycle) else if the current cell is the maze exit recolor it to the path color and return true (you have successfully completed the maze) else // Try to find a path from the current path to the exit: mark the current cell as on the path by recoloring it to the path color for each neighbor of the current cell if a path exists from the neighbor to the maze exit return true // No neighbor of the current cell is on the path recolor the current cell to the temporary color (visited) and return false

Testing Test for a variety of test cases: Mazes that can be solved Mazes that can't be solved A maze with no barrier cells A maze with a single barrier cell at the exit point CS340 27