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
Search.
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
Mysore
2022-23
3. Graphs
A graph is a non-linear data structure consisting of a
set of vertices/nodes and a set of edges that connect
these vertices.
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.
u v
5. CHAPTER 6 5
Definition
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>
tail head
7. CHAPTER 6 7
Complete Graph
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
0 2
1
(a)
2
1
0
3
(b)
self edge multigraph:
multiple occurrences
of the same edge
Figure 6.3
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
0 0
1 2 3
1 2 0
1 2
3
(i) (ii) (iii) (iv)
(a) Some of the subgraph of G1
0 0
1
0
1
2
0
1
2
(i) (ii) (iii) (iv)
(b) Some of the subgraph of G3
0
1 2
3
G1
0
1
2
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
same
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
subgraph.
A maximal subgraph is a subgraph within a larger graph that cannot be
extended by adding any more vertices or edges while maintaining the
subgraph's properties.
A tree is a graph that is connected and acyclic.
Connected Component
25. CHAPTER 6 25
A graph with two connected components
1
0
2
3
4
5
6
7
H1
H2
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.
Connected Component
28. CHAPTER 6 28
Degree
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
e di
n
( ) /
0
1
2
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
incident edge.
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)
is removed
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,
say adj_mat
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
row)
For a digraph, the row sum is the out_degree, while
the column sum is the in_degree
∑
j=0
n − 1
adjmat [i ][ j ]
ind ( vi )=∑
j =0
n −1
A [ j ,i ] outd ( vi )=∑
j= 0
n− 1
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
linked list
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
G1
0
1 2
3
0
1
2
An undirected graph with n vertices and e edges ==> n head nodes and 2e list nodes
39. CHAPTER 6 39
Interesting Operations
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
digraphs
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
adjacency list.
0
1
2
41. CHAPTER 6 41
Adjacency Lists – Orthogonal Representation
Alternatively, we can use orthogonal representation
Change the node structure of the adjacency list to
0
1
2
42. CHAPTER 6 42
Weighted Graph
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,
v V
∈ , 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
Traversal
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
Data structure:
adjacency list: O(e)
adjacency matrix: O(n2
)
49. Depth-First Search Example
Start search at vertex 1.
2
3
8
10
1
4
5 9
11
6
7
Label vertex 1 and do a depth first search
from either 2 or 4.
1
2
Suppose that vertex 2 is selected.
51. Depth-First Search Example
2
3
8
10
1
4
5 9
11
6
7
Label vertex 5 and do a depth first search
from either 3, 7, or 9.
1
2
2
5
5 9
Suppose that vertex 9 is selected.
52. Depth-First Search Example
2
3
8
10
1
4
5 9
11
6
7
Label vertex 9 and do a depth first search
from either 6 or 8.
1
2
2
5
5 9
9
8
Suppose that vertex 8 is selected.
54. Depth-First Search Example
2
3
8
10
1
4
5 9
11
6
7
1
2
2
5
5 9
9
8
8
Label vertex 6 and do a depth first search from either 4 or 7.
6
6
4
Suppose that vertex 4 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
links.
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
problems.
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
the edges).
64. Breadth First Search
Breadth First Search (BFS) is a graph traversal
algorithm that explores all the vertices of a graph in
breadthward motion.
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,
including:
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.