Presentation is loading. Please wait.

Presentation is loading. Please wait.

RELATIONAL ALGEBRA and Computer Assignment 1 Prof. Sin-Min LEE Department of Computer Science.

Similar presentations


Presentation on theme: "RELATIONAL ALGEBRA and Computer Assignment 1 Prof. Sin-Min LEE Department of Computer Science."— Presentation transcript:

1 RELATIONAL ALGEBRA and Computer Assignment 1 Prof. Sin-Min LEE Department of Computer Science

2 Codd’s Relational Algebra zA set of mathematical operators that compose, modify, and combine tuples within different relations zRelational algebra operations operate on relations and produce relations (“closure”) f: Relation  Relationf: Relation x Relation  Relation

3 A Set of Logical Operations: The Relational Algebra zSix basic operations:  Projection   (R)  Selection   (R) yUnionR 1 UR 2 yDifference R 1 – R 2  ProductR 1 X R 2  (Rename)   (R) zAnd some other useful ones:  JoinR 1 ⋈  R 2  SemijoinR 1 ⊲  R 2  IntersectionR 1 Å R 2  DivisionR 1 ¥ R 2

4

5

6 Data Instance for Operator Examples sidname 1Jill 2Qun 3Nitin fidname 1Ives 2Saul 8Roth sidexp- grade cid 1A550-0105 1A700-1005 3C501-0105 cidsubjse m 550-0105DBF05 700-1005AIS05 501-0105ArchF05 fi d cid 1550-0105 2700-1005 8501-0105 STUDENT Takes COURSE PROFESSOR Teaches

7 Rename,     zThe rename operator can be expressed several ways: yThe book has a very odd definition that’s not algebraic yAn alternate definition:     (x)Takes the relation with schema  Returns a relation with the attribute list  yRename isn’t all that useful, except if you join a relation with itself Why would it be useful here?

