Presentation is loading. Please wait.

Presentation is loading. Please wait.

DISCRETE MATHEMATICS I LECTURES CHAPTER 10 Dr. Adam Anthony Spring 2011 Some material adapted from lecture notes provided by Dr. Chungsim Han and Dr. Sam.

Similar presentations


Presentation on theme: "DISCRETE MATHEMATICS I LECTURES CHAPTER 10 Dr. Adam Anthony Spring 2011 Some material adapted from lecture notes provided by Dr. Chungsim Han and Dr. Sam."— Presentation transcript:

1 DISCRETE MATHEMATICS I LECTURES CHAPTER 10 Dr. Adam Anthony Spring 2011 Some material adapted from lecture notes provided by Dr. Chungsim Han and Dr. Sam Lomonaco

2 Section

3 Introduction to Graphs 3 Definition: A simple graph G = (V, E) consists of V, a nonempty set of vertices, and E, a set of unordered pairs of distinct elements of V called edges. For each e E, e = {u, v} where u, v V. An undirected graph (not simple) may contain: Loops: An edge e is a loop if e = {u, u} for some u V Duplicate edges: A graph is called a multi-graph if there is at least one duplicate edge.

4 Introduction to Graphs 4 Definition: A directed graph G = (V, E) consists of a set V of vertices and a set E of edges that are ordered pairs of elements in V. For each e E, e = (u, v) where u, v V. An edge e is a loop if e = (u, u) for some u V. A simple graph is just like a directed graph, but with no specified direction of its edges.

5 Graph Models 5 Example I: How can we represent a network of (bi- directional) railways connecting a set of cities? We should use a simple graph with an edge {a, b} indicating a direct train connection between cities a and b. New York Boston Washington Lübeck Toronto Hamburg

6 Graph Models 6 Example II: In a round-robin tournament, each team plays against each other team exactly once. How can we represent the results of the tournament (which team beats which other team)? We should use a directed graph with an edge (a, b) indicating that team a beats team b. Penguins Bruins Lübeck Giants Maple Leafs

7 Exercise 1 7 What might the nodes/edges be if we modeled the following data? Would the graph be best undirected or directed? Acquaintance Graph: Identifying people are mutually acquainted Influence Graph: Identifying data where one person has influence over another. Computer network Road Map The World Wide Web

8 Graph Terminology 8 Definition: Two vertices u and v in an undirected graph G are called adjacent (or neighbors) in G if {u, v} is an edge in G. If e = {u, v}, the edge e is called incident with the vertices u and v. The edge e is also said to connect u and v. The vertices u and v are called endpoints of the edge {u, v}.

9 Graph Terminology 9 Definition: The degree of a vertex in an undirected graph is the number of edges incident with it, except that a loop at a vertex contributes twice to the degree of that vertex. In other words, you can determine the degree of a vertex in a displayed graph by counting the lines that touch it. The degree of the vertex v is denoted by deg(v).

10 Graph Terminology 10 A vertex of degree 0 is called isolated, since it is not adjacent to any vertex. Note: A vertex with a loop at it has at least degree 2 and, by definition, is not isolated, even if it is not adjacent to any other vertex. A vertex of degree 1 is called pendant. It is adjacent to exactly one other vertex.

11 Graph Terminology 11 Example: Which vertices in the following graph are isolated, which are pendant, and what is the maximum degree? What type of graph is it? Solution: Vertex f is isolated, and vertices a, d and j are pendant. The maximum degree is deg(g) = 5. This graph is a non-simple undirected graph. a bc d f h g j f e

12 Graph Terminology 12 Determine the number of its edges and the sum of the degrees of all its vertices: a bc d f h g j f e Result: There are 9 edges, and the sum of all degrees is 18. This is easy to explain: Each new edge increases the sum of degrees by exactly two.

