DS Graphs
DS Graphs
Graphs
Compiled By
Ujjwal Rijal
[email protected]
[email protected] 1
Introduction to
Graph
Graph is a non-linear data structure which contains a set of
points known as nodes (or vertices) and a set of links known as
edges (or arcs), which connects the vertices.
In general, a graph G = (V, E) is a mathematical model that
consists of two non-empty sets i.e. V = {v1 , v2 , v3 , ………. , vn}
called set of vertices and E = {e1 , e2 , e3 , ………. , en} called set
of edges.
Graph is a discrete structure consisting of vertices and edges that
connect these vertices. Problems in almost every conceivable discipline
can be solved using graph model.
Graph was introduced in eighteenth century by the great Swiss
mathematician Leanhard Euler.
[email protected] 2
Applications of
Graph
Graphs are used to solve many problems in many fields as applications.
For example,
• A directed graph (V, E) consists of a set “V” of vertices, a set “E” of edges that are ordered
pairs of elements of “V”.
• In directed graph, loop is allowed but no two vertices can have multiple or parallel edges in
the same direction.
• If the edge pair is unordered, then the graph is called undirected graph.
[email protected] 4
Types of Graph
Simple Graph
• We define a simple graph as 2 – tuple consists of a non empty set of vertices V
and a set of unordered pairs of distinct elements of vertices called edges.
[email protected]
5
Types of Graph
Multi-Graph
• A graph G = (V, E) consists of a set of vertices “V”, a set of edges “E” such that
some of the edges are the parallel or multiple edges is called multi-graph, i.e. this
type of graph can have multiple edges between the same two vertices.
• Two or more edges between same pair of vertices are called parallel or multiple
edges.
[email protected]
7
Types of Graph
Directed Multigraph
• A graph G = (V, E) consists of a set of vertices “V”, a set of edges “E” is said to
be a directed multigraph if “G” has both loops and parallel (or multiple) edges.
[email protected]
8
Graph Models / Graph Terminologies
Order and Size of Graph
• If G = (V, E) be a finite graph then the number of vertices in graph “G” is called order
of graph “G” and the number of edges in graph “G” is called size of graph “G”.
• Let G = (V, E) be a graph and “v” be a vertex of G. The degree (or valency) of v,
denoted by d(v), is the number of edges incident on v.
• A vertex v in graph G = (V, E) is said to be even vertex if its degree is even and a
vertex v in graph G = (V, E) is said to be odd vertex if its degree is odd.
• In a graph G = (V, E), a vertex having degree zero(0) is called isolated vertex and
vertex having degree one (1) [email protected]
called pendant vertex.
9
Graph Models / Graph Terminologies
Degree Sequence of a Graph
• If v1 , v2 , v3 , ….. , vn are the “n” vertices of a graph “G”, then the sequence (d 1 , d2 , d3 , ……. ,
dn) where di = deg(vi), is the degree sequence of the graph “G”.
• In general, we order the vertices so that the degree sequence is monotonically increasing.
• Two vertices u and v in an undirected graph are called adjacent vertices if there is an edge between u
and v.
• Two edges e1 and e2 in an undirected graph are called adjacent edges if they share a common vertex.
• The number of incoming edges to a vertex of a graph G = (V, E) signifies the in-degree of that vertex,
and the number of outgoing edges from a vertex of a graph G = (V, E) signifies the out-degree of
that vertex.
[email protected]
10
Graph Models / Graph Terminologies
Complete Graph
[email protected] 12
K1 K3 K5
Special Types of Graphs
Round-Robin Tournament
• A game tournament where each team plays with each other exactly once is called
Round-Robin tournament.
• Such tournaments can be modelled by using complete graph where there exists
exactly one edge between each possible parts of vertices except itself.
• The following figure shows the tournament between 5 teams in Round-Robin
fashion. Here, in the graph, vertices represent the teams and the edges between
vertices represent the matches between the five teams.
[email protected]
13
Special Types of Graphs
Call Graph
• Graph can be used to model telephone calls in a telecommunication network.
Such type of graph is known as the call graph.
• In such graph, each telephone number is represented by a vertex and each
telephone call is represented by edge.
• The source vertex represents a start call (sender) and the vertex where edge
ends represents the end of the call (receiver).
[email protected]
14
Special Types of Graphs
Regular Graph
• A graph in which all the vertices are of same (equal) degree is called a regular
graph.
• If the degree of each vertex is “r”, then the graph is a r-regular graph.
• In fig (i), bipartite sets are {X1 , X2 , X3 , X4} and {Y1 , Y2 , Y3}.
• In fig (ii), bipartite sets are {1, 3, 7, 9} and {2, 4, 5, 6, 8}.
[email protected]
16
Special Types of Graphs
Complete Bipartite Graph
• A bipartite graph G is said to be complete if each vertex of first bipartite set is
connected to every vertices of another bipartite set.
• A complete bipartite graph with “m” number of vertices in first bipartite set and
“n” number of vertices in second bipartite set is denoted by Km,n.
K3, 3 K2,3
K2, 4 [email protected]
17
Special Types of Graphs
Cycle Graph
• A graph G = (V, E) with vertex n ≥ 3 in which every vertex is connected with two
other vertices one on either side of it and last vertex is connected with first one
to form a closed path is called a cycle graph.
[email protected]
18
The Handshaking Theorem
• The handshaking theorem states that," The sum of the degrees of the vertices of a
graph is equal to the twice the number of edges in that graph.“
Proof:
Consider a graph G = (V, E) with “n” vertices v 1 , v2 , ……. , vn and |E| be the number of
edges in G. Since each edge contributes a degree of 2, the sum of the degrees of all the
vertices is twice the number of edges in G.
i.e. ) = 2 |E|
[email protected]
20
The Handshaking Theorem
Example: Find the sum of degrees of the vertices of the
graph G = (V, E) where V(G) = {v1 , v2 , …. , v5} and E(G) =
{(v1, v2) , (v1, v4) , (v1, v5) , (v2, v3) , (v2, v5) , (v3, v4)}.
Solution:
Number of edges in G = |E| = 6.
Now, by handshaking theorem, Sum of degrees
of vertices in G is given by:
) = 2 |E|
=2x6
= 12.
[email protected]
21
Planar Graph
• A graph G = (V, E) is said to be a planar graph if it can be drawn in a plane such
that no intersection of edges exist at a point other than their common end-point
i.e. a graph is planar if it is drawn without crossing the edge.
• If a plannar graph is drawn in the plane, then the plane is divided into contiguous
regions, called faces.
• A face is characterized by a cycle that forms its boundary.
[email protected]
22
Planar Graph
[email protected]
23
Planar Graph
[email protected]
24
Planar Graph
[email protected]
25
Sub-Graph
• Let G=(V,E) be a graph with vertex set V(G) and edge set E(G) and
H=(V,E) be a graph with vertex set V(H) and edge set E(H), then H is
said to be subgraph of G if:
(i). All the vertices of H are in G.
(ii). All the edges of H are in G.
(iii). Each edge of H has the same end points in H as in G.
d [email protected] 27
Representation of Undirected Graphs
One of the representation method of Graph is Matrix representation,
based on adjacency of vertices ,called adjacency matrix.
Another representation is based on incidence of vertices and edges called
incidence matrix.
And, the another way of representing an undirected graph without
multiple edges and directed graphs is by listing its edges, called
adjacency list.
1. Adjacency Matrix:
Let G=(V,E) be a graph with ‘n’ vertices V1,V2,V3,…….Vn. The
adjacency matrix of G with respect to given ordered list of vertices is a
n*n matrix denoted by A(G), such that
0 if there is no edge between the vertices vi and vj.
aij =
1 if there is an edge between the vertices vi and vj.
k if there are k>=2 edges between the vertices v28i
[email protected]
[email protected] 29
2. Incidence Matrix
[email protected] 30
[email protected] 31
3. Adjacency List
One of the ways of representing a graph without multiple edges is by
listing its edges.
This type of representation is suitable for the undirected graphs without
multiple edges, and directed graphs.
This representation looks like as shown in the below table:
[email protected] 32
Representation of Directed Graphs
[email protected] 33
[email protected] 34
[email protected] 35
[email protected] 36
Matching Theory
A matching graph is a sub-graph of a graph where there are no edges
adjacent to each other.
Simply, there should not be any common vertex between any two edges.
In a matching, no two edges are adjacent i.e. matching theory discusses
about the independent edges.
In a matching,
if deg(V) = 1, then V is said to be matched.
if deg(V) = 0, then V is not matched.
It is because if any two edges are adjacent, then the degree of vertex
which is joining those two edges will have the degree of two vertices,
which violates the matching rule or theory.
[email protected] 37
[email protected] 38
[email protected] 39
Graph Connectivity
1. Walk
o A walk can be defined as a sequence of edges and vertices of a graph. When we have a graph and traverse it,
then that traverse will be known as a walk. In a walk, there can be repeated edges and vertices. The number of
edges which is covered in a walk will be known as the Length of the walk.
So for a walk, the following two points are important, which are described as follows:
• Edges can be repeated
• Vertex can be repeated
In the above graph, there can be many walks, but some of them are described as follows:
1. A, B, C, E, D (Number of length = 4)
2. D, B, A, C, E, D, B, C (Number of length = 7)
3. E, C, B, A, C, E, D (Number of length = 6)
[email protected] 40
Types of Walks
• There are two types of the walk, which are described as
follows:
1.Open walk
2.Closed walk
• Open Walk:
A walk will be known as an open walk in the graph theory if the vertices at
which the walk starts and ends are different. That means for an open walk,
the starting vertex and ending vertex must be different.
• Closed Walk:
A walk will be known as a closed walk in the graph theory if the vertices at
which the walk starts and ends are identical. That means for a closed walk,
the starting vertex and ending vertex must be the same.
[email protected] 41
• In this graph, there is also a closed walk and an open walk, which are described
as follows:
• Closed walk = A, B, C, D, E, C, A
• Open walk = A, B, C, D, E, C
[email protected] 42
2. Trails
• A trail can be described as a walk where no edge is allowed to repeat. In the
trails, the vertex can be repeated.
• In the above graph, there is a tail and closed tail, which is described as follows:
• Open Trail = A, C, H, F, C, B
• Closed Trail = A, C, H, F, C, B, A
[email protected] 43
3. Circuit
• A closed trail in the graph theory is also known as a circuit. So for a circuit, the following two
points are important, which are described as follows:
• Edges cannot be repeated
• Vertex can be repeated
Circuit: A, B, D, C, F, H, C, A
[email protected] 44
4. Cycle
• In cycle, neither edges, nor vertices are allowed to repeat. There is a possibility that only the starting vertex
and ending vertex are the same in a cycle. So for a cycle, the following two points are important, which are
described as follows:
• Edges cannot be repeated
• Vertex cannot be repeated
Cycle: A, B, D, C, A
[email protected] 45
5. Path
• A path is a type of open walk where neither edges nor vertices are allowed to
repeat. There is a possibility that only the starting vertex and ending vertex are
the same in a path. So for a path, the following two points are important,
which are described as follows:
• Edges cannot be repeated
• Vertex cannot be repeated
[email protected] 46
Isomorphism of a Graph
The isomorphism graph can be described as a graph in which a single graph can
have more than one form. That means two different graphs can have the same
number of edges, vertices, and same edges connectivity. These types of graphs are
known as isomorphism graphs. The example of an isomorphism graph is
described as follows:
[email protected] 47
Conditions for Graph isomorphism
• Any two graphs will be known as isomorphism if they
satisfy the following five conditions:
1. There will be an equal number of vertices in the given graphs.
2. There will be an equal number of edges in the given graphs.
3. There will be an equal amount of degree sequence in the given graphs.
4. Identical Mapping.
5. A(G1) = A(G2) with respect to orders of vertices v1 , v2 , ……….. , vn .
Here, A(Gi) stands for the adjacency matrix of the given two graphs.
[email protected] 48
Example-1:
[email protected] 51
Example-4: Verify whether the following given graphs G and H are isomorphic or not.
[email protected] 52
Travelling Salesman Problem (TSP)
• This problem was first formulated in 1930 and it is the most intensively studied
problem in graph theory and optimization.
• The general Travelling Salesman Problem (TSP) problem can be
formulated as:
Given a list of cities (represented by vertex of graph) and distance between
each pair of cities (edges), the problem here is to find out the shortest possible
route or path that the salesman travels each city exactly once and returns to the
origin or starting point (city).
• Example-1: Solve the Travelling Salesman Problem in the given graph where the
weights or costs of the paths are given in the following matrix.
[email protected]
53
[email protected]
54
• Example-2: Solve the Travelling Salesman Problem in the given graph.
5
10
2 10
10
3
Solution:
• First of all, we draw it’s corresponding matrix as follows:
[email protected]
55
[email protected]
56
Euler’s Path
o A connected graph “G” has an Eulerian path or trail if and only if it has
exactly two odd vertices.
o In general, a path in a graph “G” is called Euler’s path if it includes every
edges exactly once.
o Here, vertices can be repeated but edges cannot be repeated.
[email protected] 57
Examples:
[email protected] 58
[email protected] 59
[email protected] 60
Hamiltonian Path / Hamilton Path
o Hamiltonian path or Hamilton path is a path containing each vertex of a
graph “G” exactly once.
o Here, it is not necessary to cover all the edges. The concern is that
we need to cover every vertex exactly once i.e. without repeating.
o In short, in this case, neither vertices nor edges can be repeated.
2.
1.
[email protected] 62
Examples:
4.
3. 4.
[email protected] 63
Examples:
5.
Euler Path = No path (since there are not exactly two odd
vertices).
Euler Circuit = No circuit (since each vertex does not have even
degree).
Hamiltonian Path = v1 v2 v3 v7 v6 v5 v8 v4
Hamiltonian Circuit = v1 v2 v3 v7 v6 v5 v8 v4 v1
[email protected] 64
Graph Coloring
Painting all vertices of a graph with colors such that no two adjacent
vertices have the same color is called coloring of graph.
Chromatic Number: It is defined as the least number of colors
required for coloring of a graph ‘G’.
It is denoted by X(G) or “K”. It is also known as chromaticity of a
graph.
And the graph consisting the “K” chromatic number is known
as the
K-chromatic graph.
[email protected] 65
Examples:
1.What is the chromatic number of complete graph?
A graph G is said to be complete if there exists exactly one edge
between any pair of distinct vertices in the graph G.
5. 6.
9.
70
Exam Scheduling
Example-2: Use graph coloring to schedule exams of 10 subjects by assuming that
the pairs
{(1, 2), (1, 5), (1, 8), (2, 4), (2, 7), (2, 9), (3, 6), (3, 7), (3, 10), (4, 3), (4, 8), (4, 10), (5, 6),
(5, 7)} have common students for the exam.
Solution:
Now, the graph of the above given condition is drawn as follows:
Thus, we get,
Time Courses
Period
I 1, 3, 9 (Green Color)
II 2, 5, 8, 10 (Red Color)
III 4, 6, 7 (Yellow Color)
[email protected] 72
Graph Traversal
o A spanning tree is a subset of graph “G”, which has all the vertices covered
with minimum possible number of edges.
o Hence, a spanning tree does not have cycles and it cannot be disconnected.
o Simply, all the possible trees constructed from the given graph are called
spanning trees of such a graph.
o Example: Find the spanning tree of the following given graph:
[email protected] 79
Spanning Tree
» Removing one edge from the spanning tree will make the graph
disconnected i.e. the spanning tree is minimally connected.
» Adding one edge to the spanning tree will create a circuit or a cycle or a
loop, i.e. the spanning tree is maximally cycle.
[email protected] 80
Spanning Forest
o Spanning Forest consists of many kinds of different spanning trees.
o Note:
If the given graph has “N” vertices, then the resulting MST will have “N-1”
edges.
[email protected] 81
MST finding using Kruskal’s Algorithm
o Kruskal’s algorithm is a MST finding algorithm that takes a graph as
input and finds the subset of the edges of that graphs which:
forms a tree that includes every vertices i.e. connectedness.
has the minimum sum of weights among all the trees that can
be formed from the given graph.
does not contain any cycles.
o It is the procedure for finding a MST of a given graph, which is weighted and
that successively adds edges of least weight that are not already in the tree
such that no edges produce a simple circuit when it is added.
[email protected] 82
Algorithmic Steps of Kruskal’s Algorithm
Step-1: Start
Step-2: Remove all the loops and parallel edges from the given graph, if
any.
Step-3: Arrange or sort all the edges according to their weights in
ascending
(or increasing) order of their edge weights.
Step-4: Choose the edge having minimum edge weight.
Step-5: Reject that edge which forms or creates a cycle.
Step-6: Keep adding edges until we reach all the vertices i.e.
connectedness.
Step-7: Stop
[email protected] 83
Example-1: Find the Minimal Spanning Tree (MST) from
the following graph by using Kruskal’s algorithm.
[email protected] 84
[email protected] 85
[email protected]
86
[email protected] 87
[email protected] 88
Example-2: Find the MST from the following graph
using Kruskal’s Algorithm.
[email protected] 89
Example-3: Find the MST from the following graph
using Kruskal’s Algorithm.
[email protected] 90
MST using Prim's Algorithm
o It is a greedy algorithm that is used to find the minimum spanning tree from
a graph.
o Prim's algorithm finds the subset of edges that includes every vertex of the
graph such that the sum of the weights of the edges can be minimized.
Example-1 of prim's algorithm
• Now, let's see the working of prim's algorithm using an example. It will be
easier to understand the prim's algorithm using an example.
[email protected] 91
• Step 1 - First, if there are any loops and parallel edges, then we remove those
loops and parallel edges, and then we have to choose a source vertex from the
given graph. Let's choose vertex B as the source vertex.
• Step 2 - Now, we have to choose and add the shortest edge from vertex B. There
are two edges from vertex B that are B to C with weight 10 and edge B to D with
weight 4. Among the edges, the edge BD has the minimum weight. So, add it to
the MST.
Note: Remaining edge: BC(10)
[email protected] 92
• Step 3 - Now, current node is ‘D’ .In this case, the possible the edges DE(1) and
DC(2) are such edges + old edge in account BC(10). Now select minimum
weighted edge and add to MST So, selected edge is DE(1) and add it to the MST.
• Note: Remaining edge: BC(10) , DC(2)
[email protected] 93
• Step 4 - Now, current node ‘E’ ,possible travel is EC(6)+ old edges BC(10) , DC(2) select the edge DC(2) which is minimum ,
and add it to the MST.
Note: Remaining edge: EC(6), BC(10)
• Step 5 - Now, current node is ‘C’ , possible travel is CA(3)+Remaining edge: EC(6), BC(10) choose the edge CA(3). Here, we
cannot select the edge CE as it would create a cycle to the graph. So, choose the edge CA and add it to the MST.
• So, the graph produced in step 5 is the minimum spanning tree of the given graph. Since edges EC and BC form cycle, they
are rejected.
Hence, the cost of the MST is: 4 + 2 + 1 + 3 = 10 units.
[email protected] 94
Example-2: Find the MST from the following graph
using Prim’s Algorithm.
[email protected] 95
Example-3: Find the MST from the following graph
using Prim’s Algorithm.
[email protected] 96
Example-4: Find the MST from the following graph
using Prim’s Algorithm.
[email protected] 97
The Shortest Path Algorithm
[email protected] 98
Finding the Shortest Path using Dijkstra’s
Algorithm
[email protected] 100
Example-1: Find the shortest path from source vertex to othe
vertices in the following graph using Dijkstra’s algorithm and
consider vertex “A” as the source vertex.
[email protected] 101
Solution:
[email protected] 102
Example-2: Find the shortest path between a to z in the
following given weighted graph using Dijkstra’s
algorithm.
[email protected] 103
Solution:
[email protected] 104
Example-3: Find the shortest path from source vertex to othe
vertices in the following graph using Dijkstra’s algorithm and
consider vertex “t” as the source vertex.