0% found this document useful (0 votes)
10 views112 pages

DM Trees1

Uploaded by

tranphucquy2002
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)
10 views112 pages

DM Trees1

Uploaded by

tranphucquy2002
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/ 112

Discrete Mathematics

Trees: Chapter 11

Vietnamese - German University


Instructor: Tran Thi Thu Huong, VGU
Email: [email protected]
Office: A109
Topics will be covered

1. Trees: Introduction and Terminologies


2. m-ary trees: Balanced trees, binary trees, full binary trees
3. Binary Search Trees, Prefix codes, Huffman codes: covered in
Data Structure and Algorithms
4. Tree Traversals: covered in Data Structure and Algorithms
5. Labeled Trees: Cayley’s Theorem, Prüfer sequence
6. Matrix-Tree Theorem
7. Optional topics: Matching theory: On the white board!
Introduction to Trees I
1. Tree is a particular type of graph that is connected and acyclic
(no cycle).
2. Tree is the most simple graphic structure in Graph Theory
with lots of practical applications in a variety of areas like
Biology, Computer Science, Chemistry, Mathematics.
3. It is used in organizing/storing data efficiently in data
structure: Binary search trees, Red-black trees, Arithmetic
expressions, Heap, Pointer,...
4. It could help to determine the complexity of algorithms based
on a sequence of decisions: Recursive trees, decision trees
5. It also used to construct efficiently algorithms for locating
items in a list: Binary search tree, Red-Black trees, Heapsort,
Huffman coding...
6. It is used to systematically explore the vertices of a graph:
Breadth/Depth first search, backtracking (Dynamic
Programming).
Terminologies

Definition
i) A simple undirected graph is a tree if it is connected and has
no cycles.
i) In a tree, a vertex with degree 1 is called a leaf. A node is not
a leaf is called an internal node.
iii) A forest is a set of trees, i.e. is a simple graph with no circuits.
Terminologies

Remark: A graph contains no cycles also called an acyclic graph.

Examples:
Properties I

Lemma
Let G be a simple graph.
i) If every vertex in G has degree at least two, then G contains a
cycle.
ii) Every tree with at least 2 vertices has at least one leaf.
Removing it yields again a tree.

Theorem
Let T = (V , E ) be a tree. Then |V | = |E | + 1.
Properties II
Theorem (Equivalent statements for trees)
Let G = (V , E ) be a simple graph. The following statements are
equivalent:
1. G is a tree.
2. G is connected and |V | = |E | + 1.
3. G is acyclic and |V | = |E | + 1.
4. For any two vertices u, v ∈ V , there exists exactly one simple
path from u to v .
5. G is minimally connected, i.e., G is connected and removing
any edge from G disconnects it (every edge of G is a cut
edge).
6. G is maximally acyclic, i.e., G is acyclic and adding any
additional edge creates a cycle.
Examples

On the whiteboard!
Rooted Trees

A rooted tree is a tree in which one vertex is designated as a root


and every edge is directed away from the root.

Remark: Theoretically, a rooted tree is a digraph. However, we


often draw a rooted tree with its root at the top of the graph. The
arrow indicating the directions of the edges can be omitted.
Terminologies

I If there is an edge directed from u to v then u is called the


parent of v and v is called a child of u.
I Vertices having the same parent are called siblings.
I Ancestors of a vertex are the vertices on the path from the
root to that vertex, excluding the vertex itself.
I Descendants of a vertex v are the vertices that have v as an
ancestor.
I Leaves of a tree are the vertices that do not have children.
I Internal vertices are the vertices that have children.
I A subtree with root a is the subgraph consisting of a and all
its descendants and all edges incident to these descendants.
Examples

I Root: 2
2
I 4, 7, 9 are siblings
I 6 is the parent of 5 and a
3 8
child of 8
I Ancestors of 1 are 4, 3, 2
4 7 9 6 10
I Descendants of 8 are 5, 6, 10
I 1, 7, 9, 5, 10 are leaves
1 5
I 2, 3, 4, 8, 6 are internal
vertices
m-ary trees

Definition
1. A rooted tree is called an m-ary tree if each vertex has at
most m children.
2. A rooted tree is called a full m-ary tree if each internal vertex
has exactly m children.
3. A 2-ary tree is called a binary tree.
4. A full 2-ary tree is called a full binary tree.
5. A complete m-ary tree is a full m-ary tree in which all levels
except possibly the last are completely filled, and the last level
has all its nodes to the left side.
Examples I

3 8

4 7 9 6 10

1 5

Figure: A full 3-ary tree with 16


Figure: A 3-ary tree but not a full
vertices
3-ary tree since vertices 2, 8, 6, 4 do
not have enough 3 children
Examples II

Figure: Is it a full m-ary tree for some m?