13 Graph Terminology 13 The Handshaking Theorem: Let G = (V, E) be an undirected graph with e edges. Then 2e = v V deg(v) Corrolary: The total degree of any undirected graph is always even! Example: How many edges are there in a graph with 10 vertices, each of degree 6? Solution: The sum of the degrees of the vertices is 6 10 = 60. According to the Handshaking Theorem, it follows that 2e = 60, so there are 30 edges.

14 Graph Terminology 14 Theorem: An undirected graph has an even number of vertices of odd degree. Proof: Let V1 and V2 be the set of vertices of even and odd degrees, respectively (Thus V1 V2 =, and V1 V2 = V). Then by Handshaking theorem 2|E| = v V deg(v) = v V1 deg(v) + v V2 deg(v) Since both 2|E| and v V1 deg(v) are even, v V2 deg(v) must be even. Since deg(v) is odd for all v V2, |V2| must be even. QED

15 Exercise 2 15 Draw a graph with the specified properties or show that no such graph exists: A graph with 6 vertices with the following degrees: 1,1,2,2,3,4 A graph with 4 vertices of degrees 1,2,3,4 A simple graph with 4 vertices of degrees 1,2,3,4

16 Exercise 3 16 A graph has 5 vertices of degrees 1,1,4,4, and 6. How many edges does the graph have? Is it possible in a group of 13 people for each to shake hands with exactly 7 others? Is it possible to have a graph with 15 edges where each vertex has degree 4? Is it possible to have a simple graph with 10 edges where each vertex has degree 4?

17 Graph Terminology 17 Definition: When (u, v) is an edge of the graph G with directed edges, u is said to be adjacent to v, and v is said to be adjacent from u. The vertex u is called the initial vertex (or source) of (u, v), and v is called the terminal vertex (or target) of (u, v). The initial vertex and terminal vertex of a loop are the same.

18 Graph Terminology 18 Definition: In a graph with directed edges, the in- degree of a vertex v, denoted by deg - (v), is the number of edges with v as their terminal vertex. The out-degree of v, denoted by deg + (v), is the number of edges with v as their initial vertex. Question: How does adding a loop to a vertex change the in-degree and out-degree of that vertex? Answer: It increases both the in-degree and the out- degree by one.

19 Graph Terminology 19 Example: What are the in-degrees and out-degrees of the vertices a, b, c, d in this graph: a b cd deg - (a) = 1 deg + (a) = 2 deg - (b) = 4 deg + (b) = 2 deg - (d) = 2 deg + (d) = 1 deg - (c) = 0 deg + (c) = 2

20 Graph Terminology 20 Theorem: Let G = (V, E) be a graph with directed edges. Then: v V deg - (v) = v V deg + (v) = |E| This is easy to see, because every new edge increases both the sum of in-degrees and the sum of out-degrees by one.

21 Exercise 4 21 What is the maximum number of edges possible in a simple graph on n vertices? What is the maximum number of edges possible in a directed graph on n vertices (loops included)?

22 Special Graphs 22 Definition: The complete graph on n vertices, denoted by K n, is the simple graph that contains exactly one edge between each pair of distinct vertices. K1K1 K2K2 K3K3 K4K4 K5K5

23 Exercise 5 23 What is the degree of each vertex in the complete graph K 9 ? What is the total degree of K 9 ? How many edges are there in K 9 ? How many edges are there in K n ? What is the degree of a vertex in K n ? What is the total degree of K n ?

24 Special Graphs 24 Definition: The cycle C n, n 3, consists of n vertices v 1, v 2, …, v n and edges {v 1, v 2 }, {v 2, v 3 }, …, {v n-1, v n }, {v n, v 1 }. C3C3 C4C4 C5C5 C6C6

25 Special Graphs 25 Definition: We obtain the wheel W n when we add an additional vertex to the cycle C n, for n 3, and connect this new vertex to each of the n vertices in C n by adding new edges. W3W3 W4W4 W5W5 W6W6

26 Special Graphs 26 Definition: The n-cube, denoted by Q n, is the graph that has vertices representing the 2 n bit strings of length n. Two vertices are adjacent if and only if the bit strings that they represent differ in exactly one bit position. Q1Q1 Q2Q2 Q3Q

