Game Design Studio 1

advertisement
Graphs, Trees, Objects
UC Santa Cruz
CMPS 10 – Introduction to Computer Science
www.soe.ucsc.edu/classes/cmps010/Spring11
ejw@cs.ucsc.edu
8 April 2011
Class website
 http://www.soe.ucsc.edu/classes/cmps010/Spring11/
 Please write this down, and bookmark it
 Holds:




Syllabus (including homework due dates)
Homework assignment descriptions
Description of course readings
Links to class lecture notes
 The final exam is scheduled for Tuesday, June 7, 8am-11am
 This class will have a final exam. Please plan on this.
UC SANTA CRUZ
Tutoring available
 Learning Support Services (LSS)
 Has tutoring available for students in CMPS 10
 Students meet in small groups, led by a tutor
 Students are eligible for up to one-hour of tutoring per week per
course, and may sign-up for tutoring at
https://eop.sa.ucsc.edu/OTSS/tutorsignup/ beginning April 5th at
10:00am.
 Brett Care - bcare@ucsc.edu is the tutor for CMPS 10 that LSS has
hired
UC SANTA CRUZ
Abstraction and Models
 Converting the real world into data:
 Create a model of the real world
 Represent that model in data
 How do you model the real world?
 Involves a process called abstraction
 Abstraction
physical world
abstraction
model
representation
data
(inside
computer)
 Prerequisite: know your problem or application
 Focus on aspects of the real world that are important to the problem
 Add those elements to your model
 Omit elements of the real world that aren’t relevant
 Implies: the same real world scenario can be modeled in many ways,
depending on the problem at hand
UC SANTA CRUZ
Representing models as data
 Most models can be represented using:
 Basic data types





Integers
Floating point
Boolean
Characters
Strings
 Basic data structures





Arrays
Lists
Stacks/Queues
Trees
Graphs
 Clusters of data
 Modeling data as classes
UC SANTA CRUZ
Modeling a music collection
 Consider your music collection
 There are many songs
 Each song belongs to an album
 OK, OK, I know there are a lot of loose singles these days, but work with me here…
 Each album has a dominant musical style (pop, rock, classical, etc.)
 Say we want to organize this musical collection
 By style, then album, then song
All music
rock/pop
The Fame
Just Dance
Poker Face
Thriller
Beat It
classical
25 Bach Favorites
Tocatta
Alegro
bluegrass
Best Loved Bluegrass
White House Blues
Train 45
UC SANTA CRUZ
Modeling a music collection (cont’d)

This is a hierarchical structure
 Occurs frequently: organizational charts, evolutionary tree in biology, work breakdown
structure in project management,
 databases, filesystems, programming languages

Hierarchical structures are represented using trees
 Elements of a tree can be of any type. Tree of strings, tree of integers, tree of floats, etc.
 For the music collection, the hierarchical structure can be represented as a tree of
strings
 Music_collection is tree of string
All music
rock/pop
The Fame
Just Dance
Poker Face
Thriller
Beat It
classical
25 Bach Favorites
Tocatta
Alegro
bluegrass
Best Loved Bluegrass
White House Blues
Train 45
UC SANTA CRUZ
Tree terminology
 There are some special terms that are used to describe trees
 The elements of a tree are called nodes
 The topmost element is called the root
 An element can have one or more child nodes
 Node “classical” is a child of node “All music”
 Node “Thriller” is a child of node “rock/pop”
 An element with no children is a leaf node
 Every node, except the root, has a parent node
 Node “rock/pop” is the parent of node “Thriller”
root node
All music
rock/pop
The Fame
Just Dance
Poker Face
Thriller
Beat It
classical
25 Bach Favorites
Tocatta
Alegro
bluegrass
Best Loved Bluegrass
White House Blues
Train 45
One of 7 leaf nodes
UC SANTA CRUZ
Tree operations

Trees typically support the following operations
 Insert_child(given_node, contents)
 Adds a new node with contents as a child of given_node
 Example: Insert_child(Node:The Fame, “Paparazzi”)

Creates a new node, under “The Fame”, with contents
“Paparazzi”
The Fame
 Parent(given_node)
 Provides the parent node for given_node
 Example: Parent(Node:Poker Face) is Node:The Fame
 Leftmost_child(given_node)
Just Dance
Poker Face
Paparazzi
 Provides the leftmost child of the given_node
 Example: Leftmost_child(Node:The Fame) is “Just
Dance”
 Right_sibling(given_node)
 Provides the next sibling to the right, or null if there is
none
 Example: Right_sibling(Node:Poker Face) is