Enumerations on full m-ary trees

Theorem
If n is the number of vertices and i is the number of internal
vertices of a full m-ary tree then
1. n = mi + 1;
2. the tree has n − i = (m − 1)i + 1 leaves.

Corollary
A full binary tree has an odd number 2n + 1 of vertices with n
internal nodes and n + 1 leaves for some n ≥ 0.
Examples

Example 1. Does there exist a full 4-ary tree with 80 leaves?


Example 2. Some one starts a chain letter game. Each person
receiving the letter either sends it to exactly four other persons, or
to no one. Assume that no one receives more than one letter. The
game ends when there have been exactly 100 persons receiving the
letter and not sending it out.

How many persons received the letter (including the one who
starts the game)?

How many persons sent out the letter?


Level, height, balanced trees

Definition
1. The level of a vertex in a rooted tree is the length of the
unique path from the root to that vertex.
2. The height of a rooted tree is the maximum of the levels of
the vertices.
3. An m-ary tree of height h is called a balanced tree if each
leave has level h or h − 1.
Examples I

I Root: d
d I Vertices with level 1:
a, c, o, i
a c o i I Vertices with level 2:
b, k, g , f
b k g f I Vertices with level 3:
h, l, e, m, n
h l e m n I Vertices with level 4: p, q
I Height of the tree is 4
p q
I The tree is not balanced.
Examples II

I Root: d
a c I Vertices with level 1:
I Vertices with level 2:
i b k g I Vertices with level 3:
I Vertices with level 4:
f l h o I Height of the tree is
I Is the tree balanced?
e m n p q
Properties

Theorem
1. In an m-ary tree with height h and l leaves we have l ≤ mh .
Therefore h ≥ dlogm le.
2. In a balanced full m-ary tree we have h = dlogm le.

Example. Does there exist a full m-ary tree with height 4 and 100
leaves?
Ordered rooted trees

Definition
An ordered rooted tree is a tree with a root where the children of
each internal node are ordered. Ordered rooted trees are drawn
such that the children are shown in order from left to right.

Examples: On the whiteboard!


Binary Search Trees

I Binary search tree is a binary tree which allows items stored in


a list so that an item can be easily located.
I Searching for items in a list is one of the most important tasks
that arises in computer science.
I Items are labeled with keys which are nodes of the tree. Each
child of a node is designated as a right or left child. 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.
I The first item in the list is assigned as the key of the root.
Example I

I Construct a binary search tree for the words: mathematics,


physics, geography, zoology, meteorology, geology, psychology,
and chemistry with node keys are their names and ordered in
the lexicographic order.

Figure: Initiating with the root being the first item Mathematics

Figure: Locating the second item Physics in the tree: Mathematics<


Physics
Example II

Figure: Locating the third item Geography in the tree

..
.

Figure: Locating the last item Chemistry in the tree


Example III

I How many comparisons needed to determine or to locate


whether the words meteorology or oceanography are already
in the list?
I comparison between meteorology and mathematics: Locate
meteorology to the right subtree rooted at physics.
I comparison between meteorology and physics: Locate
meteorology to the left subtree rooted at meteorology
I comparison between meteorology and meteorology:
meteorology in the list.
Number of comparisons to locate an element

Theorem
The least number of comparisons to locate an element in a list of
length n is dlog(n + 1)e.

Proof.
I The tree obtained by locating items using lexicographic order
of keys is a binary tree, denoted by T ;
I We can form a full binary tree U from T by adding unlabeled
vertices, called ”null” nodes, whenever necessary so that every
vertex with a key has two children.
I The full binary tree U has n internal nodes.
Figure: Binary search tree T and its corresponding full binary tree U
Decision Trees

I Decision trees show series of decisions which should be made


to find an object with a certain property in a collection of
objects of a certain type.
I A decision tree is a rooted tree in which each internal node
corresponds to a decision, with a subtree at these vertices for
each possible outcome of the decision. A possible solution to
the problem corresponds to a path from the root to a leaf of
the decision tree.
Example: Counterfeit coin I
There are 8 coins among which 7 coins are of equal weight, and a
counterfeit coin that weighs less than the others. How many
weighing are necessary using a balance scale to determine the
counterfeit coin?

Solution: We observe that

i) There are 3 possibilities for each weighing on a balance scale:


I 2 pans are balanced
I the first pan can be lighter
I the second pan can be lighter
ii) There are 8 possible solutions, where each among 8 coins can
be the counterfeit one.
iii) If there is tree for deciding the counterfeit one using a balance
scale, then it is a 3-ary tree with at least 8 leaves.
iv) Hence, the height of a decision tree, which is the number of
weighings necessary, if it has any is at least dlog3 8e = 2.
Example: Counterfeit coin II

