what does the acronym dfs stand for? explain the dfs

68
QUIZ: Searching for paths in a graph 1 What does the acronym DFS stand for? Explain the DFS algorithm in your own words.

Upload: others

Post on 18-Feb-2022

13 views

Category:

Documents


0 download

TRANSCRIPT

QUIZ: Searching for paths in a

graph

1

• What does the acronym DFS stand for?

• Explain the DFS algorithm in your own words.

QUIZ: Searching for paths in a

graph

2

• What does the acronym DFS stand for?

• Explain the DFS algorithm in your own words.

“We go to the deepest branch, examining all the

paths beginning at the child node, before we come

back to examine the alternate paths that begin at

the sibling.”

QUIZ: Searching for paths in a

graph

3

Find a path from Chicago to Dallas using the DFS

algorithm. Show the stack and node markings at every step! (Nodes are pushed on the stack in alphabetical order)

4

5

9.1 OO Methodology

OO Design

A problem-solving methodology that produces a solution to a problem in terms of self-contained entities called objects

Object

A thing or entity that makes sense within the context of the problem

E.g., a student, a car, the time, the date

6

Object-Oriented Design

World View of OOD

Problems are solved by

– isolating the objects in a problem,

– determining their properties and actions

(responsibilities), and

– letting the objects collaborate to solve a

problem

What? Say again!

7

Object-Oriented Example

You and your friend are making dinner

Objects: you, friend, dinner, steaks, potatoes, lettuce, butter, grill, frying pan, pot, …

Classes:

• you and friend are People – People have name, eye color, …

– People can shop, cook, …

• steaks, potatoes, and butter are Ingredients – Ingredients have weights, costs, quantities, …

– Ingredients can be bought at grocery store, carried home, cooked, …

8

Object-Oriented Example

You and your friend are making dinner

Instance of a class:

• you and friend are instances of class People, – you each have your own name and eye color

– you each can shop and cook

• steaks and potatoes are instances of class Ingredients – they each have their own weight, cost and quantity

– they each can be bought, carried, cooked

Objects collaborate to make dinner!

9

Object-Oriented Lingo

Class = description of a group of similar objects

Object = instance of a class = concrete example of an object in the class

Classes contain:

• Properties (attributes) (name, eye color) • Behaviors (actions, responsibilities) (shop, cook)

Method = A named algorithm that defines behavior (shop, cook)

• Python: my_list.append(42)

10

Two types of design

Top-Down Design

decomposes problems into tasks

Object-Oriented Design

decomposes problems into

collaborating objects

Yes, but how?

QUIZ

What is the “strategic” difference

between procedural design and

OOD?

11

“Bottom-up” example

Gathering wood-chips!

• How would we do it top-down?

(i.e. procedural)

• Bottom-up: ask termites for help!

12

Not in textbook

“Bottom-up” example

What termites do:

• Search for wood-chip, pick it up

• Search for another

• Drop first next to second

• Repeat!

13

Not in textbook

Evolution of clusters

14

Initial random

distribution of

chips (yellow)

and termites

(red)

Not in textbook

Evolution of clusters

15

Follow the centers

of mass of these

last two piles!

Not in textbook

Evolution of clusters

16

A “dynamical equilibrium” is reached; periodically,

small “islands” and “peninsulas” form, only to be

reabsorbed in the central pile.

Not in textbook

Implementation: MIT StarLogo

(free to download!)

17

Not in textbook

Recommended reading

18

Not in textbook

19

OOD Steps

– isolate the real-world objects in the problem

– abstract the objects with like properties into groups (classes)

– determine the responsibilities of the group in interacting with other groups

20

OOD Example

Think of design as a mapping from real

world objects to classes of objects

birth

date

marriage

date

dog's

birth date

Date class

21

After the design phase comes

the implementation phase:

class Date

dogBirthdate

myBirthdate

marriageDate

Description Instances

We instantiate

one or more

objects of each

class

22

Going forth … and then back!

23

OOD Example

Date's

Actions in

real world

?

We call an object's interactions

with other objects its

responsibilities

• Create itself

• Know the state of its fields

• Compare itself to another date

• Return a date a number of days since another date

Responsibilities become methods

associated with the classes and objects

class Date

getMonth

getDay

getYear

daysSince

dogBirthdate.getMonth()

dogBirthdate.getDay()

dogBirthdate.getYear()

myBirthdate.getDay()

Etc.

marriageDate.getYear()

Etc.

OOD Example

crtDate.daysSince(myBirthdate)

Etc.

25

This is how Python refers to

the current object in a class,

since the name(s) of the

object(s) is(are) not know at

this time

26

Aren’t we putting the carriage in front of the horses?

27

How do we use the class in a program?

28

29

Your turn!

Create a class Fraction with two integer fields: nume and deno.

The default value of any fraction should be 1/1.

Write methods for:

• initializing nume and deno

• getting nume and deno.

To do for next time:

• Read and take notes Sec. 9.1

• SKIP Sec. 9.2 and 9.3

• Read and take notes Sec. 9.4

• Solve in notebook end-of-chapter

ex. 60 through 64.

30

31

Your turn!

Instantiate two fractions f1 and f2 in the main program:

• f1 is 3/4

• f2 is 5/4

32

Your turn!

Print f1 and f2 “nicely” the main program, using the get

methods. The output should look like this:

f1 is 3/4

f2 is 5/4

Do not confuse the “responsibilities” of real-