27 Special Graphs 27 Definition: A simple graph is called bipartite if its vertex set V can be partitioned into two disjoint nonempty sets V 1 and V 2 such that every edge in the graph connects a vertex in V 1 with a vertex in V 2 (so that no edge in G connects either two vertices in V 1 or two vertices in V 2 ). For example, consider a graph that represents each person in a village by a vertex and each marriage by an edge. This graph is bipartite, because each edge connects a vertex in the subset of males with a vertex in the subset of females (if we think of traditional marriages).

28 Special Graphs 28 Example I: Is C 3 bipartite? v1v1 v2v2 v3v3 No, because there is no way to partition the vertices into two sets so that there are no edges with both endpoints in the same set. Example II: Is C 6 bipartite? v5v5 v1v1 v2v2 v3v3 v4v4 v6v6 v1v1 v6v6 v2v2 v5v5 v3v3 v4v4 Yes, because we can display C 6 like this:

29 Special Graphs 29 Definition: The complete bipartite graph K m,n is the graph that has its vertex set partitioned into two subsets of m and n vertices, respectively. Two vertices are connected if and only if they are in different subsets. K 3,2 K 3,4

30 Exercise 6 30 Draw the complete bipartite graphs for K 2,2, K 2,3, and K 3,4

31 Exercise 7 31 Determine a formula for the number of edges in a complete bipartite graph.

32 Exercise 8 32 What is the degree of each vertex in the complete bipartite graph K 4,5 ?

33 Operations on Graphs 33 Definition: A subgraph of a graph G = (V, E) is a graph H = (W, F) where W V and F E. Note: Of course, H is a valid graph, so we cannot remove any endpoints of remaining edges when creating H. Example: K5K5 subgraph of K 5

34 Operations on Graphs 34 Definition: The union of two simple graphs G 1 = (V 1, E 1 ) and G 2 = (V 2, E 2 ) is the simple graph with vertex set V 1 V 2 and edge set E 1 E 2. The union of G 1 and G 2 is denoted by G 1 G 2. G1G1 G2G2 G 1 G 2 = K 5

35 Exercise 9 35 Let G be a simple graph with V = {a,b,c,d,e} and E = {{a,a},{a,b},{a,c},{b,c},{c,d}}. Is H = (V H,E H ) with V H ={a,b,c,d} and E H ={{a,c},{b,c},{c,d}} a subgraph of G? If so, find a second subgraph L such that H L = G

36 Representing Graphs, Walks, Paths and Circuits, Connectedness Section

37 Representing Graphs 37 a b c d a b c d a, db c a, b, cd b, c, da Adjacent Vertices Vertex ab c a, b, cd ca Terminal Vertices Initial Vertex

38 Representing Graphs 38 Definition: Let G = (V, E) be a simple graph with |V| = n. Suppose that the vertices of G are listed in arbitrary order as v 1, v 2, …, v n. The adjacency matrix A (or A G ) of G, with respect to this listing of the vertices, is the n n zero-one matrix with 1 as its (i, j)th entry when v i and v j are adjacent, and 0 otherwise. In other words, for an adjacency matrix A = [a ij ], a ij = 1 if {v i, v j } is an edge of G, a ij = 0otherwise.

39 Representing Graphs 39 Example: What is the adjacency matrix A G for the following graph G based on the order of vertices a, b, c, d ?ab c d Solution: Note: Adjacency matrices of undirected graphs are always symmetric.

40 Graph Walks 40 Definition: A walk of length n from u to v, where n is a positive integer, in an undirected graph is a sequence of edges e 1, e 2, …, e n of the graph such that e 1 = {x 0, x 1 }, e 2 = {x 1, x 2 }, …, e n = {x n-1, x n }, where x 0 = u and x n = v. When the graph is simple, we denote this path by its vertex sequence x 0, x 1, …, x n, since it uniquely determines the path. The path or circuit is said to pass through or traverse x 1, x 2, …, x n-1.

