0% found this document useful (0 votes)
52 views

Trees: Discrete Structures For Computing

The document discusses trees, which are connected graphs without simple circuits. It covers properties, traversal, applications like binary search trees and decision trees, and spanning trees. Specific algorithms for minimum spanning trees like Prim's and Kruskal's are also mentioned.

Uploaded by

Lê Văn Hoàng
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

Trees: Discrete Structures For Computing

The document discusses trees, which are connected graphs without simple circuits. It covers properties, traversal, applications like binary search trees and decision trees, and spanning trees. Specific algorithms for minimum spanning trees like Prim's and Kruskal's are also mentioned.

Uploaded by

Lê Văn Hoàng
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 48

Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen

Chapter 11
Ngoc Le

Trees
Discrete Structures for Computing
Contents

Introduction
Properties of Trees

Tree Traversal

Applications of Trees
Binary Search Trees
Decision Trees

Spanning Trees

Minimum Spanning
Trees
Prim’s Algorithm
Kruskal’s Algorithm

Huynh Tuong Nguyen, Tran Tuan Anh, Nguyen Ngoc Le


Faculty of Computer Science and Engineering
University of Technology - VNUHCM
{htnguyen;trtanh}@hcmut.edu.vn
11.1
Trees
Contents
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

1 Introduction
Properties of Trees

2 Tree Traversal
Contents

Introduction

3 Applications of Trees Properties of Trees

Tree Traversal
Binary Search Trees
Applications of Trees
Decision Trees Binary Search Trees
Decision Trees

Spanning Trees
4 Spanning Trees Minimum Spanning
Trees
Prim’s Algorithm
Kruskal’s Algorithm
5 Minimum Spanning Trees
Prim’s Algorithm
Kruskal’s Algorithm

11.2
Trees
Course outcomes
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Course learning outcomes

L.O.1 Understanding of logic and discrete structures


L.O.1.1 – Describe definition of propositional and predicate logic
L.O.1.2 – Define basic discrete structures: set, mapping, graphs
Contents
L.O.2 Represent and model practical problems with discrete structures Introduction
L.O.2.1 – Logically describe some problems arising in Computing Properties of Trees

L.O.2.2 – Use proving methods: direct, contrapositive, induction Tree Traversal


L.O.2.3 – Explain problem modeling using discrete structures Applications of Trees
Binary Search Trees

L.O.3 Understanding of basic probability and random variables Decision Trees

L.O.3.1 – Define basic probability theory Spanning Trees

L.O.3.2 – Explain discrete random variables Minimum Spanning


Trees
Prim’s Algorithm
L.O.4 Compute quantities of discrete structures and probabilities Kruskal’s Algorithm

L.O.4.1 – Operate (compute/ optimize) on discrete structures


L.O.4.2 – Compute probabilities of various events, conditional
ones, Bayes theorem

11.3
Trees
Introduction
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

• Very useful in computer science: search algorithm, game


winning strategy, decision making, sorting, . . .
• Other disciplines: chemical compounds, family trees,
organizational tree, . . . Contents

Introduction
Properties of Trees
/
Tree Traversal

Applications of Trees

home bin tmp Binary Search Trees


Decision Trees

Spanning Trees

Minimum Spanning
tan mail ls junk Trees
Prim’s Algorithm
Kruskal’s Algorithm

music latex scala

11.4
Trees
Tree
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
Definition
A tree (cây ) is a connected undirected graph with no simple circuits.
Consequently, a tree must be a simple graph.

Contents

Introduction
Properties of Trees

Tree Traversal

Applications of Trees
Binary Search Trees
Decision Trees

Spanning Trees
G1 G2 G3 G4
Minimum Spanning
circuit exists Trees
Prim’s Algorithm

not connected Kruskal’s Algorithm

Definition
Graphs containing no simple circuits that are not necessarily connected
is forest (rừng ), in which each connected component is a tree.

11.5
Trees
Rooted Trees
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition
A rooted tree (cây có gốc) is a tree in which:
• One vertex has been designated as the root and
• Every edge is directed away from the root
Contents

