# 461191 Discrete Math Lecture 11: Trees - PowerPoint PPT Presentation

PPT – 461191 Discrete Math Lecture 11: Trees PowerPoint presentation | free to download - id: 6db6dd-Yjg3M

The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
Title:

## 461191 Discrete Math Lecture 11: Trees

Description:

### Title: Discrete Mathematics for Computer Science ... – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 137
Provided by: SanR7
Category:
Tags:
Transcript and Presenter's Notes

Title: 461191 Discrete Math Lecture 11: Trees

1
461191 Discrete Math Lecture 11 Trees
• San Ratanasanya
• CS, KMUTNB

2
Todays topics
• Review of Graphs
• Trees

2
3
Graphs
• ???????????????????????????? (Discrete)
????????????? 2 ???? ???? set ??? Vertices
(??????) ??? Edges (???????)
??????????? ?????? ?????? u ??? v ?? adjacent
?????????????????????? e ???????????????????????
2 ??????????????? ??????? e incident ????????? u
??? v
• ?????????????????????????????? ????
• Networking, OCR, Facilities Planning, Searching,
etc.

4
Graphs
• ????????????????????
• ??????????????????
• Finite and Infinite Graphs
• ???????????????????????????????
• Simple-, Multi-, Directed-, Directed Multi-graphs
• ?????????????????????????
• Cycle, Wheel, Bipartite, Planar, Cyclic, Acyclic,
Connected Graphs

5
Examples
6
Euler and Hamilton
• Path (???????, ????) ?????????????????????????????
?? a ?????????????????? b ?????????? path
?????????????????????????????????????????? a ???
b
• Circuit (????) ??????? path ??????????????????????
??????????????????????
• Euler path and circuit
• Path ???? Circuit ?????????????????????????????
???????????????????? ??????????????????????????
• Hamilton path and circuit
• Path ???? Circuit ?????????????????????????????
??????????????????? ??????????????????????????

7
Examples
8
Shortest path problem
• Traveling Salesman Problem
• Salesman ????????????????????????????????????????
n ????? ??????????????????????????????????????????
? ??????????????????????????????????????????
• ???????????????????????????????????????????
• ?????????????????? Non-Polynomial time

9
Dijkstras Algorithm
10
Examples
11
Planargraph and Isomorhpism
• ????????? (Planargraph)
• ??????????????????????????????????????????
• Graph Isomorphic
• ???? 2 ??????? ????????????????????? ?? bijection
function ?????????????????????????
???? ???? 2 ??????????????????????????
(Equivalence Relation)

12
Examples
13
Graph Coloring
14
Examples
15
• New Midterm Statistics
• Max 15.3
• Mean 8.2
• Min 1.8
• Standard Deviation 2.67
• New due date for homework and assignments
submission
• All works must be submitted a week before final,
except the last programming assignment

15
16
Trees
• Connected Graph ???????? Simple Circuit
????????????? Tree ?????????????? Tree
???????????????????????? Computer Algorithm
??????????? ?????????????? Binary Tree, ????
Breath First Search ??? Depth First Search
??????? ?????????????????????????????????????????
Edge ????????????? ?????????????? Application
???? Tree ??????????????????? Weighted Tree

17
Basic Definition
• Definition 1 Tree ??? Connected Undirected Graph
???????? Simple Circuit
• ?????? Undirected Graph ???????? Simple Circuit
?????? Connect ??? ????????????????????? Tree
??????? ???????????? ???????? Tree ????????????
Forest

18
Forest
• Forest ????? simple circuit ??? vertex ???
connect ?????????? ??????? connect
????????????????? tree.

19
Tree Characteristics
• No circuit
• No multiple edges
• No loops
• Simple Graph

