2.relation and its application in computer

62
29/06/52 Rojanavasu P. 1 Relation and Its Application in Computer 305331-Discrete Mathematics .พรเทพ โรจนวสุ วิศวกรรมคอมพิวเตอร มหาวิทยาลัย นเรศวร พะเยา

Upload: others

Post on 08-Jan-2022

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 1

Relation and Its Application in Computer

305331-Discrete Mathematicsอ.พรเทพ โรจนวสุ

วิศวกรรมคอมพิวเตอรมหาวิทยาลัย นเรศวร พะเยา

Page 2: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 2

นิยามของความสัมพันธ (Relation)

ความสัมพันธโดยทั่วไปแสดงความเชื่อมโยงระหวางกลุม(เซต)ของวัตถุตัวอยางเชน

A เปนเซตของนักฟุตบอล B เปนเซตของสโมสรเราสามารถกลาวไดวา สมาชิกของเซต A มีความสัมพันธกับ สมาชิกของเซต B ถา A เปนนักฟุตบอลในสงักัดของ BA = {Ronaldo, Walcott, Fabregus, Torres} B={Man utd, Arsenal , Liverpool}

Ronaldo เปนนักฟุตบอลในสังกัดของ Man utdFabregus เปนนักฟุตบอลในสังกัดของ Arsenal ….แทนโดย (Ronaldo, Man utd), (Fabregus, Arsenal), …

Page 3: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 3

Relation คือ สับเซตของ Cartesian product ระหวางเซตแบงตามจํานวนของเซต

Binary Relation คือ สับเซตของ Cartesian product ของ A×A หรือ A×B แตมักเรียก A×A วา Relation on A

Apply to Graph Theory, Tree and Network, etc.n-ary Relation คือสับเซตของ Cartesian product ของเซตมากกวา 2 เซต

Apply to Relational Databases, etc.

Page 4: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 4

Binary relationBinary relation คือ ความสัมพันธจากเซต A ไปเซต B และเปนสับเซตของ A×B ความสัมพันธนี้ความจริงแลวคือ subset of A×B => R⊆ A×B ถา (ai,bi)∈R เรากลาววา aRb ถา (ai,bi)∉R เรากลาววา aRb∴เซตของ ordered pair ถอืเปน binary relationนิยาม Domain & Range

Domain D(R) = {a|a∈A and there exists b∈B such that (a,b)∈R}Range or Image Im(R) = {b|b∈B and there exists a∈A such that (a,b)∈R}

Page 5: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 5

รูปแบบการแทนความสัมพันธ

A={0,1,2} B={a,b}Roster => R={(0,a),(0,b),(1,a),(2,b)}

2

1

0

b

a

Arrow diagram

xx0

x

b

2x1

a

เมทริกซความสัมพันธ ?

Page 6: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 6

ตัวอยาง ความสัมพันธที่เปนเซตไมจํากัด

ถาให R เปนความสัมพันธบนเซตจํากัด A, R คือสับเซตของ A×AA={a,b,c,d}R={(a,a),(a,b),(b,b),(b,c),(b,a),(b,d),(b,d),(c,d)}สามารถเขียนในรูปของ Directed graph ไดดังนี้

Page 7: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 7

R={(a,a),(a,b),(b,b),(b,c),(b,a),(b,d),(b,d),(c,d)}

a

c d

b

Digraph of relation R

Page 8: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 8

Combine Relationsเนื่องจาก R⊆A×B ดังนั้น Relation 2 เซตใดๆ ที่เกิดจาก A ไป B จึงสามารถใชโอเปอรเรเตอรตางๆ เหมือนเซตปกติไดLet A = { 1 ,2 ,3 } and B = { 1, 2 ,3 ,4 } R1 = { (1,1 ),(2 ,2),(3 ,3)} R2 = { (1,1 ),(1,2),(1 ,3), (1,4)}

R ∪ R2 = {(1,1),(1,2),(1,3),(1,4) ,(2,2), (3,3 ) } R1 ∩ R2 = {(1,1)} R1 - R2 = {(2,2), (3,3)} R2 – R1 = {(1,2),(1,3),(1,4)}

Page 9: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 9

