DSLec 13
DSLec 13
DSLec 13
By
Syed Ijaz Hussain Bukhari
Introduction to graphs
INTRODUCTION:
Graph theory plays an important role in
several areas of computer science such as:
– switching theory and logical design
– artificial intelligence
– formal languages
– computer graphics
– operating systems
– compiler writing
– information organization and retrieval.
GRAPH
• A graph is a non-empty set of points called vertices
and a set of line segments joining pairs of vertices
called edges.
• Formally, a graph G consists of two finite sets:
• (i) A set V=V(G) of vertices (or points or nodes)
• (ii)A set E=E(G) of edges; where each edge
corresponds to a pair of vertices.
v1 e1 v2
e4
e3 e2 e5 v5
v3
v4 e6
v3
v4 e6
1. An edge connects either one or two vertices called its endpoints (edge e1
connects vertices v1 and v2 described as {v1, v2} i.e v1 and v2 are the endpoints
of an edge e1).
2. An edge with just one endpoint is called a loop. Thus a loop is an edge that
connects a vertex to itself (e.g., edge e6 makes a loop as it has only one
endpoint v3).
3. Two vertices that are connected by an edge are called adjacent; and a vertex
that is an endpoint of a loop is said to be adjacent to itself.
4. An edge is said to be incident on each of its endpoints(i.e. e1 is incident on v1
and v2 ).
5. A vertex on which no edges are incident is called isolated (e.g., v5)
EXAMPLE
Define the following graph formally by specifying
its vertex set, its edge set, and a table giving the edge
endpoint function.
e1
v1 v2
e2 v4
v3
e3
SOLUTION:
Vertex Set = {v1, v2, v3, v4}
Edge Set = {e1, e2, e3}
Edge - endpoint function is:
Edge Endpoint
e1 {v1, v2}
e2 {v1, v3}
e3 {v3}
EXAMPLE
For the graph shown below
(i) find all edges that are incident on v1;
(ii)find all vertices that are adjacent to v3;
(iii)find all loops;
(iv)find all parallel edges; e1 e e3
v2
(v)find all isolated vertices; v1
2
e6
e4
e7
e5 v3
v5
v4
SOLUTION
(i) v1 is incident with edges e1, e2 and e7
(ii) vertices adjacent to v3 are v1 and v2
(iii) loops are e1 and e3
(iv) only edges e4 and e5 are parallel
(v) The only isolated vertex is v4 in this Graph
DRAWING PICTURE FOR A GRAPH:
Draw picture of Graph H having vertex set {v1, v2, v3, v4, v5} and
edge set {e1, e2, e3, e4} with edge endpoint function
Edge Endpoint
e1 {v1}
SOLUTION:
e2 {v2,v3} Given V(H) = {v1, v2, v3, v4, v5}
e3 {v2,v3}
and E(H) = {e1, e2, e3, e4}
e4 {v1,v5}
with edge endpoint function
SIMPLE GRAPH
A simple graph is a graph that does not have any loop or
parallel edges.
EXAMPLE: e
v1 1 v2
e2 e3 e4 v5
v4 v3
It is a simple graph H
V(H) = {v1, v2, v3, v4, v5} & E(H) = {e1, e2, e3, e4}
EXERCISE
Draw all simple graphs with the four vertices {u, v, w, x} and two edges, one
of which is {u, v}.
SOLUTION:
There are C(4,2) = 6 ways of choosing two vertices from 4
vertices. These edges may be listed as:
{u,v},{u,w},{u,x},{v,w}, {v,x},{w,x}
One edge of the graph is specified to be {u,v}, so any of the remaining five
from this list may be chosen to be the second edge. This required graphs
are:
2. 4.
1. 3.
u v u v u v 5.
u v
u v
w x w x
w x w x
w x
DEGREE OF A VERTEX:
Let G be a graph and v a vertex of G. The degree
of v, denoted deg(v), equals the number of
edges that are incident on v, with an edge that
is a loop counted twice.
Note:
(i)The total degree of G is the sum of the degrees
of all the vertices of G.
(ii) The degree of a loop is counted twice.
EXAMPLE:
For the graph shown
v2
. v1
e1 e2
v3
e3
d
c
OR
c
d
The vertices a, b, c, d have degrees 1,2,3, and 4
respectively(i.e graph exists).
v3
v2 v3 v4
K4 K5
EXERCISE
For the complete graph Kn, find
(i) the degree of each vertex
(ii)the total degrees
(iii)the number of edges
SOLUTION:
(i) Each vertex v is connected to the other (n-1) vertices in Kn; hence deg (v) = n
- 1 for every v in Kn.
(ii)Each of the n vertices in Kn has degree n - 1; hence, the total degree in
Kn = (n - 1) + (n - 1) + … + (n - 1) n times
= n (n - 1)
(iii)Each pair of vertices in Kn determines an edge, and there are C(n, 2) ways of
selecting two vertices out of n vertices. Hence,
Number of edges in Kn = C(n, 2) n(n 1)
2
REGULAR GRAPH
A graph G is regular of degree k or k-regular if every
vertex of G has degree k.
In other words, a graph is regular if every vertex has
the same degree.
Following are some regular graphs.
(iii) 2-regular
EXERCISE
Draw two 3-regular graphs with six vertices.
SOLUTION
a
a b
f b
f c
e c
d d
e
BIPARTITE GRAPH
A bipartite graph G is a simple graph whose vertex set can be
partitioned into two mutually disjoint non empty subsets A
and B such that the vertices in A may be connected to vertices
in B, but no vertices in A are connected to vertices in A and no
vertices in B are connected to vertices in B.
The following are bipartite graphs
A v1 v2 v3 A B
v1 v4
v2 v5
v3 v6
B
v4 v5
DETERMINING BIPARTITE GRAPHS
The following labeling procedure determines whether
a graph is bipartite or not.
1. Label any vertex a
2. Label all vertices adjacent to a with the label b.
3. Label all vertices that are adjacent to a vertex just
labeled b with label a.
4. Repeat steps 2 and 3 until all vertices got a distinct
label (a bipartite graph) or there is a conflict i.e., a
vertex is labeled with a and b (not a bipartite graph).
EXERCISE
• Find which of the following graphs are
bipartite. Redraw the bipartite graph so that
its bipartite nature is evident.
SOLUTION
The graph is not bipartite
b b
a a,b (conflict)
(ii) b a
a b
By labeling procedure, each vertex gets a distinct
label. Hence the graph is bipartite. To redraw
the graph we mark labels a’s as a1, a2 and b’s
as b1 and b2. Redrawing graph with bipartite
nature evident.
b3 a1
b1
a1
b1 b2
a2
a2 b2 b3
COMPLETE BIPARTITE GRAPH
A complete bipartite graph on (m+n) vertices denoted Km,n is a
simple graph whose vertex set can be partitioned into two
mutually disjoint non empty subsets A and B containing m and
n vertices respectively, such that each vertex in set A is
connected (adjacent) to every vertex in set B, but the vertices
within a set are not connected.
K2,3 K3,3