Lecture 5CSE 140 - Intro to Cognitive Science1 Algorithmic Thinking III.

44
Lecture 5 CSE 140 - Intro to Cognit ive Science 1 Algorithmic Thinking III

Transcript of Lecture 5CSE 140 - Intro to Cognitive Science1 Algorithmic Thinking III.

Page 1: Lecture 5CSE 140 - Intro to Cognitive Science1 Algorithmic Thinking III.

Lecture 5 CSE 140 - Intro to Cognitive Science 1

Algorithmic Thinking III

Page 2: Lecture 5CSE 140 - Intro to Cognitive Science1 Algorithmic Thinking III.

Lecture 5 CSE 140 - Intro to Cognitive Science 2

Reprise….

Page 3: Lecture 5CSE 140 - Intro to Cognitive Science1 Algorithmic Thinking III.

Lecture 5 CSE 140 - Intro to Cognitive Science 3

Recursion

Recursion is the ability of an algorithm to be defined in terms of itself.

This may sound paradoxical, but sometimes we can• define something in terms of a few base cases, where we

just know the answer, • and then build up more complex cases from these simple

``atomic'' cases.

Page 4: Lecture 5CSE 140 - Intro to Cognitive Science1 Algorithmic Thinking III.

Lecture 5 CSE 140 - Intro to Cognitive Science 4

Flood Fill: Recursion on Visual Arrays

1. Define flood(x, y, old_color, new_color) to be

2. if pixel-at(x,y) is old_color then

3. putpixel(x, y, new_color);

4. flood(x + 1, y, old_color, new_color);

5. flood(x, y - 1, old_color, new_color);

6. flood(x - 1, y, old_color, new_color);

7. flood(x, y + 1, old_color, new_color).

x,y+1

x-1,y x,y x+1,y

x,y-1

Page 5: Lecture 5CSE 140 - Intro to Cognitive Science1 Algorithmic Thinking III.

Lecture 5 CSE 140 - Intro to Cognitive Science 5

Flood Fill on a Standard Computer

Let’s look at the behavior of this algorithm using a simulation from a Computer Graphics Class at MIT