Node:Paparazzi
 Example: Right_sibling(Node:Paparazzi) is null
 Delete(given_node)
 Deletes the given node and all children
 Delete(Node:Poker Face) deletes just Poker Face
 Delete(Node:The Fame) deletes The Fame, Just Dance,
Poker Face, and Paparazzi
The Fame
Just Dance
Poker Face
Right_sibling
Paparazzi
Right_sibling
Can navigate through a tree using
leftmost_child, right_sibling, and
parent!
UC SANTA CRUZ
Graph
 The physical world contains many networks




Towns connected by (rail)roads
Cities connected by airline flights
Pumping stations connected by water pipes
Houses and businesses connected to power stations by electrical
wires
A portion of United Airline’s Flight Route Network
content.united.com/ual/asset/UAL_NA_Map.pdf
UC SANTA CRUZ
Graph
 A graph is a set of nodes (vertices) connected by edges (arcs)
 Often (but not always) two nodes can be connected by only one edge
 An undirected graph is one where the edges have no directionality (i.e., no
arrows)
 Can represent situations like a road, where cars can go in either direction
 A directed graph (or digraph) is one where the edges are directional (have
edges)
 Can represent situations like a water pipe network, where water typically flows in
one direction
 A node can be any type (string, integer, float, etc.)
 Edges are often labeled with data as well (string, integer, etc.)
An undirected graph with 6 nodes and 7 edges
A directed graph with 3 nodes and 3 edges
en.wikipedia.org/wiki/Graph_(mathematics)
en.wikipedia.org/wiki/Graph_(mathematics)
UC SANTA CRUZ
Graphs in Computer Science
 Graphs are broadly useful in computer science
 Represent internal dependency structure inside software programs
 I.e., which functions/methods call which other ones?
 Represent network information
 How elements of the Internet are connected
 Represent relationships among items
 Dog is-a-kind-of mammal, dog is-a-kind-of pet, humans like pets, etc.
ConceptNet is a network of
common sense knowledge
about the world. Can be used
by software to reason about
items in the world. Open
source (freely available)
csc.media.mit.edu/conceptnet
Fragment of the Concept Net network
csc.media.mit.edu/conceptnet
UC SANTA CRUZ
Grouping data together, treating it as one
 So far, we have focused on real world situations that can be modeled using
combinations of one single basic data type
 Temperature, which is represented using a float
 Song title, represented using a string
 More typical are situations where multiple data items are needed for a
complete representation
 Temperature
 Value: float
 Units: Fahreheit or Celsius or Kelvin
 Time: time & date (when measurement was taken)
 Song
 Title: string
 Artist: string
 Year: integer
 Ideally want to clump these together and deal with them as a whole
UC SANTA CRUZ
Grouping data together: objects
 There are several ways of grouping data together
 Two that will be discussed in this class
 Tables in a database
 More on this when we consider databases
 Object modeling
Example of a database table
www.dwreview.com/Data_mining/DM_models.html
UC SANTA CRUZ
Class & object modeling
 Group a series of related data items together
 Package these up into a class
 A class contains a series of related data items
 A container for data
 Each data item is either a data structure or a basic data type
 A data structure contains a series of basic data types
 A specific example of a class is called an object
 An object is an instance of a class
 Use a class box to visually
depict a class
 This is part of the unified modeling
language (UML), a common way of
visually depicting software
designs…
Class name
Data item 1: data type
Data item 2: data structure of data
type
Data item 3: data type
…
Operations
These are operations that work on the data in the class. They are
super important for object oriented programming, but we’re not
going to talk about them now…
UC SANTA CRUZ
Class & object modeling example
 Consider again a song
 Song
 Title: string
 Artist: string
 Year: integer
 Its class box is
 Song is a class
Song
Title: String
Artist: String
Year: Integer
 It is what a lot of different
individual songs look like
 It represents the set of all songs
 Instances of song are song
objects
 Any individual, specific song will
have all of the data items filled in
 It has been “instantiated”
(an instance of it has been made)
Title: Poker Face
Artist: Lady Gaga
Year: 2008
Title: Video Killed the Radio Star
Artist: The Buggles
Year: 1979
Two instances of song – song objects
UC SANTA CRUZ
Relationships among classes
 Is-a relationship
Clothing
 Sometimes you have situations
where there is a general class of
item, and then there are multiple
distinct subclasses
 Example
 The term “clothing” covers a wide
range of items that people wear.
 Might want to also model pants,
shirts, skirts, dresses, belts, socks,
etc.
 Each of these has specific
measurements and hence would
need to be modeled differently
Brand name: string
Price: float
Color: string
Fabric: string
Pants is-a
Clothing
parent
child
child
Socks is-a
Clothing
Pants
Socks
Waist: integer
Inseam: integer
Size_min: integer
Size_max: integer
 Use a