v) A plausible deciding tree:

vi) 2 is the best number of weighings for determining the


counterfeit lighter coin among 8 identical coins.
Complexity of Sorting Algorithms
Example. How many comparisons are necessary to sort a list of 3
numbers using binary comparisons?

Figure: A decision tree for sorting a sequence of 3 distinct elements

The number of comparisons in this case is 3 at worst.


Theorem
A sorting algorithm based on binary comparisons requires at least
dlog(n!)e comparisons.

Note.

log(n!) = log 1 + log 2 + · · · + log n = Θ(n log n)

Proof.
I There are 2 possible cases for each comparison of the two
numbers;
I Given a sequence of n distinct numbers, there are n!
possibilities arranging linearly;
I If there is decision tree for sorting problem, then it is a binary
tree with at least n! leaves.
I The complexity, which is counted as the number of binary
comparisons, of an sorting algorithm is the height of a
decision tree. In this case, it is at least dlog(n!)e.
Prefix Codes
To encode the message ”good morning” by a bit string we can
represent each letter by a bit string of length 3. In this case the
total number of bits used is 33.

Problem: Is this possible to encode this message using fewer bits?

We cannot use bit strings of length 2 to represent the letters of


this message. We need to use bit strings of different lengths.

However, there might be troubles when decoding the encoded


message.

Example. If encode the message ”tea” using representations


e: 0
a: 1
t: 01
the bit string ”0101” can be decoded as ”eat”, ”eaea” or ”tt” also.
I Prefix codes are bit strings used to represent letters in such a
way that no string is the first part of another string.
I In the example above, the bit string 0 representing letter e is
a prefix of the bit string 01 representing letter t. Hence, the
representation:
e: 0
a: 1
t: 01
is not a prefix code
I Binary trees can be used to create prefix codes where
I the letters are the labels of the leaves in the tree.
I the edges of the tree are labeled so that an edge leading to a
left child is assigned by a 0, and an edge leading to a right
child is assigned by a 1.
I The bit string used to encode a letter is the sequence of labels
of the edges in the unique path from the root to leaf that has
this letter as its label.
Prefix codes

I the word tea is encoded by 110010;


I decoding the binary string
1110010110 gives neat. Why?
I decoding the binary string
1111010110 gives .

Figure: A prefix code for


letters: e,a,t,n,s

Remark: For decoding binary strings given a prefix code, we start


at the root, using the sequence of bits to form a path that stops
when a leaf is reached.
Prefix codes: Examples
Example.

Use binary trees to create prefix codes to encode the message


”good morning”.

Question. How many bits are used? Is this possible to construct a


binary tree for which fewer bits are used?
Huffman Coding

I Huffman coding is a greedy algorithm using the frequencies of


letters for encoding. Replacing the two subtrees with the
smallest weights at each step leads to an optimal code in the
sense that there is no binary prefix code for them using fewer
bits.
I Write out all distinct letters of the message together with
their frequencies. Consider each letter as a binary tree with
only one vertex. Call the frequency corresponding to each tree
its weight.
I At each step, combine two trees of smallest weights into a
new single tree by adding a new root connecting two roots of
the trees. The weight of the new tree is the sum of the two
weights.
I The algorithm is finished if a single tree is constructed.
Huffman code: Examples I

Please construct a Huffman coding to encode the letters with their


frequencies, and find the average number of bits used to encode a
letter:

A : 0.08, B : 0.1, C : 0.12, D : 0.15, E : 0.2, F : 0.35.


Huffman code: Examples II
Huffman code: Examples III

Hence, we can encode letters: A by 111; B by 110; C by 011; D


by 010; E by 10; F by 00.
Homework.

Construct a Huffman coding to encode the message ”good


morning”. How many bits are used, and what is the average
number of bits used for each letter?

Remark: There are many variations of Huffman coding. For


example, instead of encoding singe, we can encode blocks of
symbols of a specified length, such as blocks of two symbols. By
such doing we may reduce the number of bits required to encode
the string. Huffman coding can be used for data/image/audio
compression.
Game trees

Read textbook!
Tree Traversal

I Tree traversal is a process to visit all the nodes of a tree and


may print their values too. Because all nodes are connected
via edges, we cannot randomly access a node in a tree.
I Generally, we traverse a tree to search or locate a given item
or key in the tree (as a data structure) or to print all the
values it contains.
I Procedures for systematically visiting every vertex of a tree are
called traversal algorithms. There are three ways for tree
traverse defined recursively: In-order traversal, pre-order
traversal, post-order traversal
Pre-order traversal
Given a rooted ordered tree, where children of a node are ordered
from left to right. We visit systematically all vertices of a tree
recursively.

I Visit the root


I Traverse each of subtrees T1 , T2 , . . . , Tn recursively in the
pre-order.
In-order traversal

