Professional Documents
Culture Documents
Chap9, 10 - Graphs, Trees (HT)
Chap9, 10 - Graphs, Trees (HT)
Chap9, 10 - Graphs, Trees (HT)
&
APPLICATIONS
1
Chapter 9: Graphs
9.4. Connectivity
2
9.1 Graphs and graph model
3
DEFINITION
Definition. A graph 𝐺 = (𝑉, 𝐸) consists of
∎ a set of vertices (or nodes) 𝑉, and
∎ a set of edges 𝐸
such that each edge has either one or two vertices associated with it, called enpoints.
𝑧
𝑢
𝑤 𝑥
𝑣 𝑦
𝒄
𝒅 𝒆
the set of vertices is 𝑉 = 𝒂, 𝒃, 𝒄, 𝒅, 𝒆 , while the set of edges is 𝐸 = 𝑥, 𝑦, 𝑧, 𝑢, 𝑣, 𝑤 .
4
EXAMPLE
5
CLASSIFICATION OF GRAPHS
In this course, we will study the following classes of graphs:
1. Undirected graphs:
(1a) Simple graphs.
(1b) Multigraphs.
(1c) Pseudo-graphs.
2. Directed graphs:
(2a) Simple directed graphs.
(2b) Directed multigraphs.
6
9.1.1 Undirected Graphs
7
SIMPLE GRAPHS
8
MULTIGRAPHS
Definition. A multigraph is a graph that may have multiple edges between two vertices.
Example. In the above graph, there is a multiple edge (with multiplicity 𝟐) connecting 𝒂 and 𝒃.
9
MULTIGRAPHS - AN EXAMPLE
10
PSEUDO-GRAPHS
Definition. A pseudo-graph is a multigraph that may have edges connecting a vertex to itself.
Such edges will be called loops.
Example. In the above graph, there is a multiple edge (with multiplicity 𝟐) connecting 𝒂 and 𝒃,
and there is a loop at 𝒂.
11
PSEUDO-GRAPHS – AN EXAMPLE
12
9.1.2 Directed Graphs
13
DEFINITION
Definition. A directed graph 𝐺 = 𝑉, 𝐸 consists of
∎ a set of vertices 𝑉, and
∎ a set of edges (or arcs) 𝐸
such that each directed edge is endowed with an orientation. The edges in a directed graph are
called directed edges.
14
SIMPLE DIRECTED GRAPHS AND DIRECTED MULTIGRAPHS
Note: In this graph, the multiple edge between 𝑎 Note: In this graph, the multiple edge between 𝑐
16
SUMMARY
17
EXERCISES
18
9.2 Graph terminology and
special types of graphs
19
DEGREE OF VERTICES IN AN UNDIRECTED GRAPH
Convention. In an undirected graph, two vertices 𝑎 and 𝑏 are called adjacent if they are
endpoints of an edge 𝑒, and then 𝑒 is called incident with 𝑎 and 𝑏.
Definition. Let 𝐺 be an undirected graph and 𝑣 a vertex of 𝐺. The degree of 𝑣, denoted deg 𝑣 ,
is the number of edges incident with it and such that a loop is double-counted.
20
HANDSHAKING THEOREM FOR UNDIRECTED GRAPHS
deg 𝑣 = 2 𝐸
𝑣∈𝑉
meaning that the sum of degrees of all vertices of 𝐺 is equal to twice the number of edges.
𝒂 𝒃
𝑧
𝑢
𝑤 𝑥
𝑣 𝑦
𝒄
𝒅 𝒆
Example. In the above graph, we have deg 𝑎 = deg 𝑏 = 3 and deg 𝑐 = deg 𝑑 = deg 𝑒 = 2.
Moreover, the number of edges is 𝐸 = 6. Indeed, we have 2 𝐸 = 3 + 3 + 2 + 2 + 2.
21
A COROLLARY
Corollary. In any undirected graph, the number of vertices odd-degree is an even number.
Proof. This is for you!
22
DEGREE OF VERTICES IN A DIRECTED GRAPH
Convention. Let 𝐺 be a directed graph and let 𝒆 be an edge of 𝐺 from vertex 𝑎 to vertex 𝑏. We will say
that 𝑎 is initial vertex and 𝑏 is terminal (or end) vertex of 𝒆.
If 𝒆’ is a loop at 𝑎 then we say that 𝑎 is both initial and terminal vertex of 𝒆′ .
𝒆′ 𝒆
Exercise. Find the in-degree and out-degree for each of the vertices in the graph below. Verify the
handshaking theorem for this graph.
𝒆′ 𝒆
24
SOME SPECIAL SIMPLE GRAPHS: 𝑲𝒏
25
SOME SPECIAL SIMPLE GRAPHS: 𝑪𝒏
26
SOME SPECIAL SIMPLE GRAPHS: 𝑾𝒏
27
SOME SPECIAL SIMPLE GRAPHS: 𝑸𝒏
28
SOME SPECIAL SIMPLE GRAPHS: BIPARTITE GRAPHS
Definition. A simple graph 𝐺 = 𝑉, 𝐸 is said to be bipartite if the set of vertices 𝑉 can be partitioned
into two disjoint sets 𝑉1 and 𝑉2 such that each edge of 𝐺 connects a vertex in 𝑉1 and a vertex in 𝑉2 .
(There is not any edge connecting two vertices in 𝑉1 (or 𝑉2 )).
𝒃𝟏
𝒂𝟏
𝒃𝟐
𝒂𝟐
𝒃𝟑
𝒂𝟑
𝒃𝟒
Example. In the above diagram, the set of vertices is partitioned into two subsets:
𝑉1 = 𝑎1 , 𝑎2 , 𝑎3 and 𝑉2 = 𝑏1 , 𝑏2 , 𝑏3 , 𝑏4 .
29
EXERCISE
30
SOME SPECIAL SIMPLE GRAPHS: COMPLETE BIPARTITE GRAPHS
Definition. A simple graph 𝐺 = 𝑉, 𝐸 is said to be complete bipartite if the set of vertices 𝑉 can be
partitioned into two disjoint sets 𝑉1 and 𝑉2 such that for any two vertices 𝑢 ∈ 𝑉1 and 𝑣 ∈ 𝑉2 , there is an
edge of 𝐺 connecting them. (There is not any edge connecting two vertices in 𝑉1 (or 𝑉2 )).
31
NEW GRAPHS FROM OLD: SUBGRAPHS
Definition. A subgraph of a graph 𝐺 = 𝑉, 𝐸 is a graph 𝐻 = 𝑊, 𝐹 such that 𝑊 ⊆ 𝑉 and 𝐸 ⊆ 𝐹.
We write 𝐻 ⊆ 𝐺 to denote that 𝐻 is a subgraph of 𝐺.
32
NEW GRAPHS FROM OLD: UNION OF GRAPHS
Definition. Let 𝐺1 = 𝑉1 , 𝐸1 and 𝐺2 = 𝑉2 , 𝐸2 be two simple graphs. The union of 𝐺1 and 𝐺2 , denoted
𝐺1 ∪ 𝐺2 , is the simple graph whose set of vertices is 𝑉1 ∪ 𝑉2 and whose set of edges is 𝐸1 ∪ E2 .
33
EXERCISES
34
EXERCISES
35
EXERCISES
Exercises. 21, 22, 23, 24, 25, 26 (page 666).
36
9.3 Representing Graphs
and Graph Isomorphisms
37
9.3.1 Representing Graphs
38
ADJACENCY MATRICES OF UNDIRECTED GRAPHS
Definition. Let 𝐺 be an undirected graph with vertices 𝑣1 , 𝑣2 , … , 𝑣𝑛 . The adjacency matrix of 𝑮 is the
𝑛 × 𝑛 -matrix whose 𝑖, 𝑗 -entry is given by
𝑎𝑖,𝑗 = the number of edges connecting 𝑣𝑖 and 𝑣𝑗 .
39
EXERCISES
Exercise 1. Find the adjacency matrix of the following graph.
40
ADJACENCY MATRICES OF DIRECTED GRAPHS
Definition. Let 𝐺 be a directed graph with vertices 𝑣1 , 𝑣2 , … , 𝑣𝑛 . The adjacency matrix of 𝑮 is the
𝑛 × 𝑛 -matrix whose 𝑖, 𝑗 -entry is defined by
𝑎𝑖,𝑗 = the number of edges going 𝐟𝐫𝐨𝐦 𝑣𝑖 to 𝑣𝑗 .
Example. Find the adjacency matrix of the following graph with the alphabetic-order of vertices.
41
EXERCISES
42
INCIDENCE MATRICES OF UNDIRECTED GRAPHS
Definition. Let 𝐺 be an undirected graph with vertices 𝑣1 , 𝑣2 , … , 𝑣𝑛 and with edges 𝑒1 , 𝑒2 , … , 𝑒𝑚 . The
incident matrix of 𝑮 is the 𝑛 × 𝑚 -matrix whose 𝑖, 𝑗 -entry is given by
Example. Find the incident matrix of the following graph with the chosen order of vertices and edges.
43
9.3.2 Graph Isomorphisms
44
DEFINITION
Definition. Let 𝐺1 = 𝑉1 , 𝐸1 and 𝐺2 = 𝑉2 , 𝐸2 be two undirected graphs. We say that 𝐺1 and 𝐺2 are
isomorphic if there is a bijection 𝑓: 𝑉1 ⟶ 𝑉2 with the property that, for every 𝑢, 𝑣 ∈ 𝑉1 ,
"𝑢 and 𝑣 are adjacent in 𝐺1 ⟺ 𝑓(𝑢) and 𝑓(𝑣) are adjacent in 𝐺2 "
45
EXAMPLE
46
EXAMPLE
Example . Determine whether 𝑊3 and 𝑊4 are isomorphic?
Answer. 𝑊3 and 𝑊4 are not isomorphic because they do not have the same number of vertices.
47
EXAMPLE
Example . Determine whether two graphs below are isomorphic?
Answer. These are not isomorphic because they do not have the same number of edges.
48
INVARIANTS
Proposition. If two simple graphs are isomorphic then they have the same
(i) number of vertices,
(ii) number of edges,
(iii) degrees of vertices.
Convention. The three characteristics above are called graph invariants.
Example . Show that the two graphs below are not isomorphic.
Solution. 𝐺 and 𝐻 are not isomorphic because 𝐻 have a vertex
of degree 4, while 𝐺 does not have any such vertex.
49
9.4 Connectivity
50
9.4.1 Connectivity of
Undirected Graphs
51
PATHS IN AN UNDIRECTED GRAPH
Example. In the above graph, we have 𝑎𝑏, 𝑏𝑓, 𝑓𝑒 is a path from 𝑎 to 𝑒 of length 3.
52
CIRCUIT
53
CONNECTEDNESS IN UNDIRECTED GRAPHS
Example. The above graph is disconnected because there is not any path between vertex 𝑑
and any of the others.
54
CONNECTED COMPONENTS
and
55
CUT VERTICES
56
CUT EDGES
57
9.4.2 Connectivity of
Directed Graphs
58
PATHS IN A DIRECTED GRAPH
59
STRONGLY CONNECTEDNESS IN DIRECTED GRAPHS
60
STRONGLY CONNECTED COMPONENTS
61
9.4.3 Paths and
Isomorphisms
62
EXAMPLE
Example. Use the graph invariant of paths to show that the two graphs are not isomorphic.
Solution. 𝐺 and 𝐻 are not isomorphic because 𝐻 has circuits of length 3 (namely, 𝑣1 , 𝑣2 , 𝑣6
and 𝑣3 , 𝑣4 , 𝑣5 , while all circuits in 𝐺 have length at least 4.
63
9.4.4 Counting Paths
between Vertices
64
A THEOREM
Theorem. Let 𝐺 be a graph (undirected or directed) with vertices 𝑣1 , 𝑣2 , … , 𝑣𝑛 . Suppose the adjacency
65
EXAMPLE
Example. Count the number of paths of length 3 between 𝑎 and 𝑐 in the graph below.
𝐴=
66
9.5 Euler and Hamilton Paths
67
9.5.1 Euler Paths and Circuits
68
7 BRIDGES PROBLEM
69
DEFINITIONS
Definition. Let 𝐺 be a graph (undirected or directed).
(i) An Euler circuit in 𝐺 is a simple circuit that contains all the edges of 𝐺.
(ii) An Euler path in 𝐺 is a simple path that contains all the edges of 𝐺.
Example 1. 𝒂
𝒆 𝒃
𝒄
𝒅
In the above graph, the path 𝑎, 𝑒, 𝑑, 𝑐, 𝑏, 𝑎, 𝑐 is an Euler path from 𝑎 to 𝑐.
Exercise. (a) Find all Euler paths from 𝑐 to 𝑎.
(b) Is there any Euler path from 𝑎 to 𝑏?
70
Example 2.
71
FUNDAMENTAL THEOREMS
Theorem 1. Let 𝐺 be a connected undirected graph. Then 𝐺 has an Euler circuit if and only if every
vertex of 𝐺 has even degree.
Theorem 2. Suppose that 𝐺 is a connected undirected graph which has not any Euler circuit.
Then 𝐺 has an Euler path if and only if there are exactly two vertices of odd degree.
72
73
7 BRIDGES PROBLEM (cont)
The "7 briges problem" has a solution if and only if the graph
has an Euler circuit. But such a circuit does not exist because
not all vertices have even degree.
74
EXERCISE
75
EXERCISES – EULER PATHS/CIRCUITS IN DIRECTED GRAPHS
76
EXERCISES
77
EXERCISE
78
ALGORITHM
79
9.5.2 Hamilton Paths and
Circuits
80
DEFINITIONS
Definition. Let 𝐺 be a graph (undirected or directed).
(i) A Hamilton path in 𝐺 is a simple path that passes through every vertex 𝐺 exactly once.
(ii) A Hamilton circuit in 𝐺 is a simple circuit that passes through every vertex 𝐺 exactly once.
(1) In the graph 𝐺1 : the path 𝑎, 𝑏, 𝑒, 𝑐, 𝑑 is a Hamilton path from 𝑎 to 𝑑; the circuit 𝑎, 𝑏, 𝑐, 𝑑, 𝑒, 𝑎 is a
Hamilton circuit.
(2) In the graph 𝐺2 : the path 𝑎, 𝑏, 𝑑, 𝑐 is Hamilton path from 𝑎 to 𝑐; while there is no Hamilton circuit.
81
TWO SUFFICIENT CONDITIONS
Dirac’s theorem. Let 𝐺 be a simple undirected graph with 𝑛 vertices such that 𝑛 ≥ 3.
If the degree of every vertex in 𝐺 is larger or equal to 𝑛/2, then 𝐺 has a Hamilton circuit.
Ore’s theorem. Let 𝐺 be a simple undirected graph with 𝑛 vertices such that 𝑛 ≥ 3.
If deg 𝑢 + deg 𝑣 ≥ 𝑛 whenever 𝑢 and 𝑣 are not adjacent in 𝐺, then 𝐺 has a Hamilton circuit.
This graph has 5 vertices. It does not satisfy Dirac’s condition (since deg 𝑎 = 2 < 5/2).
This graph does not satisfy Ore’s condition as well (since deg 𝑎 + deg 𝑐 = 4 < 5).
Despite that, there is still a Hamilton circuit given by 𝑎, 𝑏, 𝑐, 𝑑, 𝑒, 𝑎 .
82
EXERCISES
83
EXERCISE
84
9.6 Shortest Path Problem
85
DISCUSSION
86
WEIGHTED GRAPHS
Definition. A weighted graph is a graph in which each edge is labeled with a number called weight of that edge.
The length of a path in a weighted graph is the sum of weights of all edges of this path.
87
DIJKSTRA’S SHORTEST PATH ALGORITHM: FIRST EXAMPLE
Example 1. Find the shortest path from 𝐴 to 𝑍 in the weighted graph.
Solution.
88
For every 𝑣 ∈ 𝐴, 𝐵, 𝐶, 𝐷, 𝐸, 𝑍 , we denote by 𝐿(𝑣) the length variable of shortest path from 𝐴 to 𝑣.
Step 1: 𝐿 𝐴 ≔ 0, 𝐿 𝐵 = 𝐿 𝐶 = 𝐿 𝐷 = 𝐿 𝐸 = 𝐿 𝑍 = ∞. Put 𝑆 ≔ 𝐴 .
Step 2: Consider all the vertices 𝑣 ∉ 𝑆. Update 𝐿 𝑣 for all the vertices 𝑣 adjacent to 𝑎.
𝐿 𝐵 ≔ min ∞, 𝐿 𝐴 + 𝐴, 𝐵 = min ∞, 4 = 4, 𝐿 𝐶 ≔ min ∞, 𝐿 𝐴 + 𝐴, 𝐶 = min ∞, 2 = 2,
𝐿 𝐷 = 𝐿 𝐸 = 𝐿 𝑍 = ∞.
Put 𝑆 ≔ 𝐴, 𝐶 (Note: 𝐶 is added because 𝐿 𝐶 is minimal label in this step).
Step 3: Consider all the vertices 𝑣 ∉ 𝑆. Update 𝐿 𝑣 for all the vertices 𝑣 adjacent to 𝐶.
𝐿 𝐵 ≔ min 4, 𝐿 𝐶 + 𝐶, 𝐵 = min 4, 3 = 3, 𝐿 𝐷 ≔ min ∞, 𝐿 𝐶 + 𝐶, 𝐷 = min ∞, 7 = 7 ,
𝐿 𝐸 ≔ min ∞, 𝐿 𝐶 + 𝐶, 𝐸 = min ∞, 12 = 12 , 𝐿 𝑍 = ∞.
Put 𝑆 ≔ 𝐴, 𝐶, 𝐵 .
89
Step 4: Consider all the vertices 𝑣 ∉ 𝑆. Update 𝐿 𝑣 for all the vertices 𝑣 adjacent to 𝐵.
𝐿 𝐷 ≔ min 7, 𝐿 𝐵 + 𝐵, 𝐷 = min 7, 9 = 7 ,
𝐿 𝐸 ≔ 12, 𝐿 𝑍 : = ∞.
Put 𝑆 ≔ 𝐴, 𝐶, 𝐵, 𝐷 .
Step 5: Consider all the vertices 𝑣 ∉ 𝑆. Update 𝐿 𝑣 for all the vertices 𝑣 adjacent to 𝐷.
𝐿 𝐸 ≔ min 12, 𝐿 𝐷 + 𝐷, 𝐸 = min 12, 9 = 9 ,
𝐿 𝑍 : = min ∞, 𝐿 𝐷 + 𝐷, 𝑍 = min ∞, 13 = 13.
Put 𝑆 ≔ 𝐴, 𝐶, 𝐵, 𝐷, 𝐸 .
Step 6: Consider all the vertices 𝑣 ∉ 𝑆. Update 𝐿 𝑣 for all the vertices 𝑣 adjacent to 𝐸.
𝐿 𝑍 : = min 13, 𝐿 𝐸 + 𝐸, 𝑍 = min 13, 12 = 12.
Put 𝑆 ≔ 𝐴, 𝐶, 𝐵, 𝐷, 𝐸, 𝑍 .
Conclusion: The shortest path from 𝐴 to 𝑍 is 𝐴, 𝐶, 𝐷, 𝐸, 𝑍 = 12.
90
DIJKSTRA’S SHORTEST PATH ALGORITHM: SECOND EXAMPLE
Example 2. Find the shortest path from 𝐴 to 𝑍 in the weighted graph.
91
DIJKSTRA’S SHORTEST PATH: ALGORITHM
Complexity: 𝑂 𝑛2 ,
including the
number of additions
and comparisons.
92
EXERCISES
For the following graphs, find the shortest path from 𝑎 to 𝑧.
93
Chapter 10: Trees
10.1. Introduction
94
PRACTICE
95
PRACTICE
96
10.1 Introduction
97
DEFINITION
Theorem. (1) An undirected graph is a tree if and only if between any two vertices, there is a
unique simple path.
(2) A tree with 𝑛 vertices has 𝑛 − 1 edges.
98
ROOTED TREES
Definition. 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.
99
TERMINOLOGIES
Example. In the above tree, the parent of ℎ is 𝑔. The children of 𝑔 are ℎ, 𝑖 and 𝑗.
𝑙 and 𝑚 are siblings with parent 𝑗.
100
TERMINOLOGIES (cont)
101
TERMINOLOGIES (cont)
102
m-ary Trees
104
EXERCISES
105
m-ary Trees (cont)
Definition. (1) The level of a vertex of a rooted tree is the length of the path from the root to
that vertex.
(2) The height of a rooted tree is the maximum of the levels of all vertices.
(3) An 𝑚-ary tree of height ℎ is called a balanced tree if each leave has level ℎ or ℎ − 1.
106
m-ary Trees (cont)
Theorem. (1) In an 𝑚-ary tree with height ℎ and 𝑙 leaves, we have 𝑙 ≤ 𝑚ℎ .
And so we have ℎ ≥ log 𝑚 𝑙 .
(2) In a full and balanced 𝑚-ary tree, we have ℎ = log 𝑚 𝑙 .
4
Solution. (1) We have 81 ≤ 𝑚4 , so 𝑚 ≥ 81 = 3.
To find the upper bound, we will use the formula 𝑙 = 𝑖 𝑚 − 1 + 1. Since 𝑙 = 81, we have 𝑖 𝑚 − 1 = 80.
80
This implies 𝑚 ≤ + 1 = 21.
4
107
10.2 Applications of Trees
108
BINARY SEARCH TREE
Example. Form a binary search tree for the numbers 7, 11, 4, 8, 6, 12, 3, 5 and 10 such that each vertex is
labeled by a number among those and such that each number is larger (resp. smaller) than all the numbers
in its left (resp. right) subtrees.
How many comparisons needed to determine whether 6 or 9 are already in the list?
7
4 11
3 6 8 12
5 10
Theorem. The least number of comparisons to locate an element in a list of length 𝑛 is log 2 𝑛 + 1
(i.e., in the worst case, at least log 2 𝑛 + 1 comparisons are needed).
109
DECISION TREES
110
SORTING ALGORITHMS
Theorem. The sorting algorithm based on binary comparisons requires at least log 2 𝑛!
comparisons.
111
PREFIX CODES: DISCUSSION
Example. To encode the message "good morning" by a bit string we can represent each letter by a bit
string of length 3 (because there are 8 letters in the message). So the total number of bits used is 33.
112
PREFIX CODES: DEFINITION
Definition. Prefix codes are bit strings used to represent letters in such a way that no string is the first part
of another string.
113
HUFFMAN CODING
Example. Use Huffman coding to encode the following symbols with the frequencies listed:
A: 0.08, B: 0.10, C: 0.12, D: 0.15, E: 0.20, F: 0.35.
What is the average number of bits used to encode a letter?
114
Solution. The Huffman encoding gives
𝐴 = 111, 𝐵 = 110, 𝐶 = 011,
𝐷 = 010, 𝐶 = 011, 𝐸 = 10, 𝐹 = 00.
115
EXERCISES
116
EXERCISES
117
10.3 Tree Traversal
118
10.3.1 Definitions and Examples
119
PREORDER TRAVERSAL
Definition. Let 𝑇 be a rooted tree with root 𝑟. The preorder traversal of 𝑇 is inductively defined as
follows.
∎ First, if 𝑇 consists of only root 𝑟 then 𝑟 is the preorder traversal of 𝑇.
The preorder traversal begins by visiting 𝑟, then continues traversing 𝑇1 in preorder, then 𝑇2 , and so on.
120
EXAMPLE
Example. Find the preorder traversal of the following tree.
Solution. 𝑎, 𝑏, 𝑒, 𝑗, 𝑘, 𝑛, 𝑜, 𝑝, 𝑓, 𝑐, 𝑑, 𝑔, 𝑙, 𝑚, ℎ, 𝑖.
121
INORDER TRAVERSAL
Definition. Let 𝑇 be a rooted tree with root 𝑟. The inorder traversal of 𝑇 is inductively defined as
follows.
∎ First, if 𝑇 consists of only root 𝑟 then 𝑟 is the inorder traversal of 𝑇.
The inorder traversal begins by traversing 𝑇1 in inorder, then visiting 𝑟. It continues by traversing 𝑇2 in
inorder, and so on.
122
EXAMPLE
Example. Find the inorder traversal of the following tree.
Solution. 𝑗, 𝑒, 𝑛, 𝑘, 𝑜, 𝑝, 𝑏, 𝑓, 𝑎, 𝑐, 𝑙, 𝑔, 𝑚, 𝑑, ℎ, 𝑖.
123
POSTORDER TRAVERSAL
Definition. Let 𝑇 be a rooted tree with root 𝑟. The postorder traversal of 𝑇 is inductively defined as
follows.
∎ First, if 𝑇 consists of only root 𝑟 then 𝑟 is the postorder traversal of 𝑇.
The postorder traversal begins by traversing 𝑇1 in postorder, then continues traversing 𝑇2 in postorder,
and so on, and finally, visiting 𝑟.
124
EXAMPLE
Example. Find the postorder traversal of the following tree.
Solution. 𝑗, 𝑛, 𝑜, 𝑝, 𝑘, 𝑒, 𝑓, 𝑏, 𝑐, 𝑙, 𝑚, 𝑔, ℎ, 𝑖, 𝑑, 𝑎.
125
10.3.2 Applications
126
EXAMPLE
Example. Find the inorder traversal of the
following rooted tree, in which
" ↑ " means exponentiation.
"/" means division.
Solution. The inorder traversal of this tree
represents the expression
2
𝑥+𝑦 + 𝑥 − 4 /3.
127
PREFIX, INFIX AND POSTFIX NOTATION
128
CALCULATIONS
To evaluate the value of a form:
∎ 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.
∎ For prefix form, follows the same procedure, but work from right to left instead.
129
EXERCISE
130
EXERCISES
131
EXERCISE
132
PRACTICE
133
134
135
136
137
138
139
10.4 Spanning Trees
140
DEFINITION
Definition. Let 𝐺 be a simple graph. A spanning tree of 𝐺 is a subgraph that is a tree and contains
all the vertices of 𝐺.
Example.
141
10.4.1 Depth – First Search
142
DEPTH – FIRST SEARCH: AN EXAMPLE
Example. Find a spanning tree of the graph below.
143
TREE EDGES AND BACK EDGES
Definition. The edges selected by depth-first search of a graph are called tree edges.
All other edges of the graph are called back edges (coloured yellow and green).
Remark. A back edge must connect a vertex of the graph to an ancestor or descendant of this
vertex in the spanning tree.
144
EXERCISE
Exercise. Find a spanning tree using DFS, by starting with the vertex 𝑎.
145
ALGORITHM
146
10.4.2 Breadth – First Search
147
BREADTH – FIRST SEARCH: AN EXAMPLE
Example. Find a spanning tree of the graph below.
148
EXERCISE
Exercise. Find a spanning tree using BFS, by starting with the vertex 𝑓.
149
ALGORITHM
150
10.4.3 Backtracking
Applications
151
GRAPH COLORINGS
Problem. Let 𝐺 be a graph. How many colors are necessary to colour all the vertices of 𝐺
such that no two adjacent vertices are of the same color?
152
EXAMPLE
153
N-QUEENS PROBLEM
Problem. The 𝑛-queens problem asks how 𝑛 queens can be placed on an 𝑛 × 𝑛 chessboard so
that no two queens can attack one another.
154
155
SUM OF SUBSETS
156
PRACTICE
157
158
159
10.5 Minimum Spanning Trees
160
DEFINITION
161
10.5.1 PRIM’S ALGORITHM
162
PRIM’S ALGORITHM
- Choose an edge in the graph with smallest weight, putting then it into the spanning tree.
- Successively add to the tree edges of minimum weight that are incident to a vertex already in the tree (never
forming a simple circuit).
- Stop when 𝑛 − 1 edges have been added.
163
EXAMPLE
164
EXAMPLE
165
10.5.2 KRUSKAL’S ALGORITHM
166
KRUSKAL’S ALGORITHM
- Choose an edge with smallest weight, putting then it into the spanning tree.
- Successively add edges with minimum weight that do not form a simple circuit with those edges already chosen.
- Stop when 𝑛 − 1 edges have been added.
167
EXAMPLE
168
PRACTICE
169
PRACTICE
170
PRACTICE
171
REVIEW
Theorem. Let 𝑇 be a full 𝒎-ary tree. Suppose that 𝑇 has 𝑖 internal vertices. Then
(i) 𝑇 has 𝑛 = 𝑚𝑖 + 1 vertices.
(ii) 𝑇 has 𝑙 = 𝑖 𝑚 − 1 + 1 leaves.
172
PRACTICE
173
PRACTICE
174
PRACTICE
175
PRACTICE
176
PRACTICE
177
PRACTICE
178
PRACTICE
179