(See demo at

http://graphics.lcs.mit.edu/classes/6.837/F00/Lecture04/Slide09.html)

Page 6: Lecture 5CSE 140 - Intro to Cognitive Science1 Algorithmic Thinking III.

Lecture 5 CSE 140 - Intro to Cognitive Science 6

On a “Serial” Computer, Recursion Is “Depth First”

• Operations after a recursive call are remembered until the recursive call is finished

x+1,y

x+1,y

Page 7: Lecture 5CSE 140 - Intro to Cognitive Science1 Algorithmic Thinking III.

Lecture 5 CSE 140 - Intro to Cognitive Science 7

Flood Fill in Parallel: The First Point

Page 8: Lecture 5CSE 140 - Intro to Cognitive Science1 Algorithmic Thinking III.

Lecture 5 CSE 140 - Intro to Cognitive Science 8

Flood Fill in Parallel: The First Step

Page 9: Lecture 5CSE 140 - Intro to Cognitive Science1 Algorithmic Thinking III.

Lecture 5 CSE 140 - Intro to Cognitive Science 9

Flood Fill in Parallel: Called on X+1

Page 10: Lecture 5CSE 140 - Intro to Cognitive Science1 Algorithmic Thinking III.

Lecture 5 CSE 140 - Intro to Cognitive Science 10

Flood Fill in Parallel: First Step of x+1

Page 11: Lecture 5CSE 140 - Intro to Cognitive Science1 Algorithmic Thinking III.

Lecture 5 CSE 140 - Intro to Cognitive Science 11

Flood Fill in Parallel: Called on y-1

Page 12: Lecture 5CSE 140 - Intro to Cognitive Science1 Algorithmic Thinking III.

Lecture 5 CSE 140 - Intro to Cognitive Science 12

Flood Fill in Parallel: First step of y-1

Page 13: Lecture 5CSE 140 - Intro to Cognitive Science1 Algorithmic Thinking III.

Lecture 5 CSE 140 - Intro to Cognitive Science 13

Flood Fill in Parallel: Called on x-1

Page 14: Lecture 5CSE 140 - Intro to Cognitive Science1 Algorithmic Thinking III.

Lecture 5 CSE 140 - Intro to Cognitive Science 14

Flood Fill in Parallel: First step of x-1

Page 15: Lecture 5CSE 140 - Intro to Cognitive Science1 Algorithmic Thinking III.

Lecture 5 CSE 140 - Intro to Cognitive Science 15

Flood Fill in Parallel: Called on y+1

Page 16: Lecture 5CSE 140 - Intro to Cognitive Science1 Algorithmic Thinking III.

Lecture 5 CSE 140 - Intro to Cognitive Science 16

Flood Fill in Parallel: First step of y+1

Page 17: Lecture 5CSE 140 - Intro to Cognitive Science1 Algorithmic Thinking III.

Lecture 5 CSE 140 - Intro to Cognitive Science 17

Flood Fill in Parallel : First Pixel

Page 18: Lecture 5CSE 140 - Intro to Cognitive Science1 Algorithmic Thinking III.

Lecture 5 CSE 140 - Intro to Cognitive Science 18

Flood Fill in Parallel: First Step

Page 19: Lecture 5CSE 140 - Intro to Cognitive Science1 Algorithmic Thinking III.

Lecture 5 CSE 140 - Intro to Cognitive Science 19

Flood Fill in Parallel: Second Step

Page 20: Lecture 5CSE 140 - Intro to Cognitive Science1 Algorithmic Thinking III.

Lecture 5 CSE 140 - Intro to Cognitive Science 20

Flood Fill in Parallel: Third Step

Page 21: Lecture 5CSE 140 - Intro to Cognitive Science1 Algorithmic Thinking III.

Lecture 5 CSE 140 - Intro to Cognitive Science 21

Flood Fill in Parallel: Fourth Step

Page 22: Lecture 5CSE 140 - Intro to Cognitive Science1 Algorithmic Thinking III.

Lecture 5 CSE 140 - Intro to Cognitive Science 22

Flood Fill in Parallel: 5th Step

Page 23: Lecture 5CSE 140 - Intro to Cognitive Science1 Algorithmic Thinking III.

Lecture 5 CSE 140 - Intro to Cognitive Science 23

Flood Fill in Parallel: 6th Step

Page 24: Lecture 5CSE 140 - Intro to Cognitive Science1 Algorithmic Thinking III.

Lecture 5 CSE 140 - Intro to Cognitive Science 24

Flood Fill in Parallel: 7th Step

Page 25: Lecture 5CSE 140 - Intro to Cognitive Science1 Algorithmic Thinking III.

Lecture 5 CSE 140 - Intro to Cognitive Science 25

Flood Fill in Parallel: 8th Step

Page 26: Lecture 5CSE 140 - Intro to Cognitive Science1 Algorithmic Thinking III.

Lecture 5 CSE 140 - Intro to Cognitive Science 26

Flood Fill in Parallel: 9th Step

Page 27: Lecture 5CSE 140 - Intro to Cognitive Science1 Algorithmic Thinking III.

Lecture 5 CSE 140 - Intro to Cognitive Science 27

Flood Fill in Parallel: 10th Step

Page 28: Lecture 5CSE 140 - Intro to Cognitive Science1 Algorithmic Thinking III.

Lecture 5 CSE 140 - Intro to Cognitive Science 28

Flood Fill in Parallel: 11th Step

Page 29: Lecture 5CSE 140 - Intro to Cognitive Science1 Algorithmic Thinking III.

Lecture 5 CSE 140 - Intro to Cognitive Science 29

Flood Fill in Parallel: 12th Step

Page 30: Lecture 5CSE 140 - Intro to Cognitive Science1 Algorithmic Thinking III.

Lecture 5 CSE 140 - Intro to Cognitive Science 30

Flood Fill in Parallel: 13th Step

Page 31: Lecture 5CSE 140 - Intro to Cognitive Science1 Algorithmic Thinking III.

Lecture 5 CSE 140 - Intro to Cognitive Science 31

Trees

I think that I shall never see…..

Page 32: Lecture 5CSE 140 - Intro to Cognitive Science1 Algorithmic Thinking III.

Lecture 5 CSE 140 - Intro to Cognitive Science 32

Trees

Trees are one of the most important data structures available. We can use trees to represent any sort of hierarchical information:

FlightlessFlying

Animal

MammalBird

PenguinOstrichPigeonSparrow

Dog …..

Page 33: Lecture 5CSE 140 - Intro to Cognitive Science1 Algorithmic Thinking III.

Lecture 5 CSE 140 - Intro to Cognitive Science 33

Tree Vocabulary

Trees are organized into:

Nodes: The points in the tree that bear labels; Branches: The edges that connect the nodes; Offspring: A node connected to another node by a

downward path along a branch; The Root: The node at the top of the tree. It is the

offspring of no other node; Leaves: Nodes that have no offspring are leaves.

Page 34: Lecture 5CSE 140 - Intro to Cognitive Science1 Algorithmic Thinking III.

Lecture 5 CSE 140 - Intro to Cognitive Science 34

Example: Game Trees

We can represent a game like chess as a tree: The root is the initial setup of the chess board; all the pieces

are in their start position; The offspring of a node are the board positions one legal move

away from the position represented by the node; The leaves are final game positions (checkmate, draw); A legal game is a path along the nodes of the tree, starting from

the root.

The computer might play chess by representing the above tree and searching it for the best move given the current board position.

Page 35: Lecture 5CSE 140 - Intro to Cognitive Science1 Algorithmic Thinking III.

Lecture 5 CSE 140 - Intro to Cognitive Science 35

Hexapawn: A very Simple Game

Hexapawn is played on a chessboard cut down to 3x3 squares. A player can • move a pawn directly forward one square onto an empty

square or• move a pawn diagonally forward one square, provided that

square contains an opponents pawn. The opponents pawn is removed from the board.

A player wins the game when: • One of that players pawns reaches the far side of the board. • The player’s opponent cannot move because no legal move

is possible. • The player’s opponent has no pawns left.

Page 36: Lecture 5CSE 140 - Intro to Cognitive Science1 Algorithmic Thinking III.

Lecture 5 CSE 140 - Intro to Cognitive Science 36

Hexapawn: Three Possible First Moves

A game tree represents each option

Page 37: Lecture 5CSE 140 - Intro to Cognitive Science1 Algorithmic Thinking III.

Lecture 5 CSE 140 - Intro to Cognitive Science 37

Hexapawn: The Game Tree for 2 Moves

Page 38: Lecture 5CSE 140 - Intro to Cognitive Science1 Algorithmic Thinking III.

Lecture 5 CSE 140 - Intro to Cognitive Science 38

Hexapawn: The Game Tree for 2 Moves

Page 39: Lecture 5CSE 140 - Intro to Cognitive Science1 Algorithmic Thinking III.

Lecture 5 CSE 140 - Intro to Cognitive Science 39

Hexapawn: The Game Tree for 2 Moves

Page 40: Lecture 5CSE 140 - Intro to Cognitive Science1 Algorithmic Thinking III.

Lecture 5 CSE 140 - Intro to Cognitive Science 40

How Good Is This Response?

?

Page 41: Lecture 5CSE 140 - Intro to Cognitive Science1 Algorithmic Thinking III.

Lecture 5 CSE 140 - Intro to Cognitive Science 41

Hexapawn: Third Move Options I

WIN!

Page 42: Lecture 5CSE 140 - Intro to Cognitive Science1 Algorithmic Thinking III.

Lecture 5 CSE 140 - Intro to Cognitive Science 42

Hexapawn: Third Move Options I

WIN!

X From the Game Tree analysis, we learn that black must take the pawn.

Page 43: Lecture 5CSE 140 - Intro to Cognitive Science1 Algorithmic Thinking III.

Lecture 5 CSE 140 - Intro to Cognitive Science 43

Hexapawn: Third Move Options II

WIN!

X

Black to win!

Black to win!

Page 44: Lecture 5CSE 140 - Intro to Cognitive Science1 Algorithmic Thinking III.

Lecture 5 CSE 140 - Intro to Cognitive Science 44

Hexapawn: Third Move Options II

WIN!

X

Black to win!

Black to win!

XX