Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Chapter 1 - 2 Elementary Notions and Notations.

Similar presentations


Presentation on theme: "1 Chapter 1 - 2 Elementary Notions and Notations."— Presentation transcript:

1 1 Chapter 1 - 2 Elementary Notions and Notations

2 2 Section 1.3 Ordered Structures Tuples: Have order and can have repetitions. For example, (6, 7, 6) is a 3-tuple and ( ) is the empty tuple. We write (x 1, …, x n ) = (y 1, …, y n ) to mean x i = y i for 1 ≤ i ≤ n.

3 3 Cartesian Product Cartesian Product: A ╳ B = {(x, y) | x ∊ A and y ∊ B}. The definition extends to more than two sets. For example, A ╳ B ╳ C = {(x, y, z) | x ∊ A, y ∊ B, z ∊ C}. Notation: A 0 = {( )}, A 1 = {(x) | x ∊ A}, and in general, A n = {(x 1, …, x n ) | x i ∊ A}. Quiz (1 minute). Does (A ╳ B) ╳ C = A ╳ (B ╳ C)?

4 4 Lists Lists: Are like tuples but there is no random access. For example, is a list with 4 elements and <> is the empty list. The operations on lists are head, tail, and cons. For example, head( ) = a tail( ) = cons(a, ) = The set of lists whose elements are in A is denoted by lists(A). Quiz (1 minute). For L =, b, >, find head(L) and tail(L).

5 5 Strings Strings: Are like lists, but are represented as juxtaposed elements from a given alphabet. For example, if A = {a, b}, then some strings over A are: a, b, aa, ab, ba, bb, aaa, bbb. The empty string is denoted by ∧. The concatenation of two strings is their juxtaposition. For example, the concatenation of ab and bab is abbab. For any string s we have s ∧ = ∧ s = s. If s is a string, s n denotes the concatenation of s with itself n times. Also s 0 = ∧. For example, (ab) 3 = ababab.

6 6 Languages A language is a set of strings, usually taken over some alphabet. Notation: If A is an alphabet, then the set of all strings over A is denoted by A*. Example. Some languages over A are: Ø, { ∧ }, A, and A*. Example. {ab n a | n ∊ N} = {aa, aba, abba, abbba, … } is a language over {a, b}.

7 7 Language Operations Let L and M be languages. The product of L and M, denoted LM, is the language LM = {st | s ∊ L and t ∊ M}. Notation: L 0 = { ∧ }; and L n = {s 1 …s n | s i ∊ L}.

8 8 Quizzes Quiz (1 minute). What are the products LØ and L{ ∧ }? Quiz (1 minute). Solve for L in the equation { ∧, a, b}L = { ∧, a, b, aa, ba, aba, bba}.

9 9 closure The closure L* is the set of all possible concatenations of strings in L. So L* = L 0 U L 1 U … U L n U … Quiz (1 minute). What are { ∧ }* and Ø*?

10 10 Example Examine the structure of an arbitrary string x ∊ L*(ML)*. A solution: Use the definitions to write x in terms of strings in L and M. 1. Since x ∊ L*(ML)*, it follows that x = uv where u ∊ L* and v ∊ (ML)*. 2. Since u ∊ L*, either u = ∧ or u = s 1 …s n for some n where s i ∊ L. 3. Since v ∊ (ML)*, either v = ∧ or v = r 1 t 1 …r k t k for some k where r i ∊ M and t i ∊ L. So x has one of four forms: ∧, s 1 …s n, r 1 t 1 …r k t k, or s 1 …s n r 1 t 1 …r k t k.

11 11 Relations A relation is a set of tuples. If R is a relation and (x 1, …, x n ) ∊ R, we write R(x 1, …, x n ). We can usually represent a relation as a subset of some cartesian product. Example. Let R = {(0, 0), (1, 1), (4, 2), (9, 3), …, (n 2, n), …} = {(n 2, n) | n ∊ N}. We might call R the “is square of” relation on N. Notice also that R ⊂ N ╳ N. Notation: If R is binary, we can use infix to represent pairs in R. For example, from the previous example, we have (9, 3) ∊ R. So we can also write R(9, 3) or 9 R 3 or 9 is square of 3.

12 12 Relational Databases A relational database is a relation where the indexes of a tuple have associated names called attributes.

13 13 Example Let Students = {(x, y, z) | x is a Name, y is a Major, and z is Credits}. Who are the people majoring in CS? {x | (x, CS, z) ∊ Students, for some z}. Note: We need “for some z” to indicate that z is a variable. How many math majors are upper division students? | {x | (x, math, z) ∊ Students and z ≥ 90} |. What is the major of AbeLincoln? {y | (AbeLincoln, y, z) ∊ Students, for some z}. What is the history department database of names and their credits? {(x, z) | (x, history, z) ∊ Students}.

14 14 Counting Tuples (or strings or lists) Product Rule: | A ╳ B | = | A | | B | and | A n | = | A | n. Example. If A = {a, b} and B = {1, 2, 3}, then A ╳ B = {(a, 1), (a, 2), (a, 3), (b, 1), (b, 2), (b, 3)}. So | A ╳ B | = 6 = (2)(3) = | A | | B |.

15 15 Example Count the number of strings of length 8 over A = {a, b, c} that begin with either a or c and have at least one b. A Solution: Split the problem up into easier problems and combine the results (divide and conquer). Let U be the universe consisting of the strings over A of length 8 that begin with either a or c. Let B be the subset of U consisting of strings with no b’s. Then the set of strings to count is U – B, as pictured. U-BU B