Introduction
Properties of Trees
f g a c Tree Traversal

Applications of Trees
Binary Search Trees
d b a Decision Trees
b c Spanning Trees
d e
Minimum Spanning
e b Trees
Prim’s Algorithm
a f g e d Kruskal’s Algorithm

c f g

11.6
Trees
Terminology
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Definition Ngoc Le

• parent (cha) of v is the unique u such that there is a directed


edge from u to v
• when u is the parent of v, v is called a child (con) of u
• vertices with the same parent are called siblings (anh em) Contents

• the ancestors (tổ tiên) of a vertex are the vertices in the path Introduction
Properties of Trees
from the root to this vertex (excluding the vertex itself)
Tree Traversal
• descendants (con cháu) of a vertex v are those vertices that Applications of Trees

have v as an ancestor Binary Search Trees


Decision Trees

Spanning Trees
a Minimum Spanning
Trees
Prim’s Algorithm

b Kruskal’s Algorithm

c
d

f g e

11.7
Trees
Terminology
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition
• a vertex of a tree is called a leaf (lá ) if it has no children
• vertices that have children are called internal vertices (đỉnh
Contents
trong ) Introduction
Properties of Trees

a Tree Traversal

Applications of Trees
Binary Search Trees
Decision Trees
b
Spanning Trees
c
Minimum Spanning
d Trees
Prim’s Algorithm
Kruskal’s Algorithm

f g e

11.8
Trees
Terminology
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition
If a is a vertex in a tree, the subtree (cây con) with a as its root is
the subgraph of the tree consisting of a and its descendants and
all edges incident to these descendants. Contents

Introduction
a Properties of Trees

Tree Traversal

Applications of Trees
b Binary Search Trees
c Decision Trees

d Spanning Trees

Minimum Spanning
Trees

f g e Prim’s Algorithm
Kruskal’s Algorithm

11.9
Trees
m-ary tree
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
Definition
• m-ary tree (cây m-phân): at most m children on each internal
vertex of a rooted tree.
• full m-ary tree (cây m-phân đầy đủ): every internal vertex has
exactly m children.
Contents
• An m-ary tree with m = 2 is called a binary tree (cây nhị phân). Introduction
Properties of Trees

Tree Traversal

Applications of Trees
Binary Search Trees
Decision Trees

Spanning Trees
Full 3-ary tree Minimum Spanning
Trees
Binary tree Prim’s Algorithm
Kruskal’s Algorithm

Full 5-ary tree 3-ary tree

11.10
Trees
Ordered Rooted Trees
Huynh Tuong Nguyen,
Definition Tran Tuan Anh, Nguyen
Ngoc Le

• An ordered rooted tree (cây có gốc có thứ tự) is a rooted tree


where the children of each internal vertex are ordered (e.g. in
order from left to right).
• In an ordered binary tree (cây nhị phân có thứ tự), if an
internal vertex has two children, the first child is called the Contents

left child (con bên trái) and the second is called the right Introduction
Properties of Trees
child (con bên phải). Tree Traversal

a Applications of Trees
Binary Search Trees
Decision Trees

b c Spanning Trees

Minimum Spanning
Trees
Prim’s Algorithm
Kruskal’s Algorithm
e
f d

g
Left subtree of a Right subtree of a
11.11
Trees
Properties & Theorems
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Theorem
A tree with n vertices has n − 1 edges.
Contents
Theorem Introduction

A full m-ary tree Properties of Trees

Tree Traversal
(i) n vertices has (n − 1)/m internal vertices and Applications of Trees
[(m − 1)n + 1]/m leaves Binary Search Trees
Decision Trees

(ii) i internal vertices has n = mi + 1 vertices and (m − 1)i + 1 Spanning Trees

leaves Minimum Spanning