41 Paths and Circuits 41 A trivial walk from v to v consists of the single vertex v, and no edges. The path is a closed-walk if it begins and ends at the same vertex, that is, if u = v. The length of a walk is the number of edges in the walk A trail is a walk with no repeated edges A path is a walk with no repeated vertices A circuit is a closed walk with no repeated edges A path or circuit is simple if it has no repeated vertices (except the first and last in a circuit). A non-trivial simple circuit is called a cycle

42 Exercise 1 42 In the given graph, determine whether each of the following is a path, simple path, circuit, or simple circuit 1. abcfb 2. abcf 3. fabfcdf 4. fabcdf 5. abfb 6. cfbbc 7. bb 8. e A BCD FE

43 Connectivity 43 Let us now look at something new: Definition: An undirected graph is called connected if there is a walk (or a path, or a simple path) between every pair of distinct vertices in the graph. For example, any two computers in a network can communicate if and only if the graph of this network is connected. Note: A graph consisting of only one vertex is always connected, because it does not contain any pair of distinct vertices.

44 Exercise 2: Connected or not? 44 d a b c e Yes. d ab c e No. d a b c e Yes. d ab c e f No.

45 Connectivity 45 A graph that is not connected is the union of two or more connected subgraphs, each pair of which has no vertex in common. A subgraph H is a connected component of a graph G if: H is connected H is not a proper subgraph of any connected subgraph of G It follows that a graph is connected G has only one connected component

46 Exercise 3 46 Example: What are the connected components in the following graph? a bc d g h i j f e Solution: The connected components are the graphs with vertices {a, b, c, d}, {e}, {f}, {g, h, i, j}.

47 Exercise 4 47 What is the minimum number of edges possible in a connected graph on 4 vertices?

48 Mathematical Induction 48 Let p(n) be a statement in an integer variable n. Suppose: i. p(1) is true [base step] ii. For all integers k 2, if p(k – 1) is true (induction hypothesis) To be shown: Given i and ii above, prove that p(k) must also be true

49 Exercise 5 49 Use induction on n to prove that a connected graph on n vertices has at least n – 1 edges.

50 Trees Section

51 What is a Tree? 51 A graph is called acyclic if it has no non-trivial circuits A tree is an acyclic, connected graph A trivial tree is a graph with a single vertex A forest is an acyclic, disconnected graph A tree is a special kind of simple graph

52 Exercise 1 52 Which of the following is a tree?

53 Tree ExampleDecision Tree 53

54 Tree Example: Directory Structures 54 C:\ WINDOWS\Documents and Settings\Program Files\ apanthon\katchins\ My Documents\Desktop\

55 Tree Example: Parse Tree 55 The young man caught the ball Sentence Verb PhraseNoun Phrase verbNoun Phrase caughttheball ArticleAdjectivenoun The youngman

56 Every Graph Contains A Tree 56 Let G be any graph If G has no cycles, then it is a tree! If G does have cycles, for each cycle Remove one edge from the cycle G will still be connected. Why? The resulting graph G will be an acyclic tree

57 Cyclical Proofs 57 How do we prove a b? Prove a b and b a a b means a and b are equivalent What if we say a, b, and c are equivalent? a b c ababcab c

58 Tree Properties 58 Let T = {V,E} be a graph. The following are equivalent: a) T is connected and removing any edge from T disconnects T into two subgraphs that are trees (subtrees) b) There is a unique path between any two distinct vertices v and w in T c) T is a tree Use a cyclical proof to show this is true

59 Proof: a b 59 Prove that if T is connected and removing any single edge results in two disconnected subgraphs, both of which are trees, then there is a unique path between any two vertices u and v Proof by contraposition: Prove that if there exists at least one alternative path between u and v, then either T is not connected or removing any edge will not result in two disconnected subgraphs.

