Part 2 Chapter 1 - Graph Fundamentals
Part 2 Chapter 1 - Graph Fundamentals
Discrete Mathematics
PART 2
GRAPH THEORY
(Lý thuyết đồ thị) 2
Content of Part 2
Chapter 1. Fundamental concepts
Chapter 2. Graph representation
Chapter 3. Graph Traversal
Chapter 4. Tree and Spanning tree
Chapter 5. Shortest path problem
Chapter 6. Maximum flow problem
Chapter 1
Fundamental concepts
• In Maths:
Drawing or diagram represent data by using coordinate
system
• In discrete mathematics:
This is discrete structure with highly intuitive, very useful
for expressing relationships.
Applications of graph in practice
Has the potential to be applied in many fields:
• Internet
• Traffic network
• Electrical network
• Water supply network
• Scheduling
• Flow optimization, circuit design
• DNA gene analysis
• Computer games
• Object oriented design
• ….
461
322
373 143
321
142 326
410 415 370 341
413 417
378
421
Vertex = subject
401
Directed graph = prerequisites
NGUYỄN KHÁNH PHƯƠNG
Bộ môn KHMT – ĐHBK HN
Represent maze
S
S
E
E
Vertex = room
Edge = doorways or hallways
Source Switch
x1 x2
x1=q+y*z
x2=y*z-q + -
q * q
Vertex = notation/operator
y z
Edge = relationship
S1 a=0; 6
S2 b=1;
S3 c=a+1
5
S4 d=b+a;
S5 e=d+1;
S6 e=c+d;
4
Which statements need to execute before S6? 3
S1, S2, S3, S4
Vertex = statement
Edge = precedence 1 2
56
Tokyo Hanoi
Seoul 128
Beijing
Sydney
Vertex = computer
Edge = communication speed
UW
Vertex = city
Edge = the amount of
traffic on highways
connecting cities
(a, b)
a b Undirected Multigraph
(b, c) Undirected (simple) graph
c
a f
b
h
e
c g
d
Graph G1
Undirected MultiGraph
• Example: Multigraph G2 = (V2, E2), where V2={a, b, c,
d, e, f, g, h},
E2={(a,b), (b,c), (b,c), (c,d), (a,d), (d,e), (a,e), (a,e),
(a, e), (d,b), (f,g)}.
a
b
f
e h
Multiple c
edges
g
d
Graph G2
Directed Graph
Definition. Directed simple (multi) graph G = (V,E) consists of 2
sets:
• Vertex set V is finite element, each element is called vertex
• Edge set E is set (family) of ordered pairs
(u, v), where u, v V, u≠v
(v, u)
(u, v)
u v Directed multigraph
(w, v)
(Simple) Directed graph
w
~ Digraph
Simple digraph
Example: Simple digraph G3= (V3, E3), where
V3={a, b, c, d, e, f, g, h},
E3={(a,b), (b,c), (c,b), (d,c), (a,d), (b, d), (a,e), (d,e), (e,a),
(f,g), (g,f)}
a f
b
h
e
c g
d
Graph G3
NGUYỄN KHÁNH PHƯƠNG
Bộ môn KHMT – ĐHBK HN
Directed MultiGraph
Example: Directed multigraph G4= (V4, E4), where
V4={a, b, c, d, e, f, g, h},
E4={(a,b), (b,c), (c,b), (d,c), (a,d), (b, d), (a,e), (a,e),
(d,e), (e,a), (f,g), (g,f)}
a
b
f
e h
c
g
d
Graph G4
Graph types: Summary
Loop (Khuyên)
• Note:
– Pseudo graph (Giả đồ thị) is multigraph that contains
loops as well as multiple edges between vertices
v v
deg(v) 2 E
vV
(why?) Every edge connects 2 vertices
Theorem 2:
An undirected graph has even number of vertices with odd degree
Proof: V1 is the set of even
degree vertices
and V2 refers
to odd degree vertices
∈ ∈ ∈
a
deg-(f) = 0
f
deg-(a) =0 deg+(f)= 0
deg+(a)= 2 d e
a- source vertex deg-(d) = 2 deg-(e) = 2
deg+(d)= 1 deg+(e)= 0
e –target vertex
vV
2 vV
H1 G
G
v w v
v w H2 G
v w
v w v w
x y
x y x y
38
G HG F G
The deletion of vertices
Definition. G = (V, E) is undirected graph. Assume S V. We call the
removal of vertices set S from the graph is to remove all vertices in S
and their adjacent edges.
We denote the obtained graph by G-S
G v w GS v w
u Assume S={x,u} u
x y x y
Definition.
We write H = G + {(u,v), (u,w)} to mean
E(H) = E(G) {(u,v), (u,w)}, where (u,v), (u,w)E(G).
G1 G2
d e d f
a b c
G1 G2
d e f
41
The union of graphs (Hợp của hai đồ thị)
The union of two simple graphs G1=(V1, E1) and G2=(V2, E2) is the simple
graph G1 G2=(V1 V2, E1 E2).
Example:
a b c a b c
d e d f
u1 u2 v1 v2
G is isomorphic to H
u3 u4 v3 v4
G H
Definition. The simple graphs G1=(V1,E1) and G2=(V2,E2) are
isomorphic if there is an one-to-one and onto function f from V1 to
V2 with the property that a and b is adjacent in G1 iff f(a) and f(b)
is adjacent in G2, a,bV1
f is called an isomorphism.
Application Example:
In chemistry, to find if two compounds have the same structure 44
Example. Show that G and H are isomorphic.
u1 u2 v1 v2
u3 u4 v4
v3
G H
Solution.
The function f with f(u1) = v1, f(u2) = v4, f(u3) = v3, and f(u4) = v2 is a
one-to-one correspondence between V(G) and V(H).
Isomorphism graphs there will be:
(1) The same number of vertices
(2) The same number of edges
(3) The same number of degree
45
Isomorphism of Graphs
Given figures, judging whether they are isomorphic in general is not an
easy task.
Example. Show that G and H are not isomorphic.
G H
Solultion :
G has a vertex of degree = 1 , H doesn’t
46
Example. Determine whether G and H are not isomorphic.
a b s t
e w x
f
g
h z y
d c
v u
G H
Solultion : In G, deg(a)=2, which must correspond to either t, u,
x, or y in H. Degree each of these four vertices in H is adjacent to
another vertex of degree two in H, which is not true for a in G
G and H are not isomorphic.
b d
a b a
d c
e
e c f
f
u1 G u2 v1 H v3
u5 v2
u6
v6
v5
u4 u3 v4
Solution:
f(u1)=v6, f(u2)=v3, f(u3)=v4, f(u4)=v5, f(u5)=v1, f(u6)=v2
Yes
a
b • Same number of vertices
• Same number of edges
d • Different number of
vertices with degree 2
c e
(1 ≠ 3)
Path (directed) 1, 2, 5, 3, 4
5
[OR: (1, 2), (2, 5), (5, 3), (3, 4) c d
OR: a, c, d, e] a b 3 e
1 2 4
• is the simple path
53
Path
• P1= (1, 2, 3) is the simple path
• P2=(4,5,2,6,5,1) is the path but not the simple path
1
a b
P1
d
4 2 3
P2 h
c e
5 g
f
6
54
Cycle
• A closed elementary path is called a cycle.
– A path is called elementary if all the edges are distinct.
– A path is closed if v0 = vn.
• A cycle is called simple if all the vertices are distinct (except v0 = vn).
2
a b
Simple cycle: (1, 2, 3, 1) e
1 3
d c
4
2
a b
e
1 3
Simple cycle: (1, 2, 3, 4, 1) d c
55
Cycle on directed graph
V
a b
U d X Z
C2 h
c e C1
W g
f
Y
56
Content
1. Graph in practice
2. Graph types
3. Degree of vertex
4. Subgraph
5. Isomorphism of Graphs
6. Path and cycle
7. Connectedness
8. Special graphs
9. Graph Coloring problem NGUYỄN KHÁNH PHƯƠNG
Bộ môn KHMT – ĐHBK HN
Connectedness
Undirected Graph
An undirected graph is connected if there exists is a simple path between
every pair of vertices
Example: G (V, E) is connected since for V = {v1, v2, v3, v4, v5}, there exists a path
between {vi, vj}, 1 ≤ i, j≤ 5 v1 v4
v3
v2 v5
Articulation Point (Cut vertex): removal of a vertex produces a subgraph with more
connected components than in the original graph. The removal of a cut vertex from a
connected graph produces a graph that is not connected
Bridge: An edge whose removal produces a subgraph with more connected components
than in the original graph.
b c e: cut vertex
e
a d (e,g): bridge
g 58
Connectedness
• If a graph is not connected then it splits up into a number of connected subgraphs,
called its connected components.
• The connected components of G can be defined as its maximal connected subgraphs.
This means that G1 is a connected component of G if:
– G1 is a connected subgraph of G
– G1 is not itself a proper subgraph of any other connected subgraph of G. This
second condition is what we mean by the term maximal; it says that if H is a
connected subgraph such that G1 H, then G1 = H.
Example: Graph G has 3 connected components: G1, G2, G3
b c
i
f a
j k G2 d e
G3
G1 g
59
Connectedness
Directed Graph
• A directed graph is strongly connected if there is a path from u to v and
from v to u whenever u and v are vertices in the graph
• A directed graph is weakly connected if its corresponding undirected graph
is connected.
A strongly connected Graph can be weakly connected but the vice-versa is
not true (why?)
b c
Strongly connected graph
a f
d e
b c b c
a f f
a
d e d e
60
Weakly connected graph
Connectedness
Directed Graph
• Strongly connected Components: subgraphs of a Graph G that are
strongly connected
Example: G1 is the strongly connected component in G
G G1
61
k-Connectivity(k-liên thông)
Not all connected graphs have the same value!
Question: Which following graphs are more valuable computer network
1) G1
2) G2
3) G3
4) G4
k-Connectivity(k-liên thông)
Answer: We want a computer network still working even
when one computer fails
G1) 2nd best. Connected, but there
is a weak point - “cut vertex”
G2) 3rd best. Connected, but each
computer is a weak point
N1 N2 N3 N4
66
Complete graph
Complete graph (Kn): is the simple graph that contains exactly one
edge between each pair of distinct vertices.
Example:
K1 K2 K3 K4 K5 K6
n 1
n(n 1)
Note: Graph Kn has i
i 1 2
edges
67
Complete Graphs (Đồ thị đầy đủ)
K25
Complete Graphs (Đồ thị đầy đủ)
K42
Cycles
Cycle (Cn , n ≥ 3): is an undirected graph consisting of n vertices v1,
v2, v3 … vn and edges {v1, v2}, {v2, v3}, {v3, v4} … {vn-1, vn}, {vn, v1}
Example:
C3 C4 C5 C6 C7 C8
70
Wheels
Wheel (Wn , n ≥ 3): is an undirected graph obtained by adding additional
vertex to Cn and connecting all vertices to this new vertex by new edges.
Example:
C3 C4 C5 C6 C7 C8
W3 W4 W5 W6 W7 W8
Q0 Q1
00 01 Q3 Q4
Q2
• Q0={{v0},} (1 vertex, not edge)
• nN: if Qn=(V,E) where V={v1,…,va} and E={e1,…,eb}, then
Qn+1=(V{v1´,…,va´},
E{e1´,…,eb´}{{v1,v1´},{v2,v2´},…,{va,va´}})
It means Qn+1 obtained by connecting pairs of verticies of Qn và Q’n
72
Some special graphs
1. Null graph
2. Complete graphs Kn
3. Cycles Cn
4. Wheels Wn
5. n-Cubes Qn
6. Bipartite graphs
7. Complete bipartite graphs Km,n
8. r-regular graph
9. Planar graph
10. Euler graph and Hamilton graph 73
Bipartite graph
Graph G=(V,E) is a bipartite graph if and only if
V = V1V2 ,V1∩V2=
eE: v1V1,v2V2: e={v1,v2}
(In a simple graph G, if V can be partitioned into two disjoint sets V1 and
V2 such that every edge in the graph connects a vertex in V1 and a vertex
V2 (so that no edge in G connects either two vertices in V1 or two vertices
in V2)
Example:
V1 V2 74
Example. Is the graph G bipartite ?
a b a
c
g
c e
b
f
f d d
g
e
G Yes !
75
Bipartite graph: Examples of application
• Document/Term Graphs: Here set V1 are documents and set V2 are terms or
words, and there is an edge (v1, v2) if the word v2 is in the document v1. Such
graphs are use often to analyze text, for example to cluster the documents.
• Movies preferences: In 2009, Netflix gave a $1Million prize to the group that
was best able to predict how much someone would enjoy a movie based on
their preferences. This can be viewed as a bipartite graph problem. The
viewers are the vertices V1 and the movies the vertices V2 and there is an edge
from v1 to v2 if v1 viewed v2. In this case, the edges are weighted by the rating
the viewer gave. The winner was algorithm called “BellKor’s Pragmatic
Chaos”.
• Students and classes: We might create a graph that maps every student to the
classes they are taking. Such a graph can be used to determine conflicts, e.g.
when classes cannot be scheduled together.
• Object recognition
• Food Recommendation System
• …
76
Complete bipartite graph
With m,nN, complete bipartite graph Km,n is bipartite graph where
• |V1| = m, |V2| = n
• E = {(v1,v2)|v1V1 và v2V2}.
Km,n is the graph that has its vertex set portioned into two subsets of m
and n vertices, respectively. There is an edge between two vertices if and
only if one vertex is in the first subset and the other vertex is in the
second subset.
Example:
K3,3 K4,3
77
r-regular graph
Definition. Graph G = (V, E) is r-regular graph if each vertex vV
has degree deg(v)= r (has the same number of neighbors)
Example:
78
Some special graphs
1. Null graph
2. Complete graphs Kn
3. Cycles Cn
4. Wheels Wn
5. n-Cubes Qn
6. Bipartite graphs
7. Complete bipartite graphs Km,n
8. r-regular graph
9. Planar graph
10. Euler graph and Hamilton graph 79
Planar Graphs (Đồ thị phẳng)
• A graph is called planar if it can be drawn in the plane
without any edges crossing.
– A crossing of edges is the intersection of the lines or arcs
representing them at a point other than their common endpoint.
– Such a drawing is called a planar representation of the graph.
Example: K4 is planar graph?
K3,3
Phrased another way, this question is equivalent to: Given
the complete bipartite graph K3,3, can K3,3 be drawn in the
plane so that no two of its edges cross? (K3,3 is planar graph?)
K3,3 and K5 are not planar graphs
• K3,3 and K5 are not planar graphs.
And the number of corresponding planar connected graphs are 1, 1, 2, 6, 20, 99,
646, 5974, 71885,…
Regions
• Euler showed that all planar representations of a graph split the plane
into the same number of regions, including an unbounded region.
Example:
G1
R3: exterior
R1
G1 has 3 regions.
R2
R4 R3 R2
G2 has 4 regions. R1
G2
Regions
• In any planar representation of K3,3, vertex v1 must be connected to
both v4 and v5, and v2 also must be connected to both v4 and v5.
v1 v2 v3
v4 v5 v6
Regions
• The four edges {v1, v4}, {v4, v2}, {v2, v5}, {v5,
v1} form a closed curve that splits the plane
into two regions, R1 and R2.
K3,3
Regions
• Euler devised a formula for expressing the relationship
between the number of vertices, edges, and regions of a
planar graph.
• These may help us determine if a graph can be planar or not.
R4 R3 R2
# of edges, e = 6
R1 # of vertices, v = 4
# of regions, r = e - v + 2 = 4
K5
Euler’s Formula (Cont.)
• Corollary 2: If G is a connected planar simple graph, then G
must have a vertex of degree not exceeding 5.
Is K3,3 planar?
• K3,3 has 6 vertices and 9 edges.
• Obviously, v 3 and there are no circuits of length 3.
• If K3,3 were planar, then e 2v – 4 would have to be true.
• 2v – 4 = 2*6 – 4 = 8
• So e must be 8.
• But e = 9.
• So K3,3 is nonplanar.
Some special graphs
1. Null graph
2. Complete graphs Kn
3. Cycles Cn
4. Wheels Wn
5. n-Cubes Qn
6. Bipartite graphs
7. Complete bipartite graphs Km,n
8. r-regular graph
9. Planar graph
10. Euler graph and Hamilton graph 97
Seven Bridges of Königsberg
• The city of Königsberg, Russia was set on both sides of the Pregel River,
and included two large islands (Kneiphof and Lomse) which were
connected to each other, or to the two mainland portions of the city, by
seven bridges. The problem was to devise a walk through the city that
would cross each of those bridges exactly once.
• In 1736, Euler proved that the problem has no solution.
Leonhard Euler
1707-1783
Seven Bridges of Königsberg
• To prove it, Euler reformulated the problem in graph terms:
– Each land mass ~ a vertex
– Each bridge ~ an edge A
1 2 6
A 5
B D
2 3
1 4 7
B
4 5 C
3 D 6
C 7
A
Is there a way to go through all 7 bridges, each exactly 1 2 6
once, and then return to the starting position? 5 D
B
3 4
Whether or not there exists a cycle on a graph G that 7
traverses through every edge of G exactly once.
C
Euler graph
• Definition
• Recognize Euler graph
Euler graph
• Euler path (Eulerian trail, Euler walk) in graph is a path
that traverses through every edge exactly once.
• Euler cycle (Eulerian circuit, Euler tour) in graph is a
Euler path begins and ends at the same vertex (is a cycle
that traverses through every edge exactly once).
c d e c d e
Euler path in graph is a path that traverses through every edge exactly once.
Euler cycle in graph is a cycle that traverses through every edge exactly once.
Algorithm:
• Step One: Randomly move from node to node, until stuck.
Since all nodes had even degree, the circuit must have
stopped at its starting point. (It is a circuit.)
• Step Two: If any of the arcs have not been included in our
circuit, find an arc that touches our partial circuit, and add in
a new circuit.
– Each time we add a new circuit, we have included more
nodes.
– Since there are only a finite number of nodes, eventually
the whole graph is included.
Euler’s 2nd theorem
• If a graph has more than two vertices of odd degree, then it cannot
have an Euler path.
• If a graph is connected and has exactly two vertices of odd degree,
then is has at least one Euler path. Any such path must start at one of
the odd degree vertices and must end at the other odd degree vertex.
Euler’s theorems
If a graph is connected and if the number of odd degree vertices
• = 0, then Euler cycle (Theorem 1)
• = 2, then Euler path (Theorem 2)
– This Euler path must start at one of the odd degree vertices and must end
at the other odd degree vertex.
Is there a way to go through all 7 bridges, each exactly
A once, and then return to the starting position?
1 2 6
5 D Whether or not there exists a cycle on a graph G that
B traverses through every edge of G exactly once.
3 4
7 Answer: There exists vertex of odd degree don’t have Euler cycle
C
Whether or not there exists Euler path in G?
Answer: There are 3 vertices of degree 3, one vertex of degree 5
don’t have Euler path
Example
Vertex of Vertex of
odd degree odd degree
a b a b
c d e c d e
x x’ x x’
y y’
y y’
time
122
145
Flights 67
257
306
99
Graph coloring problem: Application example
145
99
Graph coloring problem: Application example
Conflict Graph
257 122
145
67
306
9
flights need gates, but times overlap.
• Each vertex represents a flight
how many gates needed?
Assign gates:
67 257, 306
306 67, 122,145
99
3 colors 99
3 gates
What is Coloring?
• Graph Coloring Problem is an assignment of colors to all vertices
of a given graph such that two adjacent vertices get different
colors.
3-colorable
Optimum Coloring
(Ceven ) = 2
odd
n
W5
(Wodd ) = 4 (Weven ) = 3
Tree
root
Pick any vertex as “root.”
if (unique) path from root is
even length:
odd length:
This is 2-colorable.
Slot 2
Slot 1
Slot 3
Thus, we need 3 time slots
Coloring problem: Some examples of Applications
Application 2: Register allocation
a and b cannot use the same register, because they store different values.
c and d cannot use the same register otherwise the value of c is overwritten.