to visually
depict the is-a relationship
 Also known as subclass
relationship
 Also: parent-child relationship
UC SANTA CRUZ
Children inherit data fields


The children of a subclass relationship inherit
the data fields of all parents
In the example

Pants has








Waist: integer
Inseam: integer
And also
Brand name: string
Price: float
Color: string
Fabric: string
Socks has








Clothing
Size_min: integer
Size_max: integer
And also
Brand name: string
Price: float
Color: string
Fabric: string
Brand name: string
Price: float
Color: string
Fabric: string
Pants is-a
Clothing
parent
child
child
Socks is-a
Clothing
Pants
Socks
Waist: integer
Inseam: integer
Size_min: integer
Size_max: integer
Clothing only has




Brand name: string
Price: float
Color: string
Fabric: string
UC SANTA CRUZ
In-class exercise
 With a neighbor, make a class model of the following:
UC SANTA CRUZ
In-class exercise: answer
 There are several ways to approach this modeling problem
 Here’s one
 Let’s assume we’re modeling vegetables for a supermarket checkout
point of sale use
 In this case, we care about:
 Description of vegetable (for the register receipt)
 Price per pound
Could also just model this as “Vegetable” – there is nothing
particularly pepper-related here
Pepper
Description: string
Price: float
Some instances:
Description: “Green pepper”
Price: 1.59
Description: “Yellow pepper”
Price: 3.19
Description: “Red pepper”
Price: 2.39
UC SANTA CRUZ
In-class exercise: answer #2
 Here’s another modeling approach
 Let’s assume we’re modeling peppers for a cooking application
 In this case, we care about:
 Pepper type (red pepper, green pepper, etc.)
 Color (for presentation)
 Heat (is it a hot pepper?)
Another common basic data type is an enumeration. Using an
Pepper
Type: string
Color: string
Hot: boolean
enumeration, you can list all of the possible values a variable can
take. In this case, could model type as a Pepper_type
enumeration, with possible values of Red, Green,Yellow. (A
similar approach could be used for colors, using a Color_type
enumeration, with Red, Green, and Yellow values). Instead, for
this example, we use a string to hold the values “Red”,
“Green”, “Yellow”.
Some instances:
Type: “Green pepper”
Color: “Green”
Hot: false
Type: “Yellow pepper”
Color: “Yellow”
Hot: true
Description: “Red pepper”
Color: “Red”
Hot: true
UC SANTA CRUZ
In-class exercise: answer #3
 Here’s another modeling approach
 Let’s assume we’re modeling peppers for a cooking application
 But, we also have other vegetables we’re interested in modeling
 In this case, we care about:
 What is specific about peppers that is different from other vegetables
 All vegetables have a type, and a color
Vegetable
 Only peppers have a heat
Type: string
Color: string
Some Pepper instances:
Type: “Green pepper”
Color: “Green”
Hot: false
Type: “Yellow pepper”
Color: “Yellow”
Hot: true
Some Vegetable instances:
Type: “Eggplant”
Color: “Purple”
Pepper
Hot: boolean
Type: “Carrot”
Color: “Orange”
UC SANTA CRUZ
New exercise
 Model the following as a class
 Assume it’s for a graphic design application, so we want to model the
length, whether it is sharp, and color
Flickr: stevendepolo
UC SANTA CRUZ
In-class exercise: answer
 Represent:
 Length as a float (since the length could be a fraction of an inch, or
centimeter)
 Color as a string
 Could also be an enumeration
 Sharp as a boolean
 True means sharp
Pencil
Length: float
Color: string
Sharp: boolean
Some instances:
Length: 5.25
Color: “pink”
Sharp: true
Length: 3.125
Color: “sky blue”
Sharp: false
Length: 4.75
Color: “yellow”
Sharp: true
UC SANTA CRUZ
New exercise
 Model the following situation using classes
 Assume this is also for a graphic design use, so we’re interested in pencil vs
pen vs highlighter, color, sharpness, and type of tip (ball, chisel)
Flickr: calliope
UC SANTA CRUZ
In-class exercise
 For this situation, need to use multiple classes, and inheritance
 One class to model “drawing device”
 Used for common qualities, such as color
 Subclasses for specific qualities
 Pencil: sharpness
 Pen: tip type
 Highlighter: tip type
Drawing Device
Color: string
Pencil instance:
Color: “grey”
Sharp: true
Pen instance:
Pencil
Sharp: boolean
Pen
Tip: string
Highlighter
Tip: string
Color: “red”
Tip: “ball”
Highlighter instance:
Color: “yellow”
Tip: “chisel”
UC SANTA CRUZ
Download