I Traverse T1 recursively in the in-order.


I Visit the root.
I Traverse each of subtrees T2 , . . . , Tn recursively in the
in-order.
Post-order traversal

Post-order traversal

I Traverse each of subtrees T1 , T2 , . . . , Tn recursively in the


post-order.
I Visit the root.
Examples

c o i

b k g f

h l e m n

I Pre-order traversal: d, c, b, h, l, k, g , o, i, f , e, m, n;
I In-order traversal: h, b, l, c, k, g , d, o, e, f , m, n, i;
I Post-order traversal: h, l, b, k, g , c, o, e, m, n, f , i, d.

Question: Given a binary search tree, what is the order of


elements if you traverse the tree using the in-order way?
Infix, Prefix, and Postfix Notation

I Traversal methods in binary tree is one of the most important


topics.
I It is most commonly used in generating prefix, infix and
postfix notations. While infix notation is more human friendly,
prefix and postfix notations are preferred for machines as
they’re easily parseable and the complexity of the problem
also reduces significantly.
I Known such trees are expression trees, parce trees,etc.
Infix, prefix, and postfix notation

Consider the expression (x + y )2 + (x − 4)/3.

We can use a binary tree to represent this expression, where the


internal vertices represent operations, and the leaves represent
numbers or variables.
I Prefix notation (Polish notation):
+ ↑ +x y 2/ − x 43
I Postfix notation (reverse Polish notation):
x y + 2 ↑ x 4 − 3/+
I Infix notation:
x + y ↑ 2 + x − 4/3
To evaluate the value of a form:
I For postfix form, work from left to right, carrying out
operations whenever an operator follows two operands. After
each operation is carried out, the result of this operation is a
new operand.
I For prefix form, follows the same procedure, but work from
right to left instead.

Example.

(a) Find the value of the postfix expression:

723 ∗ −4 ↑ 93/+

(b) Find the value of the prefix expression:

+ − ∗235/ ↑ 234

and find its postfix expression.


Labeled Trees

In this section we will count the number of labeled trees with n


labels. A bijective proof between the number of labeled trees and
Prüfer sequences is presented.

References:

1. M. Aigner and G. M. Ziegler, Proofs from the book.


2. Lecture notes given by Kapralop (e-version: Kapralop.pdf)
Labeled trees I

Definition
A labeled tree is a tree with n vertices whose vertices are assigned
unique numbers from 1 to n. Two labeled trees are identical if the
identity bijection from [n] to [n] preserves their adjacent relations.

Question: How many labeled trees on n vertices are there?


Examples: On the whiteboard!

1. Case 1: n = 2: there is 1 labeled tree (please show it)


2. Case 2: n = 3: there are 3 labeled trees (please show all)
3. Case 3: n = 4: there are 16 labeled trees (explain or show all)
4. Case 4: n = 5: there are 125 labeled trees (explain)
5. General case n: There may have nn−2 labeled trees on n
vertices.
Cayley’s theorem

Theorem (Cayley’s theorem)


There are nn−2 labeled trees on n vertices.

Proof.
1. By induction: Homework
2. A combinatorial proof using Prüfer code will be presented
next pages.
One-to-one correspondence between labeled trees and
Prüfer sequences I

The idea is to construct a bijection from the set of labeled trees to


the set {1, 2, . . . , n}n−2 whose cardinality is nn−2 . Precisely, given
a labeled T we will correspond it to a sequence of n − 2 integers
(a1 , a2 , . . . , an−2 ) where ai ∈ {1, . . . , n} and converse. The
bijection is constructive as follows:
One-to-one correspondence between labeled trees and
Prüfer sequences II

1. From tree to Prüfer sequence: T → (a1 , a2 , . . . , an−2 ).


Step 1. Let u1 be the leaf of T0 = T with smallest label
Step 2. Set a1 to be the unique neighbor of u1 in the tree T0 .
Step 3. Delete the leaf u1 and the edge incident to u1 from the tree
T0 , we obtain a new tree T1 with n − 1 vertices.
Step 4. Let u2 the leaf of the tree T1 with smallest label. Set a2 to be
the unique neighbor of u2 in the tree T1 .
Step 5. Repeat until we reach a tree with 2 vertices.
At final, we obtain a unique sequence (a1 , a2 , . . . , an−2 ) with
ai ∈ [1, n] (verify this!).
One-to-one correspondence between labeled trees and
Prüfer sequences III

Example: Given a labeled tree as below. Please write down


its Prufer sequence.
1

4 2 6

5 3 7 8

10 9
One-to-one correspondence between labeled trees and
Prüfer sequences IV

