1 representing relations rosen, section 7.3 cs/apma 202 aaron bloomfield

34
1 Representing Relations Rosen, section 7.3 CS/APMA 202 Aaron Bloomfield

Upload: jordan-bryant

Post on 05-Jan-2016

217 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 1 Representing Relations Rosen, section 7.3 CS/APMA 202 Aaron Bloomfield

1

Representing Relations

Rosen, section 7.3

CS/APMA 202

Aaron Bloomfield

Page 2: 1 Representing Relations Rosen, section 7.3 CS/APMA 202 Aaron Bloomfield

2

In this slide set…

• Matrix review

• Two ways to represent relations– Via matrices– Via directed graphs

Page 3: 1 Representing Relations Rosen, section 7.3 CS/APMA 202 Aaron Bloomfield

3

Matrix review

• This is from Rosen, page 201 and 202• We will only be dealing with zero-one matrices

– Each element in the matrix is either a 0 or a 1

• These matrices will be used for Boolean operations– 1 is true, 0 is false

0101

0101

0010

0001

Page 4: 1 Representing Relations Rosen, section 7.3 CS/APMA 202 Aaron Bloomfield

4

Matrix transposition

• Given a matrix M, the transposition of M, denoted Mt, is the matrix obtained by switching the columns and rows of M

• In a “square” matrix, the main diagonal stays unchanged

654

321M

16151413

1211109

8765

4321

M

63

52

41tM

161284

151173

141062

13951

tM

Page 5: 1 Representing Relations Rosen, section 7.3 CS/APMA 202 Aaron Bloomfield

5

Matrix join

• A join of two matrices performs a Boolean OR on each relative entry of the matrices– Matrices must be the same size– Denoted by the or symbol:

0111

1101

0110

0111

0011

1100

0110

0110

0101

0101

0010

0001

Page 6: 1 Representing Relations Rosen, section 7.3 CS/APMA 202 Aaron Bloomfield

6

Matrix meet

• A meet of two matrices performs a Boolean AND on each relative entry of the matrices– Matrices must be the same size– Denoted by the or symbol:

0001

0100

0010

0000

0011

1100

0110

0110

0101

0101

0010

0001

Page 7: 1 Representing Relations Rosen, section 7.3 CS/APMA 202 Aaron Bloomfield

7

Matrix Boolean product

• A Boolean product of two matrices is similar to matrix multiplication

– Instead of the sum of the products, it’s the conjunction (and) of the disjunctions (ors)

– Denoted by the or symbol:

1,44,11,33,11,22,11,11,11,1 **** babababac

1,44,11,33,11,22,11,11,11,1 babababac

1110

1110

0110

0110

0011

1100

0110

0110

0101

0101

0010

0001

0

0011

1100

0110

0110

0101

0101

0010

0001

0011

1100

0110

0110

0101

0101

0010

0001

Page 8: 1 Representing Relations Rosen, section 7.3 CS/APMA 202 Aaron Bloomfield

8

Relations using matrices

• List the elements of sets A and B in a particular order– Order doesn’t matter, but we’ll generally use

ascending order

