Post on 26-Dec-2015
Lecture 5 CSE 140 - Intro to Cognitive Science 1
Algorithmic Thinking III
Lecture 5 CSE 140 - Intro to Cognitive Science 2
Reprise….
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.
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
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)
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
Lecture 5 CSE 140 - Intro to Cognitive Science 7
Flood Fill in Parallel: The First Point
Lecture 5 CSE 140 - Intro to Cognitive Science 8
Flood Fill in Parallel: The First Step
Lecture 5 CSE 140 - Intro to Cognitive Science 9
Flood Fill in Parallel: Called on X+1
Lecture 5 CSE 140 - Intro to Cognitive Science 10
Flood Fill in Parallel: First Step of x+1
Lecture 5 CSE 140 - Intro to Cognitive Science 11
Flood Fill in Parallel: Called on y-1
Lecture 5 CSE 140 - Intro to Cognitive Science 12
Flood Fill in Parallel: First step of y-1
Lecture 5 CSE 140 - Intro to Cognitive Science 13
Flood Fill in Parallel: Called on x-1
Lecture 5 CSE 140 - Intro to Cognitive Science 14
Flood Fill in Parallel: First step of x-1
Lecture 5 CSE 140 - Intro to Cognitive Science 15
Flood Fill in Parallel: Called on y+1
Lecture 5 CSE 140 - Intro to Cognitive Science 16
Flood Fill in Parallel: First step of y+1
Lecture 5 CSE 140 - Intro to Cognitive Science 17
Flood Fill in Parallel : First Pixel
Lecture 5 CSE 140 - Intro to Cognitive Science 18
Flood Fill in Parallel: First Step
Lecture 5 CSE 140 - Intro to Cognitive Science 19
Flood Fill in Parallel: Second Step
Lecture 5 CSE 140 - Intro to Cognitive Science 20
Flood Fill in Parallel: Third Step
Lecture 5 CSE 140 - Intro to Cognitive Science 21
Flood Fill in Parallel: Fourth Step
Lecture 5 CSE 140 - Intro to Cognitive Science 22
Flood Fill in Parallel: 5th Step
Lecture 5 CSE 140 - Intro to Cognitive Science 23
Flood Fill in Parallel: 6th Step
Lecture 5 CSE 140 - Intro to Cognitive Science 24
Flood Fill in Parallel: 7th Step
Lecture 5 CSE 140 - Intro to Cognitive Science 25
Flood Fill in Parallel: 8th Step
Lecture 5 CSE 140 - Intro to Cognitive Science 26
Flood Fill in Parallel: 9th Step
Lecture 5 CSE 140 - Intro to Cognitive Science 27
Flood Fill in Parallel: 10th Step
Lecture 5 CSE 140 - Intro to Cognitive Science 28
Flood Fill in Parallel: 11th Step
Lecture 5 CSE 140 - Intro to Cognitive Science 29
Flood Fill in Parallel: 12th Step
Lecture 5 CSE 140 - Intro to Cognitive Science 30
Flood Fill in Parallel: 13th Step
Lecture 5 CSE 140 - Intro to Cognitive Science 31
Trees
I think that I shall never see…..
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 …..
…
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.
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.
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.
Lecture 5 CSE 140 - Intro to Cognitive Science 36
Hexapawn: Three Possible First Moves
A game tree represents each option
Lecture 5 CSE 140 - Intro to Cognitive Science 37
Hexapawn: The Game Tree for 2 Moves
Lecture 5 CSE 140 - Intro to Cognitive Science 38
Hexapawn: The Game Tree for 2 Moves
Lecture 5 CSE 140 - Intro to Cognitive Science 39
Hexapawn: The Game Tree for 2 Moves
Lecture 5 CSE 140 - Intro to Cognitive Science 40
How Good Is This Response?
?
Lecture 5 CSE 140 - Intro to Cognitive Science 41
Hexapawn: Third Move Options I
WIN!
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.
Lecture 5 CSE 140 - Intro to Cognitive Science 43
Hexapawn: Third Move Options II
WIN!
X
Black to win!
Black to win!
Lecture 5 CSE 140 - Intro to Cognitive Science 44
Hexapawn: Third Move Options II
WIN!
X
Black to win!
Black to win!
XX