Session 12 - Graph Theory
Session 12 - Graph Theory
International University
School of Computer Science and Engineering
Session 9
Graph Theory
Dr. Nguyen Van Sinh
[email protected]
Part 1: Graphs and Graph Models:
Basics, Concepts and Terminologies
1. Types of Graphs
2. Basic Terminology
3. Some Special Simple Graphs
4. New Graphs from Old Graphs
2
1. Type of Graphs
Simple Graph
Definition 1. A simple graph G = (V, E) consists of V, a
nonempty set of vertices, and E, a set of unordered pairs of
distinct elements of V called edges.
Detroit
New York
San Francisco
Chicago
Denver Washington
Los Angeles
What are the vertices and edges? 3
Detroit
New York
San Francisco
Chicago
Denver Washington
Los Angeles
SET OF VERTICES
Chicago
Denver Washington
Los Angeles
What are the edges?
SET OF EDGES
6
Multigraph, A Non-Simple Graph
Chicago
Denver Washington
Los Angeles
Detroit
New York
San Francisco
Chicago
Denver Washington
Los Angeles
8
Pseudograph, A Non-Simple Graph
There can be telephone lines in the network from a computer
to itself (for diagnostic use). Detroit
New York
San Francisco
Chicago
Denver
Washington
Los Angeles
n In a pseudograph G = (V, E) two or more edges may connect
the same pair of vertices,
n and in addition, an edge may connect a vertex to itself.
9
Loops
Detroit
New York
San Francisco
Chicago
Denver
Washington
Los Angeles
pseudographs
multigraphs
simple graphs
11
A Directed Graph
l In a directed graph G = (V, E ) the edges are
ordered pairs of (not necessarily distinct)
vertices.
Detroit
New York
Chicago
San Francisco
Denver Washington
Los Angeles
Some telephone lines in the network may operate in only
one direction .
12
A Directed Graph
The telephone lines in the network that operate in two
directions are represented by pairs of edges in opposite
directions.
Detroit
New York
Chicago
San Francisco
Denver Washington
Los Angeles
13
A Directed Multigraph
In a directed multigraph G = (V, E ) the edges are ordered
pairs of (not necessarily distinct) vertices, and in addition
there may be multiple edges.
Detroit
New York
Chicago
San Francisco
Denver Washington
Los Angeles
15
2. Basic Terminology Adjacent Vertices
16
Degree of a vertex
c d
b deg( d ) = 1
deg( e ) = 0
a g f e
17
Degree of a vertex
Definition 1. The degree of a vertex in an
undirected graph is the number of edges
incident with it, except that a loop at a vertex
contributes twice to the degree of that vertex.
c d
deg( b ) = 6 b
a g f e
18
Degree of a vertex
c d
deg( b ) = 6 deg( d ) = 1
b
deg( e ) = 0
a g f e
19
Degree of a vertex
deg( e ) = 0
a g f e
20
Degree of a vertex
Find the degree of all the other vertices.
deg( a ) = 2 deg( c ) = 4 deg( f ) = 3 deg( g ) = 4
TOTAL of degrees = 2 + 4 + 3 + 4 + 6 + 1 + 0 = 20
c d
deg( b ) = 6 deg( d ) = 1
b
deg( e ) = 0
a g f e
21
Degree of a vertex
Find the degree of all the other vertices.
deg( a ) = 2 deg( c ) = 4 deg( f ) = 3 deg( g ) = 4
TOTAL of degrees = 2 + 4 + 3 + 4 + 6 + 1 + 0 = 20
TOTAL NUMBER OF EDGES = 10
c d
b deg( d ) = 1
deg( e ) = 0
a g f e
22
Handshaking Theorem
23
3. Some Special Simple Graphs
K4
K5
Complete graph Kn
24
3. Some Special Simple Graphs
C4
C5
Cycle Cn
25
3. Some Special Simple Graphs
W4
W5
Wheele Wn
26
4. New Graphs from Old Graphs
K5 C5
C5 is a subgraph of K5
27
4. New Graphs from Old Graphs
Definition 7. The union of 2 simple graphs G1 = (V1 , E1 )
and G2 = (V2 , E2 ), denoted by G1 È G2 , is the simple
graph with vertex set V = V1 È V2 and edge set E = E1 È E2 .
c
c
c
b d
b d b d f
f
e
a e a e a
S5 C5 W5
W5 is the union of S5 and C5
28
Part 2: Representing Graphs,
Connectivity, and Euler Paths
1. Representing Graphs
2. Adjacency Matrices
3. Incidence Matrices
4. Isomorphism of Graphs
5. Paths
6. Connectedness
7. Euler Paths and Circuits
29
1. Representing Graphs
30
2. Adjacency Matrix
1 if { vi, vj } is an edge in G,
aij =
0 otherwise.
31
Finding the adjacency matrix
32
Finding the adjacency matrix
TO
c a b c d e f
FROM
a
b d 0 1 0 0 1 1
f b
c
a e
d
W5 e
f
a 0 1 0 0 1 1
b d
f b 1 0 1 0 0 1
c 0 1 0 1 0 1
a e
d
W5 e
f
c FROM a b c d e f
a 0 1 0 0 1 1
b d
b 1 0 1 0 0 1
f c 0 1 0 1 0 1
a e
d 0 0 1 0 1 1
W5 e 1 0 0 1 0 1
f 1 1 1 1 1 0
Notice that this matrix is symmetric. i.e. aij = aji 36
4. Graph Isomorphism
37
4. Graph Isomorphism
Note. Isomporphic simple graphs must have the same invariants:
üThe number of vertices
üThe number of edges
üThe degrees of the vertices
No vertex of deg 1
b b deg(e) = 1
c a
a c
d
e d e
Non-isomorphic graphs 38
5. Connectivity-Paths
Definition 1. A path of length n from u to v in
an undirected graph is a sequence of edges
e1 = {u , x1}, e2 = {x1 , x2 }, . ., en = {xn – 1 , v}.
c
One path from a to e
b d This path passes through
f
vertices f and d in that order.
a e
The length of this path is 3
W5
39
One path from a to a
c
This path passes through vertices f,
d, e, in that order. It has length 4.
b d
f
It is a circuit because it begins and
a e ends at the same vertex.
b d
f
YES
a e
W5
41
6. Connectedness in Undirected Graphs
42
Counting Paths between Vertices
Theorem. Let G be a graph with adjacency
matrix A with respect to the ordering v1 , v2 ,
. . . , vn .
The number of different paths of length r
from vi to vj , where r is a postive integer,
equals the entry in row i and column j of Ar.
43
Example 0 1 1 0
a b 1 0 0 1
A=
1 0 0 1
c 0 1 1 0
d
8 0 0 8
Thus there are 8 0 8 8 0
different paths of 4
length 4 joining a
A =
0 8 8 0
and d
8 0 0 8
44
7. Euler Paths
Problem. The town of Königsberg was divided into four
sections by the branch of the Pregel River
45
7. Euler Paths
A
D
B C
Euler modeled this problem using the
multigraph:
ü four sections correspond to four A
vertices A, B, C, D. D
c
48
7. Euler Paths
deg(e) = 0 + 2 + … =2+2=4
49
7. Euler Paths
C
Example. All vertices of the multigraph
corresponding to the Königsberg
problem have odd degree so there is no A D
Euler circuit. Thus the Problem has no
solution B
Example. All four vertices of this graph have dgree 3
b
a
So there is no Euler circuit.
53
7. Algorithm for finding Euler Circuit
Example: Given G=(V, E) as follows:
a) Prove G is an Euler graph.
b) Find Euler Circuit C of G
a
b c
d e f
g h i j
54
7. Algorithm for finding Euler Circuit
Solution:
a) G is connected and all vertices have even
degree.
a
b c
d e f
g h i j
55
7. Algorithm for finding Euler Circuit
Solution:
b) Starting from vertex a,
C = (a, b, d, g, h, i, j, f, c, a).
a
b c
d f
g h i j
56
7. Algorithm for finding Euler Circuit
Solution:
- Delete edge on C out of G
a
b c
d e f
g h i j
- Delete the alone vertices: a, g, j. 57
7. Algorithm for finding Euler Circuit
Solution:
After deleting the alone vertices: a, g, j.
The remaining graph is G1
b c
d e f
h i
58
7. Algorithm for finding Euler Circuit
C and G1 have the same vertex b.
Sort C starting from b b c
C=(b, d, g, h, i, j, f, c, a, b).
e f
G1 has circuit starting from b, d
C1=( b, c, e, b).
h i
Connect C1 into C,
C=(b, d, g, h, i, j, f, c, a, b, c, e, b).
59
7. Algorithm for finding Euler Circuit
The remaining
graph is G2
d e f
h i
60
7. Algorithm for finding Euler Circuit
C and G2 have sharing vertex d.
Sort C starting from d, d e f
C= (d, g, h, i, j, f, c, a, b, c, e, b, d).
On G2 has circuit starting from d, h i
C2 = ( d, e, f, i, e, h, d).
Connect C2 into C,
C=(d, g, h, i, j, f, c, a, b, c, e, b, d, e, f, i, e, h, d).
G3=Ø, C is the final Euler circuit that we want to find.
61
7. Algorithm for finding Euler Circuit
C=(a, b, c, e, b, d, e, f, i, e, h, d , g, h, i, j, f, c, a).
a
b c
d e f
g h i j
62
CW Assignment.
l 1. Find the number of edges, number of vertices,
and degree of each vertex:
63
Homework.
l Pages 608-609: 6, 8, 10
l Pages 618-619:10, 20, 58
l Pages 644-645: 14, 16, 20
l Implement the algorithm
EulerCircuit.C/C++ following the
description in slides 53 to 62.
Deadline: before 9:00PM May 12th 2018
64