life people with the “responsibilities” of the

objects representing those people!

Example: the zoo-keeper

• In real-life, (s)he has the responsibility to feed

the animals, clean the cages, etc.

• In an OO program: zooKeeper.setSSN(), zooKeeper.getListOfAnimalsCaredFor(),

etc.

34

OOD Methodology

Four stages to the decomposition process

– Brainstorming to locate possible classes

– Filtering the classes to find duplicates or

remove unnecessary ones

– Scenarios are tried to be sure we understand

collaborations

– Responsibility algorithms are designed for all

actions that classes must perform

35

OOD Example

Let’s examine the problem-solving process for creating an address list

Brainstorming and filtering

– Circling the nouns and underlining the verbs is a good way to begin

36

OOD Example - Filtering

37

Design tool: CRC Cards

Class-Responsibility-Collaboration cards = notational

device to record information about a class, what is must do

and with whom it must collaborate

We’ll talk about inheritance next time

38

Other useful responsibilities can be added depending on the

application, e.g. sending emails, checking out books from

library, etc.

Convention: class names always

start with uppercase

39

Can you think of any other useful responsibilities?

Hint: think of Python strings.

40

Can you think on other useful responsibilities?

Hint: Think about the list algorithms from Ch.8

41

Responsibility Algorithms

Person Class

Initialize

name.initialize()

Write "Enter phone number; press return."

Get telephone number

Write "Enter email address; press return."

Get email address

Print

name.print()

Write "Telephone number: " + telephoneNumber

Write "Email address: " + emailAddress

Tells name to initialize itself

Tells name to print itself

42

Responsibility Algorithms

Name Class

Initialize

"Enter the first name; press return."

Read firstName

"Enter the last name; press return."

Read lastName

Print

Print "First name: " + firstName

Print "Last name: " + lastName

Is has the same 2

responsibilities as

Person, but the

algorithms are different!

43

Now let’s do this in Python!

Name Class

Initialize

"Enter the first name; press return."

Read firstName

"Enter the last name; press return."

Read lastName

Print

Print "First name: " + firstName

Print "Last name: " + lastName

44

Now let’s do this in Python!

Name Class

Initialize

"Enter the first name; press return."

Read firstName

"Enter the last name; press return."

Read lastName

Print

Print "First name: " + firstName

Print "Last name: " + lastName

45

Now let’s do this in Python!

Name Class

Initialize

"Enter the first name; press return."

Read firstName

"Enter the last name; press return."

Read lastName

Print

Print "First name: " + firstName

Print "Last name: " + lastName

Now add the Print

responsibility (method)

46

Name Class

Initialize

"Enter the first name; press return."

Read firstName

"Enter the last name; press return."

Read lastName

Print

Print "First name: " + firstName

Print "Last name: " + lastName

47

Name Class

Initialize

"Enter the first name; press return."

Read firstName

"Enter the last name; press return."

Read lastName

Print

Print "First name: " + firstName

Print "Last name: " + lastName

In the main program, we

instantiate (create) three

objects of this class

48

Name Class

Initialize

"Enter the first name; press return."

Read firstName

"Enter the last name; press return."

Read lastName

Print

Print "First name: " + firstName

Print "Last name: " + lastName

Now we can instruct the

objects what to do, using

their methods

49

Name Class

Initialize

"Enter the first name; press return."

Read firstName

"Enter the last name; press return."

Read lastName

Print

Print "First name: " + firstName

Print "Last name: " + lastName

Run this program!

50

Name Class

Initialize

"Enter the first name; press return."

Read firstName

"Enter the last name; press return."

Read lastName

Print

Print "First name: " + firstName

Print "Last name: " + lastName

Write the same code for

the friend’s name and the

dog’s name, and run it

again!

51

52

Can we implement the same functionality w/o OO

machinery?

Of course we can!

53

54

So then why bother with objects?

55

Name Class

Initialize

"Enter the first name; press return."

Read firstName

"Enter the last name; press return."

Read lastName

Print

Print "First name: " + firstName

Print "Last name: " + lastName

Classes

56

Name Class

Initialize

"Enter the first name; press return."

Read firstName

"Enter the last name; press return."

Read lastName

Print

Print "First name: " + firstName

Print "Last name: " + lastName

Encapsulation

57

Inheritance

58

Inheritance

email?

59

Conclusion: Inheritance

makes it easy to reuse code!

60

Polymorphism

61

Conclusion: Polymorphism

makes it easy to understand,

evolve and maintain code!

62

Inheritance

A construct that fosters reuse by allowing an application to take an already-tested class and derive a class from it that inherits the properties the application needs

Polymorphism

The ability of a language to have duplicate method names in an inheritance hierarchy and to apply the method that is appropriate for the object to which the method is applied

63

64

Inheritance and Polymorphism

Inheritance and polymorphism work together

How?

They combine to allow the programmer to build

useful hierarchies of classes that can be put into a

library to be reused in different applications

65

OO nitty-gritty: constructors

Source: http://www.tutorialspoint.com/python/python_classes_objects.htm

66

Ethical Issues

Spam, hoaxes and scams

67

Who am I?

I am:

• An advocate of structured

programming

• A recipient of the Turing

award

• The inventor of a famous

graph algorithm that now has

my name.

• Known for my wit. Can

you recall some of my witty

sayings?

• Dutch

Review questions

Answer in notebook!

• 25 – 46

• 73 only Python

• 76 only a. (Python)

68