I Choose the smallest leaf (the


4 2 6 blue node 2);
I Write down its unique neighbor
(the red node 1) in the
5 3 8 7 sequence: (1, · · · );
I Remove the chosen leaf 2 from
the tree.
10 9
One-to-one correspondence between labeled trees and
Prüfer sequences V

I Choose the smallest leaf (in the


4 6 new tree);
I Write successively its unique
5 3 8 7 neighbor in the sequence
(1, 4, · · · )
I Remove the smallest leaf 3.
10 9
One-to-one correspondence between labeled trees and
Prüfer sequences VI

I Choose the smallest leaf (in the


4 6 new tree): leaf 7
I Write successively its unique
neighbor (node 6) in the
5 8 7 sequence (1, 4, 6, · · · )
I Remove the smallest leaf from
the tree.
10 9
One-to-one correspondence between labeled trees and
Prüfer sequences VII

I Choose the smallest leaf: leaf


4 6 9;
I Write successively its unique
neighbor (node 8) in the
5 8 sequence (1, 4, 6, 8, · · · )
I Remove the smallest leaf from
the tree.
10 9
One-to-one correspondence between labeled trees and
Prüfer sequences VIII

I Choose the smallest leaf: leaf


4 6 8;
I Write successively its unique
neighbor (node 6) in the
5 8 sequence (1, 4, 6, 8, 6, · · · )
I Remove the smallest leaf from
the tree.
10
One-to-one correspondence between labeled trees and
Prüfer sequences IX

I Choose the smallest leaf: leaf


4 6 6;
I Write successively its unique
neighbor (node 1) in the
5 sequence (1, 4, 6, 8, 6, 1, · · · )
I Remove the smallest leaf from
the tree.
10
One-to-one correspondence between labeled trees and
Prüfer sequences X

I Choose the smallest leaf: leaf


4 1;
I Write successively its unique
neighbor (node 4) in the
5 sequence (1, 4, 6, 8, 6, 1, 4, · · · )
I Remove the smallest leaf from
the tree.
10
One-to-one correspondence between labeled trees and
Prüfer sequences XI

4
I Choose the smallest leaf: leaf
4;
I Write successively its unique
5 neighbor (node 5) in the
sequence
(1, 4, 6, 8, 6, 1, 4, 5, · · · )
I Remove the smallest leaf from
10
the tree.
One-to-one correspondence between labeled trees and
Prüfer sequences XII
5

10

Stop when the tree remains two leaves. We finally obtain the
sequence (1, 4, 6, 8, 6, 1, 4, 5). Therefore, we have the
correspondence:
1

4 2 6

←→ (1, 4, 6, 8, 6, 1, 4, 5).
5 3 7 8

10 9
One-to-one correspondence between labeled trees and
Prüfer sequences XIII

Lemma (Exercise)
1) If node i in a labeled tree T has degree di , then the occurrence of i
in its corresponding Prüfer code is di − 1.
2) Leaves in a labeled tree do not appear in the Prüfer code of that
tree.
One-to-one correspondence between labeled trees and
Prüfer sequences XIV

2. From Prüfer sequence to tree:


Step 1. Draw the forest with n disjoint nodes labeled from 1 to n.
Step 2. Let the list L0 = {1, 2, . . . , n} and the sequence
S0 = (a1 , a2 , . . . , an−2 ).
Step 3. Find the smallest number in the list L0 \S0 (which corresponds
to the smallest leaf),says v1 . Add an edge connecting v1 to the
vertex labeled by a1 .
Step 4. Removing the smallest number from the list and removing a1
from the sequence. We obtain a new list L1 = L0 \{v1 } and a
new sequence S1 = (a2 , a3 , . . . , an−2 ).
Step 5. Repeat steps 3,4 until we reach the empty sequence Sk . Then
Lk \Sk contains exactly 2 elements. Connect these two nodes.
One-to-one correspondence between labeled trees and
Prüfer sequences XV

Examples: On the whiteboard!


One-to-one correspondence between labeled trees and
Prüfer sequences XVI

Corollary
There are nn−1 rooted labeled trees with n vertices.

Corollary
There are
(n − 2)!
(d1 − 1)!(d2 − 1)! · · · (dn − 1)!
labeled trees with n vertices and a given degree sequence
(d1 , d2 , . . . , dn ), where di is the degree of vertex labeled by i in
trees.
One-to-one correspondence between labeled trees and
Prüfer sequences XVII

Exercises:

1. Determine the corresponding Prüfer sequences of the


following labeled trees:
1 2

9 5 3 8

7 2 3 8 4 7 9 6 10

10 6 4 1 5
One-to-one correspondence between labeled trees and
Prüfer sequences XVIII

2. Determine the corresponding labeled tree of which Prüfer


sequence is (2, 3, 2, 8, 3, 3, 4, 1).
Matrix-Tree Theorem

