Chapter 9
Trees
IEEM 2021 Discrete Mathematics 1
IEEM 2021 Discrete Mathematics
9.1: Introduction to Trees
Definition 1 (P.631): A tree is a connected undirected graph that contains no circuits.
Theorem 1 (P632): There is a unique simple path between any two of its nodes.
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.
A leaf node in a tree or forest is any pendant or isolated vertex. An internal node is any non-leaf vertex (thus it has degree ___ ).
IEEM 2021 Discrete Mathematics 3
Tree and Forest Examples
Leaves in green, internal nodes in brown.
A Tree:
A Forest:
IEEM 2021 Discrete Mathematics
family tree
IEEM 2021 Discrete Mathematics
Example 1 (P. 632): Which graphs are trees?
not connected
IEEM 2021 Discrete Mathematics 6
Rooted Trees Definition 2 (P.633): A rooted tree is a tree in which one node has been designated the root.
Every edge is (implicitly or explicitly) directed away from the root.
You should know the following terms about rooted trees:
Parent, child, siblings, ancestors, descendents, leaf, internal node, subtree.
IEEM 2021 Discrete Mathematics 7
IEEM 2021 Discrete Mathematics
IEEM 2021 Discrete Mathematics
Rooted Tree Examples Note that a given unrooted tree with n nodes yields n different rooted trees.
root Same tree except for choice of root root
IEEM 2021 Discrete Mathematics
10
Rooted-Tree Terminology Exercise Find the parent, children, siblings, ancestors, & descendants of node f. e
i p j o d b a q f k l root c g n
r m
IEEM 2021 Discrete Mathematics
11
Example 2 (P. 634): Find the parent of c. the children of g. the siblings of h. all ancestors of e. all descendants of b. all internal vertices. all leaves subtree rooted at at g
IEEM 2021 Discrete Mathematics 12
Parent of g: a Child of g: h, i, j Siblings: h, i, j are sibling. Ancestors of j: a, g (a has no ancestor.) Descendents of g: h, i, j, k, l, m. Leaf: d ,e, f, k, i, l, m. Internal node: V {leaf vertices} Subtree with root g:
IEEM 2021 Discrete Mathematics
13
n-ary trees
Definition 3 (P.634): A rooted tree is called n-ary if every vertex has no more than n children.
It is called full if every internal (non-leaf) vertex has exactly n children.
A 2-ary tree is called a binary tree.
These are handy for describing sequences of yesno decisions. Example: Comparisons in binary search algorithm.
IEEM 2021 Discrete Mathematics 14
Which Tree is Binary?
A given rooted tree is a binary tree iff every node other than the root has degree ___, and the root has degree ___.
IEEM 2021 Discrete Mathematics
15
Example 3 (P. 634): Are the following rooted trees full m-ary trees for some positive integer m.
full binary tree full 3-ary tree
full 5-ary tree
no
IEEM 2021 Discrete Mathematics
16
Ordered Rooted Tree This is just a rooted tree in which the children of each internal node are ordered. In ordered binary trees, we can define:
left child, right child left subtree, right subtree
For n-ary trees with n > 2, can use terms like leftmost, rightmost, etc.
IEEM 2021 Discrete Mathematics 17
Example 4 (P. 635): Find (1) the left children of d. (2) the right children of d. (3) the left subtree of c. (4) the right subtree of c. Solution: the left children of d : f the right children of d: g
left subtree of c
IEEM 2021 Discrete Mathematics
right subtree of c
18
Trees as Models Can use trees to model the following:
Saturated hydrocarbons Organizational structures Computer file systems
In each case, would you use a rooted or a non-rooted tree?
IEEM 2021 Discrete Mathematics
19
Example 5 (P.636): Saturated hydrocarbons
There are exactly two different isomers of C4H10.
IEEM 2021 Discrete Mathematics
Arthur Cayley 1821 - 1895
20
Example 6 (P.637): Representing Organization
IEEM 2021 Discrete Mathematics
21
Example 6 (P.637): Computer File Systems
IEEM 2021 Discrete Mathematics
22
Example 7 (P.638): Tree-Connected Parallel Processors
1st step: P4: x1 + x2 P5: x3 + x4 P6: x5 + x6 P7: x7 + x8 2nd step: P2: (x1 + x2) + (x3 + x4) P3: (x5 + x6) + (x7 + x8) 3rd step: P1: [(x1+x2)+(x3+x4)]+[(x5+x6)+(x7+x8)]
IEEM 2021 Discrete Mathematics 23
Example 7 (P.638): Tree-Connected Parallel Processors
3rd step: P1: [(x1+x2)+(x3+x4)]+[(x5+x6)+(x7+x8)]
2nd step: P2: (x1 + x2) + (x3 + x4)
2nd step: P3: (x5 + x6) + (x7 + x8)
1st step: P4: x1 + x2
1st step: 1st step: P5: x3 + x4 P6: x5 + x6
IEEM 2021 Discrete Mathematics
1st step: P7: x7 + x8
24
Properties of Trees
Theorem 2 (P. 638): Any tree with n nodes has e = n 1 edges. Proof: By Mathematical Induction. Theorem 3 (P. 638):A full m-ary tree with i internal nodes has n = mi + 1 vertices, and = (m1)i + 1 leaves.
Proof: There are mi children of internal nodes, plus the root. Thus, n = mi + 1. and, = n i = (m1)i + 1. 1. (m 1)i 1.
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.
IEEM 2021 Discrete Mathematics 25
Theorem 4 (P. 639): A full m-ary tree (i) n vertices # of internal vertices i = (n -1)/m. # of leaves = [(m -1)n + 1]/m. (ii) i internal vertices # of vertices n = mi +1. # of leaves = (m 1)i + 1. (iii) leaves # of vertices n = (m 1)/(m 1). # of internal vertices i = (i -1)/(m 1).
IEEM 2021 Discrete Mathematics
26
Some More Tree Theorems
Definition: The level of a vertex is the length of the simple path from the root to the node.
The height of a tree is maximum of the levels of vertices. A rooted m-ary tree with height h is called balanced if all leaves are at levels h or h1. Example 10 (P.640): What is the level of each node? What is the height of this tree?
IEEM 2021 Discrete Mathematics 27
Example 11 (P.640): Which trees are balanced?
Theorem 5(P.640): There are at most mh leaves in an m-ary tree of height h. Proof: mathematical induction
IEEM 2021 Discrete Mathematics
28
Corollary 1 (P. 641): An m-ary tree with leaves has height h logm . If m-ary tree is full and balanced then h = logm .
1 + 3 + 32 1 + m + m 2 + + m h
(i)
# of leaves in a full m-ary tree
l m h logm l . l = m h h = IEEM 2021 l . Mathematics logm Discrete
(ii) If m-ary tree is full,
29
9.2: Applications of Trees Binary search trees
A simple data structure for sorted lists
Decision trees
Minimum comparisons in sorting algorithms
Prefix codes
Huffman coding
Game trees
30
IEEM 2021 Discrete Mathematics
Binary Search Trees A representation for sorted sets of items.
Supports the following operations in (log n) average-case time:
Searching for an existing item. Inserting a new item, if not already present.
Supports printing out all items in (n) time.
Note that inserting into a plain sequence ai would instead take (n) worst-case time.
IEEM 2021 Discrete Mathematics 31
Binary Search Tree Format
Items are stored at individual tree nodes. We arrange for the tree to always obey this invariant:
For every item x,
Every node in xs left subtree is less than x. Every node in xs right subtree is greater than x.
Example:
7 12 5 9 8 11 15
3 1 0 2
IEEM 2021 Discrete Mathematics
32
Example 1 (P. 644): Form the binary search tree for the words: mathematics, physics, geography, zoology, meteorology, geology, psychology, and chemistry (using alphabetical order). not unique May not be a balanced binary tree Solution:
IEEM 2021 Discrete Mathematics
33
Search on a binary tree: If it present, we locate it. Otherwise, we try to add it to the binary search tree. In Algorithm 1, it locate x if it is already the key of a vertex. When x is not a key, a new vertex with key x is added to the tree. In the pseudocode, v is the vertex that has x as its key, and label(v) represents the key of vertex v.
IEEM 2021 Discrete Mathematics
34
Recursive Binary Tree Insert
IEEM 2021 Discrete Mathematics
35
Suppose we have a binary search tree T for a list of n items. We may form a full binary tree U from T by adding unlabeled vertices whenever necessary so that every vertex with a key has two children.
IEEM 2021 Discrete Mathematics
36
Decision Trees
A decision tree represents a decision-making process.
Each possible decision point or situation is point represented by a node. Each possible choice that could be made at that decision point is represented by an edge to a child node.
In the extended decision trees used in decision analysis, we also include nodes that represent random events and their outcomes.
IEEM 2021 Discrete Mathematics 37
Coin-Weighing Problem
Example 2 (P. 647): Suppose that you have 8 coins, one of 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?
IEEM 2021 Discrete Mathematics
38
A Decision-Tree Problem In each situation, we pick two disjoint and equalsize subsets of coins to put on the scale.
The balance then decides whether to tip left, tip right, or stay balanced. A given sequence of weighings thus yields a decision tree with branching factor 3.
IEEM 2021 Discrete Mathematics
39
IEEM 2021 Discrete Mathematics
40
Applying the Tree Height Theorem 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 decision tree must have height h log38 = 1.893 = 2.
Lets see if we solve the problem with only 2 weighings
IEEM 2021 Discrete Mathematics 41
General Solution Strategy
The problem is an example of searching for 1 unique particular item, from among a list of n otherwise identical items.
Somewhat analogous to the adage of searching for a needle in haystack. haystack.
Armed with our balance, we can attack the problem using a divide-and-conquer strategy, like whats done in binary search. divide- andwhat
We want to narrow down the set of possible locations where the desired desired item (coin) could be found down from n to just 1, in a logarithmic fashion.
Each weighing has 3 possible outcomes.
Thus, we should use it to partition the search space into 3 pieces that are as pieces close to equal-sized as possible. equal-
This strategy will lead to the minimum possible worst-case worstnumber of weighings required.
IEEM 2021 Discrete Mathematics 42
General Balance 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. /3
If the scale tips to the right, then:
The lightweight fake is in the left set of n/3 n/3 coins. /3
If the scale stays balanced, then:
The fake is in the remaining set of n 2n/3 n/3 coins that /3 were not weighed!
Except if n mod 3 = 1 then we can do a little better by weighing n/3 of the coins on each side.
43 You can prove that thisIEEM 2021 Discrete Mathematics to a balanced 3-ary tree. strategy always leads
Coin Balancing Decision Tree
How many weighing steps? T(n) = T(n/3) + C Here is what the tree looks like in our case:
left: 123 123 vs 456 right: 456 4 vs. 5 balanced: 78 7 vs. 8 R:8
1 vs. 2 L:1
R:2 B:3 L:4 R:5 B:6 L:7
IEEM 2021 Discrete Mathematics
44
Example 3 (P.848): Give a decision tree that orders the elements of the list a, b, c.
Theorem 1 (P.848): Any sorting algorithm based on comparison requires at least log2n! comparisons. log2n! = (nlog2n)
IEEM 2021 Discrete Mathematics 45
Complexity of Sorting Algorithms Based on Comparisons
Theorem 1 (P.848): Any sorting algorithm based on comparison requires at least log2n! comparisons. log2n! = (nlog2n) Corollary 1 (P.848): The number of comparisons used by a sorting algorithm to sort n elements based on binary comparisons is (nlog2n). Theorem 2 (P.848): The average number of comparisons used by a sorting algorithm to sort n elements based on binary comparisons is (nlog2n).
IEEM 2021 Discrete Mathematics 46
Game Trees
(Grundy's Game): n , , , , , , n = 7, ( ), , ,
(7)
(7)
( 6, 1 )
( 5, 2 )
( 4, 3 )
( 6, 1 )
( 5, 2 )
( 4, 3 )
( 5, 1, 1)
( 4, 2, 1 )
( 3, 2, 2 )
( 3, 3, 1 )
( 5, 1, 1)
( 4, 2, 1 )
( 3, 2, 2 )
( 3, 3, 1 )
( 4, 1, 1, 1 )
( 3, 2, 1, 1 )
( 2, 2, 2, 1 )
( 4, 1, 1, 1 )
( 3, 2, 1, 1 )
( 2, 2, 2, 1 )
( 3, 1, 1, 1, 1 )
( 2, 2, 1, 1, 1 )
( 3, 1, 1, 1, 1 )
( 2, 2, 1, 1, 1 )
( 2, 1, 1, 1, 1, 1 )
( 2, 1, 1, 1, 1, 1 )
n = 7
IEEM 2021 Discrete Mathematics
47
(Nim Game) n , , , , n = 5, , , ,
5
1 2 3
level 0
4
1 3
3
2 1
2
1
level
3
1 2
2
1
1
1 1
1
1
1
1
level
2
1
1
1
1
1
1
1
level 3
1
1
level 4
IEEM 2021 Discrete Mathematics
level 5
48
5
1 2 3
level 0
4
1 3 1
3
2
2
1
level 1
3
1 2
2
1
1
1 1
2
1
1
1
level 2
2
1
1
1
1
1
1
1
level 3
1
1
level 4
level 5
IEEM 2021 Discrete Mathematics 49
Example 5 (P. 653) Rule: (1) In each step, remove one or more stone from exactly one of the piles. (2) A player without a legal move loses.
IEEM 2021 Discrete Mathematics
50
Definition 1 (P. 654): The value of a vertex in a game tree is defined recursively as follows: (i) The value of a leaf is the payoff to the first palyer when the game terminates in the position represented by this leaf. (ii) The value of an internal vertex at an even level is the maximum values of its children, and the value of an internal vertex at the odd level is the minimum of values of its children. Theorem 3 (P. 654): The value of a vertex of a game tree tells us the payoff to the first player if both players follow minimax strategy and paly start from the position represented by this vertex.
IEEM 2021 Discrete Mathematics 51
Example 7 (P. 655)
IEEM 2021 Discrete Mathematics
52
Example 6 (P. 653): Tic-tac-toe
IEEM 2021 Discrete Mathematics
53
IEEM 2021 Discrete Mathematics
54
IEEM 2021 Discrete Mathematics
55
IEEM 2021 Discrete Mathematics
56
IEEM 2021 Discrete Mathematics
57
9.3: Tree Traversal Procedures for traversing all vertices of an ordered rooted tree Universal address systems Traversal algorithms
Depth-first traversal:
Preorder traversal Inorder traversal Postorder traversal
Breadth-first traversal
Infix/prefix/postfix notation
IEEM 2021 Discrete Mathematics 58
Universal Address Systems
Example 1 (P. 660): Display the labelings of the universal address system. The lexicographic ordering of the labelings is
0 < 1.1 < 1.2 < 1.3 < 2 < 3 < 3.1< 3.1.1< 3.1.2< [Link] < [Link]< [Link]<[Link] < ...
IEEM 2021 Discrete Mathematics
59
Preorder Traversal
Definition 1 (P. 661): Let T be a ordered rooted tree with root r. If T consists only 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 preorder, preorder, is traversed in preorder. preorder.
IEEM 2021 Discrete Mathematics
60
IEEM 2021 Discrete Mathematics
61
Example 2 (P. 662): List the order of elements by using a preorder traversal.
Priorities of Preorder Traversal Write, Left, Right
a b e j k n o p f c d g l m h i
IEEM 2021 Discrete Mathematics 62
Example 3 (P. 662): List the order of elements by using a preorder traversal.
IEEM 2021 Discrete Mathematics 63
Inorder Traversal
Definition 2 (P. 662): Let T be a ordered rooted tree with root r. If T consists only r, then r is the inorder traversal of T. Otherwise, suppose that T1, T2, . . . , Tn are the subtrees at r from left to right in T. The inorder traversal begins by visiting r. It continues by traversing T1 in inorder, then T2 in inorder, and so on, until Tn is inorder, inorder, traversed in inorder. inorder. Priorities of Inorder Traversal Left, Write, Right
IEEM 2021 Discrete Mathematics
64
IEEM 2021 Discrete Mathematics
65
Example 4 (P. 664): List the order of elements by using a inorder traversal.
IEEM 2021 Discrete Mathematics 66
Postorder Traversal
Definition 3 (P. 664): Let T be a ordered rooted tree with root r. If T consists only r, then r is the postorder traversal of T. Otherwise, suppose that T1, T2, . . . , Tn are the subtrees at r from left to right in T. The postorder traversal begins by visiting r. It continues by traversing T1 in postorder, then T2 in postorder, and so on, until postorder, postorder, Tn is traversed in postorder. postorder. Priorities of Posorder Traversal Left, Right, Write
IEEM 2021 Discrete Mathematics
67
IEEM 2021 Discrete Mathematics
68
Example 3 (P. 662): List the order of elements by using a postorder traversal.
Priorities of Posorder Traversal Left, Right, Write
IEEM 2021 Discrete Mathematics 69
IEEM 2021 Discrete Mathematics
70
9.4: Spanning Trees
not unique
Definition 1 (P. 675): 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.
IEEM 2021 Discrete Mathematics 71
Example 1 (P. 675): Find a spanning tree of the simple graph G.
IEEM 2021 Discrete Mathematics
72
IEEM 2021 Discrete Mathematics
73
Theorem 1 (P. 676): A simple graph is connected if and only if it has a spanning tree. Example 2 (P. 676): IP Multicasting
IEEM 2021 Discrete Mathematics
74
Depth First Search
Depth First Search is also called backtracking, since it returns to vertices previously visited to add path.
"Never burn your bridges after crossing you may have to backtrack someday"
IEEM 2021 Discrete Mathematics
75
Example 3 (P. 678): Use a Depth First Search to find spanning tree for the graph G.
IEEM 2021 Discrete Mathematics
76
Example 4 (P. 678): Use a Depth First Search to find spanning tree for the graph G.
IEEM 2021 Discrete Mathematics
77
IEEM 2021 Discrete Mathematics
78
A maze
IEEM 2021 Discrete Mathematics
79
Breadth-First Search
IEEM 2021 Discrete Mathematics
80
Example 5 (P. 680): Use a Breadth First Search to find spanning tree for the graph G.
IEEM 2021 Discrete Mathematics
81
IEEM 2021 Discrete Mathematics
82
Exploring a town depth-first not necessary be unique
Exploring a town using breadth-first search.
IEEM 2021 Discrete Mathematics 83
Backtracking Applications
Example 6 (P. 682): Graph Coloring Use backtracking (depth first search) to decide whether a graph G can be colored using n colors.
IEEM 2021 Discrete Mathematics
84
Example 7 (P. 683): The n-Queens Problem Use backtracking (depth first search) to solve the n-queens problem.
IEEM 2021 Discrete Mathematics
85
IEEM 2021 Discrete Mathematics
86
IEEM 2021 Discrete Mathematics
87
IEEM 2021 Discrete Mathematics
88
IEEM 2021 Discrete Mathematics
89
IEEM 2021 Discrete Mathematics
90
IEEM 2021 Discrete Mathematics
91
IEEM 2021 Discrete Mathematics
92
Depth First Search in Directed Graph Example 9 (P.684): Do the depth first search for the following directed graph.
Example 10 (P.685): Web Spiders IEEM 2021 Discrete Mathematics
93
Example 10 (P.685): Web Spiders To index websites, search engines such as google, yahoo systematically explore the web starting at a known site. These search engines use programs called Web spiders (or crawlers or bots) to visit websites and analyze their contents. Web spiders use both depth-first searching and breadth-first searching to create indices.
IEEM 2021 Discrete Mathematics
94
9.5: Minimum Spanning Trees
Definition 1 (P. 689): A minimum spanning tree in a connected weighted graph is a spanning tree that has the smallest sum of weights of its edges.
It may not be unique.
Robert Clay Prim 1921IEEM 2021 Discrete Mathematics 95
IEEM 2021 Discrete Mathematics
96
Prims Algorithms ( 1957)
Prims Algorithm
7 nodes MST has (7-1) edges.
A 2 O 1 C
IEEM 2021 Discrete Mathematics
2 B 3 E 1 D
19 97
Prims Algorithm
A
A
A 2 O 2 B
O
O
O
2 B 1 C
A 2 O 1 C 2 B 3 E
O 2
A 2 B 1 C 3 E 1 D
O 2
A 2 B 1 C 3 E 1 D 5
IEEM 2021 Discrete Mathematics
20 98
Example 1 (P.690): Find the minimum spanning tree by using Prims algorithm.
IEEM 2021 Discrete Mathematics
99
Example 2 (P.690): Find the minimum spanning tree by using Prims algorithm.
IEEM 2021 Discrete Mathematics
100
IEEM 2021 Discrete Mathematics
101
Joseph Bernard Kruskal 1928IEEM 2021 Discrete Mathematics 102
6-nodes 5-edges
IEEM 2021 Discrete Mathematics
103
Kruskals Algorithm (1956)
BC(1) DE(1) AB(2) AO(2) BE(3) BD(4) CE(4) CO(4) BO(5) DT(5) AD(7) ET(7)
O 1 C
O 1
B 1 E
add BC
IEEM 2021 Discrete Mathematics
add DE
104
BC(1) DE(1) AB(2) AO(2) BE(3) BD(4) CE(4) CO(4) BO(5) DT(5) AD(7) ET(7)
A
A
2
B 1 C E 1
2 B 1 C E 1 D T
add AB
add AO
A 2 2 B 1 C 3 E 1 D T 2
2 B 1 C 3 E 1 D T
add BE
IEEM 2021 Discrete Mathematics
add BD (no)
105
BC(1) DE(1) AB(2) AO(2) BE(3) BD(4) CE(4) CO(4) BO(5) DT(5) AD(7) ET(7)
A 2 2 B 1 C 3 E 1 D T 2
2 B 1 C 3 E 1 D T
add CE (no)
add CO (no)
A 2 O 1 C 2 B 3 E 1 D T
A 2 2 B 1 C 3 E 1 D 5
add BO (no)
add DT & all nodes are connected.
IEEM 2021 Discrete Mathematics
106
Example 2 (P.692): Find the minimum spanning tree by using Kruskals algorithm.
IEEM 2021 Discrete Mathematics
107
IEEM 2021 Discrete Mathematics
108