Chapter VI Tree - SI-35-02 · A connected graph that contains no simple circuits is called a tree...
Transcript of Chapter VI Tree - SI-35-02 · A connected graph that contains no simple circuits is called a tree...
Tree
Discrete Mathematics (MA 2333)Faculty of Science Telkom Institute of TechnologyBandung - Indonesia
Discrete Mathematics (MA 2333) – Faculty of Science Telkom Institute of Technology
DefinitionA connected graph that contains no simple circuits is called a treeTrees were used since 1857, when English mathematician Arthur Cayley used them to count certain types of chemical compoundsApplications in computer science :
Constructing efficient algorithms for locating items in the listTree can be used in Huffman coding, that construct efficient codes saving costs in data transmission and storageCompilation techniques, and so on
Discrete Mathematics (MA 2333) – Faculty of Science Telkom Institute of Technology
Definition
a b
c d
e f
a b
c d
e f
a b
c d
e f
a b
c d
e f
Tree Tree Not Tree Not Tree
Discrete Mathematics (MA 2333) – Faculty of Science Telkom Institute of Technology
DefinitionForest is disconnected graph that each of their connected components is a tree
Forest that contains thee trees
Discrete Mathematics (MA 2333) – Faculty of Science Telkom Institute of Technology
Properties of Trees
Given Tree with n vertexThere is a unique simple path between any two of its verticesThe tree has n-1 edges
Discrete Mathematics (MA 2333) – Faculty of Science Telkom Institute of Technology
Spanning TreeSpanning tree is the spanning subgraf in the form of tree Spanning tree is obtained by eliminating the circuit on the graph
G T1 T2 T3 T4
Every connected graph has at least a spanning tree
Discrete Mathematics (MA 2333) – Faculty of Science Telkom Institute of Technology
Applications of Spanning Tree
Constructing topology of a computer network that all host still connected, with minimum cost
Example
(a) (b)Router
Subnetwork
Discrete Mathematics (MA 2333) – Faculty of Science Telkom Institute of Technology
Minimum Spanning Tree
Connected-weighted graph has at least a spanning treeSpanning tree having minimum weight is called minimum spanning tree
a
bc
d
e
f
g
h55
5
40
25
45
30
5020
15
35 10
a
bc
d
e
f
g
h
5
40
25 30
20
15
10
Discrete Mathematics (MA 2333) – Faculty of Science Telkom Institute of Technology
Prim’s Algorithm
1. Choose a minimum weighted edge of the graph G. Insert into T
2. Choose edge (u, v) on G which has a minimum weight and is incident to vertex on T. This edge does not form a circuit on T. Insert (u, v) into T
3. Repeat step 2 n-2 times
Discrete Mathematics (MA 2333) – Faculty of Science Telkom Institute of Technology
Prim’s AlgorithmDetermine minimum spanning tree of the graph presented below
A B
C
D
E
2
1,2
0,91,3
1,6
0,80,7
2,2
1
1,4
Discrete Mathematics (MA 2333) – Faculty of Science Telkom Institute of Technology
Prim’s Algorithm
0,8(C,E)2
0,7(B,E)1
Spanning TreeWeightEdgeStep
B
E
B
E
C
Discrete Mathematics (MA 2333) – Faculty of Science Telkom Institute of Technology
Prim’s Algorithm
0,9(A,D)4
1,2(B,A)3
Spanning TreeWeightEdgeStep
B
E
CA
B
E
CA
D
Discrete Mathematics (MA 2333) – Faculty of Science Telkom Institute of Technology
Prim’s Algorithm
Minimum Spanning Tree resulted:
with total weight : 0,7 + 0,8 + 1,2 + 0,9 = 3,6
Discrete Mathematics (MA 2333) – Faculty of Science Telkom Institute of Technology
Kruskal’s Algorithm
1. Step 0 : Sort (ascending) all edges of the graph by weight
2. Step 1: T still empty 3. Step 2: choose (u, v) which has
minimum weight and does not form a circuit into T
4. Step 3: repeat step 2 n – 1 times
Discrete Mathematics (MA 2333) – Faculty of Science Telkom Institute of Technology
Kruskal’s AlgorithmDetermine minimum spanning tree of the graph presented below
A B
C
D
E
2
1,2
0,91,3
1,6
0,80,7
2,2
1
1,4
Discrete Mathematics (MA 2333) – Faculty of Science Telkom Institute of Technology
Kruskal’s Algorithm
Edges (B,E) (C,E) (A,D) (B,C) (A,B) (B,D) (D,E) (C,D) (A,C) (A,E) Weight 0,7 0,8 0,9 1 1,2 1,3 1,4 1,6 2 2,2
0,7(B,E)1Spanning TreeWeightEdgeStep
B
E
Discrete Mathematics (MA 2333) – Faculty of Science Telkom Institute of Technology
Kruskal’s Algorithm
0,9(A,D)3
0,8(C,E)2
Spanning TreeWeightEdgeStep
B
E
C
B
E
CA
D
Discrete Mathematics (MA 2333) – Faculty of Science Telkom Institute of Technology
Kruskal’s Algorithm
1,2(A,B)5
Rejected1(B,C)4
Spanning TreeWeightEdgeStep
B
E
CA
D
Discrete Mathematics (MA 2333) – Faculty of Science Telkom Institute of Technology
Kruskal’s Algorithm
Minimum Spanning Tree resulted:
with total weight : 0,7 + 0,8 + 1,2 + 0,9 = 3,6
Discrete Mathematics (MA 2333) – Faculty of Science Telkom Institute of Technology
Rooted Tree
A rooted tree is a tree in which one vertex has been designated as the root and every adge is directed away from the root
Rooted Tree Removing directions
Discrete Mathematics (MA 2333) – Faculty of Science Telkom Institute of Technology
Pohon Berakar
a
b
c
d
e f
g
h
f
g
a
b
cd
e
f
g h
d
e
hb
a c
b as root e as root
Discrete Mathematics (MA 2333) – Faculty of Science Telkom Institute of Technology
Terminology on Rooted Tree1. Child or children
and parentb, c, and d are child of a, a is parent of b, c, d
2. PathPath from a to i is a, b, e, i. length of path from a to j is 3.
a
bc
d
ef g
h i j
Discrete Mathematics (MA 2333) – Faculty of Science Telkom Institute of Technology
Terminology on Rooted Tree3. Sibling
i sibling of j, g is not sibling of e, since they have different parent
4. Subtree
a
bc
d
ef g
h i j
Discrete Mathematics (MA 2333) – Faculty of Science Telkom Institute of Technology
Terminology on Rooted Tree4. Degree
Degree of the vertex is the number of children on it degree of a is 3, degree of d is 1 degree of j is 0. Maximum degree of all vertices is the degree of the tree itselfThis tree has degree= 3
Discrete Mathematics (MA 2333) – Faculty of Science Telkom Institute of Technology
Terminology on Rooted Tree5. Leaf
Vertex with degree 0 (or having no children) is called leafVertex h, i, j, f, c, g are leaves
6. Internal vertexVertex having children is called internal nodesVertex b, c, d, e are internal vertex
Discrete Mathematics (MA 2333) – Faculty of Science Telkom Institute of Technology
Terminology on Rooted Tree7. Level 8. Height or depth
Maximum level of a tree is called height or dept of the tree. This tree has height 3
a
b
cd
ef g
h i j
0
1
2
3
Discrete Mathematics (MA 2333) – Faculty of Science Telkom Institute of Technology
m-ary Tree
Rooted tree which its internal vertex having maximum n children is called n-ary tree. If n=2, its tree is called binary tree m-ary tree is called regular or full if for each internal vertex have exactly m child
Discrete Mathematics (MA 2333) – Faculty of Science Telkom Institute of Technology
Binary Tree
a
b c
d
a
b c
d
Discrete Mathematics (MA 2333) – Faculty of Science Telkom Institute of Technology
Binary Tree
a
b
c
d
a
b
c
d
Right-skewed Tree Full Binary TreeLeft-skewed Tree
Discrete Mathematics (MA 2333) – Faculty of Science Telkom Institute of Technology
Tree Traversal
Rooted tree are often used to store information. We need procedures for visiting each vertex of an ordered rooted tree to access data. We will describe several important algorithms for visiting all the vertices of an rooted tree
Discrete Mathematics (MA 2333) – Faculty of Science Telkom Institute of Technology
Tree Traversal
PreorderIn OrderPost Order
Discrete Mathematics (MA 2333) – Faculty of Science Telkom Institute of Technology
Preorder1. Visit root2. Visit left subtree3. Visit right subtree
Given Binary Tree
Discrete Mathematics (MA 2333) – Faculty of Science Telkom Institute of Technology
Preorder
a b c
d
g h
e f
Discrete Mathematics (MA 2333) – Faculty of Science Telkom Institute of Technology
Preorder
a b c d e f
g h
Discrete Mathematics (MA 2333) – Faculty of Science Telkom Institute of Technology
Inorder1. Visit left subtree2. Visit root3. Visit right subtree
Given Binary Tree
Discrete Mathematics (MA 2333) – Faculty of Science Telkom Institute of Technology
Inorder
ab c
d
g h
e f
Discrete Mathematics (MA 2333) – Faculty of Science Telkom Institute of Technology
Postorder1. Visit left subtree2. Visit right subtree3. Visit root
Given Binary Tree
Discrete Mathematics (MA 2333) – Faculty of Science Telkom Institute of Technology
Postorder
ab ed
g h
cf
Discrete Mathematics (MA 2333) – Faculty of Science Telkom Institute of Technology
Applications of Binary Tree
Expression TreeBinary Search TreeHuffman Code
Discrete Mathematics (MA 2333) – Faculty of Science Telkom Institute of Technology
Expression Tree
Expression Tree of (a/(b*(c/d)))*((e+f)*g)
a
b
d
e
c
*
f g
Discrete Mathematics (MA 2333) – Faculty of Science Telkom Institute of Technology
Expression Tree
Expression tree is used by high level language compiler to evaluate expression which written in infix notation, prefix notation, and postfix notationInfix notation
form : <operand1> <operator> <operand2>
example : a + bfully parenthesized expressionobtained by traverse expression tree in inorder
Discrete Mathematics (MA 2333) – Faculty of Science Telkom Institute of Technology
Expression Tree
Prefix notationform : <operator> <operand1> <operand2>
example : + a bobtained by traverse expression tree in preorder
Postfix notationform : <operand1> <operand2> <operator>
example : a b +obtained by traverse expression tree in postorder
Discrete Mathematics (MA 2333) – Faculty of Science Telkom Institute of Technology
Expression Tree
Infix notation :(a/(b*(c/d)))*((e+f)*g)Prefix notation :*/a*b/cd*+efgPostfix notation :abcd/*/ef+g**
Discrete Mathematics (MA 2333) – Faculty of Science Telkom Institute of Technology
Huffman Coding
Huffman coding is fundamental algorithm in data compression, the subject devoted to reducing the number of bits required to represent informationHuffman coding is extensively used to compress bit strings representing text and it also plays an important role in compressing audio and image files
Discrete Mathematics (MA 2333) – Faculty of Science Telkom Institute of Technology
Huffman Coding
Algorithm that takes as input the frequencies (which are probability of occurrences) of symbols in a string and produces as output a prefix codes for these symbols
Discrete Mathematics (MA 2333) – Faculty of Science Telkom Institute of Technology
Huffman CodingFixed-length code (take from Rinaldi Munir)
Characters a b c d e f----------------------------------------------------------------
Frequencies 45% 13% 12% 16% 9% 5%Code 000 001 010 011 100 111
‘bad’ coded by ‘001000011’
Coding 100.000 characters needs 300.000 bits.
Discrete Mathematics (MA 2333) – Faculty of Science Telkom Institute of Technology
Huffman CodingVariable-length code (Huffman code)
Characters a b c d e f------------------------------------------------------------------------
Frequencies 45% 13% 12% 16% 9% 5%Code 0 101 100 111 1101 1100
‘bad’ coded by ‘1010111 ’
coding 100.000 characters needs (0,45 × 1 + 0,13 × 3 + 0,12 × 3 + 0,16 × 3 +0,09 × 4 + 0,05 × 4) × 100.000 = 224.000 bit
Compression ratio:
(300.000 – 224.000)/300.000 × 100% = 25,3%
Discrete Mathematics (MA 2333) – Faculty of Science Telkom Institute of Technology
c:12 b:13
cb:25
f:5 e:9
fe:14 d:16
fed:30 a:454.
Huffman Coding
Discrete Mathematics (MA 2333) – Faculty of Science Telkom Institute of Technology
cbfed:55
c:12 b:13
cb:25
f:5 e:9
fe:14 d:16
fed:30
a:455
Huffman Coding
Discrete Mathematics (MA 2333) – Faculty of Science Telkom Institute of Technology
Huffman Coding
cbfed:55
c:12 b:13
cb:25
f:5 e:9
fe:14 d:16
fed:30
a:45
acbfed:1000 1
0 1
0 1 0 1
0 1
6
Discrete Mathematics (MA 2333) – Faculty of Science Telkom Institute of Technology
Binary Search Trees (BST)
Searching for items is a list is one of the most important task that arises in computer science.Our primary goal is to implement a searching algorithm that finds item efficiently when the items are totally ordered. This can be accomplished through binary search tree
Discrete Mathematics (MA 2333) – Faculty of Science Telkom Institute of Technology
Binary Search Trees (BST)
Each child of a vertex designated as right or left childNo vertex has more than one right child or left childEach vertex labeled with a key, which is one of the itemsVertices are assigned keys so that the key of a vertex is both larger than the keys of all vertices in its left subtree and smaller than keys of all vertices in its right subtree
Discrete Mathematics (MA 2333) – Faculty of Science Telkom Institute of Technology
Binary Search Trees (BST)
Given data which ordered by input:
65, 34, 22, 45, 100, 120, 8
The BST is
Discrete Mathematics (MA 2333) – Faculty of Science Telkom Institute of Technology
Exercises1. Determine minimum spanning tree of graph below. Use
Prim’s algorithm, and Kruskal’s algorithm!
Discrete Mathematics (MA 2333) – Faculty of Science Telkom Institute of Technology
Exercises
2. Given prefix notation : /+*AB/CD*E-FG
Construct a expression tree by this notationWhat is infix notation?What is post notation?
Discrete Mathematics (MA 2333) – Faculty of Science Telkom Institute of Technology
Exercises
3. Given data which ordered by input :45, 78, 3, 100, 60, 80, 120
Construct BST!Traverse the BST in preorderTraverse the BST in inorderTraverse the BST in postorder