Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Lecture 5CSE 140 - Intro to Cognitive Science1 Algorithmic Thinking III."— Presentation transcript:

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

2 Lecture 5CSE 140 - Intro to Cognitive Science2 Reprise….

3 Lecture 5CSE 140 - Intro to Cognitive Science3 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.

4 Lecture 5CSE 140 - Intro to Cognitive Science4 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,yx,yx+1,y x,y-1

5 Lecture 5CSE 140 - Intro to Cognitive Science5 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)

6 Lecture 5CSE 140 - Intro to Cognitive Science6 On a “Serial” Computer, Recursion Is “Depth First” Operations after a recursive call are remembered until the recursive call is finished x+1,y

7 Lecture 5CSE 140 - Intro to Cognitive Science7 Flood Fill in Parallel: The First Point

8 Lecture 5CSE 140 - Intro to Cognitive Science8 Flood Fill in Parallel: The First Step

9 Lecture 5CSE 140 - Intro to Cognitive Science9 Flood Fill in Parallel: Called on X+1

10 Lecture 5CSE 140 - Intro to Cognitive Science10 Flood Fill in Parallel: First Step of x+1

11 Lecture 5CSE 140 - Intro to Cognitive Science11 Flood Fill in Parallel: Called on y-1

12 Lecture 5CSE 140 - Intro to Cognitive Science12 Flood Fill in Parallel: First step of y-1

13 Lecture 5CSE 140 - Intro to Cognitive Science13 Flood Fill in Parallel: Called on x-1

14 Lecture 5CSE 140 - Intro to Cognitive Science14 Flood Fill in Parallel: First step of x-1

15 Lecture 5CSE 140 - Intro to Cognitive Science15 Flood Fill in Parallel: Called on y+1

16 Lecture 5CSE 140 - Intro to Cognitive Science16 Flood Fill in Parallel: First step of y+1

17 Lecture 5CSE 140 - Intro to Cognitive Science17 Flood Fill in Parallel : First Pixel

18 Lecture 5CSE 140 - Intro to Cognitive Science18 Flood Fill in Parallel: First Step

19 Lecture 5CSE 140 - Intro to Cognitive Science19 Flood Fill in Parallel: Second Step

20 Lecture 5CSE 140 - Intro to Cognitive Science20 Flood Fill in Parallel: Third Step

21 Lecture 5CSE 140 - Intro to Cognitive Science21 Flood Fill in Parallel: Fourth Step

22 Lecture 5CSE 140 - Intro to Cognitive Science22 Flood Fill in Parallel: 5 th Step

23 Lecture 5CSE 140 - Intro to Cognitive Science23 Flood Fill in Parallel: 6 th Step

24 Lecture 5CSE 140 - Intro to Cognitive Science24 Flood Fill in Parallel: 7 th Step

25 Lecture 5CSE 140 - Intro to Cognitive Science25 Flood Fill in Parallel: 8 th Step

26 Lecture 5CSE 140 - Intro to Cognitive Science26 Flood Fill in Parallel: 9 th Step

27 Lecture 5CSE 140 - Intro to Cognitive Science27 Flood Fill in Parallel: 10 th Step

28 Lecture 5CSE 140 - Intro to Cognitive Science28 Flood Fill in Parallel: 11 th Step

29 Lecture 5CSE 140 - Intro to Cognitive Science29 Flood Fill in Parallel: 12 th Step

30 Lecture 5CSE 140 - Intro to Cognitive Science30 Flood Fill in Parallel: 13 th Step

31 Lecture 5CSE 140 - Intro to Cognitive Science31 Trees I think that I shall never see…..

32 Lecture 5CSE 140 - Intro to Cognitive Science32 Trees  Trees are one of the most important data structures available. We can use trees to represent any sort of hierarchical information: Flightless Flying Animal Mammal Bird Penguin Ostrich PigeonSparrow Dog ….. …

33 Lecture 5CSE 140 - Intro to Cognitive Science33 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.

34 Lecture 5CSE 140 - Intro to Cognitive Science34 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.

35 Lecture 5CSE 140 - Intro to Cognitive Science35 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.

36 Lecture 5CSE 140 - Intro to Cognitive Science36 Hexapawn: Three Possible First Moves  A game tree represents each option           

37 Lecture 5CSE 140 - Intro to Cognitive Science37 Hexapawn: The Game Tree for 2 Moves                    

38 Lecture 5CSE 140 - Intro to Cognitive Science38 Hexapawn: The Game Tree for 2 Moves                       

39 Lecture 5CSE 140 - Intro to Cognitive Science39 Hexapawn: The Game Tree for 2 Moves                                

40 Lecture 5CSE 140 - Intro to Cognitive Science40 How Good Is This Response?                                 ?

41 Lecture 5CSE 140 - Intro to Cognitive Science41 Hexapawn: Third Move Options I             WIN!

42 Lecture 5CSE 140 - Intro to Cognitive Science42 Hexapawn: Third Move Options I             WIN! X From the Game Tree analysis, we learn that black must take the pawn.

43 Lecture 5CSE 140 - Intro to Cognitive Science43 Hexapawn: Third Move Options II             WIN! X                Black to win!

44 Lecture 5CSE 140 - Intro to Cognitive Science44 Hexapawn: Third Move Options II             WIN! X                Black to win! XX


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

Similar presentations


Ads by Google