/
Programming Abstractions Programming Abstractions

Programming Abstractions - PowerPoint Presentation

widengillette
widengillette . @widengillette
Follow
342 views
Uploaded On 2020-06-30

Programming Abstractions - PPT Presentation

Cynthia Lee CS106B Todays topics Previous lectures Introduction to recursion with Factorial Mechanics of recursion looking at the stack frames Classic widelyused CS algorithm example Binary Search ID: 790647

solving maze start return maze solving return start recursion dir stack generating north step south backtracking false solvemaze loop

Share:

Link:

Embed:

Download Presentation from below link

Download The PPT/PDF document "Programming Abstractions" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

Programming Abstractions

Cynthia Lee

CS106B

Slide2

Today’s topics:

Previous lectures:Introduction to recursion with FactorialMechanics of recursion: looking at the stack frames

Classic, widely-used CS algorithm example: Binary Search

Visual example: Boxy “snowflake” fractal

Today:New patterns of recursion application: adding loopsLoops + recursion for generating combinations/permutationsLoops + recursion for recursive backtracking

2

Slide3

Generating combinations/permutations

Slide4

Recursion pattern: generating permutations

Example problems:

Given a deck of cards, output all possible distinct

5-card poker hands

Generate all strings of length NPseudocode of the approach for “generate all strings of length N”:In a loop, do:Choose a character to be the first letter, then recursively generate the rest of the string from the remaining characters

Now let a different character be the first letter, …

Slide5

Backtracking

Maze solving

Slide6

Backtracking

A particular behavior in recursive code where you tentatively explore many options, and recover to the nearest junction when you hit a “dead end”

Similar to generating all permutations/combinations, but as you go along you evaluate each one to determine if it has potential to become successful in the future or not (and then “give up” early if not)

The easiest way to understand this is probably to see literal exploration and dead ends

Slide7

Maze-solving

Θ

Slide8

Maze-solving

Θ

Thinking through the pseudo-code:

From position

Θ

, what does it mean for a step North to be a good idea?

Slide9

Maze-solving

Θ

Thinking through the pseudo-code:

From position

Θ

, what does it mean for a step South to be a good idea?

It means that from position one-step-South-of-

Θ

, there exists some step that is a good idea…

…Recursion!

Slide10

Backtracking template

bool

recursiveFunction

(){Base case test for success: return trueBase case test for failure: return falseLoop over several options for “what to do next”:Tentatively “do” one optionif (recursiveFunction())

return true

That tentative idea didn’t work, so “undo” that option

None of the options we tried in the loop worked, so

return false

Slide11

SolveMaze

codeAdapted from the textbook by Eric Roberts

bool

solveMaze

(

Maze

&

maze,

Point

start)

{

if (maze.isOutside(start)) return true; if (maze.isMarked(start)) return false; maze.markSquare(start); pause(200); for (Direction dir = NORTH; dir <= WEST;

dir

++)

{

if

(!maze.wallExists(start, dir)) { if (solveMaze(maze, adjacentPoint(start, dir))) { return true; } } } maze.unmarkSquare(start); return false;} 

enum Direction = {NORTH, EAST, SOUTH, WEST};

Slide12

Maze-solving

x1

x2

Θ

x3

In what order do we visit these spaces?

x

1, x2, x3

x2, x3, x1

x1, x3, x2

We don’t visit all three

Other/none/more

//order of for loop:

enum

Direction =

{NORTH, EAST, SOUTH, WEST};

Slide13

The stack

Θ

What is the deepest the Stack gets

(number of stack frames) during

the solving of this maze?

Less than 5

5-10

11-20

More than 20

Other/none/more

Heap

Stack

0

Slide14

Contrast: Recursive maze-solving vs. Word ladder

With word ladder, you did breadth-first search

This problem uses

depth-first search

Both are possible for maze-solving!The contrast between these approaches is a theme that you’ll see again and again in your CS career