Decision Making Under Risk Continued: Decision Trees MGS3100 - Chapter 8 Slides 8b.
Trees (slides)
-
Upload
pocong-makenon -
Category
Education
-
view
71 -
download
0
Transcript of Trees (slides)
Definition: Let π΄π΄ be a set, and let ππ be a relation on π΄π΄. We say that ππ is a tree if there is a vertex π£π£0 in π΄π΄ with the property that
1) there exists a unique path in ππ from π£π£0 to every other vertex in π΄π΄,
2) no path from π£π£0 to π£π£0.
Definition: π£π£0 is called the root of the tree, and T is referred to as a rooted tree denoted by ππ, π£π£0 .
Trees
Β© S. Turaev, CSC 1700 Discrete Mathematics 2
Theorem: Let ππ, π£π£0 be a rooted tree. Then
1. There are no cycle in ππ.
2. Vertex π£π£0 is the only root of ππ.
3. Each vertex in ππ, other than π£π£0, has in-degree one, and π£π£0 has in-degree zero.
Trees
Β© S. Turaev, CSC 1700 Discrete Mathematics 3
Levels, Parent-Offspring, Siblings
Β© S. Turaev, CSC 1700 Discrete Mathematics
2
1
5
3 4
6
7
Level 1 vertices: the vertices of the edges beginning at π£π£0 (level 0)
Level ππ vertices: the vertices of the edges beginning at those level ππ β 1vertices
Parent-offspring: for all pairs (ππ, ππ) in ππ, ππ is called the parent of ππ and ππ is called the offspring of ππ
Siblings: the vertices that have the same parent
Height of a tree: the largest level number of a tree
Leaves: the vertices that have no offspring
4
Levels, Parent-Offspring, Siblings
Β© S. Turaev, CSC 1700 Discrete Mathematics
2
1
5
3 4
6
7
Level 0
Level 1
Level 2
Level 3
Height 3Leaf
Root
Child /Offspring
Parent
Siblings
5
Theorem: Let ππ, π£π£0 be a rooted tree. Then
ππ is irreflexive
ππ is asymmetric
If ππ, ππ in ππ and ππ, ππ in ππ, then ππ, ππ is not in ππ, for all ππ, ππ and ππ in π΄π΄.
Trees
Β© S. Turaev, CSC 1700 Discrete Mathematics 6
Example: Let π΄π΄ = π£π£1, π£π£2, π£π£3, β¦ , π£π£10 and let
ππ = π£π£2, π£π£3 , π£π£2, π£π£1 , π£π£4, π£π£5 , π£π£4, π£π£6 ,π£π£5, π£π£8 , π£π£6, π£π£7 , π£π£4, π£π£2 , π£π£7, π£π£9 , π£π£7, π£π£10
Show that ππ is a rooted tree and identify the root.
Trees
Β© S. Turaev, CSC 1700 Discrete Mathematics 7
Definition:
If ππ is a positive integer, we say that a tree is an ππ-tree if every vertex has at most ππ offspring.
A 2-tree is called a binary tree.
Definition:
If all vertices of ππ, other than the leaves, have exactly ππ offspring, we say that ππ is a complete ππ-tree.
A complete 2-tree is called a completed binary tree.
ππ-trees
Β© S. Turaev, CSC 1700 Discrete Mathematics 8
Let ππ, π£π£0 be a rooted tree on the set π΄π΄, and let π£π£be a vertex of ππ.
Let π΅π΅ be the set consisting of π£π£ and all its descendants, i.e., all vertices of ππ that can be reached by a path beginning at π£π£.
Let ππ π£π£ be the restriction of the relation ππ to π΅π΅, that is ππ β© (π΅π΅ Γ π΅π΅).
Delete all vertices that are not descendants of π£π£ and all edges that do not begin and end at any such vertex.
Subtrees
Β© S. Turaev, CSC 1700 Discrete Mathematics 9
Theorem: If ππ, π£π£0 is a rooted tree and π£π£ in ππ, then ππ π£π£ is also a rooted tree with root π£π£.
We will say that ππ π£π£ is the subtree of ππ beginning at π£π£.
Subtrees
Β© S. Turaev, CSC 1700 Discrete Mathematics
2 31
0
4 5
6
10
Theorem: If ππ, π£π£0 is a rooted tree and π£π£ in ππ, then ππ π£π£ is also a rooted tree with root π£π£.
We will say that ππ π£π£ is the subtree of ππ beginning at π£π£.
Subtrees
Β© S. Turaev, CSC 1700 Discrete Mathematics
2
0
4 5
6
11
Exercise 1: Determine if π π is a tree and, if it is, find the root.
π΄π΄ = ππ, ππ, ππ,ππ, πππ π = ππ,ππ , ππ, ππ , ππ, ππ , ππ, ππ
π΄π΄ = 1, 2, 3, 4, 5, 6π π = 2,1 , 3,4 , 5,2 , 6,5 , 6,3
Exercises
Β© S. Turaev, CSC 1700 Discrete Mathematics 12
Exercise 2: Consider the rooted tree ππ, 0 .
Exercises
Β© S. Turaev, CSC 1700 Discrete Mathematics
0
1 2
4 5 6 8 9
10
11 12
7
3
13 14
15
13
Exercise 2: Consider the rooted tree ππ, 0 .
1. List all level-3 vertices
2. List all leaves
3. What are the siblings of 8?
4. What are the descendants of 3?
5. Compute ππ 26. Compute ππ 37. What is the height of ππ, 0 ?
Exercises
Β© S. Turaev, CSC 1700 Discrete Mathematics 14
Example: Use a tree to denote the following algebraic expression
3 β 2 Γ ππ + ππ β 2 β 3 + ππ
Labeled Trees
Β© S. Turaev, CSC 1700 Discrete Mathematics
+
- -
3
2 b
x -
b 2 3 b
+
15
Example: Use a tree to denote the following algebraic expression
3 Γ 1 β ππ Γ· 4 + 7 β ππ + 2 Γ 7 + ππ Γ· ππ
Labeled Trees
Β© S. Turaev, CSC 1700 Discrete Mathematics 16
Positional ππ-tree:
β’ ππ-tree: every vertex has at most ππ offspring
β’ positional ππ-tree: label the offspring of a given vertex from left to right with numbers 1,2, β¦ ,ππ
β’ some of the offspring in the sequence may be missing
Labeled Trees
Β© S. Turaev, CSC 1700 Discrete Mathematics 17
Example: positional 3-tree:
Labeled Trees
Β© S. Turaev, CSC 1700 Discrete Mathematics
2
2
3
3
3
1 1
3
2 31
2 31
18
Example: positional 2-tree:
Labeled Trees
Β© S. Turaev, CSC 1700 Discrete Mathematics
L R
L
R
R L
L L R
R
19
Visiting
Performing appropriate tasks at a vertex will be called visiting the vertex.
Tree search
The process of visiting each vertex of a tree in some specific order will be called searching the tree or performing a tree search.
Tree Searching
Β© S. Turaev, CSC 1700 Discrete Mathematics 20
Algorithm PREORDER
Step 1: Visit π£π£
Step 2: If π£π£πΏπΏ exists, then apply this algorithm to ππ π£π£πΏπΏ , π£π£πΏπΏ
Step 3: If π£π£π π exists, then apply this algorithm to ππ π£π£π π , π£π£π π
Tree Searching
Β© S. Turaev, CSC 1700 Discrete Mathematics 21
Example 1
Tree Searching
Β© S. Turaev, CSC 1700 Discrete Mathematics
A
B
C
D F
E
G J
I
L
K
H
1
2
3
4
5 6
7
8
9
10
11
A B C D E F G H I J K L
22
Example 2: ππ β ππ Γ ππ + ππ/ππ
Tree Searching
Β© S. Turaev, CSC 1700 Discrete Mathematics
Γ
-
a b
e
/
+
c
d
Γ - a b + c / d e
1
2 3
4
5
7 86
23
Prefix or Polish form:
Γ β ππ ππ + ππ / ππ ππ (ππ = 6, ππ = 4, ππ = 5,ππ = 2, ππ = 2)
1. Γ β6 4 + 5 / 2 2
2. Γ 2 + 5 / 2 2 replacing β6 4 by 2 since 6 β 4 = 2
3. Γ 2 + 5 1 replacing / 2 2 by 1 since 2/2 = 1
4. Γ 2 6 replacing + 5 1 by 6 since 5 + 1 = 6
5. 12 replacing Γ 2 6 by 12 since2 Γ 6 = 12
Tree Searching
Β© S. Turaev, CSC 1700 Discrete Mathematics 24
Algorithm INORDER
Step 1: Search the left subtree ππ π£π£πΏπΏ , π£π£πΏπΏ , if it exists
Step 2: Visit the root π£π£
Step 3: Search the right subtree ππ π£π£π π , π£π£π π , if it exists
Tree Searching
Β© S. Turaev, CSC 1700 Discrete Mathematics 25
Algorithm POSTORDER
Step 1: Search the left subtree ππ π£π£πΏπΏ , π£π£πΏπΏ , if it exists
Step 2: Search the right subtree ππ π£π£π π , π£π£π π , if it exists
Step 3: Visit the root π£π£
Tree Searching
Β© S. Turaev, CSC 1700 Discrete Mathematics 26
Example: Traveling the tree using INORDER and POSTORDER
ππ β ππ Γ ππ + ππ/ππ
Tree Searching
Β© S. Turaev, CSC 1700 Discrete Mathematics
Γ
-
a b
e
/
+
c
d
INORDER: ππ β ππ Γ ππ + ππ/ππ
POSTORDER: ππ ππ β ππ ππ ππ / +Γ
27
Infix notation: Algebraic symbols lie between their arguments
ππ β ππ Γ ππ + ππ / ππ
(ππ β ππ) Γ (ππ + (ππ/ππ))
or
ππ β (ππ Γ ( ππ + ππ /ππ))
Tree Searching
Β© S. Turaev, CSC 1700 Discrete Mathematics 28
Postfix or reverse Polish:
ππ ππ β ππ ππ ππ / + Γ (ππ = 2, ππ = 1, ππ = 3,ππ = 4, ππ = 2)
1. 2 1 β 3 4 2 / + Γ
2. 1 3 4 2 / + Γ replacing 2 1 β with 1 since 2 β 1 = 1
3. 1 3 2 + Γ replacing 4 2 / with 2 since 4/2 = 2
4. 1 5 Γ replacing 3 2 + with 5 since 3 + 2 = 5
5. 5 replacing 1 5 Γ with 5 since 1 Γ 5 =5
Tree Searching
Β© S. Turaev, CSC 1700 Discrete Mathematics 29
Show the result of performing a preorder search of the tree
Exercises
Β© S. Turaev, CSC 1700 Discrete Mathematics
x
y
s
z
t
u
v
30
Show the result of performing an inorder search of the tree
Exercises
Β© S. Turaev, CSC 1700 Discrete Mathematics
x
y
s
z
t
u
v
31
Show the result of performing a postorder search of the tree
Exercises
Β© S. Turaev, CSC 1700 Discrete Mathematics
x
y
s
z
t
u
v
32
Show the result of performing preorder, inorder and postorder searches of the tree
ππ = ππ, ππ , ππ,ππ , ππ, ππ , ππ, ππ , ππ, ππ , ππ, ππ ,ππ,ππ , ππ, β , ππ, ππ , ππ, ππ
Exercises
Β© S. Turaev, CSC 1700 Discrete Mathematics 33
Show the result of performing preorder, inorder and postorder searches of the tree
ππ = 1,2 , 1,3 , 2,4 , 3,5 , 4,6 , 5,7
Exercises
Β© S. Turaev, CSC 1700 Discrete Mathematics 34
Evaluate the expressions, which are given in Polish, or prefix, notation
β’ Γ β + 3 4 β 7 2 Γ· 12 Γ 3 β 6 4
β’ Γ· β Γ 3 2 Γ 4 3 + 15 Γ 2 β 6 Γ 3
Exercises
Β© S. Turaev, CSC 1700 Discrete Mathematics 35
Evaluate the expressions, which are given in reversePolish, or postfix, notation
β’ 4 3 2 Γ· β 5 Γ 4 2 Γ 5 Γ 3 Γ· Γ·
β’ 3 7 Γ 4 β 9 Γ 6 5 Γ 2 + Γ·
Exercises
Β© S. Turaev, CSC 1700 Discrete Mathematics 36