Module 4
Module 4
MODULE – 4
Chapter 11: Graphs
What are we studying in this chapter?
Definitions
Terminologies
Matrix and Adjacency List Representation of Graphs
Elementary Graph operations
Traversal methods:
Breadth First Search
Depth First Search
11.1 Introduction
In this chapter, let us concentrate another important and non-linear data structure called
graph. In this chapter, we discuss basic terminologies and definitions, how to represent
graphs and how graphs can be traversed.
11.2 Graph Theory terminology
First, let us see “What is a vertex?”
Definition: A vertex is a synonym for a node. A vertex is normally represented by a
circle. For example, consider the following figure:
1
3
2 4
0
Fig Vertices
In the above figure, there are four nodes identified by 1, 2, 3, 4. They are also called
vertices and normally denoted by a set V = {1, 2, 3, 4}.
Now, let us see “What is an edge?”
Definition: If u and v are vertices, then an arc or a line joining two vertices u and v is
called an edge.
Example 1: Consider the figure: 6
1
0
11.2 Graphs
G = (V, E)
where V is set of vertices and E is set of edges. For example, consider the graph shown
below:
5
4 E = { <1, 6>, <1, 2>, <2, 3>, <4, 3>, <5, 3>, <5, 6>,
2 0 <6, 4> } is set of directed edges
Note:
3 |V| = |{1, 2, 3, 4, 5, 6}| = 6 represent the number of
vertices in the graph.
Data Structures using C - 11.3
|E| = |{<1, 6>, <1, 2>, <2, 3>, <4, 3>, <5, 3>, <5, 6>, <6, 4> }| = 7 represent the
number of edges in the graph.
Definition: A graph G = (V, E) in which every edge is directed is called a directed graph.
The directed graph is also called digraph. A graph G = (V, E) in which every edge is
undirected is called an undirected graph. Consider the following graphs:
multiple edges
Definition: A graph with multiple occurrence of the 1 4
same edge between any two vertices is called multigraph. 0
Here, there are two edges between the nodes 1 and 4 and
there are three edges between the nodes 4 and 3.
2 3
Now, let us see “What is a complete graph?”
Definition: A graph G = (V, E) is said to be a complete graph, if there exists an edge
between every pair of vertices. The graph (a) below is complete. Observe that in a
complete graph of n vertices, there will be n(n-1)/2 edges. Substituting n = 4, we get 6
edges. Even if one edge is removed as shown in graph (b) below, it is not complete graph.
1 4 1 4
0 0
2 3 2 3
Complete graph Not a complete graph
2 3 2 3
In the graph, the path from vertex 1 to 4 In the graph, the path from vertex 1 to 3 is
is denoted by: 1, 2, 3, 4 which can also be denoted by 1, 4, 2, 3 which can also be
written as (1, 2), (2, 3), (3, 4). written as <1, 4>, <4, 2>, <2, 3>
Data Structures using C - 11.5
Now, let us see “What is simple path?”
Definition: A simple path is a path in which all vertices except possibly the first and last
are distinct. Consider the undirected and directed graph shown below:
1 4 1 4
0 0
2 3 2 3
Ex 1: In the graph, the path 1, 2, 3, 4 is Ex 1: In the graph, the path 1, 4, 2, 3 is
simple path since each node in the simple path since each node in the
sequence is distinct. sequence is distinct.
Ex2: In the graph, the path 1, 2, 3, 2 is not Ex 2: The sequence 1, 4, 3 is not a path
a simple path since the nodes in sequence since there is no edge <4, 3> in the graph.
are not distinct. The node 2 appears twice
in the path
Definition: The length of the path is the number of edges in the path.
Ex 1: In the above undirected graph, the path (1, 2, 3, 4) has length 3 since there are three
edges (1, 2), (2, 3), (3, 4). The path 1, 2, 3 has length 2 since there are two edges (1, 2),
(2, 3).
Ex 2: In the above directed graph, the path <1, 2, 3, 4> has length 3 since there are three
edges <1, 2>, <2, 3>, <3, 4>. The path <1, 4, 2> has length 2 since there are two edges
<1, 4>, <4, 2>.
For example, the path <4, 2, 3, 4> shown in above directed graph is a cycle, since the first
node and last node are same. It can also be represented as <4, 2>, <2, 3>, <3, 4> <4, 2>.
Note: A graph with at least one cycle is called a cyclic graph and a graph with no cycles
is called acyclic graph. A tree is an acyclic graph and hence it has no cycle.
For example, the graphs shown in figure below are connected graphs.
1 4 1 4
0 0
2 3 2 3
Figure Connected graphs
Definition: Let G = (V, E) be a graph. If there exists at least one vertex in a graph that
cannot be reached from other vertices in the graph, then such a graph is called
disconnected graph. For example, the graph shown below is a disconnected graph.
1 4 Since vertex 1 is 1 4
0 not reachable 0
5 from 3, the graph
0 is not connected
2 3 graph. 2 3
Not connected
Adjacency matrix
Representation of graph
Adjacency linked list
Definition: Let G = (V, E) be a graph where V is set of vertices and E is set of edges. Let
N be the number of vertices in graph G. The adjacency matrix A of a graph G is formally
defined as shown below:
Data Structures using C - 11.7
It is clear from the definition that an adjacency matrix of a graph with n vertices is a
Boolean square matrix with n rows and n columns with entries 1’s and 0’s (bit-
matrix)
In an undirected graph, if there exists an edge (i, j) then a[i][j] and a[j][i] is made 1
since (i, j) is same as (j, i)
In a directed graph, if there exists an edge <i, j> then a[i][j] is made 1 and a[j][i] will
be 0.
If there is no edge from vertex i to vertex j, then a[i][j] will be 0.
Note: The above definition is true both for directed and undirected graph. For example,
following figures shows the directed and undirected graphs along with equivalent
adjacency matrices:
0 1 2 3
0 1
0 0 1 1 1
0
1 0 0 1 0
2 0 0 0 1
2 3 3 0 1 0 0
0 1 2 3
0 1
0 0 0 1 1 0
1 1 0 1 1
2 1 1 0 1
2 3 3 0 1 1 0
Note: It is clear from the above definition that if i, j and k are the vertices adjacent to the
vertex u, then i, j and k are stored in a linked list and starting address of linked list is
stored in A[u] as shown below:
A
.
.
u i j k
.
.
For example, figures below shows the directed and undirected graphs along with
equivalent adjacency linked list:
A
0 1 3
0 2 1 nodes adjacent to 0
0
1 2 nodes adjacent to 1
2 3 2 3 nodes adjacent to 2
3 1 nodes adjacent to 3
A
0 1 2 nodes adjacent to 0
0 1
0
1 0 2 3 nodes adjacent to 1
2 3 2 0 1 3 nodes adjacent to 2
3 1 2 nodes adjacent to 3
Note: So, based on the nature of the problem and based on whether the graph is sparse or
dense, one of the two representations can be used.
Let us see “How the weighted graph can be represented?” The weighted graph can be
represented using adjacency matrix as well as adjacency linked list. The adjacency matrix
consisting of costs (weights) is called cost adjacency matrix. The adjacency linked list
consisting of costs (weights) is called cost adjacency linked list. Now, let us see “What is
cost adjacency matrix?”
Definition: Let G = (V, E) be the graph where V is set of vertices and E is set of edges
with n number of vertices. The cost adjacency matrix A of a graph G is formally defined
as shown below:
10 0 1 2 3
0 1
0 0 ∞ 10 20 ∞
15
20 25 1 ∞ ∞ 15 ∞
2 ∞ ∞ ∞ 30
2 3 3 ∞ 25 ∞ ∞
30 Note: Diagonal values
can be replaced by 0’s
(a) Weighted graph (b) Adjacency matrix
For the undirected graph, the elements of the cost adjacency matrix are obtained using the
following definition:
The undirected graph and its equivalent adjacency matrix is shown below:
1 0 1 2 3
0
0 0 ∞ 25 10 ∞
15 1 25 ∞ 15 20
10 20
2 10 15 ∞ 30
2 3 3 ∞ 20 30 ∞
30 Diagonal values can
be replaced by 0’s
(a) (b)
Figure: Weighted undirected graph and the adjacency matrix
Note: The cost adjacency matrix for the undirected graph is symmetric (i.e., a[i, j] is
same as a[j, i]) whereas the cost adjacency matrix for a directed graph may not be
symmetric.
Note: For some of the problems, it is more convenient to store 0’s in the main diagonal of
cost adjacency matrix instead of ∞.
Data Structures using C - 11.11
Now, let us see “What is cost adjacency linked list?”
Definition: Let G = (V, E) be a graph where V is set of vertices and E is set of edges
with n number of vertices. A cost adjacency linked list is an array of n linked lists. For
each vertex u V, A[u] contains the address of a linked list. All the vertices which are
adjacent from vertex u are stored in the form of a linked list (in an arbitrary manner) and
the starting address of first node is stored in A[u]. If i, j and k are the vertices adjacent to
the vertex u, then i, j and k are stored in a linked list along with the weights in A[u] as
shown below:
A
.
.
u k, wk
i, wi j, wj
.
.
For example, the figure below shows the weighted diagraph and undirected graph along
with equivalent adjacency list.
10 0 1,10 2,20
0 1
15 0 1
20 2,15
25
2 3,30
2 3
30
3 1,25
10 0 1,10 2,20
0 1
15 0 1 0,10 2,15 3,25
20 25
2 0,20 1,15 3,30
2 3 1,25 2,30
30
3
Now, the function to read an adjacency matrix can be written as shown below:
Now, we concentrate on a very important topic namely graph traversal techniques and
see “What is graph traversal? Explain different graph traversal techniques”
Definition: The process of visiting each node of a graph systematically in some order
is called graph traversal. The two important graph traversal techniques are:
Definition: The breadth first search is a method of traversing the graph from an
arbitrary vertex say u. First, visit the node u. Then we visit all neighbors of u. Then
we visit the neighbors of neighbors of u and so on. That is, we visit all the
neighboring nodes first before moving to next level neighbors. The search will
terminate when all the vertices have been visited.
Now, let us take an example and see how BFS traversal can be used to see what are
all the nodes which are reachable from a given source vertex.
Example 11.3: Traverse the following graph by breadth-first search and print all the
vertices reachable from start vertex a. Resolve ties by the vertex alphabetical order.
f b c g
d a e
Initialization: Insert source vertex a into queue and add a to S as shown below:
Initialization - - a a
Step 1 a b, c, d, e a, b, c, d, e b, c, d, e
Step 2 b a, d, f a, b, c, d, e, f c, d, e, f
Step 3 c a, g a, b, c, d, e, f, g d, e, f, g
Step 4 d a, b, f a, b, c, d, e, f, g e, f, g
Step 5 e a,g a, b, c, d, e, f, g f, g
Step 6 f b, d a, b, c, d, e, f, g g
Step 7 g c. e a, b, c, d, e, f, g empty
The above activities are shown below in the form of an algorithm along with
pseudocode in C when graph is represented as an adjacency matrix.
Example 11.4: C function to show the nodes visited using BFS traversal
s[u] = 1; // insert u to s
printf(“%d “, u); // print the node visited
while ( f <= r )
{
u = q[f++]; // delete an element from q
for (v = 0; v < n; v++)
{
if (a[u][v] == 1) // If v is adjacent to u
{
if (s[v] == 0) // If v is not in S i.e., v has not been visited
{
printf(“%d “, v); // print the node visited
s[v] = 1; // add v to s, mark it as visited
q[++r] = v; // Insert v into queue
}
}
}
}
printf(“\n”);
}
Now, the C program that prints all the nodes that are reachable from a given source
vertex is shown below:
Example 11.5: Algorithm to traverse the graph using BFS
#include <stdio.h>
/* Insert: Example 11.1: Function to read an adjacency matrix*/
/* Insert: Example 11.4: Function to traverse the graph in BFS */
Data Structures using C - 11.17
void main()
{
int n, a[10][10], source, i, j;
Now, let us see how to obtain the nodes reachable from each node of the following
graph using the above program:
0 1 2 3
0 1 0 0 1 1 0
0
1 0 0 1 1
2 0 0 0 1
2 3 3 0 0 0 0
Output
Enter the number of nodes: 4
Enter the adjacency matrix:
0110
0011
0001
0000
The nodes visited from 0: 0 1 2 3
The nodes visited from 1: 1 2 3
The nodes visited from 2: 2 3
The nodes visited from 3: 3
11.18 Graphs
Example 11.7: C function to show the nodes visited using BFS traversal
s[u] = 1; // insert u to s
printf("%d ", u); // print the node visited
while ( q != NULL ) // as long as queue is not empty
{
u = q->info; // delete a node from queue
q = delete_front(q);
Now, the complete C program to see the nodes reachable from each of the nodes in
the graph can be written as shown below:
Example 11.8: Program to print nodes reachable from a vertex (bfs using adjacency list)
#include <stdio.h>
#include <stdlib.h>
struct node
{
int info;
struct node *link;
};
read_adjacency_list(a, n);
Input
Enter the number of nodes: 4
Enter the number of nodes adjacent 0: 2
Enter nodes adjacent to 0: 1 2
Output
The nodes visited from 0: 0 1 2 3
The nodes visited from 1: 1 2 3
The nodes visited from 2: 2 3
The nodes visited from 3: 3
Definition: In DFS, a vertex u is picked as source vertex and is visited. The vertex u
at this point is said to be unexplored. The exploration of the vertex u is postponed and
a vertex v adjacent to u is picked and is visited. Now, the search begins at the vertex
11.22 Graphs
v. There may be still some nodes which are adjacent to u but not visited. When the
vertex v is completely examined, then only u is examined. The search will terminate
when all the vertices have been examined.
Note: The search continues deeper and deeper in the graph until no vertex is adjacent
or all the vertices are visited. Hence, the name DFS. Here, the exploration of a node is
postponed as soon as a new unexplored node is reached and the examination of the
new node begins immediately.
Design methodology The iterative procedure to traverse the graph in DFS is shown
below:
Step 1: Select node u as the start vertex (select in alphabetical order), push u onto
stack and mark it as visited. We add u to S for marking
Step 3: Repeat step 1 and step 2 until all the vertices in the graph are considered
Example 11.9: Traverse the following graph using DFS and display the nodes reachable
from a given source vertex
f b c g
d a e
Data Structures using C - 11.23
Solution: Since vertex a is the least in alphabetical order, it is selected as the start
vertex. Follow the same procedure as we did in BFS. But, there are two changes:
Instead of using a queue, we use stack
In BFS, all the nodes adjacent and which are not visited are considered. In DFS,
only one adjacent which is not visited earlier is considered. Rest of the procedure
remains same.
Now, the graph can be traversed using DFS as shown in following table
The recursive function can be written as shown below: (Assuming adjacency matrix a,
number of vertices n and array s as global variables)
11.24 Graphs
Example 11.10: Program to print nodes reachable from a vertex (dfs - adjacency matrix)
void dfs(int u)
{
int v;
s[u] = 1;
printf("%d ", u);
for (v = 0; v < n; v++)
{
if (a[u][v] == 1 && s[v] == 0) dfs(v);
}
}
The complete program that prints the nodes reachable from each of the vertex given in
the graph can be written as shown below:
Example 11.11: Program to print nodes reachable from a vertex (dfs - adjacency matrix)
#include <stdio.h>
Example 11.12: Program to print nodes reachable from a vertex (dfs - adjacency list)
void dfs(int u)
{
int v;
NODE temp;
s[u] = 1;
printf("%d ", u);
for (temp = a[u]; temp != NULL; temp = temp->link)
if (s[temp->info] == 0) dfs(temp->info);
}
11.26 Graphs
The complete program that prints the nodes reachable from each of the vertex given in
the graph using DFS represented using adjacency list can be written as shown below:
Example 11.13: Program to print nodes reachable from a vertex (dfs - adjacency matrix)
#include <stdio.h>
#include <stdlib.h>
struct node
{
int info;
struct node *link;
};
NODE a[10];
int s[10], n; // Global variables
Input
Enter the number of nodes: 4
Enter the number of nodes adjacent 0: 2
Enter nodes adjacent to 0: 1 2
Output
The nodes visited from 0: 0 1 2 3
The nodes visited from 1: 1 2 3
The nodes visited from 2: 2 3
The nodes visited from 3: 3
Exercises
d a e
14) Write a C function to show the nodes visited using BFS traversal (adjacency matrix)
15) Write a C function to show the nodes visited using BFS traversal (adjacency list)
16) What is depth first search (DFS)?”
17) Traverse the following graph using DFS and display the nodes reachable from a given
source vertex
f b c g
d a e
18) Write a program to print nodes reachable from a vertex (dfs - adjacency matrix)
19) Write a program to print nodes reachable from a vertex (dfs - adjacency matrix)