Graphs, Trees, Objects UC Santa Cruz CMPS 10 – Introduction to Computer Science [email protected] 8...

26
Graphs, Trees, Objects UC Santa Cruz CMPS 10 – Introduction to Computer Science www.soe.ucsc.edu/classes/cmps010/Spring11 [email protected] 8 April 2011
  • date post

    22-Dec-2015
  • Category

    Documents

  • view

    222
  • download

    1

Transcript of Graphs, Trees, Objects UC Santa Cruz CMPS 10 – Introduction to Computer Science [email protected] 8...

Page 1: Graphs, Trees, Objects UC Santa Cruz CMPS 10 – Introduction to Computer Science  ejw@cs.ucsc.edu 8 April 2011.

Graphs, Trees, Objects

UC Santa CruzCMPS 10 – Introduction to Computer Sciencewww.soe.ucsc.edu/classes/cmps010/[email protected] April 2011

Page 2: Graphs, Trees, Objects UC Santa Cruz CMPS 10 – Introduction to Computer Science  ejw@cs.ucsc.edu 8 April 2011.

UC SANTA CRUZ

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.

Page 3: Graphs, Trees, Objects UC Santa Cruz CMPS 10 – Introduction to Computer Science  ejw@cs.ucsc.edu 8 April 2011.

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 - [email protected] is the tutor for CMPS 10 that LSS has hired

Page 4: Graphs, Trees, Objects UC Santa Cruz CMPS 10 – Introduction to Computer Science  ejw@cs.ucsc.edu 8 April 2011.

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 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

physical world

model

data (inside

computer)

abstraction

representation

Page 5: Graphs, Trees, Objects UC Santa Cruz CMPS 10 – Introduction to Computer Science  ejw@cs.ucsc.edu 8 April 2011.

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

Page 6: Graphs, Trees, Objects UC Santa Cruz CMPS 10 – Introduction to Computer Science  ejw@cs.ucsc.edu 8 April 2011.

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

classical

bluegrass

The Fame

Thriller

25 Bach Favorites

Best Loved Bluegrass

Tocatta AlegroJust Dance

Poker Face

Beat It

White House Blues

Train 45

Page 7: Graphs, Trees, Objects UC Santa Cruz CMPS 10 – Introduction to Computer Science  ejw@cs.ucsc.edu 8 April 2011.

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

classical

bluegrass

The Fame

Thriller

25 Bach Favorites

Best Loved Bluegrass

Tocatta AlegroJust Dance

Poker Face

Beat It

White House Blues

Train 45

Page 8: Graphs, Trees, Objects UC Santa Cruz CMPS 10 – Introduction to Computer Science  ejw@cs.ucsc.edu 8 April 2011.

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”

All music

rock/pop

classical

bluegrass

The Fame

Thriller

25 Bach Favorites

Best Loved Bluegrass

Tocatta AlegroJust Dance

Poker Face

Beat It

White House Blues

Train 45

root node

One of 7 leaf nodes

Page 9: Graphs, Trees, Objects UC Santa Cruz CMPS 10 – Introduction to Computer Science  ejw@cs.ucsc.edu 8 April 2011.

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”

Parent(given_node) Provides the parent node for given_node Example: Parent(Node:Poker Face) is Node:The

Fame Leftmost_child(given_node)

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

Paparazzi

The Fame

Just Dance

Poker Face

Paparazzi

Leftm

ost_

ch

ild

Right_sibling

Right_sibling

Can navigate through a tree using leftmost_child, right_sibling, and parent!

Parent

Page 10: Graphs, Trees, Objects UC Santa Cruz CMPS 10 – Introduction to Computer Science  ejw@cs.ucsc.edu 8 April 2011.

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 Networkcontent.united.com/ual/asset/UAL_NA_Map.pdf

Page 11: Graphs, Trees, Objects UC Santa Cruz CMPS 10 – Introduction to Computer Science  ejw@cs.ucsc.edu 8 April 2011.

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 edgesen.wikipedia.org/wiki/Graph_(mathematics)

A directed graph with 3 nodes and 3 edgesen.wikipedia.org/wiki/Graph_(mathematics)

Page 12: Graphs, Trees, Objects UC Santa Cruz CMPS 10 – Introduction to Computer Science  ejw@cs.ucsc.edu 8 April 2011.

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.

Fragment of the Concept Net networkcsc.media.mit.edu/conceptnet

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

Page 13: Graphs, Trees, Objects UC Santa Cruz CMPS 10 – Introduction to Computer Science  ejw@cs.ucsc.edu 8 April 2011.

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

Page 14: Graphs, Trees, Objects UC Santa Cruz CMPS 10 – Introduction to Computer Science  ejw@cs.ucsc.edu 8 April 2011.

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 tablewww.dwreview.com/Data_mining/DM_models.html