20
Which graphs are trees?
circuit
?
?
?
?
Not connected (Forest)
21
• Theorem 1 Undirected Graph ?????? Tree
??????????????? Simple Path ??????????????????????
????????? Vertices
• ??????? Application ??????????? Vertex ????????
Tree ???? Root ?????????????? Root ????
??????????????????????? Edge ?????? Theorem 1
???????? Path ??? Root ????????? Vertex
???????????????????? ????????????????????? Edge
??????????????????????????? Root ????????????
Directed Graph ???????? Rooted Tree

22
Rooted Tree
• Definition 2 Rooted Tree ???? Tree ??????????
Vertex ???????????? Root ??????? Edge
?????????????????? Root ????
• ??????????????????? Rooted Tree ????? Recursive
???
• ?????????????? Tree ???? Rooted Tree
??????????????????? Vertex ???????????? Root
????????? Rooted Tree ???????????????????????????
Root ???????? ????????????????? Rooted Tree
??????????? Vertex ??????? Root ?????????????

23
Example Rooted Tree
24
Terminologies
• ?????????????????????? Tree ??????????????????????
???????? ????????? T ???? Rooted Tree ??? v ????
Vertex ?? T ????????? Root ????? Parent ??? v
?????? Vertex ?????????????????? ????? Directed
Edge ??? u ?? v ???????? u ???? Parent ??? v
???????? v ???? Child (???? Children) ??? u
• ???????? Vertices ?????????? Parent ????????
??????????????? Sibling (???????) ???
• Ancestor (?????????) ??? Vertex ????????? Root
??? Vertices ????????? Path ??? Root ????? Vertex
???? ????????? Vertex ??????? Root ????
• Descendant (?????) ??? Vertex v ??? ??? Vertices
???????????? v ???? Ancestor
• Vertex ?? Tree ?????????? Leaf ???????????
Children ??? Vertex ????? Children ???????
Internal Vertices
• ??? v ???? Vertex ?? Tree ??????? Subtree ????? v
???? Root ??? Subgraph ??? Tree ????????????? v
??? Descendant ????????????? ?????? Edge
????????????????

25
Example parent, child, sibling, ancestor,
descendant
ancestor
ancestor
parent
sibling
sibling
descendant
descendant
child
child
26
Terminology of Tree (cont.)
internal vertex
A root is a leaf or internal vertex?
internal vertex
internal vertex
leaf
leaf
leaf
leaf
27
Example Subgraph rooted at b
28
Example
• ????????????????? ????
• Parent of c
• Children of g
• Siblings of h
• Ancestors of e
• Descendants of b
• Internal vertices
• Leaves
• Subtree rooted at g

29
• Parent of c b
• Children of g h, i, j
• Siblings of h i, j
• Ancestors of e c, b, a
• Descendants of b c, d, e
• Internal vertices a, b, c, h, j
• Leaves d, e, f, k, i, l, m
• Subtree ????? Root ??? g ????????????????

30
M-ary Rooted Tree
31
Example full (?) m-ary tree
Full
Full
Full
binary tree
3-ary tree
5-ary tree
3-ary tree
Not full
32
Ordered Rooted Tree
• Ordered Root Tree ??? Rooted Tree ??? Children
??????? Internal Vertex ?????????????
????????????????????????????????? ?????? Ordered
Binary Tree (???????????????? Binary Tree ????)
?????????? Left Child ??? Right Child
???????????? Subtree ????????? Children ????
????????????? Left Subtree ??? Right Subtree
????????

33
Example Ordered Binary Tree
• Find the following
• Left child of d
• Right child of d

34
Tree Modeling
• Tree ??????????????????? Model ???????????????
?????????????????????????????????????
???????????????? ??????????? File System
????????????? ????????????????????????????????????
????????? ???????

35
Tree Properties
36
Example
• ??????????????????????????????????????
??????????????????????????????????????????????????
???????????????? 4 ???? ??????????????????????????
????????????????????????????? ????????????????????
??????????????????????????? ??????????????????????
??????????????????????????????????? 100 ??
??????????????????????????????????????????(???????
?????)
• ????? ?????????????????????????????????? Model
??????? Full 4-ary Tree ????????????? Tree ?????
100 Leaf ???????????????? Internal Vertices
?????? i (100-1) / (4-1) 33 ??