This section is to enumerate the number of spanning trees of an


undirected graph via determinant of its Laplacian matrix.

References:

1. Rosen’s textbook, Chapter 11.4 (Spanning Trees)


2. R. Stanley, Enumerative Combinatorics, Vol. 2, Chapter 5.6
(Matrix-Tree Theorem)
Spanning trees

Definition
Let G = (V , E ) be an undirected simple graph. A subgraph
T = (V , E 0 ) of G is called a spanning tree if it is a tree that
contains every vertex in V .
Examples I

Given the graph:

a c

e d

1. The subgraph
a c

e d

is not a tree (since it contains a cycle).


Examples II
2. The subgraph
a c

is not a spanning tree (the vertex e does not belong to the


tree).
3. The graph
a c

e d

is not a spanning tree, it is not a subgraph (the edge ac in the


tree but not in the original graph).
Examples III

4. The graph
a c

e d

is a spanning tree.
Examples IV
Given the graph

b d

All its spanning trees are

a a a

b d b d b d

c c c
How to generate efficiently a spanning tree I

Theorem
A graph G is connected if and only if it has a spanning tree.

Proof.
On the white board!
Generating a spanning tree I

I Removing edges from simple cycles:


i) Take a simple cycle in the graph
ii) Remove one edge of the cycle from the graph
iii) Repeat (i) and (ii) until reach a graph without any cycle. This
one is a spanning tree of the given graph.
• Example:

(a) Graph G (b) Remove edge ae


Generating a spanning tree II

(c) Remove edge ef (d) Remove edge cg


• Remark: This algorithm is inefficient, because it requires
determining cycles which actually are not easy to be
identified. Next, we consider two generating algorithms for
spanning trees with time complexity in O(|V |2 ) (or O(|E |)).
Using depth first search (DFS)
DFS for generating a spanning tree:

i) Choosing a vertex of the graph as a root;


ii) Adding vertices and edges successively to form a path as long
as possible, where each new edge is incident with the last
vertex in the path and a vertex not already in the path.
iii) If the path goes through all vertices of the graph, the tree
consisting of this path is a spanning tree.
iv) If the path does not go through all vertices, move back to the
previous vertex in the path, and if possible, form a new path
starting at this vertex passing through vertices not already
visited. If this cannot be done, move back to another vertex
in the path and try again.
v) Repeating step (iv) until all vertices of the graph are visited.
It then creates a rooted spanning tree.
Example

Figure: The graph G

Figure: DFS of G
Remark

The step (iv ) above is also called ”backtracking technique” which


is often used in dynamic programming.
Using breadth first search (BFS)

BFS for generating a spanning tree: Similarly to DFS, we

i) Choose arbitrarily a root from the vertices of the graph.


ii) Add edges incident to this vertex to the tree. The new
vertices added at this stage become the vertices at level 1 in
the spanning tree.
iii) 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 (the added vertex is not in the list of
added vertices).
iv) Repeat (iii) until all the vertices of the graph have been
added. It then creates a spanning tree of the graph.
Example

Figure: The graph G

Figure: A spanning tree of G generated by the BFS


Remark

Using the DFS or BFS, it takes O(|V |2 ) in time complexity.


Exercises

Using DFS or BFS to

1. find all connected components of a graphs


2. cut vertices of a connected graph
3. determine if a graph is bipartite
4. determine if a graph is acyclic
5. determine if a graph can be colored by k colors
6. to find a shortest path between two vertices in a graph.
7. find a subset, if it exists, of the set {27, 24, 19, 14, 11, 8} with
sum
a) 20
b) 41
c) 60.
Enumerating Spanning Trees I

Remark: An important invariant of a graph G is its number of


spanning trees, called the complexity of G , and denoted by κ(G ).

Examples:

1. By Cayley’s theorem: κ(K2 ) = 1, κ(K3 ) = 3, κ(K4 ) = 16,


κ(Kn ) = nn−2 .
2. Given graph
a b

d c

Its complexity is κ = 8.
Enumerating Spanning Trees II
All its spanning trees are
a b a b a b a b

d c d c d c d c

a b a b a b a b

d c d c d c d c

3. The complexity of
a c

e d

is ...
4. Could we know κ(G ) for a general graph G ?
Laplacian matrix I

Definition (Laplacian matrix)


Let G = (V , E ) be an undirected graph with V = {v1 , v2 , . . . , vn }.
Laplacian matrix L of G is an n × n matrix whose entries are

deg(vi )
 if i = j
Lij = −1 if i 6= j and (vi , vj ) ∈ E .

0 Otherwise

Equivalently, L = D − A, where D is the diagonal matrix with


Dii = deg(vi ) and A is the adjacency matrix of G .

Remark: Laplacian matrix is symmetric and singular, i.e., L = Lt


