Chapter 9
Graphs
“One graph is worth a thousand logs.”
Michal Aharon, Gilad Barash, Ira Cohen and Eli Mordechai.
Introduction
LEONHARD EULER
(1707–1783)
Multigraph Model
Introduction
Web Graph
Google PageRank
A network with 5 websites and links
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
9.1 Graphs
A graph G is a pair of (V; E), which are:
• V – nonempty set of vertices (nodes)
• E – set of edges
A graph captures abstract relationships between vertices.
Ex: Graphs = (vertices, edges) G = (V, E)
V = vertices = {a, b, c, d}
E = edges = {{a,b}, {a,c}}
Undirected graph Directed graph
9.1 Undirected Graph
Simple graph: contains exactly one edge between each pair of
distinct vertices
9.1 Undirected Graph
Multigraph: Multiple edges connect the same pair of vertices
Pseudograph: Multigraph may have loops
9.1 Directed Graph
A directed graph G is a pair of (V; E), in which:
• V – nonempty set of vertices
• E – set of directed edges
A directed edge start at u and end at v is denoted as (u; v).
Directed graph: Each edge has a determined direction
9.1 Graphs and Graph Models
9.1 Graphs and Graph Models
Niche Overlap Graph in Ecology Acquaintanceship Graph
(sinh thái học) – Đồ thị lấn tổ Đồ thị cho mô hình quan hệ giữa người
9.1 Graphs and Graph Models
An Influence Graph A Graph Model of a
An Precedence Graph
Round-Robin Tournament
A Call Graph
9.2 Graph Terminology and
Special Types of Graphs
Basic Terminology
Some Special Simple Graphs
Bipartite Graphs
Some Applications of Special Types of Graphs
New Graphs From Old
Basic Terminology - Undirected graphs
If an edge {u, v} exists, then u
and v are called adjacent The edge {u, v} is called
vertex adjacency incident with u and v
list edge incident
a a, c vertices
b c {a, a} a
c a, b {a, c} a, c
d {c, b} c, b
u and v are endpoints of an edge {u, v}
Basic Terminology - Undirected graphs
The degree of a vertex v: (Notation: deg(v))
deg(v) = the number of edges incident with v,
except that a loop at a vertex contributes twice.
Ex: vertex degree
a 3
b 1 b is called pendant
c 2
d 0 d is called isolated
degree= 6
Remark: degree = 2 | Edges |
THE HANDSHAKING THEOREM
(for undirected graphs)
THE HANDSHAKING THEOREM:
Let G = (V, E) be an undirected graph. Then
deg v 2 E always EVEN
vV
(Note that this applies even if multiple edges and loops are present)
Ex1: 3 edges: {a, a}, {c, b}, {a, c}
deg(a) = 3
deg(b) = 1
deg(c) = 2
deg(d) = 0
deg= 6 EVEN
Theorem 2:
An undirected graph has an even number of vertices of odd
degree.
THE HANDSHAKING THEOREM
(for undirected graphs)
Ex2: How many edges does a graph have if its degree sequence
is 5, 5, 4, 3, 2, 1, 0?
Draw a such graph.
Solution:
deg v 5 5 4 3 2 1 0 20 2 E
vV
E 10
Basic Terminology - Directed graphs
In an directed graph G = (V; E), if (u; v) is an edge of G then:
• u is said to be adjacent to v and v is said to be adjacent from u,
• u is called initial vertex of (u; v)
• v is called terminal or end vertex of (u; v)
• the initial vertex and terminal vertex of a loop are the same.
Ex:
Basic Terminology - Directed graphs
The degree of a vertex v:
• deg v in-degree of a vertex v, is the number of edges with v as
their terminal vertex.
• deg v out-degree of a vertex v, is the number of edges with v as
their initial vertex.
Note: a loop at a vertex contributes 1 to both the in-degree and the
out-degree of this vertex.
Ex:
Theorem 3:
Let G = (V, E) be a graph with
Vertex In-degree Out-degree directed edges. Then
deg- deg+
deg v deg v E
a 2 1
vV vV
b 0 2
c 1 0
d 1 1
deg- = deg+ (= 4 directed edges)
Special simple graphs
Complete graphs K n: A complete graph on n vertices, is a simple
graph that contains exactly one edge between each pair of distinct
vertices. The graphs K n , for n = 1, 2, 3, 4, 5, 6 are displayed in
Figure 3.
K20
2
VK n n, EK n Cn
Special simple graphs
Cycles Cn , n ≥ 3: consists of n vertices v1 , v2 , , vn and edges
v1, v2 ,v2 , v3 , ,vn1, vn , and vn , v1. The cycles C3 , C4 , C5 , and C6
are displayed in Figure 4.
C8
VCn n, ECn n
Special simple graphs
Wheels: We obtain a wheel Wn when we add an additional vertex
to a cycle Cn , for n ≥ 3, and connect this new vertex to each of the
n vertices in Cn , by new edges.
Cycles Cn
C5 C6 C8
VWn n 1, EWn n n 2n
Special simple graphs
An n-dimensional hypercube, or n-cube, denoted by Qn , is a
n
graph that has vertices representing the 2 bit strings of length n.
Two vertices are adjacent if and only if the bit strings that they
represent differ in exactly one bit position.
VQn 2n , EQn n.2n1
Special simple graphs
Bipartite graphs
A simple graph G V , E is called bipartite if:
– V V1 V2 and 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
V2 b, f , d
C6 is bipartite
Bipartite graphs
bipartite
Non- bipartite
Bipartite graphs
Theorem 4:
A simple graph is bipartite if and only if it is possible to assign
one of two different colors to each vertex of the graph so that no
two adjacent vertices are assigned the same color.
a b
• Let color the vertices
Ex: using 2 different colors
C6 is bipartite
• Two adjacent vertices
f c V1 = {a, c, e}
V2 = {b, d, f}
must have different colors
(e.g., red and black)
e d
a
c
d bipartite
b
V1 = {a, e, f}
V2 = {b, c, d}
f
e
Example
Ex: Is the graph G bipartite?
? G is not bipartite
G
A complete bipartite graph K m ,n is a graph that has:
- its vertex set partitioned into two subsets of m and n
vertices,
- 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.
m=3
n=5
K3,5 VK m ,n m n, EK m ,n m.n
Some Complete Bipartite Graphs K m,n
Some Applications of
Special Types of Graphs
New Graphs From Old
New Graphs From Old
DEFINITION:
A subgraph of a graph G = (V, E) is a graph H = (W, F), where
W ⊆ V and F ⊆ E. A subgraph H of G is a proper subgraph of
G if H = G.
New Graphs From Old
DEFINITION:
The union of two simple graphs G1 V1 , E1 and G2 V2 , E2
is the simple graph with vertex set V1 V2 and edge set E1 E2.
The union of G1 and G2 is denoted by G1 G2 .
9.3 Representing Graphs and
Graph Isomorphism
Representing Graphs:
- Adjacency list,
- Adjacent Matrices,
- Incidence Matrices
Isomorphism of Graphs
Representing Graphs
Using Adjacency list: For each vertex u in the graph, there is
a list of vertex v which there is an edge between u and v.
Ex1:
Representing Graphs
Ex2:
Adjacency Matrices
Adjacency matrix.
A = [aij],
where aij = the number of
edges that are associated to {vi, vj}
Ex1:
Adjacency Matrices
Ex2:
Ex3:
For phrase matrices: it is usually preferable to use adjacency lists rather than an
adjacency matrix to represent the graph.
For dense matrices: using an adjacency matrix to represent the graph is usually
preferable over using adjacency lists.
Incidence Matrices
1, where edge e j is incident with vi ,
M nm mij , mij
0, otherwise.
Ex1:
the first column has two 1s. This means the edge e1 is incident with v1 and v4.
Incidence Matrices
Ex2:
the first column has only one 1. This means the edge e1 is a loop.
Representing Graphs
Adjacency matrix
Incidence matrix
edge 1 edge 2 … edge m
Vertex 1
Vertex 2
…
Vertex n
Isomorphism
THE SAME?
Isomorphism
THE SAME ?
Isomorphism
Definition: G1 V1 , E1 and G2 V2 , E2
G1 and G2 are isomorphic if f : V1 V2
oone-to-one and onto
o a, b are adjacent in G if and only if f a and f b
1
are adjacent in G2 , a, b V1.
Such a function f is called an isomorphism.
Ex1:
a b a b ab ac
a a
b c ac ad
c d cd d b
d b bd cb
c d c d
G1 G2
Two graphs are called isomorphic
Isomorphism
Ex2: a
b
a
e b
c
e
d d c
YES
Isomorphism
Ex3:
G H
G and H are NOT isomorphic
(in H: deg(e) = 1, no vertex in G has degree 1)
9.4 Connectivity
Path
Connectedness In Undirected Graphs
Connectedness In Directed Graphs
Paths and Isomorphism
Counting Paths Between Vertices
A path of length n
Let n be a nonnegative integer and G an undirected graph.
A path of length n from u to v: A sequence of n consecutive
edges xo , x1,x1 , x2 , ,xn1 , xn , where xo u , xn v.
Ex: a, e, f, c, d is a simple path of length 4.
Circuits
A circuit is a path of length greater than zero that starts
and ends at the same vertex (u=v).
Ex: c, b, e, a, d, c is circuit.
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.
Path
Ex: Determine the following ordered set of vertices whether it
is a path and if it is a path, what is it’s length?, is it a circuit?, is it
simple?
{a,b,c,f,e} A simple path of length 4
{c,b,d,e,f} Not a path, since {b,d} is not an edge
{a,b,c,d,e,a,b,f} A path of length 7
Connectedness in Undirected Graphs
o Connected = there is a path between every pair of distinct
vertices of the graph.
o Not connected = disconnected.
H is a disconnected with
G is connected 3 connected components
Connectedness in Undirected Graphs
o Connected = there is a path between every pair of distinct
vertices of the graph.
o Not connected = disconnected.
G is connected H is a disconnected with
3 connected components
Connectedness in Undirected Graphs
Cut vertex (articulation point): It’s removal will produce disconnected
subgraph from original connected graph.
Remove b
Remove c
Remove b, c, e
Remove e
Connectedness in Undirected Graphs
Cut edge (bridge): It’s removal will produce subgraphs which are more
connected components than in the original graph
Connectedness in Undirected Graphs
A directed graph is strongly connected if there is a path from
a to b and from b to a whenever a and b are vertices in the graph.
A directed graph is weakly connected if there is a path
between every two vertices in the underlying undirected graph.
Ex: Strongly connected vs weakly connected
G H
G: strongly connected
weakly connected H: weakly connected
connected
Connectedness in Undirected Graphs
STRONG CONNECTED COMPONENTS:
The subgraphs of a directed graph that are strongly connected but
not contained in larger strongly connected subgraphs.
Ex:
H: weakly connected
STRONG CONNECTED COMPONENTS: b, c, d ,a,e
Path and Isomorphism
Using path to determine whether two graphs are isomorphic:
G and H have the same:
- 6 vertices
- 8 edges
- 2 vertices degree 2
- 4 vertices degree 3
But:
H has a loop with minimum length 3
and
G has a loop with minimum length 4
They are not isomorphic.
Path and Isomorphism
Using path to determine whether two graphs are isomorphic:
G and H have the same:
- 5 vertices
- 6 edges
- 2 vertices degree 3
- 3 vertices degree 2
- a loop of length 3
- a loop of length 4
u1 v3 u1u3 v3v1 - a loop of length 5
u2 v5 They are isomorphic.
u1u4 v3v2
u3 v1 u1u5 v3v4
u4 v2 u2u3 v5v1
u5 v4 u3u4 v1v2
Counting Paths Between Vertices
THEOREM 2: Let G be a graph with adjacency matrix A with respect to
the ordering v1 , v2 , , vn of the vertices of the graph (with directed or
undirected edges, with multiple edges and loops allowed). The number
of different paths of length r from vi to v j , where r is a positive integer,
r
equals the (i, j)th entry of A .
Ex: 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
Result = 8
9.5 Euler and Hamilton paths
Can one travel across all the bridges
once and return to the starting point?
LEONHARD EULER
(1707–1783)
Multigraph Model
The Seven Bridges of Königsberg.
9.5 Euler and Hamilton paths
Euler Paths and Circuit
– Paths and circuits contains all Edges of a graph.
Hamilton Paths and Circuit
– Paths and circuits contains all vertices of a graph.
Euler circuit/path
Definition:
oAn Euler circuit in a graph G is a simple circuit containing every
edge of G.
oAn Euler path in G is a simple path containing every edge of G.
Ex:
G1 has an Euler circuit G2 has no an Euler circuit, G3 has no an Euler circuit,
a, b, e, d, c, e, a G2 has no an Euler path But G3 has an Euler path
a, c, d, e, b, d, a, b
Euler circuit
Theorem:
A connected multigraph (with at least two vertices) has an
Euler circuit if and only if each of its vertices has even degree.
Ex: 3
NOT OK
2 2
2
4
2
OK
How to construct an Euler circuit?
procedure Euler (G: connected, every
a d
vertex has even degree)
construct a simple circuit in G
b c H:= G – circuit // remove passed edges
while H has edges
G construct a simple subcircuit in H
e f beginning at a vertex in circuit
H: = H – subcircuit // remove
passed edges
c circuit : = add subcircuit to circuit
{ circuit is an Euler circuit }
e f
H circuit: a, b, c, d, a
subcircuit: c, e, f, c
circuit: a, b, c, e, f, c, d, a
Euler path
Theorem:
A connected multigraph has an Euler path but not an
Euler circuit it has exactly two vertices of odd degree.
Note that: an Euler circuit is also an Euler path
Ex:
- G1 contains exactly two vertices of odd degree, namely, b and d.
So, they are endpoints of Euler path: b,d,c,b,a,d or d,a,b,c,d,b, . . .
- Do similarly on G2 (b and d).
- G3 have six vertices of odd degree. So, G 3 has no Euler path.
Hamilton Paths and Circuits
Hamilton circuit/path:
A simple circuit/path passes through every vertex exactly once.
Ex:
Hamilton circuit: none Hamilton circuit: none
Hamilton circuit:
Hamilton Paths: Hamilton Path: none
(a,b,c,d,e,a)
(a,b,c,d)
(a,b,e,d,c,a)
(a,b,d,c)
...
...
Hamilton Paths and Circuits
There are
NO known simple
necessary and sufficient criteria
for the existence of
Hamilton circuits
Hamilton circuits – sufficient conditions
Dirac’s theorem.
G has a Hamilton circuit
2 3
Ore’s theorem.
G is a graph:
? 3 3
2
•simple
•n ( 3) vertices
•u, v, non-adjacent
deg(u) + deg(v) n
G has a Hamilton circuit
9.6 Shortest Path Problems
Introduction
A Shortest Path Algorithm
The Traveling Salesman Problem
Introduction
weight
Graphs that have a number assigned to each edge
are called weighted graphs.
Introduction
Introduction
Introduction
o The length of a path in a weighted graph: the sum of the weights of
the edges of this path.
o The length of a path in a graph without weights: the number of edges
in this path.
Ex1:
Ex2:
Shortest Path Problems
Ex1:
What is a shortest path in air distance between Boston and Los Angeles?
Ex2:
Which set of telephone lines gives a fastest response time for
communications between San Francisco and New York?
Dijkstra’s algorithm
Ex1: Use Dijkstra’s algorithm to find the length of a shortest path
between the vertices a and z in the weighted graph.
Dijkstra’s algorithm
Dijkstra’s algorithm
Ex1: Use Dijkstra’s algorithm to find the length of a shortest
path between the vertices a and z in the weighted graph.
43aa,c a,ac, bc
810
5
b d
4 6
8
0 a 1 2
a , c , b, d
z 14
13 a, c, b, d , e
3
2
c e
2 aa,,cc,b, d
10
a 12
10
S aaa,,cac,,bc,,db, ed,,ze
SSSS a , c, b
NNNNSSSS edb,,z,zde, e
The length of the
shortest
N S b, c path from a to z is 13.
The shortest path from a to z: a, c, b, d, e, z.
Dijkstra’s algorithm
Ex2: What is the length of a shortest path between a and z
in the weighted graph?
4
3
b c
4 2
3
0 a z
6
1
2
d e
3
2 5
S aa,,bb,,dd,,ee, z
S
S SS a,ab,a,dd
N SS cc,z
N
NNN
S S Sc,be,be,d
The length of the shortest path from a to z is 6.
The shortest path from a to z: a, d, e, z.
Dijkstra’s algorithm
O(n2)
time complexity
The Traveling Salesman Problem
Salesman starts in one city (ex. Detroit). He wants to visit n cities exactly
once and return to his starting point (Detroit). In which order should he
visit theses cities to travel the minimum total distance ?
The Traveling Salesman Problem
- The problem is equivalent to asking for a Hamilton circuit with minimum total weight.
- How many way do we have to examine to solve the problem if there are n vertex in the
graph? Exhaustive search technique
(n 1) n 2 n 2 3.2.1 n 1! O((n-1)!) complexity
2 2
Approximation algorithm
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
Thanks