Chap9, 10 - Graphs, Trees (HT)

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 179

DISCRETE MATHEMATICS

&
APPLICATIONS

1
Chapter 9: Graphs

9.1. Graphs and Graph Models

9.2. Graph Terminologies and Special Types of Graphs

9.3. Representing Graphs and Graph Isomorphism

9.4. Connectivity

9.5. Euler and Hamilton Paths

9.6. Shortest-Path Problem

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.

Example. In the following graph,


𝒂 𝒃

𝑧
𝑢
𝑤 𝑥
𝑣 𝑦
𝒄
𝒅 𝒆
the set of vertices is 𝑉 = 𝒂, 𝒃, 𝒄, 𝒅, 𝒆 , while the set of edges is 𝐸 = 𝑥, 𝑦, 𝑧, 𝑢, 𝑣, 𝑤 .

4
EXAMPLE

One can use a graph to describe a computer network in which


- the vertices represent data centers, and
- the edges represent communication links between them.

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 𝑐

and 𝑏 is not a multiple directed edge. and 𝑑 is a multiple directed edge.


15
EXERCISE
Exercise. Draw a directed multigraph describing the compatibility of blood groups:
A, B, O and AB.

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.

Example. In the graph below, deg 𝑎 = 5, deg 𝑏 = 6, deg 𝑐 = 3 , deg 𝑑 = 6 .

20
HANDSHAKING THEOREM FOR UNDIRECTED GRAPHS

Theorem. Let 𝐺 = 𝑉, 𝐸 be an undirected graph. Then we have

෍ 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 𝒆′ .

𝒆′ 𝒆

Definition. Let 𝐺 be a directed graph and 𝑣 a vertex of 𝐺.


∎ The in-degree of 𝑣, denoted deg − (𝑣), is the number of edges whose terminal vertex is 𝑣.
∎ The out-degree of 𝑣, denoted deg + (𝑣), is the number of edges whose initial vertex is 𝑣.
For example, in the above graph, deg − 𝑎 = 2 and deg − 𝑏 = 3, while deg + 𝑎 = 4 and deg + 𝑏 = 1.
23
HANDSHAKING THEOREM FOR DIRECTED GRAPHS
Theorem. Let 𝐺 = 𝑉, 𝐸 be a directed graph. Then we have

෍ deg − (𝑣) = ෍ deg + (𝑣) = 𝐸 .


𝑣∈𝑉 𝑣∈𝑉
meaning that the sum of in-degrees of all vertices of 𝐺 is equal to the sum of out-degrees of all vertices
and moreover, is equal to the number of edges 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: 𝑲𝒏

Exercise. Draw 𝐾5 and 𝐾6 .

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 𝐺.

Exercise. Find all subgraphs of 𝐾4 which are complete bipartite.

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 .

Exercise. Verify the following statements.


(i) 𝐺1 ∪ 𝐺2 = 𝐺2 ∪ 𝐺1 .
(ii) (𝐺1 ∪ 𝐺2 ) ∪ 𝐺3 = 𝐺1 ∪ (𝐺2 ∪ 𝐺3 ).

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 𝑣𝑗 .

Example. Consider the following graph.


We order the vertices as 𝑣1 = 𝑎, 𝑣2 = 𝑏, 𝑣3 = 𝑐, 𝑣4 = 𝑑.
We then find the adjacency matrix of 𝐺

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

1 if edge 𝑒𝑗 is incident with 𝑣𝑖


𝑎𝑖,𝑗 = ቊ
0 otherwise.

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 "

Example 1. The two graphs below are isomorphic.

The bijection 𝑓: 𝐴, 𝐵, 𝐶, 𝐷 ⟶ {𝑋, 𝑌, 𝑍, 𝑊}


is given by
𝑓 𝐴 = 𝑋, 𝑓 𝐵 = 𝑌,
𝑓 𝐶 = 𝑊, 𝑓 𝐷 = 𝑍.

Question. Can we take 𝑓: 𝐴, 𝐵, 𝐶, 𝐷 ⟶ {𝑋, 𝑌, 𝑍, 𝑊} such that 𝑓 𝐴 = 𝑋, 𝑓 𝐵 = 𝑊, 𝑓 𝐶 = 𝑌, 𝑓 𝐷 = 𝑍?

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.

Convention. For simplicity, we will denote such a path by 𝑎, 𝑏, 𝑓, 𝑒 .

52
CIRCUIT

Example. In the above graph, we have 𝑎, 𝑏, 𝑒, 𝑓, 𝑎 is a circuit from 𝑎 to itself of length 4.

