Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMPS263F Unit 2 Discrete Structures Li Tak Sing( 李德成 ) Room A

Similar presentations


Presentation on theme: "COMPS263F Unit 2 Discrete Structures Li Tak Sing( 李德成 ) Room A"— Presentation transcript:

1 COMPS263F Unit 2 Discrete Structures Li Tak Sing( 李德成 ) tsli@ouhk.edu.hk Room A0936 27686816 1

2 Unit 2 Relations If R is a subset of A 1  A 2 ...  A n, then, R is said to be an n-ary relation on A 1  A 2 ...  A n. For example, assume A 1 ={tom, peter, kelvin}, A 2 ={mary, jessy}. Now, tom, peter are husbands of mary and jessy respectively. If we define R to a relation that represents marriage, then tom and mary are related this way. COMPS263F Unit 2 2

3 So R={(tom,mary),(peter,jessy)}. Note that R  A 1  A 2. Let A={1,4}, B={3,9}. Let R represents the relation that an integer from A is less than an integer from B. So R contains the following elements: {(1,3),(1,9),(4,9)}. We can also write R as: R={(x,y)|x  A and y  B and x<y} COMPS263F Unit 2 3

4 If R is a relation and (x 1,x 2,...,x n )  R, we would write: R(x 1,x 2,...,x n ). For a binary relation, R(x 1,x 2 ) is usually written as x 1 R x 2. COMPS263F Unit 2 4

5 An example. Assume that A={1,2,..,10}. Find the relation R so that: R={(x,y)| x,y  A and x 2 =y} COMPS263F Unit 2 5

6 Solution R={(1,1),(2,4),(3,9)} COMPS263F Unit 2 6

7 Examples Represent each relation as a set by listing each individual tuple. 1.{d| d  N and d>0 and d divides 12} 2.{(d,n)| d,n  {2,3,4,5,6} and d divides n} 3.{(x,y,z)|x=y+z, where x,y,z  {1,2,3}} COMPS263F Unit 2 7

8 Solution 1.{1,2,3,4,6,12} 2.{(2,2),(2,4),(2,6),(3,3),(3,6),(4,4),(5,5),(6,6 )} 3.{(2,1,1),(3,1,2),(3,2,1)} COMPS263F Unit 2 8

9 Relations problems 1.Let R be the binary relation defined on the set N5={0,1,2,3,4} by xRy iff y=x+1. Write R as a set by listing each individual tuple. 2.Evaluate {(x,y}| x>y and x,y  {1,2,3}}. 3.Evaluate {(x,y) | x,y  N and 1  x+y<4} COMPS263F Unit 2 9

10 Solution 1.{(0,1),(1,2),(2,3),(3,4)} 2.{(2,1),(3,1),(3,2)} 3.{(0,1),(0,2),(0,3),(1,0),(1,1),(1,2),(2,0),(2,1 ),(3,0)} COMPS263F Unit 2 10

11 Proofs and Challenges Prove each of the following statements about combining set operations with Cartesian product. 1.(A  B)  C=(A  C)  (B  C) 2.(A-B)  C=(A  C)-(B  C) COMPS263F Unit 2 11

12 Solution 1.(x,y)  (A  B)  C  x  (A  B) and y  C  (x  A or x  B) and y  C  (x  A and y  C) or (x  B and y  C)  (x,y)  A  C or (x,y)  B  C  (x,y)  (A  C)  (B  C) COMPS263F Unit 2 12

13 Solution 2.We can use truth table to prove that if P,Q and R are logic statements, then (P and not Q) and R = (P and R) and not (Q and R) 2.(x,y)  (A-B)  C  (x  A and not (x  B)) and y  C  (x  A and y  C) and not (x  B and y  C)  (x,y)  (A  C)-(B  C) COMPS263F Unit 2 13

14 Let L, M and N be languages. Prove each of the following properties of the product operation on languages. 1.L{  }={  }L=L 2.L  =  L=  3.L(M  N)=LM  LN 4.(M  N)L=ML  NL COMPS263F Unit 2 14

15 Solution 1.s  L{  }  there are x,y s.t. xy=s and x  L, y  {  }  y=   xy=x  xy  L  s  L for any s  L, s=s   L{  } therefore, L{  }=L. Similarly, we can prove that {  }L=L COMPS263F Unit 2 15

16 Solution 2.s  L   there are x,y s.t. s=xy, x  L and y   false (y  is false)  s   therefore, L    Obviously,   L  (  is the subset of all sets) therefore, L  =  Similarly,  L=  COMPS263F Unit 2 16

