Chapter 9 Graphs
Chapter 9 Graphs
1 Chapter 9 - Graphs
Introduction
LEONHARD EULER
(1707–1783)
Multigraph Model
2 Chapter 9 - Graphs
Introduction
Web Graph
Google PageRank
3 Chapter 9 - Graphs
Objectives
9.1- Graphs and Graph Models
9.2- Graph Terminology 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 Problems
Graphs
Graphs = (vertices, edges) G = (V,
E)
V = vertices = { a, b, c, d }
E = edges = { {a, b}, {a, c} }
Computer network
5 Chapter 9 - Graphs
Simple graphs
6 Chapter 9 - Graphs
Types of graphs
An undirected graph
A directed graph
(digraph)
7 Chapter 9 - Graphs
Graphs and Graph Models….
Graphs and Graph Models….
An Influence Graph
A Graph Model of a Round-Robin An Precedence Graph
Tournament
Call Graph
Graph models - Social networks
11 Chapter 9 - Graphs
Basic Terminology
13 Chapter 9 - Graphs
THE HANDSHAKING THEOREM (for undirected graphs)
14 Chapter 9 - Graphs
THE HANDSHAKING THEOREM -
Examples
Ex2. How many edges does a graph have
if its degree sequence is 5, 5, 4, 3, 2, 1,
0?
Draw a such graph.
=5+5+4+3+2+1+0
= 20 = 2.|E| |E| = 10.
15 Chapter 9 - Graphs
Directed graphs -Basic
Terminology
a 2 1
b 0 2
c 1 0
d 1
deg- = 1deg+= directed
4 edges
16 Chapter 9 - Graphs
Special simple graphs
K4 K5 K6
K20
17 Chapter 9 - Graphs
Cycles Cn
Cycles Cn (n )
C5 C6 C8
18 Chapter 9 - Graphs
Wheels Wn
Cycles
Cn
C5 C6
C8
Wheels
Wn (n )
W5 W6
19 Chapter 9 - Graphs
n-cubes Qn
n-dimensional hypercube
1-D
2-D 3-D
20 Chapter 9 - Graphs
n-cube Qn
Construct Q4 from two copies of Q3
0 1
0 0
0 0 1
1
0 1 1
0
1 1
0 0
1 1
21 Chapter 9 - Graphs
Bipartite graphs
A simple graph G = (V, E) is called bipartite
if:
V = V1 V2, V1 V2 =
no edge connects two vertices in V1
no edge connects two vertices in V2
We call the pair (V1,V2) a bipartition of V.
Ex. Study graph C6 (cycle)
V = { a, b, c, d, e, f }
V1 = {a, e, c} ae ac ec
V2 = {b, f, d} bf bd fd
C6 is bipartite
22 Chapter 9 - Graphs
Bipartite Graphs…
bipartite
a b
Ex. • Let color the vertices
C6 is bipartite
using 2 different colors f c V1 = {a, c, e}
• Two adjacent vertices V2 = {b, d, f}
must have different
colors (e.g., red and
e d
black) a
c
d bipartite
b
V1 = {a, e, f}
V2 = {b, c, d}
f
e
24 Chapter 9 - Graphs
Example
Ex. Is the graph G bipartite?
? G is not bipartite
G
25 Chapter 9 - Graphs
Km,n - Complete bipartite graphs
m=3
n=5
K3,5
26 Chapter 9 - Graphs
Euler path Euler circuit Hamilton path Hamilton
circuit
Kn + K2 n is odd (n >
2)
+ K2 All n > 2
e.g: K3; K4; K5
+ n is odd (n > + All n > 2
2) e.g: K3; K5; K7 e.g: K3; K4; K5
e.g: K3; K5; K7
Wn>2 NO NO
Qn
Km,n
27 Chapter 9 - Graphs
Some Applications of Special Types of Graphs
THE SAME
30 Chapter 9 - Graphs
Isomorphism
a b m n
G1 = (V1, E1), G2 = (V2, E2)
G1 and G2 are called isomorphic if
$ function f: V1 V2
o One-to-one
o Onto c d p q
o a, b are adjacent in V1
f(a), f(b) are adjacent in V2
function f = {(a, m); (b, p); (d, n); (c, q)}
Two graphs are called isomorphic
31 Chapter 9 - Graphs
Isomorphism of Graphs
Pull
out
33 Chapter 9 - Graphs
Isomorphic?
YES
34 Chapter 9 - Graphs
Isomorphic?
G H
G and H are NOT isomorphic
(in H: deg(e) = 1, no vertex in G has degree 1)
35 Chapter 9 - Graphs
Representing graphs
Adjacency matrix
Incidence matrix
edge 1 edge 2 … edge m
Vertex 1
Vertex 2
…
Vertex n
36 Chapter 9 - Graphs
Adjacency matrices
a b c d
a 0 0 1 0
b 0 0 1 2
c 1 1 0 1
d 0 2 1 0
Adjacency matrix.
A = [aij],
where aij = the number of
edges that are associated to
{vi, vj}
37 Chapter 9 - Graphs
In 2010 the Web graph was
estimated to have at least 55
billion vertices and one trillion
edges.
More than 40 TB of
computer memory would have
been needed to represent its
adjacency matrix.
38 Chapter 9 - Graphs
Incidence matrices
edges
e6
e1
e1 e2 e3 e4 e5 e6
e2 a 1 1 0 0 0 0
b 1 0 0 0 0 1
e3
e5 c 0 1 0 0 0 0
e4
d 0 0 1 1 0 0
e 0 0 1 0 1 0
Vertices = { a, b, c, d, e, f } f 0 0 0 1 1 0
Edges = { e1, e2, e3, e4, e5, e6 } g 0 0 0 0 0 0
39 Chapter 9 - Graphs
Incidence matrices
If the edge ej is incident with the vertex vi
the (vi, ej)-entry = 1
Else
the (vi, ej)-entry = 0
40 Chapter 9 - Graphs
A path of length n
• A path of length n
from u to v:
A sequence of n
consecutive edges
• Ex.
a, e, f, c, d
is a path of
length 4.
41 Chapter 9 - Graphs
Circuits
• A circuit is a path of
length greater than
zero that starts and
ends at the same
vertex.
• Ex.
c, b, e, a, d, c is
circuit.
42 Chapter 9 - Graphs
Simple paths/circuits
• A path/circuit is
simple if it does not
contain the same
edge more than once.
• Ex.
b, e, a, b, f, c
is a simple path.
c, b, e, a, d, e, b,
f, c
is NOT a simple circuit.
43 Chapter 9 - Graphs
Connectedness in Undirected
Graphs
G
A disconnected with
G is connected 3 connected
components
44 Chapter 9 - Graphs
45 Chapter 9 - Graphs
Connectedness in Directed Graphs
G H
G: strongly connected
weakly connected H: weakly connected
connected
46 Chapter 9 - Graphs
H
47 Chapter 9 - Graphs
G
48 Chapter 9 - Graphs
Counting Paths Between Vertices
How many paths of length four from
a to d in the simple graph G?
The adjacency matrix of G is given below
(ordering the vertex as a, b, c, d)
to
from
G
Result = 8
49 Chapter 9 - Graphs
Shortest-path problems
weight
50 Chapter 9 - Graphs
Shortest-path problems
51 Chapter 9 - Graphs
Dijkstra’s algorithm
c d
11
3
14
12
11
12
3 6
a 0 4 2
25 z
23
5
8 9 16
8
7
9
b e
S = {a,c,b, e, d, z}
The shortest path from a to z: a, c, e, d, z
52 Chapter 9 - Graphs
Find the length of the shortest path from A to G.
53 Chapter 9 - Graphs
om
h: A,
E,
54 Chapter 9 - Graphs
How many strongly connected?
2 STRONGLY Connected
component
55 Chapter 9 - Graphs
56 Chapter 9 - Graphs
han lien thong manh.
=(V, E)
ong. Chua A.
57 Chapter 9 - Graphs
Dijkstra’s Algorithm
O(n2)
time complexity
58 Chapter 9 - Graphs
Euler and Hamilton paths - introduction
59 Chapter 9 - Graphs
Euler circuit/path - definitions
60 Chapter 9 - Graphs
Euler circuit
Theorem.
A connected multigraph, 2 vertices,
has an Euler circuit every vertex has
even degree
3 NOT OK
2 2
4 OK
2 2
61 Chapter 9 - Graphs
How to construct an Euler circuit?
procedure Euler(G: connected, every
a d
vertex has even degree)
62 Chapter 9 - Graphs
Euler path
Theorem.
63 Chapter 9 - Graphs
Hamilton Paths and Circuits
Hamilton circuit/path:
A simple circuit/path passes through
every vertex exactly once.
start
end
?
Hamilton No Hamilton circuit
path a,b,c,d,e is a Hamilton path
64 Chapter 9 - Graphs
Example - A Hamilton circuit for Q3
65 Chapter 9 - Graphs
Hamilton Paths and Circuits
There are
NO known simple
necessary and sufficient
criteria
for the existence of
Hamilton circuits
66 Chapter 9 - Graphs
Hamilton circuits - sufficient
conditions
Dirac’s theorem.
G is a graph:
• simple
• n ( 3) vertices
G has a Hamilton circuit
• vi, deg(vi) 2 3
Ore’s theorem.
G is a graph:
? 3 3
2
• simple
• n ( 3) vertices
• u, v, non-adjacent
G has a
deg(u) + deg(v) n Hamilton circuit
67 Chapter 9 - Graphs
Lms: practice 9, 123
68 Chapter 9 - Graphs
Degree sequence: 4
2,2,2,2,2
(even degree) So:
Has Euler circuit (als
E.path)
69 Chapter 9 - Graphs
Degree sequence: 4, 3, 3,
2,2,2
Has Euler path (but not
E.circuit)
70 Chapter 9 - Graphs
Degree sequence: 4,4, 2,2,2,2 (even degree)
So, it has Euler circuit (also E.path)
The Traveling Salesman Problem
Total Distance
= 58 + 133 + 137 + 113 + 147
= 588
4!
=12
2
Approximation algorithm
73 Chapter 9 - Graphs
Summary
9.1- Graphs and Graph Models
9.2- Graph Terminology 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 Problems
74 Chapter 9 - Graphs
THANKS
75 Chapter 9 - Graphs