1. Data Structures
Module – 5 (part 1)
Graphs:Definitions, Terminologies, Matrix and Adjacency List
Representation Of Graphs, Elementary Graph
operations, Traversal methods: Breadth First Search and Depth First
Insertion Sort, Radix sort, Address
Calculation Sort. Hash Table organizations, Hashing Functions, Static
and Dynamic Hashing
Adopted/Modified for :
Data Structures – MCA I Semester
Vidya Vikas Institute of Engineering and Technology
A graph is a non-linear data structure consisting of a
set of vertices/nodes and a set of edges that connect
Graphs provide a powerful tool for modeling and
solving problems in various domains, including
computer networking, social networks, transportation
systems, and data analysis.
4. Graphs - Definition
• A graph G = (V, E), where
• V is the vertex set.
• E is the edge set.
– Vertices are also called nodes and points.
– Each edge connects two different vertices.
– Edges are also called arcs and lines.
5. CHAPTER 6 5
A graph G consists of two sets
– a finite, nonempty set of vertices V(G)
– a finite, possible empty set of edges E(G)
– G(V,E) represents a graph
An undirected graph is one in which the pair of vertices in a edge is
unordered, (v0, v1) = (v1,v0)
A directed graph is one in which each edge is a directed pair of
vertices, <v0, v1> != <v1,v0>
7. CHAPTER 6 7
A complete graph is a graph that has the
maximum number of edges
– for undirected graph with n vertices, the maximum number of
edges is n(n-1)/2
– for directed graph with n vertices, the maximum
number of edges is n(n-1)
– example: G1 is a complete graph
8. CHAPTER 6 8
Adjacent and Incident
If (v0, v1) is an edge in an undirected graph,
– v0 and v1 are adjacent
– The edge (v0, v1) is incident on vertices v0 and v1
If <v0, v1> is an edge in a directed graph
– v0 is adjacent to v1, and v1 is adjacent from v0
– The edge <v0, v1> is incident on v0 and v1
12. CHAPTER 6 12
self edge multigraph:
of the same edge
Restrictions on Graph
13. CHAPTER 6 13
A subgraph of G is a graph G’ such that V(G’)
is a subset of V(G) and E(G’) is a subset of E(G)
A path from vertex vp to vertex vq in a graph G,
is a sequence of vertices, vp, vi1, vi2, ..., vin, vq,
such that (vp, vi1), (vi1, vi2), ..., (vin, vq) are edges
in an undirected graph
The length of a path is the number of edges on it
Subgraph and Path
14. CHAPTER 6 14
1 2 3
1 2 0
(i) (ii) (iii) (iv)
(a) Some of the subgraph of G1
(i) (ii) (iii) (iv)
(b) Some of the subgraph of G3
15. CHAPTER 6 15
A simple path is a path in which all vertices, except possibly the first
and the last, are distinct
A cycle is a simple path in which the first and the last vertices are the
In an undirected graph G, two vertices, v0 and v1, are connected if
there is a path in G from v0 to v1
An undirected graph is connected if, for every pair of distinct vertices
vi, vj, there is a path from vi to vj
Simple Path and Cycle
22. CHAPTER 6 22
A connected component of an undirected graph is a maximal connected
A maximal subgraph is a subgraph within a larger graph that cannot be
extended by adding any more vertices or edges while maintaining the
A tree is a graph that is connected and acyclic.
25. CHAPTER 6 25
A graph with two connected components
G4 (not connected)
connected component (maximal connected subgraph)
26. CHAPTER 6 26
A directed graph is strongly connected if
there is a directed path from vi to vj and also
from vj to vi.
A strongly connected component is a
maximal subgraph that is strongly connected.
28. CHAPTER 6 28
The degree of a vertex is the number of edges incident
to that vertex
For directed graph,
– the in-degree of a vertex v is the number of edges
that have v as the head
– the out-degree of a vertex v is the number of edges
that have v as the tail
– if di is the degree of a vertex i in a graph G with n vertices
and e edges, the number of edges is
( ) /
30. CHAPTER 6 30
ADT for Graph
structure Graph is
objects: a nonempty set of vertices and a set of undirected edges, where each
edge is a pair of vertices
functions: for all graph ∈ Graph, v, v1 and v2 ∈ Vertices
Graph Create()::=return an empty graph
Graph InsertVertex(graph, v)::= return a graph with v inserted. v has no
Graph InsertEdge(graph, v1,v2)::= return a graph with new edge
between v1 and v2
Graph DeleteVertex(graph, v)::= return a graph in which v and all edges
incident to it are removed
Graph DeleteEdge(graph, v1, v2)::=return a graph in which the edge (v1, v2)
Boolean IsEmpty(graph)::= if (graph==empty graph) return TRUE
else return FALSE
List Adjacent(graph,v)::= return a list of all vertices that are adjacent to v
32. CHAPTER 6 32
1. Adjacency Matrix
Let G=(V,E) be a graph with n vertices.
The adjacency matrix of G is a two-dimensional n by n array,
If the edge (vi, vj) is in E(G), adj_mat[i][j]=1
If there is no such edge in E(G), adj_mat[i][j]=0
The adjacency matrix for an undirected graph is symmetric;
The adjacency matrix for a digraph need not be symmetric
34. CHAPTER 6 34
Merits of Adjacency Matrix
From the adjacency matrix, it is easy to determine the
connection of vertices.
The degree of a vertex i is
(that is, the sum of ith
For a digraph, the row sum is the out_degree, while
the column sum is the in_degree
n − 1
adjmat [i ][ j ]
ind ( vi )=∑
A [ j ,i ] outd ( vi )=∑
A [ i , j ]
35. CHAPTER 6 35
2. Adjacency List
The n rows of the adjacency matrix are represented as n
chains (either linked lists or array).
That is, replace each row of the adjacency matrix with a
Every vertex in G will have one list.
36. CHAPTER 6 36
Data Structures for Adjacency Lists
Each row in adjacency matrix is represented as an adjacency list.
37. CHAPTER 6 37
An undirected graph with n vertices and e edges ==> n head nodes and 2e list nodes
39. CHAPTER 6 39
degree of a vertex in an undirected graph
–# of nodes in adjacency list
# of edges in a graph
–determined in O(n+e)
out-degree of a vertex in a directed graph
–# of nodes in its adjacency list
in-degree of a vertex in a directed graph
–More complex. Traverse the whole data structure
40. CHAPTER 6 40
Inverse Adjacency Lists
Useful for finding in-degree of a vertex in
Contains one list for each vertex
Each list contains a node for each vertex
adjacent to the vertex that the list represents.
Maintained in addition to
41. CHAPTER 6 41
Adjacency Lists – Orthogonal Representation
Alternatively, we can use orthogonal representation
Change the node structure of the adjacency list to
42. CHAPTER 6 42
In many applications, edges of a graph have weights
assigned to them
Distance or cost or time or delay
In adjacency matrix, the weights are shown.
In adjacency list, nodes have an additional field.
44. Elementary Graph Operations
Just like traversing a tree, we would like to visit all
vertices in a graph.
Just like tree traversal are the most frequently used
operation, in graph also traversal is the most
frequently used operation
Given an undirected graph G = (V, E), and a vertex,
∈ , we wish to visit all vertices in G that are
reachable from v,
That is, all vertices that are connected to v.
45. CHAPTER 6 45
Some Graph Operations
Given G=(V,E) and vertex v, find all w V, such
that w connects v.
– Depth First Search (DFS)
similar to preorder tree traversal
– Breadth First Search (BFS)
similar to level order tree traversal
47. Depth First Search
Depth First Search (DFS) is a graph traversal
algorithm that explores a graph by traversing as far
as possible along each branch before backtracking.
It traverses the graph in a depthward motion and
uses a stack (either explicit or implicit in the recursive
implementation) to keep track of the visited vertices.
It is easy to implement recursively.
48. CHAPTER 6 48
Depth First Search
adjacency list: O(e)
adjacency matrix: O(n2
49. Depth-First Search Example
Start search at vertex 1.
Label vertex 1 and do a depth first search
from either 2 or 4.
Suppose that vertex 2 is selected.
62. Depth First Search
If we represent G by its adjacency lists, then we can
determine the vertices adjacent to v by following a chain of
Since DFS examines each node in the adjacency lists at
most once, the time to complete the search is O(e).
If we represent G by its adjacency matrix, then determining
all vertices adjacent to v requires O(n) time.
Since we visit atmost n vertices, the total time is O(n2
63. Depth First Search
DFS is a fundamental graph traversal algorithm and
forms the basis for many other algorithms and
applications in graph theory and graph-related
DFS has several applications, including:
Graph Traversal, Connected Components, Cycle
Detection, Path Finding, Topological Sorting (linear
ordering of the vertices that respects the direction of
64. Breadth First Search
Breadth First Search (BFS) is a graph traversal
algorithm that explores all the vertices of a graph in
It starts at a chosen vertex and systematically
explores all its neighboring vertices before moving on
to the next level of vertices.
Uses a dynamically linked queue.
Each queue node contains vertex and link fields.
68. Breadth First Search
BFS explores the graph in a level-by-level manner,
meaning it visits all the vertices at the same distance
(level) from the starting vertex before moving on to
the next level.
BFS guarantees that it will visit all reachable vertices
in the graph and provides the shortest path for
unweighted graphs. However, it requires additional
memory to maintain the queue data structure.
69. Breadth First Search
BFS can be used to solve various graph-related problems,
Shortest Path, Connected Components, Bipartite Graphs,
Web Crawling, : Explore web pages in a systematic
manner, starting from a given webpage and following links
to other pages.
Social Network Analysis: Explore social networks and
analyze properties such as finding friends of friends,
identifying clusters or communities, or measuring
distances between individuals.