17 Solution 3.s  L(M  N)  there are x,y s.t. s=xy, x  L and y  M  N  x  L and (y  M or y  N)  (x  L and y  M) or (x  L and y  N)  xy  LM or xy  LN  xy  LM  LN  s  LM  LN COMPS263F Unit 2 17

18 If L, M are languages, prove that 1.L  L* 2.L  LM* and L  M*L 3.{  }*={  } 4.L*=L*L*=(L*)* 5.All languages that formed from L and M must be subset of (L  M)* COMPS263F Unit 2 18

19 Solution 1.L*=L 0  L 1 .... Thefore L= L 1  L* 2.since {  }  M*  L=L {  }  LM* similarly, we can prove that L  M*L COMPS263F Unit 2 19

20 Solution 3.{  } 0 = {  } Assume that {  } k = {  } where k≥0 {  } k+1 = {  } {  } = {  } Therefore, {  } n = {  } where n≥0 {  } * = {  } 0  {  } 1 ... = {  }  {  } ... = {  } COMPS263F Unit 2 20

21 Solution 4.Obviously, L*=L*{  }  L*L* Now, we want to prove that L*L*  L* s  L*L*  there are x,y s.t. s=xy and x  L* and y  L*  x,y are formed with elements of L  xy  L* Therefore L*L*  L* Therefore L*L*=L* COMPS263F Unit 2 21

22 Solution 4.Obviously, L*  (L*)* Now, we want to prove that (L*)*  L* s  (L*)*  s is formed by elements of L*  each of these elements is formed by elements of L  s is formed by elements of L  s  L* Therefore (L*)*  L* Therefore (L*)*=L* COMPS263F Unit 2 22

23 Solution 5.s  any language formed by M and L  s is formed by elements from either M or L  s is formed by elements of M  N  s  (M  N)* Therefore, the language must be a subset of (M  N)*. COMPS263F Unit 2 23

24 Counting examples 1.Calculate the number of strings over the alphabet {a,b} that have length 7, begin with a, and contain at least one b. 2.Calcuate the number of strings over the alphabet {a,b,c} that have length 6, begin with an a or b, and contain at least one c. COMPS263F Unit 2 24

