Your SlideShare is downloading. ×
Discrete-Chapter 10 Trees
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Discrete-Chapter 10 Trees

1,806
views

Published on


0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,806
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
34
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Trees - 10 CSC1001 Discrete Mathematics 1 CHAPTER ต้นไม้ 10 (Trees) 1 Introduction to Trees1. Deffinition of Trees Definition 1 A tree is a connected undirected graph with no simple circuits. Because a tree cannot have a simple circuit, a tree cannot contain multiple edges or loops. Therefore any tree must be a simple graph.Example 1 (5 points) Is it tree or not tree? 1. 2. 3. a d f a d f a d f b g b g b g c e h c e h c e h 4. a d f 5. a d f 6. a d f b g b g b g c e h c e h c e h Definition 2 A rooted tree is a tree in which one vertex has been designated as the root and every edge is directed away from the root.The Terminology of TreesSuppose that T is a rooted tree and v is a vertex in T other than the root, The parent of v is the unique vertex u such that there is a directed edge from u to v. When u is the parent of v, v is called a child of u. Vertices with the same parent are called siblings. The ancestors of a vertex other than the root are the vertices in the path from the root to this vertex, excluding the vertex itself and including the root. The descendants of a vertex v are those vertices that have v as an ancestor.มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555) เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
  • 2. 2 CSC1001 Discrete Mathematics 10 - Trees A vertex of a rooted tree is called a leaf if it has no children. Vertices that have children are called internal vertices. The root is an internal vertex unless it is the only vertex in the graph, in which case it is a leaf. If a is a vertex in a tree, the subtree with a as its root is the subgraph of the tree consisting of a and its descendants and all edges incident to these descendants.Example 2 (5 points) In the rooted tree T (with root a) shown in Figure, can you find;1) The parent of c …………………………………………………………………………………………………………… a2) The children of g ……………………………………………………………………………………………………….....3) The siblings of h …………………………………………………………………………………………………………..4) All ancestors of e ………………………………………………………………………………………………...………. b g f5) All descendants of b …………………………………………………………………………………………………….. c h j6) All internal vertices ………………………………………………………………………………………………………. i7) All leaves ………………………………………………………………………………………………………………….. d e k l m8) What is the subtree rooted at b, g and j Definition 3 A rooted tree is called an m-ary tree if every internal vertex has no more than m children. The tree is called a full m-ary tree if every internal vertex has exactly m children. An m-ary tree with m = 2 is called a binary tree.Example 3 (4 points) Are the rooted trees in Figure full m-ary trees for some positive integer m?มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555) เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
  • 3. Trees - 10 CSC1001 Discrete Mathematics 3 Definition 4 A tree with n vertices has n–1 edges. Definition 5 A full m-ary tree with (1) n vertices has i = (n–1)/m internal vertices and l = [(m–1)n + 1]/m leaves, (2) i internal vertices has n = mi + 1 vertices and l = (m–1)i + 1 leaves, (3) l leaves has n = (ml–1)/(m–1) vertices and i = (l–1)/(m–1) internal vertices.Example 4 (4 points) From an Example 3, find the number of edges of trees T1, T2, T3 and T4?Example 5 (2 points) Find the number of internal vertices and leaves if full 3-ary tree has 15 vertices?Example 6 (2 points) Find the number of vertices and leaves if full 4-ary tree has 39 internal vertices?Example 7 (2 points) Find the number of vertices and internal vertices if full 4-ary tree has 50 leaves?Example 8 (4 points) Suppose that someone starts a chain letter. Each person who receives the letter isasked to send it on to four other people. Some people do this, but others do not send any letters. How manypeople have seen the letter, including the first person, if no one receives more than one letter and if the chainมหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555) เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
  • 4. 4 CSC1001 Discrete Mathematics 10 - Treesletter ends after there have been 100 people who read it but did not send it out? How many people sent outthe letter?Example 9 (4 points) A chain letter starts when a person sends a letter to five others. Each person whoreceives the letter either sends it to five other people who have never received it or does not send it toanyone. Suppose that 10,000 people send out the letter before the chain ends and that no one receives morethan one letter. How many people receive the letter, and how many do not send it out?Example 10 (4 points) Suppose 1000 people enter a chess tournament. Use a rooted tree model of thetournament to determine how many games must be played to determine a champion, if a player is eliminatedafter one loss and games are played until only one entrant has not lost. (Assume there are no ties.)?Example 11 (4 points) Draw the Fibonacci trees of f(5), how many the number of internal vertices and leavesof Fibonacci tree?มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555) เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
  • 5. Trees - 10 CSC1001 Discrete Mathematics 5 2 Applications of Trees1. Binary Search Trees Definition 1 A binary search tree is a binary tree in which each child of a vertex is designated as a right or left child, no vertex has more than one right child or left child, and each vertex is labeled with a key, which is one of the items. Vertices are assigned keys so that the key of a vertex is both larger than the keys of all vertices in its left subtree and smaller than the keys of all vertices in its right subtree.Example 12 (5 points) Draw a binary search tree of set {19, 8, 27, 23, 11, 4, 6, 2, 37, 30, 20, 22, 10, 3}.Example 13 (5 points) Draw a binary search tree of set {50, 25, 73, 35, 63, 84, 10, 15, 31, 22, 44, 4, 1, 72}.Example 14 (4 points) Show the path of binary search tree in an Example 13 if find 10, 31, 35, 19.มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555) เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
  • 6. 6 CSC1001 Discrete Mathematics 10 - Trees2. Decision Trees Definition 2 A rooted tree in which each internal vertex corresponds to a decision, with a subtree at these vertices for each possible outcome of the decision, is called a decision tree.Example 15 (5 points) Draw a decision tree to find the possible solutions when the coin are rolled at 3 times.Example 16 (5 points) Draw a decision tree of sorted a, b and c variables.3. Trees Traversal We will describe three of the most commonly used such algorithms, preorder traversal, inorder traversal,and postorder traversal. Definition 3 Let T be an ordered rooted tree with root r. If T consists only of r, then r is the preorder traversal of T . Otherwise, suppose that T1, T2, . . . , Tn are the subtrees at r from left to right in T . The preorder traversal begins by visiting r. It continues by traversing T1 in preorder, then T2 in preorder, and so on, until Tn is traversed in preorder.มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555) เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
  • 7. Trees - 10 CSC1001 Discrete Mathematics 7 Definition 4 Let T be an ordered rooted tree with root r. If T consists only of r, then r is the inorder traversal of T . Otherwise, suppose that T1, T2, . . . , Tn are the subtrees at r from left to right. The inorder traversal begins by traversing T1 in inorder, then visiting r. It continues by traversing T2 in inorder, then T3 in inorder, . . . , and finally Tn in inorder. Definition 5 Let T be an ordered rooted tree with root r. If T consists only of r, then r is the postorder traversal of T. Otherwise, suppose that T1, T2, . . . , Tn are the subtrees at r from left to right. The postorder traversal begins by traversing T1 in postorder, then T2 in postorder, . . . , then Tn in postorder, and ends by visiting r.Example 17 (6 points) Show the order list of preorder, inorder and postorder traversal visit the vertices in theordered rooted tree T shown in Figure?Example 18 (6 points) Show the order list of preorder, inorder and postorder traversal visit the vertices in theordered rooted tree shown in Figure?มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555) เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
  • 8. 8 CSC1001 Discrete Mathematics 10 - TreesExample 19 (6 points) Show the order list of preorder, inorder and postorder traversal visit the vertices in theordered rooted tree shown in Figure?4. Infix, Prefix, and Postfix Notation Definition 6 An ordered rooted tree can be used to represent such expressions, where the internal vertices represent operations, and the leaves represent the variables or numbers. Each operation operates on its left and right subtrees (in that order). There are 3 forms to represent the expressions, (1) Infix form A+B, (2) Prefix form +AB, and (3) Postfix form AB+Example 20 (4 points) Show the prefix and postfix form of the expression a + b * c – d / e?Example 21 (8 points) Draw a tree which represented the expression a + b * c – d / e and show prefix andpostfix form by using preorder and postorder traversal?มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555) เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
  • 9. Trees - 10 CSC1001 Discrete Mathematics 9Example 22 (8 points) Draw a tree which represented the expression (a / b – c * d) / (e * f + g – h) and showprefix and postfix form by using preorder and postorder traversal?Example 23 (8 points) Draw a tree which represented the expression a / b – c * (d + e) * (f / g + h) – i * j andshow prefix and postfix form by using preorder and postorder traversal? 3 Spanning Trees1. Introduction to Spanning Trees Definition 1 Let G be a simple graph. A spanning tree of G is a subgraph of G that is a tree containing every vertex of G. A simple graph with a spanning tree must be connected, because there is a path in the spanning tree between any two vertices.มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555) เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
  • 10. 10 CSC1001 Discrete Mathematics 10 - TreesExample 24 (15 points) Find a spanning tree for the graph shown by removing edges in simple circuits.มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555) เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
  • 11. Trees - 10 CSC1001 Discrete Mathematics 112. Depth-First Search and Breadth-First Search We can also produce a spanning tree of a simple graph by the use of depth-first search and breadth-first search algorithm.1) Example of depth-first search of graph G (start with the vertex f)2) Example of breadth-first search of graph G (start with the vertex e)Example 25 (15 points) Use depth-first search and breadth-first search to produce a spanning tree for thegiven simple graph. Choose a as the root of this spanning tree and assume that the vertices are orderedalphabetically.มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555) เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
  • 12. 12 CSC1001 Discrete Mathematics 10 - Trees3. Minimum Spanning Trees Definition 2 A minimum spanning tree in a connected weighted graph is a spanning tree that has the smallest possible sum of weights of its edges.มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555) เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
  • 13. Trees - 10 CSC1001 Discrete Mathematics 13 Definition 3 The Prim’s Algorithm procedure Prim(G: weighted connected undirected graph with n vertices) { T = a minimum-weight edge for i = 1 to n − 2 { e = an edge of minimum weight incident to a vertex in T and not forming a simple circuit in T if added to T T = T with e added } return T //a minimum spanning tree of G } Definition 4 The Kruskal’s Algorithm procedure Kruskal(G: weighted connected undirected graph with n vertices) { T = empty graph for i = 1 to n − 1 { e = any edge in G with smallest weight that does not form a simple circuit when added to T T = T with e added } return T //a minimum spanning tree of G }มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555) เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
  • 14. 14 CSC1001 Discrete Mathematics 10 - TreesExample 26 (10 points) Use Prim’s algorithm and Kruskal’s algorithm to find a minimum spanning tree in theweighted graph shown in Figure.1) Use Prim’s algorithm2) Use Kruskal’s algorithmExample 27 (10 points) Use Prim’s algorithm and Kruskal’s algorithm to find a minimum spanning tree in theweighted graph shown in Figure.1) Use Prim’s algorithm2) Use Kruskal’s algorithmมหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555) เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
  • 15. Trees - 10 CSC1001 Discrete Mathematics 15Example 28 (5 points) The roads represented by this graph are all unpaved. The lengths of the roadsbetween pairs of towns are represented by edge weights. Which roads should be paved so that there is a pathof paved roads between each pair of towns so that a minimum road length is paved?1) Use Prim’s algorithm2) Use Kruskal’s algorithmมหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555) เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี

×