Example. In the above graph, we have 𝑎, 𝑏, 𝑒, 𝑓, 𝑎 is a simple circuit, while 𝑎, 𝑏, 𝑓, 𝑒, 𝑏, 𝑎 is not


simple because it passes through edge 𝑎𝑏 twice.

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

Example. The above graph has two components

and

55
CUT VERTICES

Example. Find all cut vertices of the graph below.

Answer. The only cut vertex is 𝑏.

56
CUT EDGES

Example. Find all cut edges of the graph 𝐺1 below.

Answer. The cut edges are 𝑎𝑏 and 𝑐𝑒.

57
9.4.2 Connectivity of
Directed Graphs

58
PATHS IN A DIRECTED GRAPH

For example, in the graph above, we have


∎ 𝑎, 𝑏, 𝑑, 𝑒 is a simple path from 𝑎 to 𝑒.
∎ 𝑎, 𝑏, 𝑐, 𝑑, 𝑒, 𝑎 is a simple circuit from 𝑎 to itself.

59
STRONGLY CONNECTEDNESS IN DIRECTED GRAPHS

Example. The graph 𝐺 is strongly connected, while 𝐻 is only weakly connected.

60
STRONGLY CONNECTED COMPONENTS

Example. Find all strongly connected components of the graph below.

Answer. The only strongly connected component is

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

matrix of 𝐺 is 𝐴 = 𝑎𝑖,𝑗 . Then we have


𝑛×𝑛

the number of paths of length 𝑟 from 𝑣𝑖 to 𝑣𝑗 = 𝑖, 𝑗 -entry of 𝐴𝑟 .

Proof. This is for you!


Hint: first, consider the cases 𝑟 = 1 and 𝑟 = 2. Then proceed by an inductive argument.

65
EXAMPLE
Example. Count the number of paths of length 3 between 𝑎 and 𝑐 in the graph below.

Solution. We order the vertices as 𝑣1 = 𝑎, 𝑣2 = 𝑏, 𝑣3 = 𝑐, 𝑣4 = 𝑑. The adjacency matrix of the


graph is

𝐴=

We need to find the 1,3 -entry of 𝐴3 . The solution is .

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.

In the above graph, the circuit 𝑎, 𝑒, 𝑑, 𝑐, 𝑏, 𝑎, 𝑑, 𝑏, 𝑒, 𝑐, 𝑎 is an Euler circuit from 𝑎 to itself.


Exercise. Is there any Euler path that is not a curcuit in this graph?

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.

Example. Consider the graphs below.

(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.

Example. Consider the following graph.

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.

Example. In the above weighted graph, the lenght of path 𝑎, 𝑏, 𝑒, 𝑧, 𝑐 from 𝑎 to 𝑐 is


𝑎, 𝑏 + 𝑏, 𝑒 + 𝑒, 𝑧 + 𝑧, 𝑐 = 4 + 3 + 1 + 2 = 10.
Exercise. Find the shortest/longest simple path between 𝑎 and 𝑧.

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.

Homework. For more example, see Example 2 (p. 712) in Textbook.

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

10.2. Applications of Trees

10.3. Tree Traversal

10.4. Spanning Trees

10.5. Minimum Spanning Trees

94
PRACTICE

95
PRACTICE

96
10.1 Introduction

97
DEFINITION

Definition. A tree is a connected undirected graph that has no simple circuits.


In particular, any tree must be a simple graph.

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)

Example. In the tree 𝑇, the ancestors of 𝑙 are 𝑗, 𝑔, 𝑎 .


The descendants of 𝑔 are ℎ, 𝑘, 𝑖, 𝑗, 𝑙, 𝑚 .
All the leaves are 𝑑, 𝑒, 𝑘, 𝑙, 𝑚, 𝑓, 𝑖 .
All the internal vertices are 𝑎, 𝑏, 𝑔, 𝑐, ℎ, 𝑗 .

101
TERMINOLOGIES (cont)

102
m-ary Trees

Example. 𝑇1 is a full binary tree; 𝑇2 is a full 3-ary tree;


𝑇3 is a full 5-ary tree and 𝑇4 is a (not full) 3-ary tree.
103
m-ary Trees (cont)
Theorem. Let 𝑇 be a full 𝑚-ary tree. Suppose that 𝑇 has 𝑖 internal vertices. Then
(i) 𝑇 has 𝑛 = 𝑚𝑖 + 1 vertices.
(ii) 𝑇 has 𝑙 = 𝑖 𝑚 − 1 + 1 leaves.
Proof. This is for you!

Homework. See Example 9 (p. 753).

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.

Example. 𝑇1 is balanced because all its leaves has levels 3 or 4.