8 Deriving Intersection Intersection: as with set operations, derivable from difference A-B B-A A B A Å B ≡ (A [ B) – (A – B) – (B – A) ≡ A – (A – B)

9 Division zA somewhat messy operation that can be expressed in terms of the operations we have already defined zUsed to express queries such as “The fid's of faculty who have taught all subjects” zParaphrased: “The fid’s of professors for which there does not exist a subject that they haven’t taught”

10 Division Using Our Existing Operators zAll possible teaching assignments: Allpairs: zNotTaught, all (fid,subj) pairs for which professor fid has not taught subj: zAnswer is all faculty not in NotTaught:  fid,subj (PROFESSOR x  subj (COURSE)) Allpairs -  fid,subj (Teaches ⋈ COURSE)  fid (PROFESSOR) -  fid (NotTaught) ´  fid (PROFESSOR) -  fid (  fid,subj (PROFESSOR x  subj (COURSE)) -  fid,subj (Teaches ⋈ COURSE))

11 Division: R 1  R 2  Requirement: schema(R 1 ) ÷ schema(R 2 ) zResult schema: schema(R 1 ) – schema(R 2 ) z“Professors who have taught all courses”: zWhat about “Courses that have been taught by all faculty”?  fid (  fid,subj ( Teaches ⋈ COURSE)   subj (COURSE))

12 DIVISION - The division operator is used for queries which involve the ‘all’ qualifier such as “Find the names of sailors who have reserved all boats”. - The division operator is a bit tricky to explain, and perhaps best approached through examples as will be done here. zCartesian Product (R 1 × R 2 ) combines two relations by concatenating their tuples together, evaluating all possible combinations. If the name of a column is identical for two relations, this ambiguity is resolved by attaching the name of each relation to a column. e.g., Emp × Dept y(SS#, name, age, salary, Emp.dno, Dept.dno, dname, floor, mgrSS#) zIf t(Emp) and t(Dept) is the cardinality of the Employee and Dept relations respectively, then the cardinality of Emp × Dept is: t(Emp) × t(Dept)

13

14

15

16 EXAMPLES OF DIVISION

17 DIVISION Interpretation of the division operation A/B: - Divide the attributes of A into 2 sets: A1 and A2. - Divide the attributes of B into 2 sets: B2 and B3. - Where the sets A2 and B2 have the same attributes. - For each set of values in B2: - Search in A2 for the sets of rows (having the same A1 values) whose A2 values (taken together) form a set which is the same as the set of B2’s. - For all the set of rows in A which satisfy the above search, pick out their A1 values and put them in the answer.

18 DIVISION Example: Find the names of sailors who have reserved all boats: (1) A =  sid,bid (Reserves). A1 =  sid (Reserves) A2 =  bid (Reserves) (2) B2 =  bid (Boats) B3 is the rest of B. Thus, B2 ={101, 102, 103, 104} (3)Find the rows of A such that their A.sid is the same and their combined A.bid is the set B2. Thus we find A1 = {22} (4) Get the set of A2 corresponding to A1: A2 = {Dustin}

19 FORMAL DEFINITION OF DIVISION The formal definition of division is as follows: A/B =  x (A) -  x ((  x (A)  B) – A)

20

21 CARTESIAN PRODUCT (Cont … ) zExample: Emp table: Dept table: 345John Doe2325,0001 943Jane Java2528,0002 876Joe SQL2232,0001 SS#Nameagesalarydno 1Toy1345 2Shoe2943 dnodnamefloormgrSS#

22 CARTESIAN PRODUCT (Cont … ) zCartesian product of Emp and Dept: Emp × Dept: 34 5 John Doe 2325,00 0 11Toy1345 94 3 Jane Java 2528,00 0 21Toy1345 87 6 Joe SQL2232,00 0 11Toy1345 John Doe 2325,00 0 12Shoe2943 Jane Java 2528,00 0 22Shoe2943 87 6 Joe SQL2232,00 0 12Shoe2943 SS#NameagesalaryEmp.dnoDept.dnodnamefloormgrSS#

23 CARTESIAN PRODUCT zExample: retrieve the name of employees that work in the toy department:

24 CARTESIAN PRODUCT zExample: retrieve the name of employees that work in the toy department:  ∏ name (б Emp.dno=Dept.dno (Emp × б dname= ‘ toy ’ (Dept)))

25 CARTESIAN PRODUCT (Cont … )  ∏ name (б dname= ‘ toy ’ (б Emp.dno=Dept.dno (Emp × Dept))) 345John Doe2325,00011Toy1345 943Jane Java2528,00021Toy1345 876Joe SQL2232,00011Toy1345 John Doe2325,00012Shoe2943 Jane Java2528,00022Shoe2943 876Joe SQL2232,00012Shoe2943 SS#NameagesalaryEmp.dnoDept.dnodnamefloormgrSS#

26 CARTESIAN PRODUCT (Cont … )  ∏ name (б dname= ‘ toy ’ (б Emp.dno=Dept.dno (Emp × Dept))) 345John Doe 2325,00011Toy1345 876Joe SQL2232,00011Toy1345 943Jane Java 2528,00022Shoe2943 SS#NameagesalaryEmp.dnoDept.dnodnamefloormgrSS#

27 CARTESIAN PRODUCT (Cont … )  ∏ name (б dname= ‘ toy ’ (б Emp.dno=Dept.dno (Emp × Dept))) 345John Doe 2325,00011Toy1345 876Joe SQL2232,00011Toy1345 SS#NameagesalaryEmp.dnoDept.dnodnamefloormgrSS#

28 CARTESIAN PRODUCT (Cont … )  ∏ name (б dname= ‘ toy ’ (б Emp.dno=Dept.dno (Emp × Dept))) John Doe Joe SQL Name

29

30

31 EQUALITY JOIN, NATURAL JOIN, JOIN, SEMI-JOIN zEquality join connects tuples from two relations that match on certain attributes. The specified joining columns are kept in the resulting relation.  ∏ name (б dname= ‘ toy ’ (Emp Dept))) zNatural join connects tuples from two relations that match on the specified common attributes  ∏ name (б dname= ‘ toy ’ (Emp Dept))) zHow is an equality join between Emp and Dept using dno different than a natural join between Emp and Dept using dno?  Equality join: SS#, name, age, salary, Emp.dno, Dept.dno, …  Natural join: SS#, name, age, salary, dno, dname, … zJoin is similar to equality join using different comparison operators yA S op = {=, ≠, ≤, ≥, } att op att (dno)