60 Proof a b (continued) 60 Suppose T is any graph, such that there are two distinct paths between some vertices u and v. If there are two distinct paths, then there must be a cycle in the graph, found by following the first path from u to v, then reversing the second path to get back from v to u. We have already observed that removing an edge in a cycle will not disconnect a graph. Therefore, there is at least one edge which can be removed from this graph that will not disconnect T, which is what was to be shown.

61 Proof b c 61 Prove that if there exists a unique path between all pairs of vertices in T, then T must be a tree. Proof by contraposition that if T is not a tree, then there is not a unique path between all pairs of vertices in T There are two cases: T is not connected T has a cycle

62 Proof of b c (continued) 62 Case 1: T is not connected Then there must not be a path between at least two vertices u and v, which is what was to be shown. Case 2: T has a cycle Pick any edge {u,v} on the cycle. This represents a path from u to v. Now follow a path the long way around the cycle, starting at v and ending at u. This path is distinct from the first, which is what was to be shown.

63 Proof of c a 63 Prove that if T is a tree, then T is connected and removing any edge results in two disconnected subgraphs that are themselves trees. Suppose T is a graph that is a tree Then T is, by definition, connected and has no cycles Remove any one edge {u,v} from T. Since T has no cycles, then this edge represents a unique path from u to v because if there were another path, then {u,v} would complete a cycle Therefore, removing {u,v} disconnects the graph into two distinct components. The two subgraphs have no cycles, and since only one edge was removed, they are connected. Thus they are trees.

64 Proof: a b (OLD ORDER) 64 Prove that if T is a tree, then for any vertices u,v there is a unique path from u to, v By contradiction, suppose not, that T is a tree and there are two non-unique paths p 1 and p 2 from u to v. Since T is a simple graph, the paths can be reversed. Let p 2rev be the reversal of p 2. Following p 1, then p 2rev, we follow a conjoined path that starts at u, passes through v and ends at u. This is a cycle, contradicting the claim that T is a tree.

65 Proof: b c (OLD ORDER) 65 Prove that if every distinct pair of vertices u and v have a unique path, then removing one edge from T will result in two disconnected subgraphs that are trees Take any edge {x,y} in T. This edge identifies the one unique path between x and y. Removing this edge results in two disconnected subgraphs since there are no other paths connecting x and y. These subgraphs must be trees, since the presence of a cycle would imply that there is more than one path between two vertices (one that goes around the top of the cycle, one around the bottom)

66 More tree properties! 66 A tree T with n vertices has n-1 edges. Proof (by induction): A tree with n = 1 vertex has n – 1 = 0 edges. For a graph with n 2 vertices, suppose that any graph with n – 1 or fewer vertices has one less edge than it does vertices. Take a tree with n vertices and remove a single edge from the tree. This results in two smaller subtrees T 1 = (V 1,E 1 ) and T 2 = (V 2 E 2 ). n = |V| = |V 1 | + |V 2 | |E| = |E 1 | + |E 2 | + 1 By the induction hypothesis, |E 1 | = |V 1 | - 1 and |E 2 | = |V 2 | - 1 Then |E| = (|V 1 | - 1) + (|V 2 | - 1) + 1 = |V 1 | + |V 2 | - 1 = |V| - 1, which is what was to be shown

67 Exercise 2 67 Prove by contradiction that a finite tree with more than 1 vertex has at least 1 vertex of degree 1

68 Exercise 3 68 Is a graph with 12 vertices and 12 edges a tree? Is any graph with 5 vertices and 4 edges a tree? Is any connected graph with 5 vertices and 4 edges a tree?

69 Exercise 4 69 Let T be a graph on n vertices. Prove that the following are all equivalent: a) T is a tree b) T is connected and has n – 1 edges c) T is acyclic and has n – 1 edges

70 Rooted Trees and Tree Traversals Section

71 What is a rooted tree? 71 Root(level = 0) Internal Vertex (level = 1) Leaf(level = 2) Parent Child