ให R เปน Relation จาก A ไป B , (a,b)∈Rให S เปน Relation จาก B ไป C , (b,c)∈Sเราสามารถสราง Relation T ใหมจาก R,S คือ (a,c)∈T โดยที่ a∈A และc∈C ก็ตอเมื่อ (a,b)∈Rและ(b,c)∈Sเรียกวา T วาเปน composition ของ R และ S => SοRตัวอยางA={1,2,3,4}, B={p,q,r,s,t,w}, C={a,e,i,o,u}R=A×B={(1,p),(1,s),(2,s),(3,q),(4,t),(4,w)}S=B×C={(p,i),(r,o),(s,a),(t,e),(w,o)}SοR = ?

Page 10: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 10

ใช arrow diagram ชวย

4

3

2

1

w

t

s

r

q

p

u

o

i

e

a

4

3

2

1

u

o

i

e

a

Page 11: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 11

จงหา SοR ตอไปนี้

Page 12: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 12

A = {a,b,c}, R={(a,c),(b,a),(b,b),(c,b)}R1=RRn=RοRn-1 =>recursive compositionR2={(a,b),(b,c),(b,a),(c,a),(c,b)}เรานํา composition ของ relation ไปใชงานบอยในระบบฐานขอมูล ในการ join ตาราง 2 ตาราง

Page 13: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 13

n-ary Relationกําหนด sets D1, D2, …. Dn

a relation R is a subset of D1 x D2 x … x DnDi ในทางคณิตศาสตรจะมองเปนเซตที่มีการกําหนดสมาชิกไวกอนลวงหนาและไมไดกําหนดเงื่อนไขใด ๆ ไวในทาง Database จะมอง Di กวางขึ้น มองเปนโดเมนที่มีการกําหนดเงื่อนไขไวเชน D1 เปนโดเมนชื่อคนมีอักษรไมเกิน 15 ตัว D2 เปนโดเมนของรหัสนิสิตเลข 7 หลัก

Page 14: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 14

Edgar Frank "Ted" Codd (August 23, 1923 – April 18, 2003) was a British computer scientist who, while workingfor IBM, invented the relational model for databasemanagement, the theoretical basis for relational databases.

ที่มา http://en.wikipedia.org/wiki/Edgar_F._Codd

Page 15: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 15

ดังนั้น relation ที่เกดิขึ้นคือ a set of n-tuples(a1, a2, …, an) โดยที่ ai ∈ Di , i=1,2,..,nX={Mark, Sara, Julia}, Y={A,B,C,D,F}Z={math, data com} ให R เปนความสัมพันธของนิสิตที่ไดเกรดในแตละวิชาR= {(Mark,math,D), (Mark,data com,F),

(Sara,math,B), (Sara,data com,C), (Julia,math,C), (Julia,data com,D)}

Page 16: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 16

ดังนั้นใน Table ก็จะเก็บ Relation สับเซตของผลคูณคารทีเซียน (Unordered)

CMathJuliaDData comJulia

BMathSaraCData comSara

FData comMarkD

เกรดMath

วิชาMark

ชื่อ

Fields/Attributes/Columns

Records/Tuples/Rows

ตาราง Student

Page 17: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 17

โดเมนจะถูกเรียกเปน Primary key ก็ตอเมื่อคาของโดเมนนั้นจะไมซ้ํากันเลยใน Relation หรือตารางนั้น

เราสามารถใชมากกวา 1 โดเมนเปนทําหนาที่เปน Primary key ได เรียกวา Composite key

ที่มา Discrete Mathematical and Its application by rosen

Page 18: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 18

Operation on n-ary Relationsเราสามารถสราง relation ใหมไดจาก relation เดิมที่มีอยูโดยอาศัย operation ตาง ๆ ในระบบฐานขอมูล Operation ตางๆคือ query (คําถาม) ที่ผูใชตองการคําตอบตามเงื่อนไขที่ตองการSelection operator

Selection operator (Sc) จะทําการแมพขอมูลจาก Relation เดิมไปยัง Relation ใหมที่มี record สอดคลองกับเงื่อนไข CจากTable1ใช operator Sc โดยที่ C คือ Major=“Computer Science”จะได tuples (Ackermann, 231455, Computer Science, 3.88) and (Chou, 102147, Computer Science,3 .49)

Page 19: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 19

Projection operator Pi1,i2,..im จะทําการแมพเรคอรดทั้งหมดบน Relation เดิมไปยัง Relation ใหมโดยเลือกเฉพาะโดเมนหรือฟลดที่กําหนดจากตาราง Table 1 P1,3 จะได