32 EXAMPLE JOIN zEquality Join, (Emp Dept))) SS#NameAgeSalarydno 1Joe24200002 2Mary20250001 3Bob22270001 4Kathy30300002 5Shideh440001 EMP dnodnamefloormgrss# 1Toy15 2Shoe21 Dept (dno) SS#NameAgeSalaryEMP.dnoDept.dn o dnamefloormgrss # 1Joe242000022Shoe21 2Mary202500011Toy15 3Bob222700011Toy15 4Kathy303000022Shoe21 5Shideh4400011Toy15

33 EXAMPLE JOIN zNatural Join, (Emp Dept))) SS#NameAgeSalarydno 1Joe24200002 2Mary20250001 3Bob22270001 4Kathy30300002 5Shideh440001 EMP dnodnamefloormgrss# 1Toy15 2Shoe21 Dept (dno) SS#NameAgeSalarydnodnamefloormgrss# 1Joe24200002Shoe21 2Mary20250001Toy15 3Bob22270001Toy15 4Kathy30300002Shoe21 5Shideh440001Toy15

34 EXAMPLE JOIN zJoin, (Emp ρ x (Emp)))) SS#NameAgeSalarydno 1Joe24200002 2Mary20250001 3Bob22270001 4Kathy30300002 5Shideh440001 EMP dnodnamefloormgrss# 1Toy15 2Shoe21 Dept Salary > 5 * salary SS#NameAgeSalarydnox.SS#x.Namex.Ag e x.Salar y x.dno 2Mary202500012Shideh440001 3Bob222700013Shideh440001 4Kathy303000024Shideh440001

35

36 EQUALITY JOIN, NATURAL JOIN, JOIN, SEMI-JOIN (Cont … ) zExample: retrieve the name of employees who earn more than Joe:  ∏ name (Emp (sal>x.sal) б name= ‘ Joe ’ (ρ x (Emp))) zSemi-Join selects the columns of one relation that joins with another. It is equivalent to a join followed by a projection: yEmp (dno) Dept ≡ ∏ SS#, name, age, salary, dno (Emp Dept)

37

38 JOIN OPERATORS Condition Joins: - Defined as a cross-product followed by a selection: R ⋈ c S = σ c (R  S) ( ⋈ is called the bow-tie) where c is the condition. - Example: Given the sample relational instances S1 and R1 The condition join S ⋈ S1.sid<R1.sid R1 yields

39 JOIN OPERATORS Condition Joins: - Defined as a cross-product followed by a selection: R ⋈ c S = σ c (R  S) ( ⋈ is called the bow-tie) where c is the condition. - Example: Given the sample relational instances S1 and R1 The condition join S ⋈ S1.sid<R1.sid R1 yields

40

41

42

43 Equijoin: Special case of the condition join where the join condition consists solely of equalities between two fields in R and S connected by the logical AND operator ( ∧ ). Example: Given the two sample relational instances S1 and R1 The operator S1 R.sid=Ssid R1 yields

44 Natural Join - Special case of equijoin where equalities are implicitly specified on all fields having the same name in R and S. - The condition c is now left out, so that the “bow tie” operator by itself signifies a natural join. - N. B. If the two relations have no attributes in common, the natural join is simply the cross-product.

45

46

47

48 Computer 1 st Project Show how to place non-attacking queens on a triangular board of side n. Show that this is the maximum possible number of queens.

49 Tree search example You need to use (1)Depth First Search (2) Backtracking Algorithm Due Date: Feb. 12, Input, Output format Feb. 19 Depth first search Feb. 26 Complete the project.

50 Tree search example

51

52 Implementation: general tree search

