CH 05
CH 05
Discrete Math 2
Content
• Tree and basic properties of tree.
• The spanning tree of the graph & the basic algorithms
for constructing the spanning tree.
• The problem of finding the smallest spanning tree &
algorithms for finding the smallest spanning tree.
• Kruskal algorithm finding the smallest spanning tree.
• Prim algorithm finding the smallest spanning tree.
2
Tree and the basic properties
Definition
• Definition 1: We call a tree an undirected , connected graph without
cycles.
• Definition 2: We call the forest an undirected graph with no cycles.
– Thus, the forest is a graph where each connected component is a tree.
• Example: a forest has 3 trees
4
Some properties of tree
• Theorem: Suppose T= <V, E> is an undirected graph with n vertices.
Then the following statements are equivalent:
a) T is a tree ;
b) T has no cycles and has n-1 edges;
c) T is connected and has exactly n-1 edges;
d) T is connected and each of its edges is a bridge;
e) Between any two vertices of T are connected by exactly one simple path;
f) T does not contain a cycle, but every time we add an edge to it, we get
exactly one cycle.
5
Rooted tree
• Definition 1: Let the tree T = (X, U). In the tree T, we proceed to determine the
level for each vertex as follows:
– (1) Choose an arbitrary vertex as the root of the tree, denoted x0 , whose level is 0.
– (2) The vertex adjacent to the origin is called a peak of level 1. Likewise, a vertex that has not
yet been determined but is adjacent to a peak of level k-1 is called a peak of level k.
• The tree that has determined the level for the vertices above is called a rooted tree.
6
Rooted tree
Definition 2: Given a tree with root T.
1) If v is a vertex other than the origin x0 then there is a single path from the origin x0 to v. On
this path: we call the vertex u adjacent to v the father of v and vice versa v called the child of
u; All vertices that are not adjacent to v are called ancestors of v and vice versa v are called
descendants of these vertices.
2) Vertices with the same father are called brothers.
3) Let y be a vertex of the tree T . The symbol T(y) is a subgraph of the tree T under
consideration, including the vertex y and its descendants and all adjacent edges of y 's
descendants. Then T(y) is called a subtree at the vertex r of the original tree T (here r is the
parent of y).
4) The vertices of a tree that have no children are called leaves. In other words, every hanging
vertex that has no children (not a root) is a leaf of a tree. Every vertex that is not a leaf is
called an inner vertex of the tree (the root x0 is also an inner vertex). The height h(T) of the
tree is the maximum level of a leaf in the tree.
5) A tree with a root T is said to be symmetric (or symmetrical) if every leaf vertex has a level
equal to the height h(T) of the tree.
7
Rooted tree
A tree T 0 is a tree with a
disproportionate root.
Theorem 2: A m- ary tree with height h and number of leaves s then: s≤mh or h ≥logm (s) .
- Example : Given a binary tree T with 18 leaves. We have log2 18 = 4.17, so the minimum height of T
is: h = 5.
9
Decision tree
Definition:
• A decision tree is a rooted tree that can be used to model problems in which a sequence
of decisions leads to a solution.
• Each inner vertex corresponds to a decision and each of its subtrees each of the possible
outcomes of the parent decision.
• The possible solutions of the problem correspond to each path from the root to the leaf
of this decision tree.
Example: What is the maximum number of binary comparisons (each time comparing two
elements) to sort n distinct real numbers a1, a2, …, an in descending order .
10
Tree traversal algorithms
• Definition: Let T = T(r) be a rooted and ordered tree with
root r. Let T1, T2, …, Tn be the subtrees at r of T in order
from left to right.
– (1) Pre-order traversal: Visit root r / Browse subtree T1 /…/
Browse subtree Tn.
– (2) Mid-order traversal: Browse subtree T1 / Visit root r /
Browse subtree T2 / Browse subtree T3 /…/ Browse subtree Tn
.
– (3) Post-order traversal: Traverse subtree T1 / …/ Browse
subtree Tn / Visit root r .
11
Tree traversal algorithms
Infix, prefix and suffix notations:
• We can represent complex expressions using binary and ordered trees. In which,
the inner vertices represent operations (operators) and the leaves represent
numbers or variables (operators).
Example:
12
Tree traversal algorithms
Example : Find Polish notation and reverse Polish notation of two
expressions A, B.
Solution:
• In the prefix form, we evaluate the expression from right to left, when
an operator is encountered, we perform the operation corresponding
to the two operands that go to the right of this operator.
• To evaluate an expression in suffix form, we must proceed from left to
right and perform an operation each time an operator follows two
operands. (Each result of the operation just performed continues to be
treated as a new operand).
13
Applying Tree to encode information – Huffman's Algorithm
14
Applying Tree to encode information – Features
1. For prefix codes, each word is encoded by a binary string, and conversely, each
binary string is the code of exactly one word.
2. The prefix codes can be represented by a binary tree (also called a code tree)
respectively as follows:
a) The letters are the labels of the leaves on the tree. Edges are labeled as 0 or 1.
b) The binary string encoding a letter is the sequence of labels of the edges on the path
from the root to the leaf labeled with this letter.
3. To check in a way that a code is a prefix code, it is necessary to represent the
code as a binary tree. If there exists an inner vertex labeled as a letter, it is not a
prefix code.
4. To decode a binary string, we use a sequence of bits to form a path that starts at
the root, ends when a leaf is encountered, and starts a new path from the root.
15
Applying Tree to encode information – Features
16
Applying Tree to encode information –
Huffman's Algorithm
• Definition 2: The prefix code T of the alphabet X is said to be the optimal
prefix code for message Y if the number of bits used to encode the message is
at least.
17
Huffman algorithm (determining the optimal prefix code of
the alphabet X for message Y )
1. Treat each letter xi X as a card and each card is accompanied by a label f(xi ) which is the
frequency of that letter.
2. Arrange the leaves in order of non-decreasing frequency: f(x1 ) ≤ f(x2 ) ≤ … ≤ f(xn ). Connect
the two leaves with the lowest frequency x1 , x2 we get a vertex in y1 , labeled f(y1 )=f(x1
)+f(x2 ).
3. Consider y1 is a leaf (remove the two letters x1 , x2 ). Repeat step 2. Do this until all the
cards are matched.
From the obtained Huffman cipher tree, infer the optimal charset.
18
Huffman's Algorithm (cont'd)
19
Huffman's Algorithm (cont'd)
20
Spanning Tree
Definition of Spanning Tree
• Definition 3: Let G be a connected undirected graph. We call
the subgraph T of G a spanning tree of G if T satisfies two
conditions:
a) T is a tree;
b) The vertex set of T is equal to the vertex set of G.
• For example:
22
Two basic math problems about tree
• Problem 1. Given an undirected graph G=<V,E>. Let's build a spanning
tree of the graph starting at the vertex u V.
– Solved by basic search algorithms: DFS or BFS algorithms .
• Problem 2. Given a weighted undirected graph G=<V,E>. Let's construct
a spanning tree of minimum length.
– Solve with Kruskal or PRIM algorithm .
23
Building the spanning tree of the graph based
on the DFS algorithm
• Problem:
– Given undirected graph G=<V,E>. Let's build a spanning tree of the graph starting
at vertex u V using the DFS algorithm.
• How to:
– Every time we reach vertex v ie ( chuaxet [v] = True // unverified vertex v ) from
vertex u, edge (u,v) is included in the spanning tree.
24
Algorithm description
• Spanning tree starting at vertex u based on the DFS algorithm:
25
Algorithm to build a spanning tree
26
Tree-Graph-DFS algorithm test (1/2)
27
Tree-Graph-DFS algorithm checking (2/2)
28
Algorithm Implementation
• Procedure Init(): reads data and sets the value of the sourxet[] array.
• Procedure Tree-DFS (u): DFS algorithm starts at vertex u.
• Procedure Result(): records the edge set of the spanning tree.
29
Building a spanning tree of a graph based
on the BFS algorithm
30
Tree-BFS algorithm checking (1/2)
• For graphs represented
as an adjacency matrix.
Construct a spanning
tree using BFS with a
starting vertex u=1.
31
Tree-BFS algorithm checking (2/2)
32
Algorithm Implemetation
• Procedure Init(): reads data and sets the value of the sourxet[] array.
• Procedure Tree-BFS (u): BFS algorithm starts at vertex u.
• Procedure Result(): records the edge set of the spanning tree.
33
The problem of finding the
smallest spanning tree &
algorithms for finding the
smallest spanning tree.
Spanning tree of minimum length
• Description
– Let G=<V, E> be an undirected graph connected with the set of vertices
V = {1, 2, . . ., n } and the edge set E consists of m edges.
– Each edge e of the graph is assigned a non-negative number c(e) called
the edge length.
– Suppose H=<V, T> is a spanning tree of the graph G. We call the length
c(H) of the spanning tree H the sum of the lengths of the sides:
• Problem :
– Among the spanning trees of the graph, find the spanning tree of the
shortest length of the graph.
35
Two realistic models of the problem
• Computer networking problem
– A computer network consists of n computers numbered 1, 2, . . ., n. Given that the cost of
connecting machine i to machine j is c[i, j] , i, j = 1, 2, . . ., n.
– Find a way to network so that the cost is minimal.
• The problem of building a cable system
– Suppose we want to build a telephone cable system connecting n points of a telecommunications
network so that any point in the network has a transmission line to other points.
– Knowing the cost of building a cable system from point i to point j is c[i,j] .
– Find a way to build a cable network so that the cost is minimal.
36
Kruskal algorithm (1/2)
• Add each edge to the spanning tree
• Each step selects the least weighted edge not yet in the spanning tree
– If adding this edge to the spanning tree does not create a cycle, add this edge
• The algorithm stops when
– The spanning tree has enough (n - 1) edges,
– Or there are no edges left in the graph.
37
Kruskal algorithm (2/2)
38
Algorithm checking (1/4)
• Apply Kruskal's
algorithm to find the
smallest spanning
tree for the graph
represented by the
weight matrix as
shown in the figure
below?
39
Algorithm checking (2/4)
• Step 1. T = ; D( T ) = 0;
• Step 2. Sort edges in ascending order of weights.
40
Algorithm checking (3/4)
• Step 3. (repeat)
41
Algorithm checking (4/4)
• Step 4: Return the result
42
Algorithm Implemetation
• Procedure Init(): reads data represented by a list of weights.
• Heap() procedure: sort edges in ascending order of weight using Heap Sort
algorithm.
• Procedure Find(), Union(): find and check when adding edges to the spanning tree
creates a cycle or not.
• Procedure Result(): returns the set of edges and the minimum length of the
spanning tree.
43
Prim Algorithm (ver. 1)
• Maintain two top episodes: V H (vertex set of the spanning tree)
and V (the set of vertices not yet in the spanning tree)
– Initially V H = {s} , s is any vertex of the graph
– DRAW is equal to the set of vertices of the graph minus s.
• Each step selects the edge with the least weight and has 1 vertex in
VH and 1 vertex in V.
– Insert this edge into the spanning tree
– Take the vertex adjacent to this edge from V to VH .
• The algorithm stops when
– The spanning tree has enough (n - 1) edges,
– Or there are no vertices left in V.
44
Prim Algorithm (ver. 1)
45
Algorithm checking (1/2)
46
Algorithm checking (2/2)
47
Algorithm Implementation
• Procedure Init(): reads data represented by a list of weights.
• Prim procedure: The PRIM algorithm builds the smallest
spanning tree.
• Procedure Result(): returns the set of edges and the minimum
length of the spanning tree.
48
Exercise 1
Let the undirected graph shown below adjacency matrix as
shown on the right:
a) Present the algorithm to build a spanning tree of the graph
starting at the vertex u V based on the BFS(u) algorithm?
b) Test the algorithm BFS(u) catches start at vertex u=1? Specify
the average result time according to each execution step of the
technique maths.
c) Test the algorithm BFS(u) catches start at vertex u=7? Specify
the average result time according to each execution step of the
technique maths.
d) Write a program to build a spanning tree of the graph
starting at vertex u V based on the algorithm BFS(u)?
50
Exercise 2
Let the undirected graph shown below adjacency list as shown above:
a) Present the algorithm to build a spanning tree of the graph starting at the vertex u V based on the
algorithm D FS(u)?
b) Construct the spanning tree of the graph starting at the vertex u=3? Specify the result for each
execution step of the algorithm?
c) Write a program to build a spanning tree of a graph starting at vertex u V based on the DFS(u)?
51
Exercise 3
Weighted undirected graph shown below adjacency
matrix as shown on the right:
a) T presents Prim . algorithm find minimum spanning
tree on weighted undirected graph?
b) Apply the algorithm, find the smallest spanning tree
at vertex 1 of the graph G, only clear results according
to each implementation step of the algorithm?
c) Write a program to find the minimum spanning tree
of a graph using PRIM algorithm?
52
Exercise 4
Weighted undirected graph shown below adjacency matrix
as shown on the right:
a) T presents the Kruskal algorithm find minimum spanning
tree on weighted undirected graph?
b) Apply the algorithm, find the smallest spanning tree of
the graph G, only clear results according to each
implementation step of the algorithm?
c) Write a program to find the minimum spanning tree of a
graph using Kruskal's algorithm?
53