Lecture13 Trees Full
Lecture13 Trees Full
Aaron Tan
AY2023/24 Semester 1 1
14. Trees
10.5 Trees
• Definitions: circuit-free, tree, trivial tree, forest
• Characterizing trees: terminal vertex (leaf), internal vertex
2
Trees Rooted Trees Spanning trees and Shortest Paths
10.5 Trees
3
Trees Rooted Trees Spanning trees and Shortest Paths
Definition
Definition
Definition: Tree
(The graph is assumed to be undirected here.)
A graph is said to be circuit-free if and only if it has no circuits.
A simple graph is called a tree if and only if it is circuit-free and connected.
A trivial tree is a tree that consists of a single vertex.
A simple graph is called a forest if and only if it is circuit-free and not
connected.
4
Trees Rooted Trees Spanning trees and Shortest Paths
Example
Possibility Tree
Applications
Applications
Trees are used to store hierarchically ordered data.
Computer file systems
And many
others…
6
Trees Rooted Trees Spanning trees and Shortest Paths
Characterizing Trees
Lemma 10.5.1
Any non-trivial tree has at least one vertex of degree 1.
Characterizing Trees
Lemma 10.5.1
Any non-trivial tree has at least one vertex of degree 1.
8
Trees Rooted Trees Spanning trees and Shortest Paths
Characterizing Trees
9
Trees Rooted Trees Spanning trees and Shortest Paths
Characterizing Trees
Theorem 10.5.2
Any tree with n vertices (n > 0) has n – 1 edges.
Characterizing Trees
Proof: (continued…)
3. Hence, by Lemma 10.5.1, T has a vertex v of degree 1, and has at least
another vertex in T besides v.
4. Thus, there is an edge e connecting v to the rest of T.
5. Define a subgraph T' of T so that VT' = VT – {v} and ET' = ET – {e}.
5.1 The number of vertices of T' is (k + 1) – 1 = k.
5.2 T' is circuit-free.
e T'
5.3 T' is connected. …
v
6. Hence by definition, T' is a tree.
7. Since T' has k vertices, by inductive hypothesis,
number of edges of T' = (number of vertices of T') – 1 = k – 1.
8. Hence, number of edges of T = (number of edges of T') + 1 = k.
9. Hence P(k+1) is true.
11
Trees Rooted Trees Spanning trees and Shortest Paths
Characterizing Trees
12
Trees Rooted Trees Spanning trees and Shortest Paths
Characterizing Trees
13
Trees Rooted Trees Spanning trees and Shortest Paths
Characterizing Trees
Lemma 10.5.3
If G is any connected graph, C is any circuit in G, and one of the edges
of C is removed from G, then the graph that remains is still connected.
Essentially, the reason why Lemma 10.5.3 is true is that any two
vertices in a circuit are connected by 2 distinct paths. It is possible
to draw the graph so that one of these goes “clockwise” and the
other goes “counter-clockwise” around the circuit.
For example, in the circuit shown below:
The clockwise path from
v2 to v3 is: v2 e3 v3
The counter-clockwise
path from v2 to v3 is:
v2 e2 v1 e1 v0 e6 v5 e5 v4 e4 v3
14
Trees Rooted Trees Spanning trees and Shortest Paths
Characterizing Trees
Theorem 10.5.4
If G is a connected graph with n vertices and n – 1 edges, then G
is a tree.
Proof:
1. Suppose G is a particular but arbitrarily chosen graph that is
connected and has n vertices and n – 1 edges.
2. Since G is connected, it suffices to show that G is circuit-free.
3. Suppose G is not circuit free
3.1 Let C be the circuit in G.
3.2 By Lemma 10.5.3, an edge of C can be removed from G to obtain a
graph G' that is connected.
3.3 If G' has a circuit, then repeat this process: Remove an edge of the
circuit from G' to form a new connected graph.
3.4 Continue the process of removing edges from the circuits until
eventually a graph G'' is obtained that is connected and is circuit-free.
15
Trees Rooted Trees Spanning trees and Shortest Paths
Characterizing Trees
Proof: (continued…)
3.4 Continue the process of removing edges from the circuits until
eventually a graph G'' is obtained that is connected and is circuit-free.
3.5 By definition, G'' is a tree.
3.6 Since no vertices were removed from G to form G'', G'' has n vertices.
3.7 Thus, by Theorem 10.5.2, G'' has n – 1 edges.
3.8 But the supposition that G has a circuit implies that at least one edge
of G is removed to form G''.
3.9 Hence G'' has no more than (n – 1) – 1 = n – 2 edges, which
contradicts its having n – 1 edges.
3.10 So the supposition is false.
4. Hence G is circuit-free, and therefore G is a tree.
G’’ is the result of
Assume G is not removing edges from
circuit-free. circuits in G. At least 1
G has n vertices and edge removed from G.
n – 1 edges. G’’ has n vertices and at
most n – 2 edges.
16
Trees Rooted Trees Spanning trees and Shortest Paths
Characterizing Trees
17
Trees Rooted Trees Spanning trees and Shortest Paths
18
Trees Rooted Trees Spanning trees and Shortest Paths
Definitions
Definitions
20
Trees Rooted Trees Spanning trees and Shortest Paths
Example
21
Trees Rooted Trees Spanning trees and Shortest Paths
Binary Trees
Binary Trees
Binary Trees
Binary Trees
23
Trees Rooted Trees Spanning trees and Shortest Paths
24
Trees Rooted Trees Spanning trees and Shortest Paths
25
Trees Rooted Trees Spanning trees and Shortest Paths
26
Trees Rooted Trees Spanning trees and Shortest Paths
28
Trees Rooted Trees Spanning trees and Shortest Paths
Theorem 10.6.2
For non-negative integers h, if T is any binary tree with height h
and t terminal vertices (leaves), then
t 2h
Equivalently,
log2 t h
30
Trees Rooted Trees Spanning trees and Shortest Paths
Proof: (continued…)
Case 1 (v has only one child):
v
Level 0
vL
Level 1
Level 2
Level 3
Left subtree TL
31
Trees Rooted Trees Spanning trees and Shortest Paths
Proof: (continued…)
7. Case 1 (v has only one child):
7.1 Without loss of generality, assume that v’s child is a left child
and denote it by vL. Let TL be the left subtree of v.
7.2 Because v has only one child, v has degree 1 (leaf), so the total
number of leaves in T equals the number of leaves in TL + 1.
Thus, if tL is the number of leaves in TL , then t = tL + 1.
7.3 By inductive hypothesis, tL 2k because the height of TL is k,
one less than the height of T.
7.4 Also, because v has a child, k+1 1 and so 2k 20 = 1.
v
7.5 Therefore, Level 0
t = tL + 1 2k + 1 2k + 2k = 2k+1 vL Level 1
Level 2
Level 3
32
Left subtree TL
Trees Rooted Trees Spanning trees and Shortest Paths
Proof: (continued…)
Case 2 (v has two children):
v
Level 0
vL
Level 1
vR
Level 2
Level 3
Level 4
33
Trees Rooted Trees Spanning trees and Shortest Paths
Proof: (continued…)
8. Case 2 (v has two children):
8.1 Now v has a left child vL and a right child vR , and they are the
roots of a left subtree TL and a right subtree TR respectively.
8.2 Let hL and hR be the heights of TL and TR respectively.
8.3 Then hL k and hR k since T is obtained by joining TL and TR
and adding a level.
8.4 Let tL and tR be the number of leaves of TL and TR respectively.
8.5 Then, since both TL and TR have heights less than k + 1, by
inductive hypothesis, tL 2hL and tR 2hR.
8.6 Therefore,
t = tL + tR 2hL + 2hR 2k + 2k = 2k+1
9. In both cases, P(k+1) is true.
10. Hence if T is any binary tree with height h and t terminal vertices
(leaves), then t 2h. 34
Trees Rooted Trees Spanning trees and Shortest Paths
35
Trees Rooted Trees Spanning trees and Shortest Paths
36
Trees Rooted Trees Spanning trees and Shortest Paths
Breadth-First Search
7 8 9
37
Trees Rooted Trees Spanning trees and Shortest Paths
Breadth-First Search
The figure on the left shows a graph representing cities in
Germany. The figure on the right shows the breadth-first
traversal on the graph, starting with Frankfurt.
Depth-First Search
39
Trees Rooted Trees Spanning trees and Shortest Paths
Depth-First Search
41
Trees Rooted Trees Spanning trees and Shortest Paths
Definitions
Figure 10.7.1
42
Trees Rooted Trees Spanning trees and Shortest Paths
Definitions
The company wishes to legitimately advertise service to all the cities shown
but, for reasons of economy, wants to use the least possible number of
individual routes to connect them. One possible route system is given in
Figure 10.7.2, where the chosen routes are in red.
43
Trees Rooted Trees Spanning trees and Shortest Paths
Definitions
Lemma 10.5.3
Is the number of individual If G is any connected graph, C is any
routes minimal? circuit in G, and one of the edges of
C is removed from G, then the graph
The fact is that the graph of any that remains is still connected.
system of routes that satisfies
the company’s wishes is a tree,
because if the graph were to
contain a circuit, then one of
the routes in the circuit could
be removed without
disconnecting the graph (by
Lemma 10.5.3), and that would
give a smaller total number of
routes. Figure 10.7.2
44
Trees Rooted Trees Spanning trees and Shortest Paths
Definitions
Proposition 10.7.1
1. Every connected graph has a spanning tree.
2. Any two spanning trees for a graph have the same
number of edges.
45
Trees Rooted Trees Spanning trees and Shortest Paths
Definitions
The graph G has one circuit v2v1v4v2 and removal of any edge
of the circuit gives a tree. Hence there are three spanning
trees for G.
46
Trees Rooted Trees Spanning trees and Shortest Paths
Figure 10.7.3
47
Trees Rooted Trees Spanning trees and Shortest Paths
48
Trees Rooted Trees Spanning trees and Shortest Paths
Kruskal’s Algorithm
Kruskal’s Algorithm
50
Trees Rooted Trees Spanning trees and Shortest Paths
Kruskal’s Algorithm
Figure 10.7.4
51
Trees Rooted Trees Spanning trees and Shortest Paths
Kruskal’s Algorithm
Kruskal’s Algorithm
53
Trees Rooted Trees Spanning trees and Shortest Paths
Prim’s Algorithm
Robert C. Prim
(1921 - )
54
Trees Rooted Trees Spanning trees and Shortest Paths
Prim’s Algorithm
Prim’s Algorithm
Figure 10.7.6
56
Trees Rooted Trees Spanning trees and Shortest Paths
Prim’s Algorithm
57
Trees Rooted Trees Spanning trees and Shortest Paths
Prim’s Algorithm
58
Trees Rooted Trees Spanning trees and Shortest Paths
59
Trees Rooted Trees Spanning trees and Shortest Paths
1
61
Trees Rooted Trees Spanning trees and Shortest Paths
1
L(u) of each vertex u
other than a is set to .
62
Trees Rooted Trees Spanning trees and Shortest Paths
The rest of
a the vertices
68
Trees Rooted Trees Spanning trees and Shortest Paths
3
Step 1: Going into the while loop:
0
V(T) = {a}, E(T) = Ø, and F = {a}
1
During iteration:
F = {b, c}, L(b) = 3, L(c) = 4.
Since L(b) < L(c), b is added to V(T) and {a, b} is added
to E(T).
69
Trees Rooted Trees Spanning trees and Shortest Paths
3
Step 2: Going into the while loop:
0
V(T) = {a, b}, E(T) = {{a, b}}
4 1
During iteration:
F = {c, d, e}, L(c) = 4, L(d) = 9, L(e) = 8.
Since L(c) < L(d) and L(c) < L(e), c is added to V(T) and
{a, c} is added to E(T).
70
Trees Rooted Trees Spanning trees and Shortest Paths
3
Step 3: Going into the while loop:
0
V(T) = {a, b, c},
E(T) = {{a, b}, {a, c}}
4 1
5
During iteration:
F = {d, e}, L(d) = 9, L(e) = 5.
L(e) becomes 5 because ace, which has length 5, is a
shorter path to e than abe, which has length 8. Since
L(e) < L(d), e is added to V(T) and {c, e} is added to E(T).
71
Trees Rooted Trees Spanning trees and Shortest Paths
3 7
Step 4: Going into the while loop:
0
V(T) = {a, b, c, e},
E(T) = {{a, b}, {a, c}, {c, e}}
4 1 5
During iteration:
F = {d, z}, L(d) = 7, L(z) = 17.
L(d) becomes 7 because aced, which has length 7, is a
shorter path to d than abd, which has length 9. Since
L(d) < L(z), d is added to V(T) and {e, d} is added to E(T).
72
Trees Rooted Trees Spanning trees and Shortest Paths
3 7
Step 5: Going into the while loop:
0
V(T) = {a, b, c, e , d},
14
E(T) = {{a, b}, {a, c}, {c, e}, {e, d}}
4 1 5
During iteration:
F = {z}, L(z) = 14.
L(z) becomes 14 because acedz, which has length 14, is
a shorter path to z than acez, which has length 17.
Since z is the only vertex in F, its label is a minimum,
and so z is added to V(T) and {d, z} is added to E(T).
Algorithm terminates at this point because z V(T).
The shortest path from a to z has length L(z) = 14.
73
Trees Rooted Trees Spanning trees and Shortest Paths
{d, z}}
Table 10.7.1
74
END OF FILE
75