and det L = 0.
Laplacian matrix II

Examples: Laplacian matrix of


   
2 0 0 0 0 1 1 0
0 2 0 0 1 0 1 0
a L=D −A=
0 0 3
− 
0 1 1 0 1
0 0 0 1 0 0 1 0
b d  
2 −1 −1 0
−1 2 −1 0 
c =
−1 −1

3 −1
0 0 −1 1
Matrix-Tree Theorem

Theorem (Kirchoff’s Matrix-Tree Theorem, 1847)


Let G = (V , E ) be an undirected simple graph and L be its
Laplacian matrix. Then the number of spanning trees κ(G ) of G is
equal to the determinant of the reduced Laplacian matrix L̃j of G :

κ(G ) = det L̃j ,

where the reduced Laplacian matrix L̃j is obtained from L by


eliminating the jth row and column from L.
Examples: I

Reduced Laplacian matrix of L(G


 ) by deleting
Given a graph G as 2 −1 0
follows row and column 1 is L̃1 (G ) = −1 3 −1

0 −1 1
and so the number of spanning trees is
a

b d
κ(G ) = det L̃1 (G )
   
3 −1 −1 0
= 2 det − (−1) det
c
−1 1 −1 1
= 3.

Remark: We can get the same result by calculating


det(L̃2 ), det(L̃3 ), det(L̃4 ).
Exercise

Find the complexity κ of the graph below by using Matrix-Tree


Theorem.

a c

e d
Spanning forest

Definition
A spanning forest of a graph G is a forest that contains every
vertex of G such that two vertices are in the same tree of the
forest when there is path in G between these two vertices.

Example: On the whiteboard!


Exercises I
1. How many trees are in the spanning forest of a graph?
2. How many edges must be removed to produce the spanning
forest of a graph with n vertices, m edges, and c connected
components?
3. Using Matrix-Tree theorem to count the number of spanning
trees for the following graphs.

I K2,2 I Kn
I K1,n I Km,n
I K2,3 I W4
I Cn I Q3

4. Which connected simple graphs have exactly one spanning


tree?
5. When must an edge of a connected simple graph be in every
spanning tree for this graph?
Exercises II

6. Prove that if G is a connected, simple graph with n vertices


and G does not contain a simple path of length k, then it
contains at most (k − 1)n edges.
7. Use Matrix-Tree Theorem on the complete graph with n
vertices to reprove Cayley’s Theorem.
8. Let G be a graph and v be a cut vertex of G . Assume that
cutting vertex v creates k connected components
G1 , G2 , . . . , Gk . Knowing that the complexities of graphs Gi ,
for i = 1, 2, . . . , k, accumulated to v are κ1 , κ2 , . . . , κk
respectively. Prove that the complexity of G is equal to
κ1 κ2 . . . κk .
Minimum spanning trees

I MST can be applied in


I designing electronic circuit using the least amount of wire.
I constructing trees for broadcasting in computer networks.
I Reference:
1. T. Cormen et. al., Introduction to algorithms, The MIT press.
Chapter 23.
2. K. Rosen, Discrete mathematics and its applications. Chapter
11.5.
I Next we present two polynomial time algorithms for finding a
MST in a connected graph using a greedy idea.
Minimum spanning trees

I Given a graph G = (V , E ) and an edge weight function


w : E → R which assigns each edge of G to a real number,
called its weight.
I Let T be a spanning tree of G . The weight of T , denoted by
w (T ), is the total weight of edges of T
X
w (T ) = w (e).
e∈T

I T is called a minimum spanning tree of G if it is a spanning


tree with the minimum total weight.
I In a connected graph, a minimum spanning tree exists.
Cut I
I A cut (S, V \S) is a partition of V into 2 blocks.
I An edge uv crosses the cut (S, V \S) if one of its endpoints is
in S and the other is in V \S.
I A light edge is an edge crossing a cut if its weight is the
minimum of any edges crossing the cut.
I Notice that there may have more than one light edge crossing
a cut.

Figure: A cut of G and cd is a light edge for the cut


Cut II

I Given a subset of edge set A ⊆ E . A cut (S, V \S) respects A


if no edge in A crosses the cut.
I Safe edge: If A is a subset of some MST, an edge uv is safe
for A if and only if A ∪ uv is also a subset of some MST.
Lemma
Let G = (V , E ) be a graph with edge weight function w . Assume
that all edge weight are different from each other. Let X be any
subset of vertices of G . Let e be the smallest edge connecting X
to V \X . Then e is in the minimum spanning tree.

Actually, we have a more general result for safe edge