72 Tree Definitions 72 The root is any vertex in a tree that is selected to be the root Level(v) = #edges it takes to reach v from the root height(T) = the maximum level of any vertices in T Children(v) all vertices adjacent to v whose level is Level(v) + 1 Parent(v) The unique vertex that is adjacent to v whose level is Level(v) – 1 A leaf is a node with no children. An ancestor v is any vertex w that lies on the path from the root to v. v would be considered a descendant of all such vertices.

73 Exercise 1 73 e v w r s x t z u y ih dc b a gf Find the: a) a)Level of e b) b)Height of the tree c) c)Children of t d) d)Parent of t e) e)Ancestors of g f) f)Descendants of t g) g)Leaves of the tree

74 Exercise 2 74 Design a tree to represent the table of contents of a book: C1 S1.1 S1.2 S1.3 C2 S2.1 S2.2 S2.2.1 S2.2.2 C3 S3.1 S3.2

75 Binary Trees 75 A Binary Tree is a rooted tree in which each internal vertex has at most two children Since there are only two, they get special names: Left child Right child If there is only one, call it the left child Subtrees get special names too! The left subtree of a vertex v is the subtree rooted at the left child of v The right subtree of a vertex v is the subtree rooted at the right child of v A full (or complete) binary tree is a binary tree in which each internal vertex has exactly two children

76 Exercise 3 76 Use a complete binary tree to represent the following mathematical expressions: a + b (a + b) ((c * d) – e)

77 M-ary Trees 77 An m-ary tree is a rooted tree in which each internal vertex has at most m children A full m-ary tree is an m-ary tree in which each internal vertex has exactly m children m = 2 (binary tree), m = 3 (ternary tree)

78 Theorem 1 78 Let T be a full m-ary tree with n vertices, i internal vertices, and L leaves. Then each of the following is true:

79 Corollary to Theorem 1 79 Let T be a full binary tree with n vertices, i internal vertices, and L leaves. Then each of the following is true:

80 Exercise 4 80 How many vertices does a full ternary tree with 11 leaves have? Is there a full binary tree with 12 vertices? How many edges does a full 5-ary tree with 100 internal vertices have? Is there a full binary tree that has 10 internal vertices and 13 leaves?

81 Exercise 5 81 The Wimbledon tennis championship is a single- elimination tournament in which a player is eliminated after a single loss. If 31 women compete in the championship, how many matches must be played to determine the champion?

82 Exercise 6 82 Suppose someone starts a chain letter. Each person who receives the letter is asked to send it to 5 other people. If everyone who receives the letter follows the instructions, how many people can be reached in a tree of height 2? If 125 people received the letter, but did not send it, determine the following: How many people sent the letter? How many people in total have seen the letter, including the person who started it?

83 Exercise 7 83 A computer lab has a single wall socket with 6 outlets in it. Using power strips with 6 connections each, how many extension cords do we need to power 46 all-in-one computers?

84 Balanced Trees 84 An m-ary tree of height h is balanced if every leaf is at level h or h – 1. Which of the following trees are balanced?

85 Number of Vertices in a Binary Tree Based on Height 85 If we have binary a tree of height h, what is the maximum number of vertices n we can have in that tree? Hint: its when every internal vertex has as many children as possible! Given the solution to the above, how many leaves are in the tree, given the height?

86 Minimum Binary Tree Height Based on Number of Vertices 86 If a binary tree has n vertices, what is the maximum height? More interesting, what is the minimum height? 2 0 n < 2 1, h n < 2 2, h n < 2 3, h n < 2 4, h 3 2 k n < 2 k+1, h k (Now lets solve this for k!) h log(n)

87 M-Ary Tree Version 87 In an m-ary tree of height h, what is the maximum number of vertices n? #V at Level 0: m 0,n 1 #V at Level 1: m 1,n m + 1 = (m 2 – 1)/(m – 1) #V at Level 2: m 2,n m 2 + m + 1 = (m 3 – 1)/(m – 1) #V at Level 3: m 3, n m 3 + m2 + m + 1 = (m 4 – 1)/(m – 1) #V at Level k: m k, n m k + … + m2 + m + 1 = (m k – 1)/(m – 1) Back to the chain-letter example, now how many vertices would there be if the letter was sent by everybody for 10 levels?

