Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Chapter 8. Trees
Mai Van Duy
August 12, 2022
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Contents
1. Introduction to Trees
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Contents
1. Introduction to Trees
2. Applications of Trees
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Contents
1. Introduction to Trees
2. Applications of Trees
3. Tree Traversal
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Contents
1. Introduction to Trees
2. Applications of Trees
3. Tree Traversal
4. Spanning Trees
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Contents
1. Introduction to Trees
2. Applications of Trees
3. Tree Traversal
4. Spanning Trees
5. Minimum Spanning Trees
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Definition
A tree is a connected undirected graph with no simple
circuits.
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Definition
A tree is a connected undirected graph with no simple
circuits.
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Definition
A tree is a connected undirected graph with no simple
circuits.
G1 , G2 are trees and G3 , G4 are not.
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Theorem
An undirected graph is a tree if and only if there is a
unique simple path between any two of its vertices.
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Definition
A rooted tree is a tree in which one vertex has been
designated as the root and every edge is directed away
from the root.
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Definition
A rooted tree is a tree in which one vertex has been
designated as the root and every edge is directed away
from the root.
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Some common concepts
I g is root of tree
I j is parent of m
and l
I m or l is a child of j
I m, l are siblings
I g is ancestor of m
and m is
descendant of g
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Definition
A rooted tree is called an m-ary tree if every internal
vertex has no more than m children. The tree is called a
full m-ary tree if every internal vertex has exactly m
children. A complete m-ary tree is a full m-ary tree in
which every leaf is at the same level. An m-ary tree with
m = 2 is called a binary tree.
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Definition
A rooted tree is called an m-ary tree if every internal
vertex has no more than m children. The tree is called a
full m-ary tree if every internal vertex has exactly m
children. A complete m-ary tree is a full m-ary tree in
which every leaf is at the same level. An m-ary tree with
m = 2 is called a binary tree.
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Definition
An ordered rooted tree is a rooted tree where the children
of each internal vertex are ordered (which is usually
represented as in the graph).
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Definition
An ordered rooted tree is a rooted tree where the children
of each internal vertex are ordered (which is usually
represented as in the graph).
f is the left child of d and g is the right child of d
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Definition
An ordered rooted tree is a rooted tree where the children
of each internal vertex are ordered (which is usually
represented as in the graph).
f is the left child of d and g is the right child of d
(b): the left subtree of c and (c): the right subtree of c.
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Theorem
A tree with n vertices has n − 1 edges.
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Theorem
A full m-ary tree with i internal vertices contains n = mi + 1
vertices.
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Theorem
A full m-ary tree with
n vertices has i = (n − 1)/m internal vertices and
l = [(m − 1)n + 1]/m leaves;
i internal vertices has n = mi + 1 vertices and
l = (m − 1)i + 1 leaves;
l leaves has n = (ml − 1)/(m − 1) vertices and
i = (l − 1)/(m − 1) internal vertices.
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Example
Suppose that someone starts a chain letter. Each person
who receives the letter is asked to send it on to four other
people. Some people do this, but others do not send any
letters. How many people have seen the letter, including
the first person, if no one receives more than one letter
and if the chain letter ends after there have been 100
people who read it but did not send it out? How many
people sent out the letter?
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Solution
The chain letter can be represented using a 4-ary tree.
The internal vertices correspond to people who sent out
the letter, and the leaves correspond to people who did
not send it out. Because 100 people did not send out the
letter, the number of leaves in this rooted tree is l = 100.
Hence, part (iii) of Theorem 4 shows that the number of
people who have seen the letter is
n = (4.100 − 1)/(4 − 1) = 133. Also, the number of internal
vertices is 133 − 100 = 33, so 33 people sent out the letter.
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Definition
The level of a vertex v in a rooted tree is the length of the
unique path from the root to this vertex. The level of the
root is defined to be zero.
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Definition
The level of a vertex v in a rooted tree is the length of the
unique path from the root to this vertex. The level of the
root is defined to be zero.
The height of a rooted tree is the maximum of the levels of
vertices.
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Definition
The level of a vertex v in a rooted tree is the length of the
unique path from the root to this vertex. The level of the
root is defined to be zero.
The height of a rooted tree is the maximum of the levels of
vertices.
A rooted m-ary tree of height h is balanced if all leaves
are at levels h or h − 1.
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Definition
The level of a vertex v in a rooted tree is the length of the
unique path from the root to this vertex. The level of the
root is defined to be zero.
The height of a rooted tree is the maximum of the levels of
vertices.
A rooted m-ary tree of height h is balanced if all leaves
are at levels h or h − 1.
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Theorem
There are at most mh leaves in an m-ary tree of height h.
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Theorem
There are at most mh leaves in an m-ary tree of height h.
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Corollary
If an m-ary tree of height h has l leaves, then h ≥ dlogm l e.
If the m-ary tree is full and balanced, then h = dlogm l e.
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Definition
A binary search tree is an ordered rooted binary tree.
Each of its vertices is labeled with a key, which is one of
the items and vertices 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 the keys of all vertices in its right
subtree.
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Definition
A binary search tree is an ordered rooted binary tree.
Each of its vertices is labeled with a key, which is one of
the items and vertices 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 the keys of all vertices in its right
subtree.
Note: Just the label of vertices can be compared !.
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Definition
A binary search tree is an ordered rooted binary tree.
Each of its vertices is labeled with a key, which is one of
the items and vertices 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 the keys of all vertices in its right
subtree.
Note: Just the label of vertices can be compared !.
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Algorithm
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Algorithm
Note: The algorithm returns the location of x or adds a
new vertex with label x into binary search tree.
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Example
Find the word “oceanography” (or insert it into the binary
search tree) in the following binary search tree.
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Definition
A rooted tree in which each internal vertex corresponds
to a decision, with a subtree at these vertices for each
possible outcome of the decision, is called a decision
tree.
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Example
Suppose there are seven coins, all with the same weight,
and a counterfeit coin that weighs less than the others.
How many weighings are necessary using a balance
scale to determine which of the eight coins is the
counterfeit one?
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Solution
There are three possibilities for each weighing on a
balance scale. Consequently, the decision tree for the
sequence of weighings is a 3-ary tree. There are at least
eight leaves in the decision tree because there are eight
possible outcomes. The largest number of weighings
needed to determine the counterfeit coin is the height of
the decision tree. Hence, at least dlog3 8e = 2 weighings
are needed.
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Example
Form a decision tree of sorting three number a, b, c.
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Example
Form a decision tree of sorting three number a, b, c.
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Theorem
A sorting algorithm based on binary comparisons requires
at least dlog n!e comparisons.
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Corollary
The number of comparisons used by a sorting algorithm to
sort n elements based on binary comparisons is Ω(n log n).
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Theorem
The average number of comparisons used by a sorting
algorithm to sort n elements based on binary comparisons
is Ω(n log n).
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Example
Represent this word using bit strings: “SEEN”.
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Example
Represent this word using bit strings: “SEEN”.
I Using ASCII encoding (S:1010011, E:1000101,
N:1001110)
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Example
Represent this word using bit strings: “SEEN”.
I Using ASCII encoding (S:1010011, E:1000101,
N:1001110)
I Using fix-length encoding (S:00, E:01, N:10)
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Example
Represent this word using bit strings: “SEEN”.
I Using ASCII encoding (S:1010011, E:1000101,
N:1001110)
I Using fix-length encoding (S:00, E:01, N:10)
I Using variable-length encoding (E:0, S:10, N:11)
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Definition
Prefix code is a type of codes which encodes the letters
so that the bit string for a letter never occurs as the first
part of the bit string for another letter.
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Representing a prefix code
A prefix code can be represented using a binary tree,
where the characters are the labels of the leaves in the
tree. The edges of the tree are labeled so that an edge
leading to a left child is assigned a 0 and an edge
leading to a right child is assigned a 1. The bit string used
to encode a character is the sequence of labels of the
edges in the unique path from the root to the leaf that
has this character as its label.
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Example of Representing a prefix code
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
How to construct a prefix code that is using the fewest
bits?.
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Huffman coding
Huffman coding is an algorithm that takes as input the
frequencies (which are the probabilities of occurrences)
of symbols in a string and produces as output a prefix
code that encodes the string using the fewest possible
bits, among all possible binary prefix codes for these
symbols.
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Huffman coding
Given symbols and their frequencies, our goal is to
construct a rooted binary tree where the symbols are the
labels of the leaves. The algorithm begins with a forest of
trees each consisting of one vertex, where each vertex
has a symbol as its label and where the weight of this
vertex equals the frequency of the symbol that is its label.
At each step, we combine two trees having the least total
weight into a single tree by introducing a new root and
placing the tree with larger weight as its left subtree and
the tree with smaller weight as its right subtree.
Furthermore, we assign the sum of the weights of the two
subtrees of this tree as the total weight of the tree. The
algorithm is finished when it has constructed a tree, that
is, when the forest is reduced to a single tree.
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Example
Use Huffman coding to encode the following symbols with
the frequencies listed: A: 0.08, B: 0.10, C: 0.12, D: 0.15, E:
0.20, F: 0.35. What is the average number of bits used to
encode a character?
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Solution
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Solution
The average number of bits used to encode a symbol
using this encoding is
3 × 0.08 + 3 × 0.10 + 3 × 0.12 + 3 × 0.15 + 2 × 0.20 + 2 × 0.35 = 2.45
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Definition
Universal address system is a way that we can totally
order the vertices of an ordered rooted tree.
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Definition
Universal address system is a way that we can totally
order the vertices of an ordered rooted tree.
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Definition
Tree traversal is a procedure that visiting all the vertices of
an ordered rooted tree.
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Definition of Preorder Traversal
Let T be an ordered rooted tree with root r. If T consists
only of 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.
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Definition of Preorder Traversal
Let T be an ordered rooted tree with root r. If T consists
only of 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.
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Preorder Algorithm
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Definition of Inorder Traversal
Let T be an ordered rooted tree with root r. If T consists
only of r, then r is the inorder traversal of T . Otherwise,
suppose that T1 , T2 , ..., Tn are the subtrees at r from left to
right. The inorder traversal begins by traversing T1 in
inorder, then visiting r. It continues by traversing T2 in
inorder, then T3 in inorder, . . . , and finally Tn in inorder.
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Definition of Inorder Traversal
Let T be an ordered rooted tree with root r. If T consists
only of r, then r is the inorder traversal of T . Otherwise,
suppose that T1 , T2 , ..., Tn are the subtrees at r from left to
right. The inorder traversal begins by traversing T1 in
inorder, then visiting r. It continues by traversing T2 in
inorder, then T3 in inorder, . . . , and finally Tn in inorder.
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Inorder Algorithm
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Definition of Postorder Traversal
Let T be an ordered rooted tree with root r. If T consists
only of r, then r is the postorder traversal of T . Otherwise,
suppose that T1 , T2 , ..., Tn are the subtrees at r from left to
right. The postorder traversal begins by traversing T1 in
postorder, then T2 in postorder, . . . , then Tn in postorder,
and ends by visiting r.
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Definition of Postorder Traversal
Let T be an ordered rooted tree with root r. If T consists
only of r, then r is the postorder traversal of T . Otherwise,
suppose that T1 , T2 , ..., Tn are the subtrees at r from left to
right. The postorder traversal begins by traversing T1 in
postorder, then T2 in postorder, . . . , then Tn in postorder,
and ends by visiting r.
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Postorder Algorithm
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Representing the expression
The expression can be represent by the binary trees with
the rules: the internal vertices represent operations, and
the leaves represent the variables or numbers.
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Example
What is the ordered rooted tree that represents the
expression ((x + y ) ↑ 2) + ((x − 4)/3)?
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Example
What is the ordered rooted tree that represents the
expression ((x + y ) ↑ 2) + ((x − 4)/3)?
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Definition
We obtain prefix form of an expression when we traverse
its rooted tree in preorder.
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Definition
We obtain prefix form of an expression when we traverse
its rooted tree in preorder.
We obtain infix form of an expression when we traverse its
rooted tree in inorder.
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Definition
We obtain prefix form of an expression when we traverse
its rooted tree in preorder.
We obtain infix form of an expression when we traverse its
rooted tree in inorder.
We obtain postfix form of an expression when we traverse
its rooted tree in postorder.
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Definition
We obtain prefix form of an expression when we traverse
its rooted tree in preorder.
We obtain infix form of an expression when we traverse its
rooted tree in inorder.
We obtain postfix form of an expression when we traverse
its rooted tree in postorder.
Note: Prefix form is called Polish notation and Postfix form is
called reverse Polish notation.
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Example
What is the prefix (infix, postfix) form of the following tree?
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Example
What is the prefix (infix, postfix) form of the following tree?
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Definition
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.
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Example
Find a spanning tree of this simple graph:
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Theorem
A simple graph is connected if and only if it has a
spanning tree.
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Depth-First Search
We will form a rooted tree. Arbitrarily choose a vertex of
the graph as the root. Form a path starting at this vertex
by successively adding vertices and edges, where each
new edge is incident with the last vertex in the path and a
vertex not already in the path. Continue adding vertices
and edges to this path as long as possible. If the path
goes through all vertices of the graph, the tree consisting
of this path is a spanning tree. If it is not the case, move
back to the next to last vertex in the path, and, if possible,
form a new path starting at this vertex passing through
vertices that were not already visited. If this cannot be
done, move back another vertex in the path, that is, two
vertices back in the path, and try again.
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Depth-First Search Algorithm
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Example
Find a spanning tree of this graph using Depth-First Search:
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Breadth-First Search
Arbitrarily choose a root from the vertices of the graph.
Then add all edges incident to this vertex. The new
vertices added at this stage become the vertices at level
1 in the spanning tree. Arbitrarily order them. Next, for
each vertex at level 1, visited in order, add each edge
incident to this vertex to the tree as long as it does not
produce a simple circuit. Arbitrarily order the children of
each vertex at level 1. This produces the vertices at level 2
in the tree. Follow the same procedure until all the
vertices in the tree have been added. The procedure
ends because there are only a finite number of edges in
the graph.
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Breadth-First Search Algorithm
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Example
Find a spanning tree of this graph using Breadth-First
Search:
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Definition
A minimum spanning tree in a connected weighted
graph is a spanning tree that has the smallest possible
sum of weights of its edges.
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Prim’s Algorithm
Begin by choosing any edge with smallest weight, putting
it into the spanning tree. Successively add to the tree
edges of minimum weight that are incident to a vertex
already in the tree, never forming a simple circuit with
those edges already in the tree. Stop when n − 1 edges
have been added.
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Prim’s Algorithm
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Example
Find a minimum spanning tree for this graph:
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Solution
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Kruskal’s Algorithm
Choose an edge in the graph with minimum weight.
Successively add edges with minimum weight that do not
form a simple circuit with those edges already chosen.
Stop after n − 1 edges have been selected.
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Kruskal’s Algorithm
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Example
Find a minimum spanning tree for this graph:
Mai Van Duy Chapter 8. Trees
Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees
Note: In Prim’s algorithm, edges of minimum weight that
are incident to a vertex already in the tree, and not
forming a circuit, are chosen; whereas in Kruskal’s
algorithm edges of minimum weight that are not
necessarily incident to a vertex already in the tree, and
that do not form a circuit, are chosen.
Mai Van Duy Chapter 8. Trees