37
Height and Level of Tree
38
Example
• Level of d
• Level of k
• Height of tree
• Balanced tree?

39
Balanced Tree?
?
?
?
40
Application of Tree Binary Search
• ???????? Item ????????? List ?????????????????????
????????????????????????? ????????????????????????
?????? Searching Algorithm ??????? Item
?????????????????????? ???? Binary Search Tree
???????????????????????
• ????????? Binary Tree ???????? Internal Vertex
???? Left Child ??? Right Child ???????? Vertex
??????????? Key ???????????????? Key ???????
Vertices ????????? Subtree ???????????????????????
????? Vertices ????????? Subtree ???????

41
Application of Tree Binary Search
• ???????? Binary Search Tree ?????????????????
Recursive Algorithm ???????? Tree ????????????? 1
Vertex ??? Root ??????? Assign ??? Key ?????????
List ?????? Root ???????????????? Item ?? List
??????????? Binary Search Tree ???????????????????
? Item ??? Key ??????????? Vertices ????????????
Tree ???? ???????? Root ???????????????????
?????? Item ????????????? ???? Vertex ?????? Left
Child ???????????????????? ?????? Item
???????????? ???? Vertex ?????? Right Child ???
Vertex ????? Left Child ?????? Item ?????????????
Key ??? Vertex ???? ??????? Vertex
??????????????????????????????????????? Key
??????? Item ??????????????? ??? Vertex ?????
Right Child ?????? Item ???????????? Key ???
Vertex ???? Vertex ???????????????????????????????
??????? Key ??????? Item

42
????? binary search tree ?????????????????
• Mathematics
• Physics
• Geography
• Zoology
• Meteorology
• Geology
• Psychology
• Chemistry

43
Form Binary Search Tree
44
(No Transcript)
45
• ?????? Complexity ??? Binary Search Algorithm
???????????????????? ????????? Tree ????????????
Item ?????????????????????? Full Binary Tree
??????????? Vertices ???????? Key ?????????
??????? Vertices ????? Key ?????????? 2 Children
??????????????? Full Binary Tree ????????
?????????????????? Item ????????? Item
?????????????????? Vertex ???? ????????????

46
(No Transcript)
47
Complexity of Binary Search Tree
48
Decision Tree
• Rooted Tree ???? Internal Vertex
?????????????????????????? Decision Tree
???????????
• Example 2 ?????????????????????????? 8
??????????????????????????????????????????????????
???????????? ????????????????????????????????
??????????????????????????????????????????????????
??????????
• ????? 2 ????? ??????

49
(No Transcript)
50
Complexity of Sorting Algorithm
51
(No Transcript)
52
Prefix Code
• ?????? Code ???????? Code ????????????????????????
????? ???????? Prefix Free Codes ???? Prefix Code
???????????????????????? ???????? Binary Code
????????????????????? ?????? Binary Tree
????????? Edge ??? Left Child ???? 0 ?????? Right
Child ???? 1 ??? Code ???????????? Prefix Free
• Prefix Code ?????????? Data Compression
??????????????????? Code ????????????????????????
Probability ?????????? ????????????????????? Code
????????????????????????? ????????????????????????
????????? ???????????? Code ??????????????????????
???????? ????????????? Code ??????????????????????
? Prefix Code
• Algorithm ????????????? Data Compression ????????
Prefix Code ????????????????????????????? Huffman
Algorithm ??????????? Huffman Tree
??????????????????? Probability ??????? ?????????
Algorithm ????????

53
(No Transcript)
54
• Example 3 ??? Huffman Coding ?????????????
Symbol ???????????????????????????
• A0.08, B0.10, C0.12, D0.15, E0.20, F0.35
??????????????????????????????????????????????????
??????
• ????? ????? ?????????????????????? Code

