0% found this document useful (0 votes)
0 views54 pages

CH 05

The document provides an overview of trees in graph theory, including definitions, properties, and types of trees such as rooted trees and m-ary trees. It discusses algorithms for constructing spanning trees, including Depth-First Search (DFS) and Breadth-First Search (BFS), as well as methods for finding minimum spanning trees using Kruskal's and Prim's algorithms. Additionally, it covers applications of trees in decision-making and information encoding, specifically through Huffman's algorithm.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views54 pages

CH 05

The document provides an overview of trees in graph theory, including definitions, properties, and types of trees such as rooted trees and m-ary trees. It discusses algorithms for constructing spanning trees, including Depth-First Search (DFS) and Breadth-First Search (BFS), as well as methods for finding minimum spanning trees using Kruskal's and Prim's algorithms. Additionally, it covers applications of trees in decision-making and information encoding, specifically through Huffman's algorithm.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 54

Spanning Tree

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 & algorithms for finding the minimum spanning tree
• Kruskal algorithm finding the minimum spanning tree
• Prim algorithm finding the minimum spanning tree

2
Tree and the basic properties
Definition
• Definition 1: Tree is a connected undirected graph with no simple cycles.
• Definition 2: Forest is an undirected graph with no simple cycles.
– Thus, the forest is a graph which each connected component is a tree.
• Example: a forest has 3 trees

4
Some properties of tree
Theorem: Let 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 edge 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 an edge is added, we get exactly
one cycle.

5
Rooted tree
• Definition 1: Let T = (X, U) is a tree. In 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. (ie.
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)

6
Rooted tree
Definition 2: Given a rooted tree T.
1) If v is a vertex other than the root x0 then there is a simple path from the root 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 siblings.
3) Let y be a vertex of the tree T . The symbol T(y) is a subgraph of the tree T, 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 rooted 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 internal vertex of the tree (the root x0 is also an interal vertex). The height h(T) of
the tree is the maximum level of a leaf in the tree.
5) A rooted tree 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 rooted tree T0 is not
symmetrical.

The trees are symmetrical with the root x0


8
m-ary Tree
Definition:
• (1) A rooted tree T is said to be an m-ary tree, if each interal vertex of T has at most m children and at
least one vertex has m children.
• (2) A rooted tree T is said to be a full m-ary Tree if all its internal vertices have m children.
• (3) An m-ary Tree with m = 2 is called a binary tree .
Theorem 1: A full m-ary tree with k internal vertices has mk + 1 vertices and (m - 1). k + 1 leaves.

Theorem 2: A m- ary tree with height h and number of leaves s then: s≤mh or h ≥logm (s). (ie. There are
at most mh leaves in an m-ary tree of height h)
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 internal vertex corresponds to a decision and each subtree is a possible outcome 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
Traversal algorithms
• Definition: Let T = T(r) be an ordered rooted 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) In-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
Traversal algorithms
Infix, Prefix and Suffix Notation:
• We can represent complicated expressions using binary and ordered trees. In
which, the internal vertices represent operations (+,-,*,/,^) and the leaves
represent numbers or variables (operators).
Example:

12
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 next 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 to encode information – Huffman's Algorithm

• Definition 1: An encoding is called a prefix code if the binary string corresponding


to a certain letter is not the prefix (first part) of the binary string corresponding to
another letter.

14
Applying 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 the 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 a prefix code, it is necessary to represent the code as a binary tree. If
there exists an internal 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 to encode information – Features

16
Applying to encode information – Huffman's
Algorithm
• Definition 2: The prefix code T of the alphabet table X is said to be the optimal
prefix code for message Y if the number of bits used to encode the message is
minimum.

17
Huffman algorithm (determining the optimal prefix code of
the alphabet table X for message Y )
1. Treat each letter xi  X as a leaf and each leaf 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 internet 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
leaves are matched.
From the obtained Huffman code tree, infer the optimal coded set.

18
Huffman's Algorithm (cont'd)

19
Huffman's Algorithm (cont'd)

20
Spanning Tree
Definition of Spanning Tree
• Definition: 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.
– Solve with 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 ) from vertex u, edge (u,v) is
included in the spanning tree.

24
Algorithm description
• Construct the spanning tree starting at vertex u using the DFS
algorithm:

25
Algorithm to Construct a spanning tree

26
Tree-Graph-DFS algorithm - Ex (1/2)