ที่มา Discrete Mathematical and Its application by rosen

Page 20: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 20

P1,2 สําหรับตาราง Enrollments จะได

ที่มา Discrete Mathematical and Its application by rosen

Page 21: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 21

Join operation ใชรวม 2 Relations ใหกลายเปน 1 Relation โดยที่ทั้งสอง Relation นั้นใชฟลดใดฟลดหนึ่งรวมกัน คลาย ๆJ2(T5,T6) Join 2 fields ทายของ T5 กับ 2 field แรกของ T6

ที่มา Discrete Mathematical and Its application by rosen

Page 22: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 22

ผลลัพธของ J2(T5,T6)

ที่มา Discrete Mathematical and Its application by rosen

Page 23: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 23

SQLSQL (Structured Query Language)

SQL is a database computer language designedfor the retrieval and management of data inrelational database management systems(RDBMS), database schema creation andmodification, and database object access controlmanagementIn 1974, Donald D. Chamberlin and Raymond F. Boyce of IBM subsequently created the Structured English Query Language (SEQUEL, and was changed to SQL later) to manipulate and manage data stored in System R.

Page 24: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 24

SQLSQL (Structured Query Language) เปนภาษาที่ใชทํา Operator ตางๆ กับฐานขอมูล“SELECT Departure_time

FROM FlightsWHERE Destination=‘Detroit”

ที่มา Discrete Mathematical and Its application by rosen

Page 25: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 25

From table 5 and table 6 ในทางปฏิบัติ join เราสามารถกําหนดชือ่ไดเลยSELECT Teaching_assingments .professor,

Class_schedule.timeFROM Teaching_assingmentsINNER JOIN Class_scheduleON Teaching_assingments.Course_Number=

Class_schedule.Course_NumberWHERE Teaching_assingments.dartment = ‘Mathematics’What is the result?

Page 26: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 26

SELECT Persons.LastName, Persons.FirstName, Orders.OrderNoFROM PersonsINNER JOIN OrdersON Persons.P_Id=Orders.P_Id

Page 27: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 27

ขอพึงระวัง !!!ผลลัพธที่ไดจากคําสั่ง select ใน SQL เรียกวา SQL Table ซึ่งจะไมถือวาเปน Relationตางจากผลลัพธที่ไดจากการใชงาน Operation on n-ary relation จะเปน Relation จากตาราง Teaching_assigments ผลลัพธจากการ “SELECT professor FROM Teaching_assignments” จะไดดัง Query1 ถาเปน projection operation P1 จะไดแค {cruz, farber, grammer, rosen}

Page 28: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 28

ถาจะใหไดผลลัพธเหมือน projection ตองใช “SELECT DISTINCT professor FROM Teaching_assignments”

Page 29: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 29

Equivalence Relationให A เปนเซตและ R เปน relation บน A. R จะเรียกวามีคุณสมบัติดังตอไปนี้

Reflexive, if for all a∈A, aRa; Symmetric, if for all a,b∈A wherever aRb holds, bRa must also hold;Transitive, if for all a,b,c ∈A, wherever aRb and bRc hold, aRc must also hold;

R เปน equivalence relation ก็ตอเมื่อมีคุณสมบัติครบทั้ง reflexive, symmetric, transitive

Page 30: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 30

R is not reflexive, if there exists an a∈A such that (a,a)∈RR is not symmetric, if there exists a,b∈A such that (a,b)∈R but (b,a)∈RR is not transitive, if there exists a,b,c∈A such that (a,b)∈R,(b,c)∈R but (a,c) ∈R

ExampleA={a,b,c,d} R={(a,a),(b,b),(c,c),(d,d),(a,b),(b,a)}

พิจารณาวา R เปน equivalence relation หรือไม?

Page 31: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 31

A={a,b,c,d} , R={(a,a),(a,b),(a,c),(b,b),(b,a),(c,a),(d,d)}

a

c d

b

Page 32: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 32

A={1,2,3,4}R เปนความสัมพันธนิยามโดย (x,y)∈R ไดถา x ≤ yR เปน equivalence relation?A=ZR เปนความสัมพันธนิยามโดย (x,y)∈R ไดถา x = yR เปน equivalence relation?