𝑇2 is not balanced because its leaves are at levels 2, 3 or 4.

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

(2) When 𝑇 is also balanced, we have 4 = log 𝑚 81 . And thus we have 𝑚 = 3.

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

Answer. At least log 3 8 = 2 weighings are needed.

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.

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


Note: To be able to do this, we have to use bit strings of different lengths.

Example. We try to encode the letters 𝑒, 𝑎 and 𝑡 by using 𝑒 = 0, 𝑎 = 1 and 𝑡 = 01.


Then the string 0101 could be 𝑒𝑎𝑡, 𝑡𝑒𝑎, 𝑒𝑎𝑒𝑎 or 𝑡𝑡 . (Thus, the encoding is failed!).
One way to fix this is to use prefix codes. For instance, we can let 𝑒 = 0, 𝑎 = 10 and 𝑡 = 11.

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.

Example. One can use binary trees to creat prefix codes


such that each edge leading a left child is labeled by 0
and each edge leading a right child is labeled by 1.
Each leaf is labeled by a letter.
In this example, we have encoded
𝑒 = 0, 𝑎 = 10, 𝑡 = 110, 𝑛 = 1110, 𝑠 = 1111.

Exercise. Decode the bit string 11101111010101110.

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.

The average number of bits used to encode a


letter is
3 × 0.08 + 3 × 0.10 + 3 × 0.12 + 3 × 0.15
+ 2 × 0.20 + 2 × 0.35 = 2.45.

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 𝑇.

∎ Otherwise, suppose 𝑇1 , 𝑇2 , … , 𝑇𝑛 are the subtrees at 𝑟 from left to right.

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 𝑇.

∎ Otherwise, suppose 𝑇1 , 𝑇2 , … , 𝑇𝑛 are the subtrees at 𝑟 from left to right.

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 𝑇.

∎ Otherwise, suppose 𝑇1 , 𝑇2 , … , 𝑇𝑛 are the subtrees at 𝑟 from left to right.

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.

Exercise. Construct a binary tree whose inorder traversal


represents the expression
𝑥 + 𝑦 2 + 𝑥 − 4/3.
Note: we must label internal vertices with operations, and label
leaves with variables (or numbers).

127
PREFIX, INFIX AND POSTFIX NOTATION

The inorder traversal gives the infix notation:


𝑥 + 𝑦 ↑ 2 + 𝑥 − 4 / 3.
The preorder traversal gives the prefix notation (also called Polish notation):
+ ↑ +𝑥 𝑦 2 / −𝑥 4 3.
The postorder traversal gives the postfix notation (also called reverse Polish notation):
𝑥 𝑦 + 2 ↑ 𝑥 4 − 3 /+.

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

Exercise. Find the compound proposition


represented by the following tree. Then find
the infix, prefix and postfix forms of that.
Solution. ¬ 𝑝 ∧ 𝑞 ⟷ ¬𝑝 ∨ ¬𝑞 .
(i) Infix form: 𝑝 ∧ 𝑞 ¬ ⟷ 𝑝 ¬ ∨ 𝑞 ¬.
(ii) Prefix form: ⟷ ¬ ∧ 𝑝 𝑞 ∨ ¬ 𝑝 ¬ 𝑞.
(iii) Postfix form: 𝑝 𝑞 ∧ ¬ 𝑝 ¬ 𝑞 ¬ ∨ ⟷.

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.

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

141
10.4.1 Depth – First Search

142
DEPTH – FIRST SEARCH: AN EXAMPLE
Example. Find a spanning tree of the graph below.

Solution. Arbitrarily start with the vertex 𝑓.

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.

Solution. Arbitrarily start with the vertex 𝑒.

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?

Example. In this graph, one needs 3


colors, the minimum number possible.

Exercise. Find minimum number of colors needed to colour 𝐾𝑛 , 𝐶𝑛 , 𝑊𝑛 and 𝑄𝑛 .

152
EXAMPLE

Let us try to colour the graph below using 3 colors.

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.

Example. Solve the 4-queens problem.

154
155
SUM OF SUBSETS

156
PRACTICE

157
158
159
10.5 Minimum Spanning Trees

160
DEFINITION

Definition. Let 𝐺 be a weighted graph. A minimum spanning tree in 𝐺 is a spanning tree


that has the smallest possible sum of weights of its edges.

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

Exercise. Find a minimum spanning tree using


(a) Prim’s algorithm,
(b) Kruskal’s algorithm.

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.

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 𝑚 𝑙 .

172
PRACTICE

173
PRACTICE

174
PRACTICE

175
PRACTICE

176
PRACTICE

177
PRACTICE

178
PRACTICE

179

You might also like