CH 5 - Graph Theory

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 20

CS104 : Discrete Structures

Chapter V
Graph Theory

07/15/24 Prepared by Dr. Zakir H. Ahmed 1


What is a Graph?
 A Graph, G = (V, E), is a data structure which consists of a
nonempty set of vertices (or nodes), V, and a set of edges, E,
that connect (some of) them.
 The set of vertices V of a graph G may be infinite.
 A graph with infinite vertex set is called infinite graph.
 A graph with finite vertex set is called finite graph.
 For example,
1 3
Vertex
Edge 2
(Node)
4
5
 V = {1, 2, 3, 4, 5}
 E = { (1,2), (1,3), (1,4), (2,3), (3,5), (4,5) }

07/15/24 Prepared by Dr. Zakir H. Ahmed 2


Application of Graphs
 Computer Networks
Computer

 Electrical Circuits Resistor/Inductor/…

 Road Map

City
07/15/24 Prepared by Dr. Zakir H. Ahmed 3
Graph Categorization
 An Undirected Graph is a graph where the edges have no
directions. 3
1
 The edges in an undirected graph 2
are called Undirected Edges 4
5
 For example, G = (V, E), where
 V = {1, 2, 3, 4, 5} and E = {(1,2), (1,3), (1,4), (2,3), (3,5), (4,5)}
 A Directed Graph or Digraph is a graph where each edge has a
direction. 1
6
 The edges in a digraph 2
4

are called Arcs or Directed Edges 3 5


 For example, G = (V, E), where V = {1, 2, 3, 4, 5, 6} and
E = {(1,4), (2,1), (2,3), (3,2), (4,3), (4,5), (4,6), (5,3), (6,1), (6,5)}
 The edge (1, 4) = 1→4 where 1 is the tail and 4 is the head
07/15/24 Prepared by Dr. Zakir H. Ahmed 4
Graph Categorization
 A graph with both directed and undirected edges is called a
mixed graph. 1 3
2
 Adjacent vertices: If (u, v) is an edge 4
5
of the graph, then the nodes u and v are adjacent.
 An edge (u, v) is incident to vertices u and v.
 For example, Vertices 1 and 2 are adjacent,
1 40 3
but vertices 2 and 5 are not adjacent
10 60 2 20
 A Weighted Graph is a graph where 4
70
all the edges are assigned weights. 50 5
 If the same pair of vertices have more 1
than one edges (multiple edges), that
graph is called a Multigraph 2 3

07/15/24 Prepared by Dr. Zakir H. Ahmed 5


Graph Categorization
 Loop or self edges: An edge (u, u) is called a self edge or a
loop. 1 3
 ( 1,1 ) and ( 4,4 ) are self edges 2
4
 Graphs that may include loops, 5
and possibly multiple edges connecting same pair of nodes, are
called pseudographs.
 In simple graphs (or graphs) loops and multiple edges are not
permitted
 The complete graph on n vertices, Kn, is the simple graph that
contains exactly one edge between each pair of distinct nodes.
1 2 1
1
1 2 2 3
2 3
3 4 4 5
07/15/24 Prepared by Dr. Zakir H. Ahmed 6
Graph Categorization
 The cycle Cn, n≥3, consists of n vertices v1, v2, ….., vn, and edges
(v1, v2), (v2, v3), … , (vn-1, vn), and (vn, v1).
 That is, Cn consists of n vertices and n edges. 1
1 2
1 2 3

2 3 3 4 4 5

 A simple graph G is called bipartite if its vertex set can be


partitioned into two disjoint sets V1 and V2 such that every
edge in the graph connects a vertex in V1 and a vertex in V2.
When this condition holds, we call the pair (V1, V2) a bipartition
of the vertex set V of G.

07/15/24 Prepared by Dr. Zakir H. Ahmed 7


Graph Categorization
 Example 1:- C6 is bipartite, because V1 V2
its vertex set can be partitioned v1 . . v2
into two disjoint sets V1={v1,v3,v5} v3 . . v4
v5 . . v6
and V2={v2,v4,v6}, and every edge
of C6 connects a vertex in V1 and a vertex in V2.
 Example 2:- K3 is not bipartite, because if we divide the vertex