Page 33: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 33

A=ZR เปนความสัมพันธ (a,b)∈Z ไดถาThe congruent modulo m relation on the set of

integers i.e. {<a, b>| a ≡ b(mod m)},การหาความสัมพันธ a ≡ b(mod m) สามารถหาไดดังนี้ (a-b)=mk โดยที่ k∈Z และโดยที่กําหนด m คาคงที่และm∈ Z+

ให m=4Reflexive, ให a∈Z จะไดวา a-a = 0 = m0, k=0, k∈Zเชน (3,3),(7,7),…. 3-3 = 4(0) , k=0, k∈Z

Page 34: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 34

Symmetric, ให a,b∈Z จะไดวา a-b=mk, b-a=m(-k), k∈Z เชน (3,7),(7,3),.. 3-7=-4=4(-1) , 7-3=4=4(1)Transitive, ให ให a,b,c∈Z จะไดวา a-b=pm, b-c=qmp,q∈Zเชน (3,7), (7,15), (3,15) จะได 3-7=-4=4(-1) , 7-15=-8=4(-2), 3-15=-12=4(-3) p=-1, q=-2, p,q∈Zดังนัน้จะได R เปน equivalence relation บน ZR เรียกวา congruence modulo m

Page 35: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 35

Equivalence Classes and Partitionsให R เปน equivalence relation บนเซต S สําหรับ a∈Sให[a] แทนเซตของสมาชิกของ S ที่

[a]={x|(a,x)∈R} เรียก [a] วาเปน equivalence class ของ a บนเซต SS/R={[a]|a ∈S} เรียกวา quotient set S by R

S={a,b,c,d} R={(a,a),(b,b),(c,c),(a,b),(b,a),(c,d),(d,c)}[a]={a,b}, [b]={a,b}, [c]={c,d}, [d]={c,d}

Page 36: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 36

Theorem ให R เปน equivalence relation บน AFor all a∈A,[a]≠φIf b∈[a] then [a]=[b] where a,b ∈AFor all a,b ∈A , ถาไม [a]=[b] ก็จะ [a]∩[b]=φA สามารถหาไดจากการ union equivalence class ทั้งหมดของ R

UAa

aA∈