53 Implementation: states vs. nodes zA state is a (representation of) a physical configuration zA node is a data structure constituting part of a search tree includes state, parent node, action, path cost g(x), depth  The Expand function creates new nodes, filling in the various fields and using the SuccessorFn of the problem to create the corresponding states.

54 Search strategies zA search strategy is defined by picking the order of node expansion zStrategies are evaluated along the following dimensions: ycompleteness: does it always find a solution if one exists? ytime complexity: number of nodes generated yspace complexity: maximum number of nodes in memory yoptimality: does it always find a least-cost solution? zTime and space complexity are measured in terms of yb: maximum branching factor of the search tree yd: depth of the least-cost solution ym: maximum depth of the state space (may be ∞)

55 Depth-first search zExpand deepest unexpanded node zImplementation: yfringe = LIFO queue, i.e., put successors at front

56 Depth-first search zExpand deepest unexpanded node zImplementation: yfringe = LIFO queue, i.e., put successors at front

57 Depth-first search zExpand deepest unexpanded node zImplementation: yfringe = LIFO queue, i.e., put successors at front

58 Depth-first search zExpand deepest unexpanded node zImplementation: yfringe = LIFO queue, i.e., put successors at front

59 Depth-first search zExpand deepest unexpanded node zImplementation: yfringe = LIFO queue, i.e., put successors at front

60 Depth-first search zExpand deepest unexpanded node zImplementation: yfringe = LIFO queue, i.e., put successors at front

61 Depth-first search zExpand deepest unexpanded node zImplementation: yfringe = LIFO queue, i.e., put successors at front

62 Depth-first search zExpand deepest unexpanded node zImplementation: yfringe = LIFO queue, i.e., put successors at front

63 Depth-first search zExpand deepest unexpanded node zImplementation: yfringe = LIFO queue, i.e., put successors at front

64 Depth-first search zExpand deepest unexpanded node zImplementation: yfringe = LIFO queue, i.e., put successors at front

65 Depth-first search zExpand deepest unexpanded node zImplementation: yfringe = LIFO queue, i.e., put successors at front

66 Depth-first search zExpand deepest unexpanded node zImplementation: yfringe = LIFO queue, i.e., put successors at front

67 Properties of depth-first search zComplete? No: fails in infinite-depth spaces, spaces with loops yModify to avoid repeated states along path  complete in finite spaces zTime? O(b m ): terrible if m is much larger than d y but if solutions are dense, may be much faster than breadth-first zSpace? O(bm), i.e., linear space! zOptimal? No

68 Depth-limited search = depth-first search with depth limit l, i.e., nodes at depth l have no successors zRecursive implementation:

69 Backtracking zSuppose you have to make a series of decisions, among various choices, where yYou don’t have enough information to know what to choose yEach decision leads to a new set of choices ySome sequence of choices (possibly more than one) may be a solution to your problem zBacktracking is a methodical way of trying out various sequences of decisions, until you find one that “works”

70 Backtracking (animation) start? ? dead end ? ? ? success! dead end

71 Terminology I There are three kinds of nodes: A tree is composed of nodes The (one) root node Internal nodes Leaf nodes Backtracking can be thought of as searching a tree for a particular “goal” leaf node

72 Terminology II zEach non-leaf node in a tree is a parent of one or more other nodes (its children) zEach node in the tree, other than the root, has exactly one parent parent children parent children Usually, however, we draw our trees downward, with the root at the top

73 Real and virtual trees zThere is a type of data structure called a tree yBut we are not using it here zIf we diagram the sequence of choices we make, the diagram looks like a tree yIn fact, we did just this a couple of slides ago yOur backtracking algorithm “sweeps out a tree” in “problem space”

74 The backtracking algorithm zBacktracking is really quite simple--we “explore” each node, as follows: zTo “explore” node N: 1. If N is a goal node, return “success” 2. If N is a leaf node, return “failure” 3. For each child C of N, 3.1. Explore C 3.1.1. If C was successful, return “success” 4. Return “failure”


Download ppt "RELATIONAL ALGEBRA and Computer Assignment 1 Prof. Sin-Min LEE Department of Computer Science."

Similar presentations


Ads by Google