Theorem
Let G = (V , E ) be a graph with edge weight function w . Let A be
a subset of E that is included in some minimum spanning tree for
G . Let (S, V \S) be any cut of G respects A, and let uv be a light
edge crossing the cut. Then, edge uv is safe for A.
Prim’s algorithm
I The idea of Prim’s algorithm is similar to Dijsktra’s algorithm
for finding the shortest path connecting two given vertices.
I General idea:
I Start with a tree T of single vertex set S = {s}, where s ∈ V ;
I Grow the tree T by adding ”good” edges until the tree
contains all vertices of G ;
I At a time, add to T a light edge connecting T to a vertex
outside T of smallest key attribute. Here the key attribute of a
vertex v outside T , denoted by key (v ), is the minimum weight
of an edge connecting v to a vertex in T . Initially, key
attribute of v is infinity for all vertices v ∈ V .
I Stop until all vertices of G are added to T .
I Details:
I Initiate a tree of a single vertex: T = {s};
I While T has fewer than n vertices
I Find the smallest edge connecting T to V \T .
I Add it to T .
I Update key attributes.
Implementations

I Start with a single vertex s. Assign key attribute to each


vertex of G ;
I All vertices not in the tree reside in a min-priority queue based
on a key attribute;
I Choose the vertex of minimum key attribute to add it to
vertex set of T ;
I v.parent names the parent of v in T ;
I Update key attributes, parent names;

I Using the structure of the min-heap or Fibonacci-heap in


order to obtain a more efficient algorithm.
I Please refer to DSA course or Cormen et.al.’s textbook
(Chapter 23).
Example

Figure: A MST by Prim’s algorithm


Figure: Given weighted graph starting with a

Added vertices Updated weights (in blue) Added edges

a
a key(b)=4, key(h)=8 +ab
a,b key(c)=8, key(h)=8 + bc
a,b,c key(i)=2, key(f)=4, key(d)=7, key(h)=8 +ci
a,b,c,i key(f)=4, key(g)=6, key(d)=7, key(h)=7 +cf
a,b,c,i,f key(g)=2, key(d)=7, key(h)=7, key(e)=10 +fg
a,b,c,i,f,g key(h)=1, key(d)=7, key(e)=10 +gh
a,b,c,i,f,g,h key(d)=7, key(e)=10 +cd
a,b,c,i,f,g,h,d key(e)=9 +de
a,b,c,i,f,g,h,d,e
Remark

I Prim’s algorithm is a greedy one. At each step we choose the


best one to add to the tree. Finally, local optimal solutions
converge to global optimal solution.
I For each intermediate step, the set of edges in T is a tree.
Actually, we can prove that it is a minimum tree spanning the
set of vertices added to T .
I The correctness of Prim’s algorithm is based on the cut T and
V \T .
Kruskal’s algorithm

I The idea of Kruskal’s algorithm is similar to Huffman code by


connecting two ”good” connected components at a time.
I General idea:
I Initiate the forest T being the empty set (with no edges);
I Grow the forest by adding edges;
I Add a ”good” edge to the forest at a time until it becomes a
tree containing all vertices of the graph, i.e., till (n − 1) edges
are added. Here, a ”good” edge is an edge with smallest
weight connecting two different connected components.
I Details:
I Sort the edges of G in the increasing order by their weights
I Initiate the forest T to be an empty set;
I for each edge in sorted order, if the ending points of the edge
are in different trees (i.e., in different connected components)
in T , then add e to T ;
I Return T .
Example
List of edges weight T
hg 1 +hg
gf 2 +gf
ic 2 +ic
ab 4 +ab
Figure: First edge added
cf 4 +cf
hi 7 -
cd 7 +cd
ah 8 +ah
bc 8 -
de 9 +de
ef 10
bh 11
Figure: A MST generated using df 14
Kruskal algorithm

Finally, T = {hg , gf , ic, ab, cf , cd, ah, de}.


Implementations

I Use a disjoint-set data structure to maintain disjoint sets of


elements;
I Each set contains the vertices in one tree of the current forest;
I The operation Findset(u) returns a representative element
from the set containing u.
I Determine if two vertices belonging to the same tree or not by
testing if Findset(u) = Findset(v ).
I Combine trees by calling the Union procedure.
Details

I Initiate: T = ∅
I for each v ∈ V , makeset(v );
I Sort the edges E of G in nondecreasing order by their weights.
I for e = uv ∈ E taken in the nondecreasing order above, check
if findset(u) 6= findset(v ), then add e to T , and union(u, v )
return T .
Remark

Both Prim and Kruskal’s algorithms use a greedy approach to the


problem which grows the minimum spanning tree one edge at a
time. The method manages a set of edges S, maintaining the loop
invariant:

”Prior to each iteration, S is a subset of some minimum spanning


tree.”

At each step, we determine an edge uv that can be added to S


such that S ∪ uv is also a subset of a minimum spanning tree.

You might also like