1 / 32

Discrete Structures

Discrete Structures. Lecture 12: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten. Introduction to Trees. Definition : A tree is a connected undirected simple graph that contains no circuits . Example : Bernoulli Family Tree. Introduction to Trees.

urbano
Download Presentation

Discrete Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Discrete Structures Lecture 12: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten

  2. Introduction to Trees • Definition: A tree is a connectedundirected simple graph that contains no circuits. • Example: Bernoulli Family Tree

  3. Introduction to Trees • Class Exercise: Which of the following graphs are trees?

  4. Introduction to Trees • Theorem: An undirected graph T is a tree iff there is a unique simple path between any two vertices in the graph. • Proof: -> If T is a tree then it is a connected graph with no circuits. Let x and y be two vertices of T. Then, there is a path from x to y. If there was a second path from x to y, then we could combine the two paths to get a circuit. This is impossible, as T has no circuits. So, the path from x to y must be unique.

  5. Introduction to Trees • Theorem: An undirected graph T is a tree iff there is a unique simple path between any two vertices in the graph. • Proof: <- Assume that there is a unique path between any two vertices a and y. Then, T is connected. Suppose there was a circuit containing x and y. Then, we could spilt the circuit into two paths from x to y. This contradicts the fact that there is only one path from x to y. Thus, T is a tree. QED

  6. Introduction to Trees • Definition: A (not-necessarily-connected) undirected graph without simple circuits is called a forest. • You can think of it as a set of trees having disjoint sets of nodes.

  7. Introduction to Trees • A Tree: • A Forest:

  8. Rooted Trees • Definition: A rooted tree is a tree in which one node has been designated the root. • Definition: A vertex of a 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. • Every edge is directed away from the root. • Definitions: Given a vertex v in a rooted tree T: • Parent: unique vertex u such that (u,v) is an edge • Child: any vertex for which v is a parent • Siblings : vertices with the same parent • Ancestors : all vertices from root to v (except root and itself) • Descendents : vertices with v as an ancestors

  9. Example

  10. Rooted Trees • Note that a given unrooted tree with n nodes yields n different rooted trees. Any node can be chosen to be the root. Same tree exceptfor choiceof root root root

  11. m-ary Trees • Definition: A rooted tree is called m-ary if every internal vertex has no more than m children. • It is called full if every internal (non-leaf) vertex has exactlym children. • Definition: A 2-ary tree is called a binary tree. If v is an internal vertex of a binary tree, it has two children. The first we call the left child and the second the right child. • Definition: If vertex v is in a tree T, the subtree with root v is the tree consisting of v and all its descendants.

  12. Binary Trees • Example: A binary tree with the left and right subtrees of vertex c.

  13. m-ary Trees • Example: Computer file system

  14. Tree Theorems • Theorem: Any tree with n vertices has e =n−1 edges. • Proof: Induction on n. • Theorem: A full m-ary tree with i internal vertices has n=mi+1 vertices total, and =(m−1)i+1 leaves. • Proof: There are mi children of internal vertices, plus the root. And,  = n−i = (m−1)i+1. QED • Thus, when m is known and the tree is full, we can compute all four of the values e, i, n, and , given any one of them.

  15. Tree Theorems • Theorem: A full m-ary tree with • n vertices has i= (n-1)/m internal vertices and =[(m−1)n+1]/m leaves. • i internal vertices has n= mi+1 total vertices and =(m−1)i+1 leaves. •  leaves has has n= (m  -1)/(m-1) total vertices and i= ( -1)/(m-1) internal vertices. • Proof: Algebra. QED

  16. Tree Theorems • Definition: The level of a node is the length of the path from the root to the node (The level of the root is defined to be zero). • The height of a tree is maximum node level. • A rooted m-ary tree with height h is called balanced if all leaves are at levels h or h−1. • Which are balanced?

  17. Tree Theorems • Theorem: There are at most mh leaves in an m-ary tree of height h. • Proof: Induction on h. Base Case: h=1. Tree has a root and at most m= mh children. Induction Hypothesis: Assume the result is true for all trees of height less than h.

  18. Tree Theorems • Proof: Induction on h. Induction Step: Let T be a tree of height h. Each subtree of the root of T has height less than or equal to h-1. By the induction hypothesis, each of these has at most mh-1 leaves. Since there are at most m subtrees, then there are at most m* mh-1 = mh leaves total. QED.

  19. Tree Theorems • Corollary: An m-ary tree with  leaves has height h≥logm . If m is full and balanced then h=logm. Hint: mh-1 < l é ù £ Þ £ Þ £ h l m log l h log l h m m

  20. Example • Exercise: Can there be a full and balanced m-ary tree with 84 leaves and height 3? • No. If such a tree existed, we would have 3=logm84. If m= 2, log284 =7; m = 3, logm84 = 5; m = 4, logm84 = 4 If m = 5,6,7,8,9 logm84 = 3. Let T be an m-ary full and balanced tree of height 3. At height 2 there are m2 vertices. Some of these, say k vertices, have m children (leaves). So, T has mk leaves at height 3 and (m2 -k) leaves at height 2. Then, mk + (m2 -k) = (m-1)k + m2 = 84 m=5,7,9 -> get even + odd = even Impossible. M = 6,8 -> get 5k= 48, 7k = 20 Impossible.

  21. Tree Applications • Definition: A binary search tree is a binary tree that has the following properties for each vertex v • v’s value is greater than all values in its left subtree TL • v’s value is less than all values in its right subtree TR • Both TL and TR are binary search trees

  22. Binary Search Tree • Construction: Start with one item as root. • To add each item, compare to the root. If greater then root, go to right subtree. If less than root, go to left subtree. • At each subtree root, compare again and go left or right. • Keep going down tree, until we reach a point where there is no left (right) subtree. We add the element there.

  23. Binary Search Tree

  24. Binary Search Tree • Algorithm for Inserting new item: procedure insert(T: binary tree, x: item)v := root[T]if v = null then root[T] := x; return “Done” endelse if v = xreturn “Already present”else if x < vthenreturn insert(leftSubtree[T], x)else{must be x > v}return insert(rightSubtree[T], x)

  25. Decision Tree • Definition: A decision tree represents a decision-making process. • Each possible “decision point” or situation is represented by a vertex. • In the extended decision trees used in decision analysis, we also include vertices that represent random events and their outcomes.

  26. Decision Tree • Imagine you have 8 coins, oneof which is a lighter counterfeit, and a free-beam balance. • No scale of weight markings is required for this problem! • How many weighings are needed to guarantee that the counterfeit coin will be found? ?

  27. Decision Tree • In each situation, we pick two disjoint and equal-size subsets of coins to put on the scale A given sequence ofweighings thus yieldsa decision tree withbranching factor 3. The balance then“decides” whether to tip left, tip right, or stay balanced.

  28. Decision Tree • The decision tree must have at least 8 leaf nodes, since there are 8 possible outcomes. • In terms of which coin is the counterfeit one. • Recall the tree-height theorem, h≥logm. • Thus the optimal decision tree must have heighth≥ log38 = 1.893… = 2. • Let’s see if we can solve the problem with only 2 weighings…

  29. Solution Strategy • Each weighing has 3 possible outcomes. • Thus, we should use it to partition the search space into 3 pieces that are as close to equal-sized as possible. • This strategy will lead to the minimum possible worst-case number of weighings required.

  30. Solution Strategy • On each step, put n/3 of the n coins to be searched on each side of the scale. • If the scale tips to the left, then: • The lightweight fake is in the right set of n/3≈ n/3 coins. • If the scale tips to the right, then: • The lightweight fake is in the left set of n/3 ≈ n/3 coins. • If the scale stays balanced, then: • The fake is in the remaining set of n − 2n/3 ≈ n/3 coins that were not weighed!

  31. Decision Tree Solution • Here is what the decision tree will look like: 123 vs 456 left: 123 balanced:78 right: 456 4 vs. 5 1 vs. 2 7 vs. 8 L:1 R:2 B:3 L:4 R:5 B:6 L:7 R:8

  32. Ass 13 • P693 3, 11(a)(b) • P708 5 , 7

More Related