Professional Documents
Culture Documents
Trees
IEEM 2021 Discrete Mathematics 1
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
A Tree:
A Forest:
family tree
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.
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
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
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:
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.
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
no
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
19
21
22
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. 1. and, = n i = (m1)i + 1. 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).
26
Theorem 5(P.640): There are at most mh leaves in an m-ary tree of height h. Proof: mathematical induction
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
Decision trees
Minimum comparisons in sorting algorithms
Prefix codes
Huffman coding
Game trees
30
Note that inserting into a plain sequence ai would instead take (n) worst-case time.
IEEM 2021 Discrete Mathematics 31
Example:
7 12 5 9 8 11 15
3 1 0 2
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:
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.
34
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.
36
Decision Trees
A decision tree represents a decision-making process.
Each possible decision point point or situation is 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?
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.
39
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.
Armed with our balance, we can attack the problem using a dividedivide-andand-conquer strategy, like what whats done in binary search.
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.
This strategy will lead to the minimum possible worstworst-case number of weighings required.
IEEM 2021 Discrete Mathematics 42
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 strategy always leads to a balanced 3-ary tree. 2021 Discrete Mathematics
1 vs. 2 L:1
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
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
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
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
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.
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
52
53
54
55
56
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
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 is traversed in preorder.
60
61
Example 2 (P. 662): List the order of elements by using a preorder traversal.
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.
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 traversed in inorder. Priorities of Inorder Traversal Left, Write, Right
64
65
Example 4 (P. 664): List the order of elements by using a inorder traversal.
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 Tn is traversed in postorder. Priorities of Posorder Traversal Left, Right, Write
67
68
Example 3 (P. 662): List the order of elements by using a postorder traversal.
Priorities of Posorder Traversal Left, Right, Write
70
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
72
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
74
75
Example 3 (P. 678): Use a Depth First Search to find spanning tree for the graph G.
76
Example 4 (P. 678): Use a Depth First Search to find spanning tree for the graph G.
77
78
A maze
79
Breadth-First Search
80
Example 5 (P. 680): Use a Breadth First Search to find spanning tree for the graph G.
81
82
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.
84
Example 7 (P. 683): The n-Queens Problem Use backtracking (depth first search) to solve the n-queens problem.
85
86
87
88
89
90
91
92
Depth First Search in Directed Graph Example 9 (P.684): Do the depth first search for the following directed graph.
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.
94
96
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
20 98
Example 1 (P.690): Find the minimum spanning tree by using Prims algorithm.
99
Example 2 (P.690): Find the minimum spanning tree by using Prims algorithm.
100
101
6-nodes 5-edges
103
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)
106
Example 2 (P.692): Find the minimum spanning tree by using Kruskals algorithm.
107
108