55
(No Transcript)
56
Game Tree
• Tree ??????????????????????????? Game ?????????
?????????????????? ??????????? ???????????????????
??????????????? ??????????????????????????????????
?

57
(No Transcript)
58
Tree Traversal
• Ordered Rooted Tree ??????????????????????????????
?? ???????????????????????????????????????????????
???????

59
• ????????????????? (Traversal) ????? Vertices
????????????? Ordered Rooted Tree
????????????????????????? Children
????????????????? Label ??? Level ???????????
???????? Root ???????????? ??? Label Child
????????????????? Label ??? Parent
??????????????? Child ??????????????

60
• Example 1 ??????????? Label ??? Universal
Order ?????????????? ???? Lexicographic Order
?????? 0lt1lt1.1lt1.2lt1.3lt2lt3lt3.1lt3.1.1lt3.1.2lt3.1.2.1
lt3.1.2.2lt3.1.2.3lt3.1.2.4lt3.1.3lt3.2lt4lt4.1lt5lt5.1lt5.1
.1lt5.2lt5.3

61
(No Transcript)
62
Traversal Algorithm
• ?????????????????????????????????????? Vertex ???
Ordered Rooted Tree ???????? Traversal Algorithm
??????????????????????????????????
• Preorder Traversal
• Inorder Traversal
• Postorder Traversal

63
Preorder Traversal
64
Example Preorder Traversal
r
T1
T2
Tn

65
Example Preorder Traversal
a
b
c
d
66
Example Preorder Traversal
67
Example Preorder Traversal
68
Example Preorder Traversal
69
(No Transcript)
70
Preorder Traversal Algorithm
71
Inorder Traversal
72
Example Inorder Traversal
r
T1
T2
Tn
T3

73
Example Inorder Traversal
a
b
c
d
74
Example Inorder Traversal
75
Example Inorder Traversal
76
(No Transcript)
77
Postorder Traversal
78
Example Postorder Traversal
r
T1
T2
Tn
T3

79
Example Postorder Traversal
a
b
c
d
80
Example Postorder Traversal
81
Example Postorder Traversal
82
(No Transcript)
83
Exercise Tree Traversal
84
Tree Traversal
• ?????????????? Preorder, Inorder, Postorder
????????????????????????? Curve ???? Ordered
Rooted Tree ?????? ???????? Root ???????? Edge
????? List Vertices ??? Preorder ?????? List
????? Curve ??????? Vertices ???????? ????? List
Vertices ??? Inorder ?????? List ???? Leaf ?????
Curve ???????????? ??? List Internal Vertices
????? Curve ??????????????? ????? List Vertices
??? Postorder ?????? List ????? Curve
????????????????????????????????????? Parent ???
Vertex ????

85
• ???????????? Preorder ???? a, b, d, h, e, i, j,
c, f, g, k ?????? Inorder ???? h, d, b, i, e, j,
a, f, c, k, g ????????? Postorder ???? h, d, i,
j, e, b, f, k, g, c, a

86
(No Transcript)
87
Infix, Prefix, ??? Postfix Notation
• ????????????? Expression ?????????????????????????
???????? Ordered Rooted Tree ?????????????????????
?????
• Example ???? Ordered Rooted Tree ??????
Expression

88
(No Transcript)
89
Infix Form by Inorder Traverse
90
Infix Form Ambiguous
91
Prefix Form by Preorder Traverse
• ???????? Expression ????? Prefix Form ??????????
Preorder Traversal ??? Ordered Rooted Tree ????
Expression ????????????? Prefix Form
?????????????? Polish Notation ??? Expression
?????????????????? ???????????????????????????????
Expression

92
(No Transcript)
93
Prefix (Polish Notation)
• ??????????? Prefix Form ??? Binary Operator
???????? Operand ??????? ?????????????????????????
????????????????????? ??????????? Operator
??????? ??????? Operation ?????????? Operand
??????????? ????????????? Operation ????????????
Operand ???????