• Create a matrix ][ ijR mM

Rba

Rbam

ji

ji

ij ),( if 0

),( if 1

Page 9: 1 Representing Relations Rosen, section 7.3 CS/APMA 202 Aaron Bloomfield

9

Relations using matrices

• Consider the relation of who is enrolled in which class– Let A = { Alice, Bob, Claire, Dan }– Let B = { CS101, CS201, CS202 }– R = { (a,b) | person a is enrolled in course b }

110

000

110

001

RM

CS101 CS201 CS202

Alice X

Bob X X

Claire

Dan X X

Page 10: 1 Representing Relations Rosen, section 7.3 CS/APMA 202 Aaron Bloomfield

10

Relations using matrices

• What is it good for?– It is how computers view relations

• A 2-dimensional array

– Very easy to view relationship properties

• We will generally consider relations on a single set– In other words, the domain and co-domain are

the same set– And the matrix is square

Page 11: 1 Representing Relations Rosen, section 7.3 CS/APMA 202 Aaron Bloomfield

11

Reflexivity

• Consider a reflexive relation: ≤– One which every element is related to itself– Let A = { 1, 2, 3, 4, 5 }

10000

11000

11100

11110

11111

M

If the center (main) diagonal is all 1’s, a relation is reflexive

Page 12: 1 Representing Relations Rosen, section 7.3 CS/APMA 202 Aaron Bloomfield

12

Irreflexivity

• Consider a reflexive relation: <– One which every element is not related to itself– Let A = { 1, 2, 3, 4, 5 }

00000

10000

11000

11100

11110

M

If the center (main) diagonal is all 0’s, a relation is irreflexive

Page 13: 1 Representing Relations Rosen, section 7.3 CS/APMA 202 Aaron Bloomfield

13

10010

01001

00001

10000

01101

M

Symmetry

• Consider an symmetric relation R– One which if a is related to b then b is related to a for

all (a,b)– Let A = { 1, 2, 3, 4, 5 }

• If, for every value, it is the equal to the value in its transposed position, then the relation is symmetric

Page 14: 1 Representing Relations Rosen, section 7.3 CS/APMA 202 Aaron Bloomfield

14

00000

10000

11000

11100

11110

M

Asymmetry

• Consider an asymmetric relation: <– One which if a is related to b then b is not related to a

for all (a,b)– Let A = { 1, 2, 3, 4, 5 } • If, for every value and

the value in its transposed position, if they are not both 1, then the relation is asymmetric

• An asymmetric relation must also be irreflexive

• Thus, the main diagonal must be all 0’s

Page 15: 1 Representing Relations Rosen, section 7.3 CS/APMA 202 Aaron Bloomfield

15

10000

11000

11100

11110

11111

M

Antisymmetry

• Consider an antisymmetric relation: ≤– One which if a is related to b then b is not related to a

unless a=b for all (a,b)– Let A = { 1, 2, 3, 4, 5 } • If, for every value

and the value in its transposed position, if they are not both 1, then the relation is antisymmetric

• The center diagonal can have both 1’s and 0’s

Page 16: 1 Representing Relations Rosen, section 7.3 CS/APMA 202 Aaron Bloomfield

16

Transitivity

• Consider an transitive relation: ≤– One which if a is related to b and b is related to c then

a is related to c for all (a,b), (b,c) and (a,c)– Let A = { 1, 2, 3, 4, 5 }

10000

11000

11100

11110

11111

M

• If, for every spot (a,b) and (b,c) that each have a 1, there is a 1 at (a,c), then the relation is transitive

• Matrices don’t show this property easily

Page 17: 1 Representing Relations Rosen, section 7.3 CS/APMA 202 Aaron Bloomfield

1717

2004 IOCCC winners2004 IOCCC winners 2004 winners:2004 winners:

– 2004 2004 anonymousanonymous Rendering of a stroked fontRendering of a stroked font– 2004 2004 arachnidarachnid Curses maze displayer/navigator with only line-of-sightCurses maze displayer/navigator with only line-of-sight

visibilityvisibility– 2004 burley 2004 burley A Poker gameA Poker game– 2004 2004 gavaregavare A ray tracerA ray tracer– 2004 gavin 2004 gavin Mini-OSMini-OS– 2004 hibachi 2004 hibachi A CGI capable HTTP serverA CGI capable HTTP server– 2004 2004 hoylehoyle Curses based polynomial graphing with auto-Curses based polynomial graphing with auto-

scalescale– 2004 jdalbec 2004 jdalbec Conway's look'n'say sequence split into elementsConway's look'n'say sequence split into elements– 2004 kopczynski 2004 kopczynski OCR of 8, 9, 10 and 11OCR of 8, 9, 10 and 11– 2004 2004 newbernnewbern Renders arbitary bitmapped fontsRenders arbitary bitmapped fonts– 2004 omoikane 2004 omoikane A CRC inserterA CRC inserter– 2004 schnitzi 2004 schnitzi Editor animationEditor animation– 2004 sds 2004 sds Space/tab/linefeed steganographySpace/tab/linefeed steganography– 2004 vik1 2004 vik1 X Windows car racing gameX Windows car racing game– 2004 vik2 2004 vik2 Calculates prime numbers using only CPPCalculates prime numbers using only CPP

At At http://www1.us.ioccc.org/years.html#2004http://www1.us.ioccc.org/years.html#2004

Page 18: 1 Representing Relations Rosen, section 7.3 CS/APMA 202 Aaron Bloomfield

19

Combining relations: via Boolean operators

• Example 4 from Rosen, section 7.3

• Let:

• Join:

• Meet:

010

001

101

RM

001

110

101

SM

011

111

101

SRSR MMM

000

000

101

SRSR MMM

Page 19: 1 Representing Relations Rosen, section 7.3 CS/APMA 202 Aaron Bloomfield

20

Combining relations: via relation composition

• Example 4 from Rosen, section 7.3

• Let:

• But why is this the case?

010

001

101

RM

001

110

101

SM

010

101

101

SRRS MMM

a

b

c

d

e

f

d e f g h i

a

b

c

g h i

Page 20: 1 Representing Relations Rosen, section 7.3 CS/APMA 202 Aaron Bloomfield

21

Representing relations using directed graphs

• A directed graph consists of:– A set V of vertices (or nodes)– A set E of edges (or arcs)– If (a, b) is in the relation, then there is an arrow from a to b

• Will generally use relations on a single set• Consider our relation R = { (a,b) | a divides b }

• Old way:1

2

3

4

1

2

3

4

1 2

3 4

Page 21: 1 Representing Relations Rosen, section 7.3 CS/APMA 202 Aaron Bloomfield

22

Reflexivity

• Consider a reflexive relation: ≤– One which every element is related to itself– Let A = { 1, 2, 3, 4, 5 }

If every node has a loop, a relation is reflexive

1 2

5 3

4

Page 22: 1 Representing Relations Rosen, section 7.3 CS/APMA 202 Aaron Bloomfield

23

Irreflexivity

• Consider a reflexive relation: <– One which every element is not related to itself– Let A = { 1, 2, 3, 4, 5 }

If every node does not have a loop, a relation is irreflexive

1 2

5 3

4

Page 23: 1 Representing Relations Rosen, section 7.3 CS/APMA 202 Aaron Bloomfield

24

Symmetry

• Consider an symmetric relation R– One which if a is related to b then b is related to a for

all (a,b)– Let A = { 1, 2, 3, 4, 5 }

• If, for every edge, there is an edge in the other direction, then the relation is symmetric

• Loops are allowed, and do not need edges in the “other” direction

1 2

5 3

4 Note that this relation is neither reflexive nor irreflexive!

Called anti-parallel pairs

Page 24: 1 Representing Relations Rosen, section 7.3 CS/APMA 202 Aaron Bloomfield

25

Asymmetry

• Consider an asymmetric relation: <– One which if a is related to b then b is not related to a

for all (a,b)– Let A = { 1, 2, 3, 4, 5 }

• A digraph is asymmetric if:

1. If, for every edge, there is not an edge in the other direction, then the relation is asymmetric

2. Loops are not allowed in an asymmetric digraph (recall it must be irreflexive)

1 2

5 3

4

Page 25: 1 Representing Relations Rosen, section 7.3 CS/APMA 202 Aaron Bloomfield

26

Antisymmetry

• Consider an antisymmetric relation: ≤– One which if a is related to b then b is not related to a

unless a=b for all (a,b)– Let A = { 1, 2, 3, 4, 5 }

1 2

5 3

4

• If, for every edge, there is not an edge in the other direction, then the relation is antisymmetric

• Loops are allowed in the digraph

Page 26: 1 Representing Relations Rosen, section 7.3 CS/APMA 202 Aaron Bloomfield

27

Transitivity

• Consider an transitive relation: ≤– One which if a is related to b and b is related to c then

a is related to c for all (a,b), (b,c) and (a,c)– Let A = { 1, 2, 3, 4, 5 }

1 2

5 3

4

• A digraph is transitive if, for there is a edge from a to c when there is a edge from a to b and from b to c

Page 27: 1 Representing Relations Rosen, section 7.3 CS/APMA 202 Aaron Bloomfield

28

Applications of digraphs: MapQuest

Start

End

•Not reflexive•Is irreflexive•Not symmetric•Not asymmetric•Not antisymmetric•Not transitive

•Not reflexive•Is irreflexive•Is symmetric•Not asymmetric•Not antisymmetric•Not transitive

Page 28: 1 Representing Relations Rosen, section 7.3 CS/APMA 202 Aaron Bloomfield

29

Rosen, questions 31 & 32, section 7.3

Which of the graphs are reflexive, irreflexive, symmetric, asymmetric, antisymmetric, or transitive

23 24 25 26 27 28

Reflexive Y Y YIrreflexive Y YSymmetric Y YAsymmetric YAnti-symmetric

Y Y

Transitive Y

Page 29: 1 Representing Relations Rosen, section 7.3 CS/APMA 202 Aaron Bloomfield

30

Rosen, section 7.1 (sic) question 45 (a)

• How many symmetric relations are there on a set with n elements?

• Solution guide explanation is pretty poorly worded

• So instead we’ll use matrices

Page 30: 1 Representing Relations Rosen, section 7.3 CS/APMA 202 Aaron Bloomfield

31

Rosen, section 7.1 (sic) question 45 (a)

• Consider the matrix representing symmetric relation R on a set with n elements:

• The center diagonal can have any values• Once the “upper” triangle is determined,

the “lower” triangle must be the transposed version of the “upper” one

• How many ways are there to fill in the center diagonal and the upper triangle?

• There are n2 elements in the matrix• There are n elements in the center diagonal

– Thus, there are 2n ways to fill in 0’s and 1’s in the diagonal

• Thus, there are (n2-n)/2 elements in each triangle– Thus, there are ways to fill in 0’s and 1’s in the triangle

• Answer: there are possible symmetric relations on a set with n elements

101

00

01

101

2/)( 2

2 nn

2/)(2/)( 22

22*2 nnnnn

Page 31: 1 Representing Relations Rosen, section 7.3 CS/APMA 202 Aaron Bloomfield

32

Quick surveyQuick survey

I felt I understood the material in this I felt I understood the material in this slide set…slide set…

a)a) Very wellVery well

