MT131 Tutorial - 7 Graphs
MT131 Tutorial - 7 Graphs
Graphs
Graphs and Graph Models
2
Summary
8
Directed Graphs
Definition 2: A directed graph (or digraph) (V, E)
consists of a nonempty set of vertices V and a set
of directed edges (or arcs) E. Each directed edge
is associated with an ordered pair of vertices.
The directed edge associated with the ordered pair
(u, v) is said to start at u and end at v. When a
directed graph has no loops and has no multiple
directed edges, it is called a simple directed
graph.
9
Simple Directed Graph
10
Directed Multigraph
Directed graphs that may have multiple directed edges from a
vertex to a second (possibly the same) vertex are to used
model such networks. We call such graphs directed
multigraphs.
Graph Terminology: Summary
To understand the structure of a graph and to build a graph model, we
ask these questions:
• Are the edges of the graph undirected or directed (or both)?
• If the edges are undirected, are multiple edges present that connect
the same pair of vertices? If the edges are directed, are multiple
directed edges present?
• Are loops present?
12
Graph Terminology
13
Undirected Graph: Adjacency
14
Undirected Graph: Degree of a Vertex
15
Handshaking Theorem
deg(v) 2 E
v V
16
Handshaking Theorem
Example 1: How many edges are there in a graph with
ten vertices each of degree 6?
Solution: Sum of the degrees of the vertices 6×10 =
60. Therefore: 60 = 2|E| , |E| = 30.
18
Directed Graph: Adjacency
• Let G be a directed (possibly multi-) graph, and
let e be an edge of G that is (u ,v). Then we say:
u v
– u is adjacent to v, v is adjacent from u
– e comes from u, e goes to v.
– e connects u to v, e goes from u to v
– The initial vertex of e is u
– The terminal vertex of e is v
19
Directed Graph: Degree of a vertex
20
Directed Handshaking Theorem
v V
2 v V
• Note that the degree of a node is unchanged by
whether we consider its edges to be directed or
undirected.
21
Example
deg+(a) = 3 deg−(a) = 3
deg+(b) = 3 deg−(b) = 1
deg+(c) = 0 deg−(c) = 1
deg+(d) = 0 deg−(d) = 0
deg+(e) = 1 deg−(e) = 2
deg+(f ) = 2 deg−(f ) = 2
deg+(g) = 1 deg−(g) = 1
23
Complete Graphs
• For any n N, a complete graph on n vertices,
Kn , is a simple graph with n nodes in which every
node is adjacent to every other node:
u , v V : u v {u , v} E.
K1 K2 K3 K4
K5 K6
K2,3 K3,5
Note that Km,n has m+n vertices and mn edges.
Cycles
• For any n 3, a cycle on n vertices, Cn , is a
simple graph where V = {v1,v2, … ,vn} and
E = {{v1, v2}, {v2, v3}, …, {vn1, vn}, {vn , v1}}.
C3 C4 C5 C6 C8
C7
Note that Cn has n vertices and n edges.
26
Wheels
• For any n 3, a wheel Wn , is a simple graph
obtained by taking the cycle Cn and adding one
extra vertex vhub and n extra edges {{vhub, v1}, {vhub,
v2}, …, {vhub, vn}}.
W3 W4 W5 W6 W8
W7
Note that Wn has n+1 vertices and 2n edges.
27
n -Cubes (Hypercubes)
• For any n N, the hypercube Qn is a simple
graph consisting of two copies of Qn-1 connected
together at corresponding nodes. Q0 has 1 node.
Q0 Q1 Q2 Q3 Q4
Note that Qn has 2n vertices and n2n − 1 edges.
28
Graph Representations
• Graph representations:
– Adjacency Lists.
– Adjacency Matrices.
– Incidence Matrices.
29
Undirected: Adjacency Lists
• A table with 1 row per vertex, listing its adjacent
vertices.
a b Adjacent
Vertex
Vertices
c d a b, c
e
f b a, c, e, f
c a, b, f
d -
e b
f c, b
30
Undirected: Adjacency Matrices
• Matrix A = [aij], where aij is 1 if {vi ,vj } is an edge
of G, 0 otherwise.
a b a b c d e
a 0 1 1 0 0
c e b1 0 1 0 1
d
c 1 1 0 0 0
d 0 0 0 0 0
e
0 1 0 0 0
31
Example 1
1 1 1 0 a b
1 0 0 1
A
1 0 0 1
0 1 1 0 d c
32
Example 2
33
Example 3
Let A be the adjacency matrix of a graph G with vertices a, b, c,
d . Find the degrees of the vertices of G.
0 1 1 1
1 0 1 0
A
1 1 0 0
Answer: The degrees of thevertices a, b, c and d of G are 3, 2, 2
and 1, respectively. 1 0 0 0
34
Directed Adjacency Lists
• 1 row per node, listing the terminal nodes of each
edge incident from that node.
Initial Terminal
vertex vertices
V1 V2, V4, V5
V2 V4
V3 V5
V4
V5 V2
35
Directed Adjacency Matrices
36
Incidence Matrices
• It is a matrix A = [aij], where aij is 1 if the edge ej
is incident with the vertex vi , 0 otherwise.
1 e1 2 e1 e2 e3 e4
e2 e4 1 1 1 0 0
e3
3 4 2 1 0 1 1
5 3 0 1 1 0
4 0 0 0 1
50 0 0 0
37
Incidence Matrices
38
Example
Let A be the incident matrix of a graph G. Draw G.
0 1 1 0 1
0 1 0 1 0
A
1 0 1 0 0
1 0 0 1 1
e2
a b
Let G = {V, E}, e3 e4
e5
where V= {a, b, c, d} and
E = {e1, e2, e3, e4, e5}. d c
e1
Connectivity
40
Connectivity
• Simple path: a, d, c, f, e
The edges are {a, d }, {d, c}, {c, f }, {f, e}
Length = 4
• Circuit: b, c, f, e, b
The edges are {b, c},{c, f }, {f, e},{e, b}
Length = 4
• Not a path: d, e, c, a because {e, c} is not an edge
• Not a simple path: a, b, e, d, a, b because {a, b} appears
twice, Length = 5
41
Paths in Directed Graphs
b d
42
Undirected Graphs: Connectedness
43
Connected Components
• A graph that is not connected is the union of two
or more connected subgraphs with no vertices in
common. These disjoint connected subgraphs are
called the connected components of the graph.
44
Directed Graphs: Connectedness
• Definition 1:
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.
• Definition 2:
A directed graph is weakly connected if there is a
path between every two vertices when the
direction of edges are disregarded.
45
Example
a b a b
c d c d
46
Shortest-Path Problems*
47
Example*
Flight mileage
48
Example*
Flight times
Example*
Flight Fares
Question*
Path length
abcz 4+3+2=9
abez 4+3+1=8
adez 2+3+1=6 (the shortest path)
The Traveling Salesman Problem*