94
Postfix Form by Post Order Traverse
• ?????????? Postorder Traversal ??? Expression
??????????? Binary Tree ???????? Postfix Form
????????????????????????? Reverse Polish Notation
?????????????????????????????? ????????? Postfix
Form ??? Operator ????????? Operand ???????
?????????????????????????? Reverse Polish
Notation ??????????????????????? ???????????
Operator ??????? ??????? Operation ?????? Operand
????????????? ??????????????????? Operand ???????

95
• ????????????? Rooted Tree ?????????????????????
Expression ???????? ???? Proposition
??????????????? Set ?????????? Unary Operator
?????????? Tree ?????????????? Operator ????
Vertex ????? Operand ???? Child
• ????????????????????????????? Polish Notation ???
Reverse Polish Notation

96
(No Transcript)
97
(No Transcript)
98
(No Transcript)
99
(No Transcript)
100
(No Transcript)
101
• ?????????????????????? Prefix ??? Postfix
??????????????????? ??????????????????????????????
??????????????????????

102
SPANNING TREE
• ??????????????????????????????????????????????????
?????? ???????????????????????????????????????????
????????????? ????????????????????????????????????
????????????????????????????????????
??????????????????????? ??????????????????????????
????????????? ???????????????????????????????
????????????????????????????????????? 5 ???????

103
SPANNING TREE
104
SPANNING TREE
• ???????????????? ?????????????????????
????????????????????????????????????? Model
??????????? Simple Graph ??????????????????????
Subgraph ??????? Tree ???????????????? Vertices
???? Tree ???????????????????????????????
Spanning Tree
• Definition 1 ??? G ???? Simple Graph ????
Spanning Tree ??? G ??? Subgraph ??? G ???????
Tree ????????????????? Vertex ??? G
• Simple Graph ??????? Spanning Tree ?????????????
Connected Graph ???????????????????????? Tree
?????????? Vertex ???

105
Example
• ???? Spanning Tree ??? Simple Graph ????????

106
• ???????? ????? Edge a,e, e,f ??? c,g ???

107
• ???????????????????? Spanning Tree
???????????????????????? ????

108
• Theorem 1 Simple Graph ?? Connect
?????????????????? Spanning Tree
• ???????? Spanning Tree ??? Simple Graph
?????????????????????? Vertex ????? ???????????
Edge ?? Graph ???? Edge ???? Tree ????????????
??????????? Tree ???????????????? Vertex
???????????? ?????????????????????? Spanning Tree
??????? Algorithm ?????? Depth-First Search ???

109
Depth-First Search
• ???????? Spanning Tree ?????? Depth-First Search
????????????? Rooted Tree ??? Spanning Tree
??????????????? Undirected Graph ??? Rooted Tree
??? ????????? ?????????? Vertex
??????????????????????? Root ?????????????????
Path ???????? Vertex ?????????????? Vertices ???
Edge ?????? Edge ???????????????? Vertex
???????????????? Path ??? Vertex
???????????????????? Edge ????????????????????
?????????????????????????? Path
?????????????????????????? ???????? Path
?????????????? Vertex ???????? Spanning Tree
?????????? ???????????? ??? Path
???????????????????? Vertex ????????????????
Vertex ????????????????????????? Path ???????
Vertex ???? ????????????????? ????????????????????
??????????????? Spanning Tree

110
Depth-First Search
• ?????????? Depth-First Search ???????????????
Recursive ???????????? Algorithm
???????????????????????? Vertex ????????
??????????????????????????????? Backtracking

111
Example
• ????? Depth-First Search ??????? Spanning Tree
??? Graph ????????????? ???????????????????
Vertex f

112
(No Transcript)
113
• ??????? Edge ?????????????? Depth-First Search
?????????? Tree Edge ???? Edge ??????????????????
Back Edge