set into two disjoint sets, one set must contain two vertices. If
the graph were bipartite, these two vertices could not be
connected by an edge, but in K3 each vertex is connected to
every other vertex by an edge.
 Theorem:- A simple graph is bipartite if and only if it is
possible to assigns one of two different colors to each vertex
of the graph so that no two adjacent vertices are assigned the
same color.
07/15/24 Prepared by Dr. Zakir H. Ahmed 8
Graph Categorization
 A Subgraph of graph G=(V,E) is a graph H=(U,F) such that U⊆ V
and F ⊆ E. A subgraph H is a proper subgraph of G if H≠G.
1 3 1 3
2
4 2
5

H=(U,F)
G=(V,E)

 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. For example, 3 3 1
1 3
2 2
2
4 4
4 5 5

G2 G1 U G 2
G1

07/15/24 Prepared by Dr. Zakir H. Ahmed 9


Graph Terminology
 Degree of a Vertex : In an undirected graph, the no. of edges
incident to the vertex. Degree of vertex v is denoted by deg(v).
 For example, deg(1)=3, deg(2)=2, 1 3
2
deg(3)=2, deg(4)=1 and deg(5)=0. 4
 A vertex of degree zero is called isolated vertex. 5

 A vertex of degree one is called pendant vertex.


 For example, vertex 4 is pendant and vertex 5 is isolated.
 The Handshaking Theorem: - Let G=(V, E) be an undirected
graph. Then 2|E| = ∑deg(v), ∀v ∈ V
 Example 3:- How many edges are there in a graph with 10
vertices each of degree six?
 Solution:- Because the sum of the degrees of the vertices is
6x10=60, it follows that 2|E|=60. Therefore, |E|=30.
07/15/24 Prepared by Dr. Zakir H. Ahmed 10
Graph Terminology
 In diagram, there are two types of degree of a vertex -
in-degree and out-degree
 In-degree: In-degree of a vertex v, denoted by deg-(v),
is the no. of edges entering the vertex in a digraph
 Out-Degree: Out-degree of a vertex v, denoted by deg+(v), is
the no. of edges leaving the vertex 1 3
 For example, deg-(1)=3 and deg+(1)=1. 2
4
deg (2)=1 and deg (2)=1.
- + 5
 Theorem:- For a digraph G=(V, E), ∑deg-(v)+∑deg+(v)=|E|, ∀v∈V

07/15/24 Prepared by Dr. Zakir H. Ahmed 11


Representation of Graphs
 Adjacency Matrix:- The Adjacency Matrix A=(ai,j) of a graph
G=(V,E) with n nodes is an nXn zero-one matrix. Element of A
can be defined as follows:
aij = 1, if (vi, vj) Є E,
= 0, otherwise
 Example 4: Find the adjacency matrices of the following
graphs. 1 3
5 4 3 2 1
1 3 2
0 1 1 1 0 1
2 0 0 1 0 1 2 3 2 1
4 1 0 0 1 1 3 0 1 0 1
5
1 0 0 0 1 4 1 0 0 2
0 1 1 0 0 5 0 1 1 3

07/15/24 Prepared by Dr. Zakir H. Ahmed 12


Representation of Graphs
 Adjacency Matrix of a Weighted Graph:- The weight of the
edge can be shown in the matrix when the vertices are
adjacent
 A nil value (0 or ∞) depending on the problem is used when
they are not adjacent 9
1 3 3 2 1
 Example 5: Adjacency matrix
4 5 ∞ 4 ∞ 1
2
to find the minimum distance 5 ∞ ∞ 2
between nodes ∞ 5 9 3
 Example 6: Draw graph with 3 2 1
the adjacency matrix 9 1 ∞ 1 9
3 1
∞ ∞ 1 2
1 2 8
∞ 8 ∞ 3

07/15/24 Prepared by Dr. Zakir H. Ahmed 13


Representation of Graphs
 Adjacency List:- An Adjacency list is an array of lists, each
list showing the vertices a given vertex is adjacent to….
 Example 7: Find the adjacency list of the following graph.
1 3 1 2
2 1 3
2
3 1 5
4 1 5
4
5
5

 Adjacency List of a Weighted Graph:- The weight is included


in the list
 Example 8: Find the adjacency list of the following graph.
9 3 2 4
1 1
2 3 5
4 2 5
3 1 9 2 5