b)b) With some review, I’ll be goodWith some review, I’ll be good

c)c) Not reallyNot really

d)d) Not at allNot at all

Page 32: 1 Representing Relations Rosen, section 7.3 CS/APMA 202 Aaron Bloomfield

33

Quick surveyQuick survey

The pace of the lecture for this The pace of the lecture for this slide set was…slide set was…

a)a) FastFast

b)b) About rightAbout right

c)c) A little slowA little slow

d)d) Too slowToo slow

Page 33: 1 Representing Relations Rosen, section 7.3 CS/APMA 202 Aaron Bloomfield

34

Quick surveyQuick survey

How interesting was the material in How interesting was the material in this slide set? Be honest!this slide set? Be honest!

a)a) Wow! That was SOOOOOO cool!Wow! That was SOOOOOO cool!

b)b) Somewhat interestingSomewhat interesting

c)c) Rather bortingRather borting

d)d) ZzzzzzzzzzzZzzzzzzzzzz

Page 34: 1 Representing Relations Rosen, section 7.3 CS/APMA 202 Aaron Bloomfield

3535

Biggest software errorsBiggest software errors Ariane 5 rocket explosion (1996)Ariane 5 rocket explosion (1996)

– Due to loss of precision converting 64-bit double to 16-bit intDue to loss of precision converting 64-bit double to 16-bit int Pentium division error (1994)Pentium division error (1994)