114
• ???????? Spanning Tree ?????????????????????
????? Vertex ?????????????????? Root
????????????????????? Edge ??? Incident
(?????????) ??? Vertex ??? ?????? Vertices
????????????? Edge ???? Tree ?????????? Vertices
?? Level 1 ?????????? Tree ??????
????????????????? Vertex ?? Level 1 ????????????
Edge ??? Incident ??? Vertex ???(???????? Edge
???????? ?????????????????) ?????? Vertex
????????? Edge ??????????? ?????????? Level 2 ???
Tree ????????????????????????????? Vertex
?????????????? Vertex ???????????????? Tree
????????????

115
Example
• ????? Breadth-First Search ??????? Spanning Tree
??? Graph ????????????? ???????????????????
Vertex e

116
(No Transcript)
117
(No Transcript)
118
Minimum Spanning Trees
• ??????????????????????????????????????????????????
???????????? Computer Center ??? 5 ??????
??????????????????????????????????????????????????
??????????????????? ???????????????????? ????????
?????????????????????????????? ???????????????????
??????????????????????? ??????????????????
Spanning Tree ?????? Model ??????? Weighted Graph
???????? Cost ????????????? ???? Spanning Tree
???????? ???????? Minimum Spanning Tree

119
(No Transcript)
120
• Definition 1 Minimum Spanning Tree ??????
Connected Weighted Graph ??? Spanning Tree
??????????????????????????????? Edge
????????????????????????

121
Minimum Spanning Tree Algorithm
• ????????????? Algorithm ???????????????? Minimum
Spanning Tree
• ??????????? Algorithm ?????????????????? Edge
?????? Tree ???? Edge ???????? Weight ?????????
• ?????????? Algorithm ?????????????????? Greedy
Algorithm ?????????????????????? Optimum ???????
Step ?????????????????????? 3
• ??????????? Greedy Algorithm ?????????????????????
?????????????? Step ??????????????????????????????
????????????????????????????? (Optimal Total
Solution) ??????????????????? Algorithm
?????????????????? ????? ????????????????? Optimum

122
Prims Algorithm
123
(No Transcript)
124
Example
• ????? Prims Algorithm ??????????? Minimum-Cost
Communication Network ????????????????????????????
?????????????????

125
(No Transcript)
126
Example
• ????? Prims Algorithm ?? Minimum Spanning Tree
???????????????????????

127
(No Transcript)
128
(No Transcript)
129
Kruskals Algorithm
• Algorithm ????????????????????????? Kruskals
Algorithm ???????? Joseph Kruskal ???? 1956
???????????????????????
• ??????????????? Edge ?? Graph ????? Weight ??????
??????????????? Edge ????? Weight ??????
?????????? Simple Circuit ??? Edge
??????????????? ??????????????????? n-1 Edge

130
(No Transcript)
131
(No Transcript)
132
Example
• ????? Kruskals Algorithm ??????? Minimum
Spanning Tree ?????? Weighted Graph ???????????

133
(No Transcript)
134
(No Transcript)
135
Homework 10
• 10.1.1-3, 10.1.11, 10.1.33, 10.2.6, 10.2.26,
10.2.27, 10.2.37-38, 10.3.3-4, 10.3.17-18,
10.3.23-25, 10.4.13-16, 10.5.11, 10.5.15

135
136
Programming Assignment 3
1. ????????????????????????? Caesar
??????????????????????? key ??????
??????????????????????????????????????????????????
??????????????????????????????????? Space
????????????? Z
2. ????????????????????????? Huffman
?????????????????????? 10.2.27
3. ?????????????????????????????????????? input
??????? pre-, in-, post-fix
• ?????????????
• - ?????? e-mail ??????? Subject ??? 461191
Programming Assignment 3
• ???????????????????????????????????? ??????
format ???? zip ???? rar
• ??????????????????????????????????????????.

Each program should be submitted with a report
showing its code and how it works (some screen
shorts plus descriptions).