Graphs
Graphs
INTRODUCTION TO GRAPHS
► A graph G = (V, E) consists of
o V – a nonempty set of vertices (or nodes) ⇛ represents by point
Infinite vertex set – Infinite graph
Finite vertex set – Finite graph
o E – a set of Edges ⇛ represents by line segments or curve
Each edge has either one or two vertices associated with it,
called its endpoints. An edge is said to connect its endpoint.
o The way we draw a graph is arbitrary as long as the correct
connections between vertices are depicted. Avoid crossing edge.
INTRODUCTION TO GRAPHS
► A simple graph contains no loop or parallel edges
o No loop ⇛ Each edge connects two different vertices
o No parallel ⇛ No two edges connect the same pair of vertices
e2 e3 e2 e3
e1 e2 e3
a a c
e1 c
a
simple graph not a simple graph not a simple graph
What is Directed Graph?
INTRODUCTION TO GRAPHS
► A graph directed (or digraph) (V, E) consists of
o V – a nonempty set of Vertices (or nodes) ⇛ represents by point
o E – a set of directed Edges ⇛ represents by line segments or
curve
Each directed edge associated with an ordered pair of
vertices (u, v), which said to start at u and end at v.
GRAPH TERMINOLOGY
• Two vertices u and v in an undirected graph G are called
adjacent (or neighbors) in G if u and v are endpoints of
an edge of G.
• Example:
b
– a and b are adjacent
e2 e3
– a and c not adjacent
e1
– e2 incident with a and b a c
Complete Graph
GRAPH TERMINOLOGY
• The Complete Graph on n vertices, denoted by Kn, is the
simple graph that contains exactly one edge between each
pair of distinct vertices.
• Example: the graph Kn for n = 1, 2, 3, 4, 5, 6 are shown below:
•
K1 K2 K3 K4 K5
Cycles
GRAPH TERMINOLOGY
• The Cycle Cn, n ≥ 3, consists of n vertices v1, v2, … vn and
edges {v1, v2}, {v2, v3}, …, {vn-1,vn} and {vn, v1}.
• Example: the Cn for 3 ≤ n ≤ 6.
C3 C4 C5 C6
GRAPHS CAN HAVE CYCLES BUT TREES DON’T
Representing Graphs
• Adjacency Matrix
– Represents graph by a matrix based on the adjacency
of vertices
Adjacency List (UnDirected Graph)
Adjacency List
Vertex Adjacent Vertices
b
a b, c, e
a c
b a
c a, d, e
e d d c, e
e a, c, d
Adjacency List (Directed Graph)
Adjacency List
a b
Initial Vertex Terminal Vertices
a b, d
b c
c b
d c
d b, c, d
EXERCISE
a b c a b
FIGURE 1 FIGURE 3
d e f
c d e
b
a
FIGURE 2 FIGURE 4
d
a c b c
e
d
Adjacency Matrix (Simple UnDirected Graph)
a b 3 0 1 0 b
A
0 1 1 2 c
c
d
d 1 0 2 0
Adjacency Matrix (Directed Graph)
a b c a b
FIGURE 1 FIGURE 3
d e f
c d e
b
a
FIGURE 2 FIGURE 4
d
a c b c
e
d
Draw Graph from Adjacency Matrix
a b c d e
b
0 1 1 0 1 a
1 0 0 0 0 b
a c
A 1 0 0 1 1 c
0 0 1 0 1 d
1 0 1 1 0 e e d
EXERCISE
a) b)
0 1 1 0 0 3 0 2
1 3 1
0 0 1 0 1
A A
1 0 0 1 0 1 1 2
0 1 1 0 2 1 2 0
c)
0 1 0 0
0 0 2 0
A
0 0 0 0
1 1 1 1
1 2 3 4 E
0 3 0 2 1 X
3 0 1 1 2
A
A M
0 1 1 2 3 P
LE
2 1 2 0 4
1 2 3 4
1
0 1 0 0
0 0 2 0 2
A
0 0 0 0 3
1 1 1 1 4