Page 15: Graphs, Trees, Objects UC Santa Cruz CMPS 10 – Introduction to Computer Science  ejw@cs.ucsc.edu 8 April 2011.

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 ofvisually depicting softwaredesigns…

Class nameData item 1: data type

Data item 2: data structure of data typeData item 3: data type…OperationsThese 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…

Page 16: Graphs, Trees, Objects UC Santa Cruz CMPS 10 – Introduction to Computer Science  ejw@cs.ucsc.edu 8 April 2011.

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

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)

Song

Title: String

Artist: String

Year: Integer

Title: Video Killed the Radio StarArtist: The BugglesYear: 1979

Title: Poker FaceArtist: Lady GagaYear: 2008

Two instances of song – song objects

Page 17: Graphs, Trees, Objects UC Santa Cruz CMPS 10 – Introduction to Computer Science  ejw@cs.ucsc.edu 8 April 2011.

UC SANTA CRUZ

Relationships among classes Is-a relationship

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

Use a to visually depict the is-a relationship

Also known as subclass relationship

Also: parent-child relationship

Clothing

Brand name: stringPrice: floatColor: stringFabric: string

Pants

Waist: integerInseam: integer

Socks

Size_min: integerSize_max: integer

Pants is-a Clothing

Socks is-a Clothing

parent

child child

Page 18: Graphs, Trees, Objects UC Santa Cruz CMPS 10 – Introduction to Computer Science  ejw@cs.ucsc.edu 8 April 2011.

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 Size_min: integer Size_max: integer And also Brand name: string Price: float Color: string Fabric: string

Clothing only has Brand name: string Price: float Color: string Fabric: string

Clothing

Brand name: stringPrice: floatColor: stringFabric: string

Pants

Waist: integerInseam: integer

Socks

Size_min: integerSize_max: integer

Pants is-a Clothing

Socks is-a Clothing

parent

child child

Page 19: Graphs, Trees, Objects UC Santa Cruz CMPS 10 – Introduction to Computer Science  ejw@cs.ucsc.edu 8 April 2011.

UC SANTA CRUZ

In-class exercise

With a neighbor, make a class model of the following:

Page 20: Graphs, Trees, Objects UC Santa Cruz CMPS 10 – Introduction to Computer Science  ejw@cs.ucsc.edu 8 April 2011.

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

Pepper

Description: stringPrice: float

Some instances:

Description: “Green pepper”Price: 1.59

Description: “Red pepper”Price: 2.39

Description: “Yellow pepper”Price: 3.19

Could also just model this as “Vegetable” – there is nothing particularly pepper-related here

Page 21: Graphs, Trees, Objects UC Santa Cruz CMPS 10 – Introduction to Computer Science  ejw@cs.ucsc.edu 8 April 2011.

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?)

Pepper

Type: stringColor: stringHot: boolean

Some instances:Type: “Green pepper”Color: “Green”Hot: false

Description: “Red pepper”Color: “Red”Hot: true

Type: “Yellow pepper”Color: “Yellow”Hot: true

Another common basic data type is an enumeration. Using an 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”.

Page 22: Graphs, Trees, Objects UC Santa Cruz CMPS 10 – Introduction to Computer Science  ejw@cs.ucsc.edu 8 April 2011.

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 Only peppers have a heat Vegetab

leType: stringColor: string

Some Pepper instances:

Type: “Green pepper”Color: “Green”Hot: false

Type: “Eggplant”Color: “Purple”

Type: “Yellow pepper”Color: “Yellow”Hot: true

Pepper

Hot: boolean

Some Vegetable instances:

Type: “Carrot”Color: “Orange”

Page 23: Graphs, Trees, Objects UC Santa Cruz CMPS 10 – Introduction to Computer Science  ejw@cs.ucsc.edu 8 April 2011.

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

Page 24: Graphs, Trees, Objects UC Santa Cruz CMPS 10 – Introduction to Computer Science  ejw@cs.ucsc.edu 8 April 2011.

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: floatColor: stringSharp: boolean

Some instances:

Length: 5.25Color: “pink”Sharp: true

Length: 4.75Color: “yellow”Sharp: true

Length: 3.125Color: “sky blue”Sharp: false

Page 25: Graphs, Trees, Objects UC Santa Cruz CMPS 10 – Introduction to Computer Science  ejw@cs.ucsc.edu 8 April 2011.

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

Page 26: Graphs, Trees, Objects UC Santa Cruz CMPS 10 – Introduction to Computer Science  ejw@cs.ucsc.edu 8 April 2011.

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

Color: “red”Tip: “ball”

Pencil

Sharp: boolean

Pen

Tip: string

Highlighter

Tip: stringPen instance:

Highlighter instance:Color: “yellow”Tip: “chisel”