Graph
Graph
● Introduction to Graphs
IT205: Data Structures (AY 2024/25 Sem II Sec B) — Dr. Arpit Rana
Graph: Definition
A graph G is a non-linear data structure made up of a set of nodes (vertices, i.e., V) and a set of
edges (arcs, i.e., E) that connect them.
● Example, G = (V, E):
○ V = {A, B, C, D}
○ E = {(A,B), (A,C), (A,D), (B,D), (C,D)}
When we characterize the running time of a graph algorithm on a given graph G = (V, E), we
usually measure the size of the input in terms of
Hence, the input is denoted using two parameters and not just one.
● If the pair of nodes that make up the edges are ordered pairs, the graph is said to be a
directed graph (or digraph), otherwise undirected graph.
● The graph in which from one node we can visit any other node in the graph is known as a
connected graph.
○ The graph in which from each node there is an edge to each other node is known as
fully connected (or complete) graph..
● The graph in which at least one node is not reachable from a node is known as a
disconnected graph.
● A graph in which each edge is associated with a weight given by a weight function w: E→R
is known as a weighted graph.
○ Weight typically shows cost of traversing, for example, weights are distances
between cities
● A graph in which vertex can be divided into two sets such that vertex in each set does not
contain any edge between them is known as bipartite graph.
Two structures are similar in the sense that they represent connectivity among the nodes
● They are different in the sense that tree is acyclic whereas a graph usably have cycle(s).
An undirected
graph G (V, E)
with | V | = 5,
and | E | = 7.
The sum of all
adjacency lists
is 2.| E |.
A directed
graph G (V, E)
with | V | = 6,
and | E | = 8.
The sum of all
adjacency lists
is | E |.
● A potential disadvantage of adjacency list is that it provides no quicker way to find whether
an edge (u, v) is present in a graph G. (it take 𝚯 (|V| + |E|) time).
● In case of unweighted graphs, adjacency matrix require just one bit per entry (0 or 1) which
makes it more space efficient for smaller graphs.
● Given a graph G = (V, E) and a source vertex s, BFS explores the edges of G to “discover”
every vertex that is reachable from s.
○ It computes the distance from s to each reachable vertex v: the smallest number of
edges needed to go from s to v.
○ Starting from s, the algorithm first discovers all neighbors of s which have distance 1,
then discovers vertices with distance 2, and so on, until it has discovered every vertex
reachable from s.
● A vertex is enqueued and dequeued at most once which takes O (1) time. So, the total time
devoted to queue operations is O (|V|).
● The procedure scans the adjacency list of each vertex when the vertex is dequeued, so, at
most once.
○ The sum of the lengths of all | V | adjacency lists is 𝚯 (|E|), total time spent in
scanning adjacency lists is O (|V| + |E|).
Alternatively, a graph can be traversed using Depth-First Search (similar to in-order traversal of
the tree). I am leaving this topic as DIY exercise..
● Insertion
○ To insert a vertex and hence establishing connectivity with other vertices in the
existing graph.
○ To insert an edge between two vertices in the graph.
● Deletion
○ To delete a vertex from the graph.
○ To delete an edge from the graph.
● Merging
○ To merge two graphs G1 and G2 into a single graph.