16 16 Example (cont’d) It is easy to calculate the cardinality of U – B: | U – B | = | U | – | U ∩ B | = | U | – | B | (since B is a subset of U) It is also easy to count U because it has the same cardinality as the set {a, c} ╳ A 7, which is | {a, c} ╳ A 7 | = | {a, c} | | A 7 | = | {a, c} | | A | 7 = (2)3 7. It is also easy to count B because it has the same cardinality as the set {a, c} 8, which is | {a, c} 8 | = | {a, c} | 8 = 2 8. So we have the answer: | U – B | = | U | – | U ∩ B | = | U | – | B | = (2)3 7 – 2 8, which is 4118.

17 17 Section 1.4 Graphs and Trees A graph is set of objects called vertices or nodes where some pairs of objects may be connected by edges. (A directed graph has edges that point in one direction.)

18 18 Example Draw a graph of the South American countries that touch the Pacific Ocean and their neighbors, where the vertices are countries and an edge indicates a common border. Vertices = {Co, V, E, Br, P, Bo, Ch, A} Edges = {{Co, V}, {Co, E}, ….}. Co V E P Ch Bo Br A

19 19 Some definitions A path from vertex x 0 to x n is a sequence of edges that we denote by vertices x 0, x 1, …, x n, where there is an edge from x i–1 to x i for 1≤ i ≤ n. The length of a path is the number of edges. A cycle is a path with distinct edges that begins and ends at the same vertex. Example. A, Bo, A, is not a cycle since the edge{A, Bo} occurs twice. A, Bo, Br, A, is a cycle.

20 20 Quiz (1 minute) What is a longest path from A to V with distinct edges and no cycles? Answer: The length is 6. For example, A, Bo, Br, P, E, Co, V.

21 21 n-colorable A graph is n-colorable if it’s vertices can be colored with n colors with distinct colors for adjacent vertices. The chromatic number of a graph is the smallest such n. Quiz (1 minute). What is the chromatic number of the example graph?

22 22 Graph Traversals A graph traversal starts at some vertex v and visits all vertices x that can be reached by a path from v to x. But don’t visit any vertex more than once.

23 23 Breadth-First If the graph has n vertices then start with a vertex v and do the following: for k := 0 to n – 1 do visit(v, k) od where visit(v, k) visits all x not visited if there is a path from v to x of length k.

24 24 Quizzes Use the pictured graph for the following quizzes. Quiz (1 minute). Find a breadth- first traversal that starts at F. One answer: F, H, D, G, B, A, E, C. Quiz (1 minute). Find a breadth- first traversal that starts at C. One answer: C, A, E, D, B, F, H, G. A B D F H G E C

25 25 Depth-First Start at a vertex v and call the procedure D(v), which is defined as follows: D(v): if v has not been visited then visit(v); for each edge from v to x do D(x) od fi

26 26 Quizzes Quiz (1 minute). Find a depth-first traversal of the pictured graph that starts at F. One answer: F, H, G, D, B, A, C, E. Quiz (1 minute). Find a depth-first traversal of the pictured graph that starts at E. One answer: E, D, F, H, G, A, C, B.

27 27 Trees A tree is a connected graph (a path between any two points) with no cycles. Most trees are oriented so that they look like upside-down trees, such as the tree pictured. The top node is the root, the nodes directly below a node are its children, the node directly above a node is the parent, the bottom nodes are leaves, and the height or depth of the tree is the length of the longest path of distinct edges from root to a leaf. Example. For this tree the root is A. The children of A are B, C, D. D is the parent of G. The height or depth of the tree is 3. The leaves are E, F, C, H, I. A BCD EFG H I

28 28 A Recursive Definition Any node of a tree is the root of a subtree. One way to represent a tree is as a list whose head is the root of the tree and whose tail is the list of subtrees, where each subtree is represented in the same way. Example. The pictured tree can be represented by the list, >,,, >>>.

29 29 Expression Tree Any algebraic expression can be represented as a tree. For example, the tree for the expression (x – y) + log(z + w) is pictured to the right. Quiz (1 minute). Do a depth-first (left to right) traversal. Answer: + – x y log + z w. This is the prefix form of the expression. + - log x y + z w

30 30 Binary Trees A binary tree is either empty, denoted by <>, or each node has two subtrees that are binary trees and are called the left and right subtrees of the node. If a binary tree is not empty, we’ll represent it as a list of the form, where x is the root and L and R are the left and right subtrees, respectively. Example. The binary tree with a single node x is denoted by, x, <>>.

31 31 binary search tree A binary search tree represents ordered information, where the predecessors and successors of a node are in its left and right subtrees, respectively. Example. A binary search tree for the first six prime numbers is pictured. 7 3 11 2513

32 32 Spanning Trees A spanning tree for a connected graph is a tree whose nodes are the nodes of the graph and whose edges are a subset of the edges of the graph. A minimal spanning tree minimizes the sum of weights on the edges of all spanning trees.

33 33 Example Use Prim’s algorithm to construct a minimal spanning tree for the pictured graph, starting with node D. Solution: A minimal spanning tree is constructed in 4 steps AB C D E 3 1 3 2 3 2 1 B D 1111 BBB DD D CC C 2 2 2 AA 1 1 E 2

34 34 The End of Chapter 1 – 2


Download ppt "1 Chapter 1 - 2 Elementary Notions and Notations."

Similar presentations


Ads by Google