– Due to incomplete look-up table (like an array)Due to incomplete look-up table (like an array) Patriot-Scud missile error (1991)Patriot-Scud missile error (1991)

– Rounding error on the timeRounding error on the time– The missile did not intercept an incoming Scud missile, leaving 28 dead and 98 The missile did not intercept an incoming Scud missile, leaving 28 dead and 98

woundedwounded Mars Climate Orbiter (1999)Mars Climate Orbiter (1999)

– Onboard used metric units; ground computer used English unitsOnboard used metric units; ground computer used English units AT&T long distance (1990)AT&T long distance (1990)

– Wrong break statement in C codeWrong break statement in C code Therac-25, X-ray (1975-1987)Therac-25, X-ray (1975-1987)

– Badly designed software led to radiation overdose in chemotherapy patientsBadly designed software led to radiation overdose in chemotherapy patients NE US power blackout (2003)NE US power blackout (2003)

– Flaw in GE software contributed to itFlaw in GE software contributed to it

References: References: http://www5.in.tum.de/~huckle/bugse.htmlhttp://www5.in.tum.de/~huckle/bugse.html, , http://en.wikipedia.org/wiki/Computer_bughttp://en.wikipedia.org/wiki/Computer_bug, , http://http://www.cs.tau.ac.il/~nachumd/verify/horror.htmlwww.cs.tau.ac.il/~nachumd/verify/horror.html