• For graphs represented


as an adjacency matrix.
Construct a spanning
tree using DFS with a
starting vertex u=1.

27
Tree-Graph-DFS algorithm - Ex (2/2)

28
Algorithm Implementation
• Procedure Init(): reads data and sets the value of the chuaxet[] array.
• Procedure Tree-DFS (u): DFS algorithm starts at vertex u.
• Procedure Result(): records the edge set of the spanning tree.

See illustrative code

29
Building a spanning tree of a graph based
on the BFS algorithm

30
Tree-BFS algorithm - Ex (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 - Ex (2/2)

32
Algorithm Implemetation
• Procedure Init(): reads data and sets the value of the chuaxet[] array.
• Procedure Tree-BFS (u): BFS algorithm starts at vertex u.
• Procedure Result(): records the edge set of the spanning tree.

See illustrative code

33
The problem & algorithms for
finding the minimum spanning
tree
Spanning tree of minimum length
• Description
– Let G=<V, E> be a connected undirected graph with the vertex set 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 edges:

• 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.
– How to make a 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] .
– How 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 minimum 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 - Ex (1/4)
• Apply Kruskal's
algorithm to find the
minimum spanning
tree for the graph
represented by the
weight matrix as
shown in the figure
below?

39
Algorithm - Ex (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.

See illustrative code

43
Prim Algorithm (ver. 1)
• Maintain two vertex sets: VH (vertex set of the spanning tree) and V
(the set of vertices that are not included the spanning tree yet)
– Initially VH = {s}, s is any vertex of the graph
– V is equal to the set of vertices of the graph minus s.
• Each step selects the edge with the minimum weight and has 1
vertex in VH and 1 vertex in V.
– Insert this edge into the spanning tree
– Take the vertex adjacenting 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 - Ex (1/2)

• Apply Prim's algorithm


to find the minimum
spanning tree for the
graph represented by
the weight matrix as
shown in the figure
below?

46
Algorithm - Ex (2/2)

47
Algorithm Implementation
• Procedure Init(): reads data represented by a list of weights.
• Prim procedure: The PRIM algorithm builds the minimum
spanning tree.
• Procedure Result(): returns the set of edges and the minimum
length of the spanning tree.

See illustrative code

48
Prim algorithm (ver. 2)
Each vertex u assigns two values called labels:
d[u]: is the shortest distance from u to the spanning tree
near[u]: is the vertex of the spanning tree closest to u
Initially assign every vertex u has:
d[u]=a[s][u];//s is any chosen starting vertex.
near[u]=s
mark s visited //visit[s]=1
In turn, select (n-1) the remaining vertices to visit {
- the vertex u selected to visit is the unvisited vertex, has the smallest d[u] and the selected edge
is (u, near[u])
- mark the vertex u visited already.
- update labels for vertices v satisfying the following conditions: v unvisited, v adjacent to u,
d[v]>a[u][v].
Update by assigning d[v]=a[u][v] and near[v]=u;
}
print the list of edges of the spanning tree
49
Ex: Prim v2

1 3
4
1 4 2
3 1
2 1
7 2
4
6 4
1 2 3

50
Exercise 1
Let the undirected graph using 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) Construct the spanning tree with the algorithm BFS(u)
starting at vertex u=1. Show steps in details.
c) Construct the spanning tree with the algorithm BFS(u)
starting at vertex u=7. Show steps in details.
d) Write a program to build a spanning tree of the graph
starting at vertex u  V based on the algorithm BFS(u).

52
Exercise 2

Let the undirected graph using 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. Show steps in details.
c) Write a program to build a spanning tree of a graph starting at vertex u  V based on the DFS(u).

53
Exercise 3
The weighted undirected graph using adjacency matrix
as shown on the right:
a) Presents Prim algorithm find minimum spanning tree
on weighted undirected graph.
b) Apply the algorithm, find the minimum spanning tree
at vertex 1 of the graph, Show steps in details.
c) Write a program to find the minimum spanning tree
of a graph using PRIM algorithm.

54
Exercise 4
The weighted undirected graph using adjacency matrix as
shown on the right:
a) Presents the Kruskal algorithm find minimum spanning
tree on weighted undirected graph.
b) Apply the algorithm, find the minimum spanning tree of
the graph. Show steps in details.
c) Write a program to find the minimum spanning tree of a
graph using Kruskal's algorithm.

55

You might also like