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

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

  • View
    214

  • Download
    1

Embed Size (px)

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

  • Slide 1
  • Lecture 5CSE 140 - Intro to Cognitive Science1 Algorithmic Thinking III
  • Slide 2
  • Lecture 5CSE 140 - Intro to Cognitive Science2 Reprise.
  • Slide 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.
  • Slide 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
  • Slide 5
  • Lecture 5CSE 140 - Intro to Cognitive Science5 Flood Fill on a Standard Computer Lets 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)
  • Slide 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
  • Slide 7
  • Lecture 5CSE 140 - Intro to Cognitive Science7 Flood Fill in Parallel: The First Point
  • Slide 8
  • Lecture 5CSE 140 - Intro to Cognitive Science8 Flood Fill in Parallel: The First Step
  • Slide 9
  • Lecture 5CSE 140 - Intro to Cognitive Science9 Flood Fill in Parallel: Called on X+1
  • Slide 10
  • Lecture 5CSE 140 - Intro to Cognitive Science10 Flood Fill in Parallel: First Step of x+1
  • Slide 11
  • Lecture 5CSE 140 - Intro to Cognitive Science11 Flood Fill in Parallel: Called on y-1
  • Slide 12
  • Lecture 5CSE 140 - Intro to Cognitive Science12 Flood Fill in Parallel: First step of y-1
  • Slide 13
  • Lecture 5CSE 140 - Intro to Cognitive Science13 Flood Fill in Parallel: Called on x-1
  • Slide 14
  • Lecture 5CSE 140 - Intro to Cognitive Science14 Flood Fill in Parallel: First step of x-1
  • Slide 15
  • Lecture 5CSE 140 - Intro to Cognitive Science15 Flood Fill in Parallel: Called on y+1
  • Slide 16
  • Lecture 5CSE 140 - Intro to Cognitive Science16 Flood Fill in Parallel: First step of y+1
  • Slide 17
  • Lecture 5CSE 140 - Intro to Cognitive Science17 Flood Fill in Parallel : First Pixel
  • Slide 18
  • Lecture 5CSE 140 - Intro to Cognitive Science18 Flood Fill in Parallel: First Step
  • Slide 19
  • Lecture 5CSE 140 - Intro to Cognitive Science19 Flood Fill in Parallel: Second Step
  • Slide 20
  • Lecture 5CSE 140 - Intro to Cognitive Science20 Flood Fill in Parallel: Third Step
  • Slide 21
  • Lecture 5CSE 140 - Intro to Cognitive Science21 Flood Fill in Parallel: Fourth Step
  • Slide 22
  • Lecture 5CSE 140 - Intro to Cognitive Science22 Flood Fill in Parallel: 5 th Step
  • Slide 23
  • Lecture 5CSE 140 - Intro to Cognitive Science23 Flood Fill in Parallel: 6 th Step
  • Slide 24
  • Lecture 5CSE 140 - Intro to Cognitive Science24 Flood Fill in Parallel: 7 th Step
  • Slide 25
  • Lecture 5CSE 140 - Intro to Cognitive Science25 Flood Fill in Parallel: 8 th Step
  • Slide 26
  • Lecture 5CSE 140 - Intro to Cognitive Science26 Flood Fill in Parallel: 9 th Step
  • Slide 27
  • Lecture 5CSE 140 - Intro to Cognitive Science27 Flood Fill in Parallel: 10 th Step
  • Slide 28
  • Lecture 5CSE 140 - Intro to Cognitive Science28 Flood Fill in Parallel: 11 th Step
  • Slide 29
  • Lecture 5CSE 140 - Intro to Cognitive Science29 Flood Fill in Parallel: 12 th Step
  • Slide 30
  • Lecture 5CSE 140 - Intro to Cognitive Science30 Flood Fill in Parallel: 13 th Step
  • Slide 31
  • Lecture 5CSE 140 - Intro to Cognitive Science31 Trees I think that I shall never see..
  • Slide 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 ..
  • Slide 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.
  • Slide 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.
  • Slide 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 players opponent cannot move because no legal move is possible. The players opponent has no pawns left.
  • Slide 36
  • Lecture 5CSE 140 - Intro to Cognitive Science36 Hexapawn: Three Possible First Moves A game tree represents each option
  • Slide 37
  • Lecture 5CSE 140 - Intro to Cognitive Science37 Hexapawn: The Game Tree for 2 Moves
  • Slide 38
  • Lecture 5CSE 140 - Intro to Cognitive Science38 Hexapawn: The Game Tree for 2 Moves
  • Slide 39
  • Lecture 5CSE 140 - Intro to Cognitive Science39 Hexapawn: The Game Tree for 2 Moves
  • Slide 40
  • Lecture 5CSE 140 - Intro to Cognitive Science40 How Good Is This Response? ?
  • Slide 41
  • Lecture 5CSE 140 - Intro to Cognitive Science41 Hexapawn: Third Move Options I WIN!
  • Slide 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.
  • Slide 43
  • Lecture 5CSE 140 - Intro to Cognitive Science43 Hexapawn: Third Move Options II WIN! X Black to win!
  • Slide 44
  • Lecture 5CSE 140 - Intro to Cognitive Science44 Hexapawn: Third Move Options II WIN! X Black to win! XX