25 Solution 1.The number of choices for the 1st character is 1. The number of choices for the rest=(all possible cases)-(only a's cases)=2 6 -1. Therefore answer=1  (2 6 -1) 2.The number of choices for the 1st character=2. The number of cases for the rest=(all possible cases)-(only a or b cases)= 3 5 -2 5. Therefore answer=2  (3 5 - 2 5 ) COMPS263F Unit 2 25

26 Graphs A graph is a set of objects in which some of the objects are connected to each other in some ways. The objects are called vertices or nodes and connections are called edges. COMPS263F Unit 2 26

27 Graph example COMPS263F Unit 2 27

28 Directed graph A directed graph is a graph where each edge points in one direction. For example, the vertices could be cities and the edges could be the one-way air routes between them. COMPS263F Unit 2 28

29 Representation of graphs A graph can be represented by an ordered pair (V,E) where V is a set of vertices. E is a set of edges. If the graph is non-directed, then an edge can be represented as a set that contains the two vertices. In the second last example, V={1,2,3,4,5,6}, E={{1,2},{1,5},{2,5},{2,3},{3,4},{4,5},{4,6}} COMPS263F Unit 2 29

30 In the last example, G=(V,E) where V={A,B,C} and E={(A,B),(A,C),(B,C),(C,A)} COMPS263F Unit 2 30

31 Weighted Graphs An edge can have value itself. For example, if a graph represents highways between different cities, and an edge represents a highway, then every edge can have the information that represents the length of the highway. Or it can have the value represents the cost to travel the highway. COMPS263F Unit 2 31

32 Weighted graphs Such information is called the weight of the edge. Such edges are represented by 3-tuples: (a,b,w) where a, b are the vertices and w is the weight. COMPS263F Unit 2 32

33 Graphs and Binary Relations A binary relation R on a set A can be thought of as a digraph G=(A,R) with vertices A and edges R. Let R={(3,4),(1,3),(3,2),(1, 1)}, the graph is: COMPS263F Unit 2 33

34 Subgraphs A graph (V’,E’) is a subgraph of a graph (V,E) if V’  V and E’  E. COMPS263F Unit 2 34

35 Paths in Graphs A path from x 0 to x n is a sequence of edges that we denoted by a sequence of vertices x 0, x 1,..., x n such that there is an edge from x i-1 to x i from 1  i  n. A path allows the possibility that some edges or some vertices occurs more than once. A cycle is a path whose beginning and ending vertices are equal and in which no edge occurs more than once. COMPS263F Unit 2 35

36 A graph with no cycles is called acyclic. The length of the path x 0,...,x n is the number n of edges. COMPS263F Unit 2 36

37 Connected graphs A graph is connected if there is a path between every pair of vertices. A directed graph is connected if, when direction is ignored, the resulting undirected graph is connected. COMPS263F Unit 2 37

38 Graph traversals A graph traversal starts at some vertex v and visits all vertices x that can be reached from v to x. If a vertex has already been visited, it is not visited again. Two popular traversal algorithms are called breadth-first and depth-first. COMPS263F Unit 2 38

39 Breadth-first traversal If visit(v,k) denote the procedure that visits every vertex x not yet visited for which there is a length k path from v to x. In the breadth-frist traversal, it can be described as: for k:=0 to n-1 do visit(v,k) od. COMPS263F Unit 2 39

40 Breadth-first traversal example a b c d e f If you want to travel from b with breadth first traversal, then the first node visited is b, then d or a, then e,f,c. COMPS263F Unit 2 40

41 Depth-first traversal If DF(v) represents the depth-first procedure that traverses the graph starting at vertex v. Then DF(v) has the following definition. DF(v): if v has not been visited then visit v; for each edge from v to x do DF(x) od fi COMPS263F Unit 2 41

42 Depth-first traversal In the last graph, if we travel from f and visit all nodes using the depth-first traversal method, one possible solution is: f,d,c,b,a,e COMPS263F Unit 2 42

43 Trees A tree is a graph with no cycles. The vertices and edges of a tree are called nodes and braches, respectively. Trees are usually shown as upside down versions of real trees. COMPS263F Unit 2 43

44 Trees The node at the top is called the root. The nodes that hang immediately below a given node are its children, and the node immediately above a given node is its parent. If a node is childless, then it is a leaf. The height or depth of a tree is the length of the longest path from the root to its leaves. The path from a node to the root contains all the ancestors of the node. Any path from a node to a leaf contains descendants of the node. COMPS263F Unit 2 44

45 Trees A tree with a designated root is often called a rooted tree. Otherwise, it is called a free tree or an unrooted tree. COMPS263F Unit 2 45

46 Subtrees If x is a node in a tree T, then x together with all its descendants forms a tree S with x as its root. S is called a subtree of T. If y is the parent of x, then S is sometimes called a subtree of y. COMPS263F Unit 2 46

47 Ordered and unordered trees If we don’t care about the ordering of the children of a tree, then the tree is called an unordered tree. A tree is ordered if there is a unique ordering of the children of each node. COMPS263F Unit 2 47

48 Representation of trees A tree can be represented as list where a represents the root and b, c are subtrees of a. For example, the following tree:,, >, > COMPS263F Unit 2 48

49 Binary trees A binary tree is an ordered tree that may be empty or else has the property that each node has two subtrees, called the left and right substrees of the node, which are binary trees. COMPS263F Unit 2 49

50 Binary Search Trees Binary trees can be used to represent sets whose elements have some ordering. Such a tree is called a binary search tree and has the property that for each node of the tree, each element in the left subtree precedes the node element and each element in its right subtree succeeds the node element. COMPS263F Unit 2 50

51 Spanning trees A spanning tree for a connected graph is a subgraph that is a tree and contains all the vertices of the graph. COMPS263F Unit 2 51

52 Minimal spanning tree A minimal spanning tree for a connected weighted graph is a spanning tree such that the sum of the edge weights is minium among all spanning trees. COMPS263F Unit 2 52

53 Prim’s algorithm 1.Initialize S:= . 2.Pick any vertex v  V and W:={v}. 3.While W  V do Find a minimum weight edge {x,y}, where x  W and y  V-W; S:=S  {{x,y}}; W:=W  {y} od COMPS263F Unit 2 53

54 Minimal spanning tree WS c c,f{c,f} c,f,d{c,f},{c,d} c,f,d,b{c,f},{c,d},{f,b} c,f,d,b,a{c,f},{c,d},{f,b},{d, a} Total weight=1+2+2+3=8 COMPS263F Unit 2 54

55 Graph and tree examples Given the following graph. A B C D F G H Write down the vertices of the graph in the order that they are visited by a breadth-first search of the graph that starts at vertex A. COMPS263F Unit 2 55

56 Solution {A},{B,C,F},{D,G,H} COMPS263F Unit 2 56

57 Write down the vertices of the graph in the order that they are visited by a depth-first search of the graph that starts at vertex A. COMPS263F Unit 2 57

58 Solution A,B,D,G,H,F,C (There are many other solutions) COMPS263F Unit 2 58


Download ppt "COMPS263F Unit 2 Discrete Structures Li Tak Sing( 李德成 ) Room A"

Similar presentations


Ads by Google