Trees
(iii) ` leaves has n = (m` − 1)/(m − 1) vertices and Prim’s Algorithm
Kruskal’s Algorithm
(` − 1)/(m − 1) internal vertices

11.12
Trees
Example
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Example (Chain Letter Game)

• Each person who receives the letter is asked to send it on to Contents


four other peoples. Introduction
Properties of Trees
• Some peoples do this, but others do not send any letters.
Tree Traversal
• How many people have seen the letter, including the first Applications of Trees

person, if no one receives more than one letter and if the Binary Search Trees
Decision Trees

chain letter ends after there have been 100 people who read it Spanning Trees

but did not send it out ? Minimum Spanning


Trees
• How many people sent out the letter? Prim’s Algorithm
Kruskal’s Algorithm

11.13
Trees
Level and Height
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Definition Ngoc Le

• The level (mức) 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 (độ cao) of a rooted tree is the maximum of the
Contents
levels of vertices (i.e. the length of the longest path from the Introduction
root to any vertex). Properties of Trees

Tree Traversal

a Applications of Trees
Binary Search Trees

Example Decision Trees

b k Spanning Trees
• Level of root a = 0, Minimum Spanning
j b, j, k = 1 and
Trees
Prim’s Algorithm

c f l c, e, f, l = 2 . . . Kruskal’s Algorithm

e
• Because the largest
g level of any vertex is
d i m n
4, this tree has height
4.
h
11.14
Trees
Balanced m-ary Trees
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition
A rooted m-ary tree of height h is balanced (cân đối) if all leaves
are at levels h or h − 1.

Contents

Introduction
Properties of Trees

Tree Traversal

Applications of Trees
Binary Search Trees
Decision Trees

T1 Spanning Trees

Minimum Spanning
Trees
Prim’s Algorithm
Kruskal’s Algorithm

T2

11.15
Trees
Balanced m-ary Tree
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Theorem
There are at most mh leaves in an m-ary tree of height h. Contents

Introduction

It can be proved by using mathematical induction on the height. Properties of Trees

Tree Traversal

Corollary Applications of Trees


Binary Search Trees

• If an m-ary tree of height h has ` leaves, then h ≥ dlogm `e. Decision Trees

Spanning Trees
• If the m-ary tree is full and balanced, then h = dlogm `e. Minimum Spanning
Trees
Prim’s Algorithm
Kruskal’s Algorithm

11.16
Trees
Exercise
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Exercise (Chess tournament)


Suppose 1000 people enter a chess tournament. Use a rooted tree
model of the tournament to determine how many games must be
played to determine a champion. If a player is eliminated after one Contents

loss and games are played until only one entrant has not lost. Introduction

(Assume there are no ties) Properties of Trees

Tree Traversal

Applications of Trees
Exercise (Isomorphic) Binary Search Trees
Decision Trees

How many different isomers (đồng phân) do the following Spanning Trees

saturated hydrocarbons have ? Minimum Spanning


Trees
• C3 H8 Prim’s Algorithm
Kruskal’s Algorithm

• C5 H12
• C6 H14

11.17
Trees
Question
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Exercise
Contents
• How many vertices and how many leaves does a complete Introduction
m-ary tree of height h have? Properties of Trees

Tree Traversal
• Show that a full m-ary balanced tree (cây m-phân hoàn hảo)
Applications of Trees
of height h has more than mh−1 leaves. Binary Search Trees
Decision Trees
• How many edges are there in a forest of t trees containing a
Spanning Trees
total of n vertices? Minimum Spanning
Trees
Prim’s Algorithm
Kruskal’s Algorithm

11.18
Trees
Labeling Ordered Rooted Trees
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
• Ordered rooted trees are often used to store information. Ngoc Le

• Need a procedure for visiting each vertex of an ordered rooted tree to


access data.
• Ordering and labeling the vertices is important to traverse them in any
procedure
• Universal address system (hệ địa chỉ phổ dụng )
0 < 1 < 1.1 < 1.1.1 < 1.2 < 1.3 < . . . < 2 < 3 < 3.1 < . . . Contents

Introduction
0 Properties of Trees

Tree Traversal

Applications of Trees
Binary Search Trees
1 2 3 Decision Trees

Spanning Trees

Minimum Spanning
1.1 1.2 1.3 3.1 Trees
Prim’s Algorithm
Kruskal’s Algorithm

1.3.1
1.1.1 1.3.2 3.1.1 3.1.2

1.3.1.1

11.19
Trees
Traversal Algorithms (Thuật toán duyệt cây)
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
Preorder Traversal (duyệt tiền thứ tự - NLR)
procedure preorder (T : ordered rooted tree)
r := root of T
print r
for each child c of r from left to right
Contents
T (c) := subtree with c as its root
Introduction
preorder (T (c)) Properties of Trees

Tree Traversal

a Applications of Trees
Binary Search Trees
Decision Trees

Spanning Trees

b c Minimum Spanning
Trees
d Prim’s Algorithm
Kruskal’s Algorithm

f g e

a b f g c e d
11.20
Trees
Traversal Algorithms
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Inorder Traversal (Duyệt trung thứ tự - LNR)


Suppose a tree T with root r. If T consists only of r, then r is
inorder traversal of T . Otherwise, suppose r has subtrees T1 , T2 ,
. . . , Tn from left to right, inorder traversal:
T1 → r → T2 → . . . → Tn . Contents

Introduction
Properties of Trees
a Tree Traversal

Applications of Trees
Binary Search Trees
Decision Trees
b c
Spanning Trees
d
Minimum Spanning
Trees
Prim’s Algorithm

f g e Kruskal’s Algorithm

f b g a e c d

11.21
Trees
Traversal Algorithms
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
Postorder Traversal (Duyệt hậu thứ tự - LRN)
procedure postorder (T : ordered rooted tree)
r := root of T
for each child c of r from left to right
T (c) := subtree with c as its root
Contents
postorder (T (c))
Introduction
print r Properties of Trees

Tree Traversal
a Applications of Trees
Binary Search Trees
Decision Trees

Spanning Trees

b c Minimum Spanning
d Trees
Prim’s Algorithm
Kruskal’s Algorithm

f g e

f g b e c d a
11.22
Trees
Infix, Prefix and Postfix Notations
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

• Infix (trung tố ):
((x + y) ↑ 2) + ((x − 4)/3) +
Contents

Introduction
↑ / Properties of Trees

• Prefix (tiền tố ): Tree Traversal

Applications of Trees
+ ↑ +xy2/ − x43 + −
2 3 Binary Search Trees
Decision Trees

Spanning Trees

x y x 4 Minimum Spanning
• Postfix (hậu tố ): Trees
Prim’s Algorithm

x y + 2 ↑ x 4 −3 / + Kruskal’s Algorithm

11.23
Trees
Exercise
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Exercise
Implement postorder, inorder and preorder traversal of the
following tree.
Contents

Introduction
A Properties of Trees

Tree Traversal

Applications of Trees
B C Binary Search Trees
Decision Trees

Spanning Trees

D E F Minimum Spanning
Trees
Prim’s Algorithm
Kruskal’s Algorithm
G H I

11.24
Trees
Exercise
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Exercise
Find the ordered rooted tree representing Contents

Introduction

(¬(p ∧ q) ∨ (¬q ∧ r)) → (¬p ∨ ¬r) Properties of Trees

Tree Traversal

Applications of Trees
Then use this rooted tree to find the prefix, postfix and infix forms Binary Search Trees

of this expression Decision Trees

Spanning Trees

Minimum Spanning
Solution Trees
Prim’s Algorithm
Kruskal’s Algorithm

11.27
Trees
Exercise
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents
Exercise
Introduction
Determine postorder of a binary tree with inorder D B H E I A F C Properties of Trees

J G K and preorder A B D E H I C F G J K. Tree Traversal

Applications of Trees
Binary Search Trees
Solution Decision Trees

Spanning Trees

Minimum Spanning
Trees
Prim’s Algorithm
Kruskal’s Algorithm

11.28
Trees
Binary Search Trees
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition
Binary search tree (cây tìm kiếm nhị phân - BST) is a binary tree
in which the assigned key of a vertex is:
• larger than the keys of all vertices in its left subtree, and
Contents
• smaller than the keys of all vertices in its right subtree. Introduction
Properties of Trees

Tree Traversal

6 Applications of Trees
Binary Search Trees
Decision Trees

2 7 Spanning Trees

Minimum Spanning
Trees
Prim’s Algorithm
1 4 9 Kruskal’s Algorithm

3 5 8

11.32
Trees
Adding and Locating an Item in BST
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Example
Form a BST for the words mathematics, physics, geography ,
zoology , meteorology , geology , psychology , chemistry using
alphabetical order.
Contents
mathematics Introduction
Properties of Trees

Tree Traversal
geography physics Applications of Trees
Binary Search Trees
Decision Trees

chemistry geology meteorology zoology Spanning Trees

Minimum Spanning
Trees
Prim’s Algorithm
psychology Kruskal’s Algorithm

Complexity in searching
O(log(n)) vs. O(n) in linear list

11.33
Trees
Decision Trees (Cây quyết định)
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Example Contents

Introduction
There are seven coins, all with the same weight, and a counterfeit Properties of Trees

coin that weighs less than the others. How many weighings are Tree Traversal

necessary using a balance scale to determine which of the eight Applications of Trees

coins is the counterfeit one? Give an algorithm for finding this Binary Search Trees
Decision Trees

counterfeit coin. Spanning Trees

Minimum Spanning
Trees
Prim’s Algorithm
Kruskal’s Algorithm

11.34
Trees
Yet Another Application
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
Example
If we know that the probability that a person has tuberculosis
(TB) is p(TB) = 0.0005.
We also know p(+|TB) = 0.999 and p(−|TB) = 0.99.
What is p(TB|+) and p(TB|−)? Contents

Introduction

0.999
Properties of Trees
+ TB & + = 0.0004995
Tree Traversal
TB 0.001 Applications of Trees
5
00 − TB & − = 0.0000005 Binary Search Trees

0.0 Decision Trees

Start! 0.99 Spanning Trees


95
0.01
Minimum Spanning
+ TB & + = 0.009995 Trees

TB 0.99 Prim’s Algorithm


Kruskal’s Algorithm

− TB & − = 0.989505

p(TB∩+) 0.0004995
p(TB|+) = p(+) = 0.0004995+0.009995 ≈ 0.0476

11.35
Trees
Problem
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition
• A spanning tree (cây khung ) in a graph G is a subgraph of G
Contents
that is a tree which contains all vertices of G. Introduction
Properties of Trees

a b c d Tree Traversal

Applications of Trees
Binary Search Trees
Decision Trees

e g Spanning Trees

f Minimum Spanning
Trees
Prim’s Algorithm
Kruskal’s Algorithm

11.36
Trees
Depth-First Search (Tìm kiếm ưu tiên chiều sâu)
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

End! Start!
1

Contents
2 3 4 Introduction
Properties of Trees

Tree Traversal
Stuck! Stuck! Applications of Trees

5 6 7 8 Binary Search Trees


Decision Trees

Spanning Trees

Minimum Spanning
Property Trees
Prim’s Algorithm

• Go deeper as you can Kruskal’s Algorithm

• Backtrack (quay lui) to possible branch when you are stuck.


• O(e) or O(n2 )

11.37
Trees
Depth-First Search
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Algorithm
procedure DFS (G) Contents

T := tree consisting only vertex v1 Introduction


Properties of Trees
visit(v1 )
Tree Traversal

Applications of Trees
procedure visit(v: vertex of G) /* recursive */ Binary Search Trees
Decision Trees
for each vertex w adjacent to v and not in T Spanning Trees
add w and edge {v, w} to T Minimum Spanning
visit(w) Trees
Prim’s Algorithm
Kruskal’s Algorithm

11.38
Trees
Breadth-First Search (Tìm kiếm ưu tiên chiều rộng)
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

vertex L
Start! ∅
1 1 2, 3, 4
2 3, 4, 5, 6 Contents
3 4, 5, 6 Introduction

4 5, 6, 7, 8 Properties of Trees

2 3 4 5 6, 7, 8 Tree Traversal

Applications of Trees
6 7, 8 Binary Search Trees

End! 7 8 Decision Trees

8 ∅ Spanning Trees
5 6 7 8 Minimum Spanning
Trees
Prim’s Algorithm
Property Kruskal’s Algorithm

• O(e) or O(n2 )

11.39
Trees
Breadth-First Search
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Algorithm
procedure BFS (G)
T := tree consisting only vertex v1 Contents

L := empty list Introduction


Properties of Trees
put v1 in the list L of unprocessed vertices Tree Traversal
while L is not empty Applications of Trees

remove the first vertex, v, from L Binary Search Trees


Decision Trees
for each neighbor w of v Spanning Trees
if w is not in L and not in T then Minimum Spanning
Trees
add w to the end of the list L Prim’s Algorithm

add w and edge {v, w} to T Kruskal’s Algorithm

11.40
Trees
Exercise
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
Exercise
Find spanning tree in the following graphs.

a b c

Contents

Introduction
d e f g Properties of Trees

Tree Traversal

Applications of Trees
Binary Search Trees
h i j Decision Trees

Spanning Trees
a b c
Minimum Spanning
Trees
Prim’s Algorithm
Kruskal’s Algorithm

d e f g

h i

11.41
Trees
Minimum Spanning Trees
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition
• A minimum spanning tree (cây khung nhỏ nhất) in a
connected weighted graph is a spanning tree that has the
smallest possible sum of weights of its edges.
Contents

Introduction
Properties of Trees

Tree Traversal
$2000 Applications of Trees
Binary Search Trees
Decision Trees
$1200 Chicago $1000 New York Spanning Trees
San Francisco $9
00 00
$13
Minimum Spanning
Trees
Denver
$70

00
Prim’s Algorithm

$1600

$8
Kruskal’s Algorithm
0
$140
0
$220 Atlanta
0

11.42
Trees
Prim’s Algorithm (Nearest-Neighbor)
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Prim’s Algorithm (1957)


Contents
procedure Prim(G) Introduction
T := a minimum-weight edge Properties of Trees

for i := 1 to n − 2 Tree Traversal

e := an edge of minimum weight incident to a vertex in T Applications of Trees


Binary Search Trees
and not forming a simple circuit in T if added to T Decision Trees

T := T with e added Spanning Trees

return T Minimum Spanning


Trees
Prim’s Algorithm
Kruskal’s Algorithm

11.43
Trees
Prim’s Algorithm (Nearest-Neighbor)
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
• Pick a vertex to start from
• Iteratively absorb smallest edge possible

Contents
a 4 b 5 c Introduction
Properties of Trees

Tree Traversal
4 Applications of Trees
1 1 Binary Search Trees
10 Decision Trees

Spanning Trees
d e Minimum Spanning
Trees
Prim’s Algorithm
5 Kruskal’s Algorithm
10
3

f 10 g

11.44
Trees
Kruskal’s Algorithm (Lightest-Edge)
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Kruskal’s Algorithm (1958)


Contents
procedure Kruskal (G) Introduction
T := empty graph Properties of Trees

for i := 1 to n − 1 Tree Traversal

e := any edge in G with smallest weight that does not form Applications of Trees
Binary Search Trees
a simple circuit when added to T Decision Trees

T := T with e added Spanning Trees

return T Minimum Spanning


Trees
Prim’s Algorithm
Kruskal’s Algorithm

11.45
Trees
Kruskal’s Algorithm (Lightest-Edge)
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

• Iteratively add smallest edge possible

a 4 b 5 c Contents

Introduction
Properties of Trees

4 Tree Traversal
1 1 Applications of Trees
10 Binary Search Trees
Decision Trees

d e Spanning Trees

Minimum Spanning
Trees
5
10 Prim’s Algorithm

3 Kruskal’s Algorithm

f 10 g

11.46
Trees
Exercise
Huynh Tuong Nguyen,
Exercise Tran Tuan Anh, Nguyen
Ngoc Le

By using Prim’s and Kruskal’s algorithm, determine minimum


spanning tree in the following graphs. (and maximum spanning
tree (cây khung cực đại).

a 4 b c
Contents
1 2 5 2 4 1 Introduction
Properties of Trees

d e 2 f g Tree Traversal
1 Applications of Trees
2 3 1 3 1 Binary Search Trees
Decision Trees

h i j Spanning Trees

Minimum Spanning
a 1 b 8 c Trees
Prim’s Algorithm

4 2 4 6 1 2 Kruskal’s Algorithm

d e f g
5 1 3
3 2 7
h i
11.47
Trees
Exercise
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Cho một cây có gốc với n đỉnh. Giả thiết một đỉnh trong tập đỉnh Contents

có bậc là n − 1. Chiều cao của cây là Introduction


Properties of Trees

Tree Traversal
A 1 Applications of Trees

B n−1 Binary Search Trees


Decision Trees

C n Spanning Trees

Minimum Spanning
D 2 Trees
Prim’s Algorithm
Kruskal’s Algorithm

11.48
Trees
Exercise
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Xác định tiền tố (prefix) của cây nhị phân có gốc và có thứ tự
(ordered rooted tree) dùng để biểu diễn Contents

Introduction

(¬(p ∧ q) ∨ (¬q ∧ r)) → (¬p ∨ ¬r) Properties of Trees

Tree Traversal

Applications of Trees
Binary Search Trees
A → ∨¬ ∧ p q ∨ ¬q r ∨ ¬p r Decision Trees

Spanning Trees
B p q ∧ ¬ ∨ q¬r ∧ p¬r∨ →
Minimum Spanning
C p q¬ ∨ q¬ ∧ r → p¬ ∨ r Trees
Prim’s Algorithm

D p q¬ ∨ q¬ ∧ r → p¬ ∨ r Kruskal’s Algorithm

11.49
Trees
Exercise
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Có bao nhiêu cây nhị phân có tiền tố (pre-order traversal) là Contents

Introduction
ABC? Properties of Trees

Tree Traversal
A 1 Applications of Trees

B 3 Binary Search Trees


Decision Trees

C 5 Spanning Trees

Minimum Spanning
D 7 Trees
Prim’s Algorithm
Kruskal’s Algorithm

11.50
Trees
Exercise
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Hãy cho biết hậu tố (post-order traversal) của một cây nhị phân Contents
biết rằng tiền tố (pre-order traversal) là HBGF DECIA và trung Introduction
tố (in-order traversal) là GBF HCEIDA. Properties of Trees

Tree Traversal

Applications of Trees
A GF BCIEADH Binary Search Trees
Decision Trees
B BGF DECIAH
Spanning Trees
C GF BCIEJADH Minimum Spanning
Trees
D GF BHCIEADH Prim’s Algorithm
Kruskal’s Algorithm

11.51
Trees
Exercise
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
Cho đồ thị như trong hình vẽ dưới.

8 14

2 32 12 Contents
24 26 Introduction
Properties of Trees

Tree Traversal
18 28 Applications of Trees
Binary Search Trees

16 30 4 Decision Trees

22 Spanning Trees

Minimum Spanning
Trees
10 20 . Prim’s Algorithm
Kruskal’s Algorithm

Cây phủ tối thiểu có tổng trọng số là


A 40
B 60
C 84
D 100 11.52
Trees
Exercise
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Cho trước số tự nhiên a > 1, và xét đồ thị đầy đủ K2a+3 . Số lượng Contents
cạnh ta phải xóa khỏi đồ thị K2a+3 để thu được một cây phủ (cây Introduction
khung hay bao trùm, spanning tree) của K2a+3 là bao nhiêu? Properties of Trees

Tree Traversal

Applications of Trees
A 2a + 2 Binary Search Trees

B 2a2 + 3a − 1 Decision Trees

Spanning Trees
C 4a2 + 3a + 1 Minimum Spanning
Trees
D 2a2 + 3a + 1 Prim’s Algorithm
Kruskal’s Algorithm

11.56

You might also like