07/15/24 Prepared by Dr. Zakir H. Ahmed 14


Representation of Graphs
 Incidence Matrix:- Let G=(V,E) be a graph with v1, v2, …, vn are
vertices and e1, e2, …., vm are edges. Then the incidence matrix
with respect to this ordering of V and E is the nxm matrix
M=[mij], where
mij = 1, when edge ej is incidence on vi,
= 0, otherwise
 Example 9: Find the incidence matrices of the following
e1 graphs.
e2 v3 v1 v3
v1 e6 e5 e4 e3 e2 e1
0 0 0 1 1 1 v1 e2 v2 e3
e3 e1 v2 e4
e5 0 0 1 0 0 1 v2
v4 e3 e2 e1
e6 0 1 1 0 1 0 v3
v5 0 0 1 v1
1 0 0 1 0 0 v4 1 1 0 v2
1 1 0 0 0 0 v5 0 0 0 v3
07/15/24 Prepared by Dr. Zakir H. Ahmed 15
Isomorphism of Graphs
 Def:- The simple graphs G1=(V1, E1) and G2=(V2, E2) are isomorphic
if there is one-to-one and onto function f from V1 to V2 with the
property that a and b are adjacent in G1 if and only if f(a) and
f(b) are adjacent in G2, for all a, b in V1. Such a function f is
called an isomorphism.
 Example 10: Show that the graphs G=(V, E) and H=(U, F) are
v1 v2 u1 u2
isomorphic.
G
v3 v4 u3 H u4

 Proof:- The function f with f(v1)=u1, f(v2)=u4, f(v3)=u3, and f(v4)=u2


is a one-to-one correspondence between V and U. Next, to verify
that this function preserves adjacency also. Adjacency vertices
in G are v1 and v2, v1 and v3, v2 and v4, and v3 and v4. Also, the pairs
f(v1)=u1 and f(v2)=u4, f(v1)=u1 and f(v3)=u3, f(v2)=u4 and f(v4)=u2,
f(v3)=u3, and f(v4)=u2 are Prepared
07/15/24 adjacent inH. Ahmed
by Dr. Zakir H. 16
Connectivity
 A a path a sequence of edges that begins at a vertex of a graph
and travels from vertex to vertex along the edges of the graph.
 There can be more than one path between two vertices.
 Vertex u is reachable from v if there is a path from u to v.
 Paths from 5 to 1 are 5, 3, 1 and 5, 3, 2, 1 1 3
 A Simple Path is a path, where all the 2
4
vertices are distinct. 5
 For example, 1,4,5,3 is a simple path, but 1,4,5,4 is not
 Length of a path is the no. of edges considered in the path.
 Length of the path 1,4,5,3 is 3
 A circuit is a path whose first and last vertices are the same.
 For example, the path 3,2,1,4,5,3 is a circuit

07/15/24 Prepared by Dr. Zakir H. Ahmed 17


Euler Paths
 A Cycle is a circuit where all the vertices are distinct except
for the first (and the last) vertex.
 For example, 1,4,5,3,1 is a cycle, but 1,4,5,4,1 is not a cycle.
1 3
 Hamiltonian Cycle: A Cycle that contains all
the vertices of the graph. 2
4
 1,4,5,3,2,1 is a Hamiltonian Cycle. 5
 A graph is said to be connected if there is at least one path
from every vertex to every other vertex in the graph.
 An Euler circuit in a graph G is a simple circuit containing
every edge of G. An Euler path in G is a simple path containing
every edge of G. 1 2 1 2
 For example, G has an Euler 5 5
circuit, but H does not have 3 4 3 4
G H
07/15/24 Prepared by Dr. Zakir H. Ahmed 18
Spanning Tree
 Tree: A connected undirected graph that contains no cycles
 Forest: A graph that does not contain a cycle
1 3 1 3 1 3
2 2
4 4 4
5 5 5
Tree Tree Forest

 The Spanning Tree of a Graph G is a subgraph of G that is a


tree and contains all the vertices of G.
1 3 3
1
2 2
4 4
5 5
Graph Spanning Tree

07/15/24 Prepared by Dr. Zakir H. Ahmed 19


End of
Chapter V

07/15/24 Prepared by Dr. Zakir H. Ahmed 20

You might also like