88 Theorem 88 Prove by induction that if T is an m-ary tree of height h, then the number of leaves l m h Then prove the corrolary that if T is an m-ary tree with l leaves, then log m l h

89 Exercise 8 89 A full binary tree has 500 leaves What is the minimum height of the tree? Is there a ternary tree of height 4 with 100 leaves?

90 YES THIS MATERIAL WILL BE ON THE EXAM!!! Binary Tree Traversals 90

91 Tree Traversals 91 In mathematics, were always studying tree properties, and they are useful In computing, were not just studying trees, we are storing and processing trees A tree traversal is a method for efficiently retrieving information from a tree There are three different traversals that have different applications: Pre-order In-order Post-order

92 Preorder Traversal of a Binary Tree 92 Let T be a rooted binary tree with root R, and left subtree T L and right subtree T R. The Preorder traversal of T is as follows: Visit R (print value, perform computation, etc.) Perform a preorder traversal on T L Perform a preorder traversal on T R A CB A B C

93 About Recursion 93 Recursion is weird…but really cool! The pre-order procedure is very easy to define because it uses itself as part of the definition! Visit R (print value, perform computation, etc.) Perform a preorder traversal on T L Perform a preorder traversal on T R If we follow this precisely, then it will gradually take us throughout the entire tree!

94 Result: Exercise 1 94 Find the pre-order traversal of the following tree (ROOT-L-R): r a dc b e ihgf j on klm psq Your Turn! Finish the traversal by performing a pre- order traversal of the right subtree.

95 Post-Order and In-Order Traversals 95 As before, let T be a rooted binary tree with root R, and left subtree T L and right subtree T R. The post-order traversal of T is: Perform a post-order traversal of T L Perform a post-order traversal of T R Visit R The in-order traversal of T is: Perform a post-order traversal of T L Visit R Perform a post-order traversal of T R A CB B C A A CB B A C

96 Exercise 2 96 Find the Post-Order (L-R-Root) traversal: r a dc b e ihgf j on klm psq

97 Exercise 3 97 Find the in-order (L-Root-R) traversal: r a dc b e ihgf j on klm psq

98 Arithmetic Trees, Revisited 98 Consider the tree for ((a + b) * (c – d)): Perform an in-order (L-Root-R) traversal of the tree How can we get the parentheses back? * + ba - dc

99 Arithmetic Trees, Revisited 99 Consider the tree for ((a + b) * (c – d)): Perform a pre-order traversal Such an expression is called the prefix form of an arithmetic computation * + ba - dc

100 Arithmetic Trees, Revisited 100 Consider the tree for ((a + b) * (c – d)): Perform a post-order traversal Such an expression is called the postfix form of an arithmetic computation * + ba - dc

101 Why pre-fix, post-fix matter 101 You dont need parentheses!!! Consider the prefix notation: * + a b – c d Scan right left. When you reach an operator, apply it to the two values to the right Then replace the three items with the single answer Try it! *

102 Computing Post-fix Expressions 102 Similarly, compute post-fix expressions from left- right: Youll always have two values, then an operand Apply the operand to the values, then replace all three with the answer you get. Try again! 4 6 – *

103 Exercise Try some more! Pre-fix: ^ 8 * Post-fix: 3 5 * ^ 8 – How about logic (works similarly)? Let a = TRUE, b = FALSE Prefix: a b a b Postfix: a b a ~ b Thought exercise: postfix is easier for humans, but prefix is easier for computers. Why?


Download ppt "DISCRETE MATHEMATICS I LECTURES CHAPTER 10 Dr. Adam Anthony Spring 2011 Some material adapted from lecture notes provided by Dr. Chungsim Han and Dr. Sam."

Similar presentations


Ads by Google