= ][

ที่มา Discrete Mathematical and Its application by rosen

Page 37: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 37

ที่มา Discrete Mathematical and Its application by rosen

Page 38: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 38

Application of Equivalence Classesin Rough Set

Let U denote a finite and non-empty set called the universe. Let R ⊆ U×U and R be an equivalence relation that partitions U.

that create approximation aprR=(U,R). Let the partition be denoted as U/R ={C1,C2, …,Cn},

where Cn is an equivalence class of R.

Given an arbitrary set X ⊆ U. It may be describe X precisely in the approximation space aprR(U,R) by a pair of lower and upper approximations as below.

Page 39: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 39

Reference set: A ⊆ U Lower approximation:

aprR(A) = R(A) = {x∈U | [x]R ⊆ A}

Upper approximation:aprR(A) = R(A) = {x∈U | [x]R ∩ A ≠ ∅}

[x]R are equivalence classes

Page 40: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 40

ที่มา Rough set and Information Retrieval by Padmini Das-Gupta

Page 41: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 41

T1={t1, t2, t3} T2={t4, t5} T3={t6, t7, t8} T4={t9, t10} T5={t11}D1={t1, t2, t4, t5, t6}D2={t4, t5}D3={t1, t4, t5}D4={t1, t4, t6, t9, t11}D5={t3, t7, t10}D6={t3, t7}D7={t1, t2, t3}D8={t1, t2, t3, t5, t6 , t9, t11}D9={t2, t4, t5}D10={t2, t5, t9, t10, t11}D11={t5, t11}

D1 and D2 are rough bottom equal iff R(D1) = R(D2)D1 and D2 are rough top equal iff R(D1) = R(D2)D1 and D2 are equal iff R(D1) = R(D2) and R(D1) = R(D2)

Page 42: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 42

R(D1) ={T2}, R(D2) =?.... R(D11) =?R(D1) ={T1, T2, T3}, R(D2) =?.... R(D11) =?

Di and Dj are rough bottom equal iff R(Di) = R(Dj)Di and Dj are rough top equal iff R(Di) = R(Dj)Di and Dj are equal iff R(Di) = R(Dj) and R(Di) = R(Dj)

Family subset FR(Di) : Dj belong to Di if R(Dj) ⊂ R(Di)Family subset FR(Di) : Dj belong to Di if R(Dj) ⊂ R(Di)Family subset FR(Di) : Dj belong to Di if R(Dj) ⊂ R(Di) and R(Dj) ⊂ R(Di)

Page 43: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 43

Page 44: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 44

Page 45: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 45

Closures Relationsให relation R = {(1,1),(1,2),(2,1),(3,2)} บนเซต A = {1,2,3}

คําถามใหหา Relation ที่เล็กที่สุดที่มี R เปนสับเซตและมีคุณสมบัติ Reflexive ?

R= {(1,1),(1,2),(2,1),(3,2),(2,2),(3,3)}

Reflexive Closure the reflexive closure of a relation R on a set A is the smallest reflexive relation on A that contains R.The reflexive closure S of a relation R on a set X is given by S=R ∪{(x,x) | x∈A}

Page 46: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 46

Symmetric closure The symmetric closure of a relation R on a set A is the smallest symmetric relation on A that contains R.The symmetric closure S of a relation R on a set A

is given by S=R∪{(x,y),(y,x) | x,y∈A}

Page 47: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 47

Transitive closureThe transitive closure of a relation R on a set A is the smallest transitive relation on A that contains R.

For example, if X is a set of airports xRy means "there is a direct flight from airport x to airport y",then the transitive closure of R on X is the relation "it is possible to fly from x to y in one or more flights."

Page 48: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 48

After the transitive closure is constructed, as depicted in the following figure, in an O(1) operation one may determine that node d is reachable from node a.

ที่มา http://en.wikipedia.org/wiki/Transitive_Closure

Page 49: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 49

การหา Transitive closure ของ Relation ใด ๆ สามารถหาไดจากR∞=R∪R2∪R3∪….. = ตัวอยาง

A={1,2,3,4}, R ={(1,1),(3,3),(1,3),(2,3),(3,2),(4,2)}R2={(1,1),(3,3),(1,3),(2,2),(2,3),(3,2),(3,3),(4,3)}R3={(1,1),(3,3),(1,3),(2,2),(2,3),(3,2),(3,3),(4,2),(4,3)}R4={(1,1),(3,3),(1,3),(2,2),(2,3),(3,2),(3,3),(4,2),(4,3)}R∞= R∪R2∪R3∪ R4

U∞

=1n

nR

Page 50: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 50

Directed walkให R เปน Relation บนเซต A และ a,b ∈ ADirected walk from a to b in R คือ aRa1, a1Ra2,…., akRb, หรืออาจเขียนเปน (a,a1),(a1,a2),…(ak,b)เราเรียก a1, a2,.. ak internal verticesa initial vertex, b terminal vertexdirected walk from a to b เรยีกวา Path from a to b ถา vertices ทุกตัวใน path นั้น ไมซ้ํากัน (distinct)

Page 51: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 51

Let A = {a,b,c,d,e,f,g,h,i,j} และ R มี relation ดังกราฟdirected walk จาก a ไป g ?Walk1: aRb, bRc, cRd, dRe, eRf, fRc, cRgมี internal vertices – b,c,d,e,f,c,มี c ซ้ําจงึไมเรียกวา path

Walk2: aRb, bRc, cRgมี internal vertices – b,c

ab

c

d

e

f

g

hi

j

Page 52: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 52

Representing Relation using MatricesA = {1,2,3} B={1,2} และ Relation R = {(2,1),(3,1),(3,2)}

MR=

A = {a,b,c} R คือ Relative on A มีความสัมพันธดังนี้

MR=

⎥⎥⎥

⎢⎢⎢

110100

⎥⎥⎥

⎢⎢⎢

011010101

Page 53: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 53

R1∪R2 = MR1∨ MR2 (∨ = or)R1∩R2 = MR1∧ MR2 (∧ = and)R1

-1 = RT (Transpose)SοR = MR • MS (Boolean Produce)Example of Boolean produceA= B=

A•B=

A•B=

⎥⎦

⎤⎢⎣

⎡101100

⎥⎥⎥

⎢⎢⎢

000110101

⎥⎦

⎤⎢⎣

⎡∧∨∧∨∧∧∨∧∨∧∧∨∧∨∧∧∨∧∨∧∧∨∧∨∧∧∨∧∨∧)01()10()11()01()10()01()01()00()11()01()10()10()01()10()00()01()00()10(

⎥⎦

⎤⎢⎣

⎡101000

Page 54: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 54

ถา A เปนเซตใด ๆ R เปน relation บน A จะได R2=RοRจะได MRοR=MR•MR

ดังนัน้ในการหา Transitive closure ของ R ใด ๆ สามารถหาไดจากMR^n.=MR•MR •…. • MR

การหา MR^n นั้นใชเวลาในการคํานวณมาก Stephen Warshall นาํเสนอ Warshall’s algorithm ในการหา Transitive closure

n times

Page 55: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 55

transclosure( int adjmat[max][max], int path[max][max]){

for(i = 0; i < max; i++)for(j = 0; j < max; j++)

path[i][j] = adjmat[i][j];

for(i = 0;i <max; i++)for(j = 0;j < max; j++)

if(path[j][i] == 1)for(k = 0; k < max; k++)

if(path[i][k] == 1)path[j][k] = 1;

}ตัวอยาง http://www.cs.nmsu.edu/~ipivkina/TransClosure/index.html

Page 56: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 56

Partially Ordered SetsA relation R on a set S is called Antisymmetric if for all a,b ∈S, aRb and bRa then a=b

A relation R on a set S is called Symmetric if for all a,b∈Awherever aRb holds, bRa must also hold;

Example ให A={1,2,3} R1={(1,2),(2,3),(1,1)}, R1 is antisymmetric.R2={(1,2),(2,1),(2,2)}, R2 is not antisymmetric.ขอพึงระวัง !!!!antisymmetric ไมไดมคีวามหมายเปน negative ของ symmetric แตสามารถเกิดรวมกับ symmetric ได เชน ให A={1,2} R เปน relation บน A, R={(1,1),(2,2)} จะเห็นไดวา R เปนทั้ง symmetric และ antisymmetric

Page 57: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 57

A relation R on set A called a partial order on A if R is reflexive, antisymmetric and transitive

Reflexive, if for all a∈A, aRa; Antisymmetric, if for all a,b∈A if aRb and bRahold then a=bTransitive, if for all a,b,c ∈A, if aRb and bRc hold, then aRc must also hold;

A set A together with a partial order relation R is called a partial ordered set or poset or (A,R)

Page 58: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 58

ตัวอยาง The relation "x is even, y is odd" between a pair (x, y) of integers is antisymmetric:

ที่มา http://en.wikipedia.org/wiki/Antisymmetric_relation

Page 59: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 59

ความสัมพันธนอยกวาเทากับ ≤ บนเซตใด ๆ จะเปน posetLet (S, ≤) be a poset and a,b∈S.

if either a ≤b or b ≤a, then we say that a and b are comparable.The poset (S, ≤) is called a linearly ordered set, or a totally ordered set or a chain, if for all a,b ∈S either a ≤b or b ≤a.

Page 60: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 60

Lexicographic orderให (A, ≤) และ (B, ≤) เปน poset ทั้งคูA×B เปน relation โดยที่ (a,b)R(c,d) ถา a ≤c and b ≤d for all (a,b), (c,d) ∈ A×B เรียกวา product partial orderถา a<c หรือ (a=c และ b ≤d) เรียกวา lexicographic orderเขียนแทนโดย (a,b) (c,d)

Exampleถามี A1, A2,… An, n ≥1 เปน poset (Ai, ≤) i = 1,2,..n กําหนด Relation บน A1x A2 x… x An-1 x An

p

p

Page 61: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 61

ให (a1, a2,…, an), (b1, b2,…, bn) ∈ A1x A2 x… x An-1 x An

ดังนั้น (a1, a2,…, an) (b1, b2,…, bn) ก็ตอเมื่อa1<b1 ora1=b1 and a2<b2 ora1=b1 and a2=b2 and a3<b3or….

a1=b1 and a2=b2 and a3<b3 …. and an-1=bn-1 and an<bn ora1=b1 and a2=b2 and a3<b3 …. and an-1=bn-1 and an=bn

p

Page 62: 2.Relation and Its application in Computer

29/06/52 Rojanavasu P. 62

In dictionary, the word mango comes before moneybecause of the letters in the second position: a<o.