08 Graphs
08 Graphs
Module 8
Graphs
4
1 Connected Disconnected 1
4
2 3
2 3 2 3
Directed Undirected Mixed
Department of Electronics And Electrical Engineering, IIT Guwahati
Graphs - Types
Graphs
Weighted Unweighted
1 1
3 5
8
2 3 2 3
Weighted Undirected
2 3 2 3
Unweighted Unweighted
Directed Undirected
1 1
3 5 3
5
8 8
2 3 2 3
4 Weighted
Weighted Undirected
Directed
Department of Electronics And Electrical Engineering, IIT Guwahati
Graphs - Types
5 6 Graphs 1
4
4 1 Sparse Dense 5
2 3
Dense
Complete
1
2 3 • Sparse graphs have relatively fewer edges 4
compared to the maximum possible edges. 5
Sparse
• Dense graphs have a large number of
edges, often close to the maximum
"sparsity" of a graph refers 2
possible.
to how many edges it has 3
relative to the maximum
• In a complete graph, every pair of distinct
Complete
possible number of edges nodes is connected by an edge.
between its vertices Department of Electronics And Electrical Engineering, IIT Guwahati
Graphs - Types
Graphs
1 Cyclic Acyclic 1
Tree 2 3 2 3
Acyclic Tree
1 5
Eulerian Tree
2 3 6
Eulerian Tree 2
1
Hamiltonian 1
4
Both 2 3
3
7 5 4
Hamiltonian Graph is a Special 6
Case of Cyclic Graph Hamiltonian but not Eulerian
Eulerian but not Hamiltonian
Department of Electronics And Electrical Engineering, IIT Guwahati
Graphs - Types
Graphs
5 6
4 1
Bipartite Non-bipartite 1
4
2 3 Star
Complete
Bipartite
Graph
2 3
a b e(a) = 3
e(b) = 3
e(c) = 3
e(d) = 2
c d e
e(e) = 3
e(f) = 3
f g e(g) = 3
So the minimum eccentricity of the graph is 2, which is for the
vertex ‘d’, hence, is the radius
a b e(a) = 3
e(b) = 3
e(c) = 3
e(d) = 2
c d e
e(e) = 3
e(f) = 3
f g e(g) = 3
So the maximum eccentricity of the graph is 3, which is the
diameter of the graph
a b e(a) = 3
e(b) = 3
e(c) = 3
e(d) = 2
c d e
e(e) = 3
e(f) = 3
f g e(g) = 3
So the ‘d’ is the only central point of the graph
The set of all central points of ‘G’ is called the centre of the Graph.
a b
the circumference is 6, which we derived
from the longest cycle
• a-c-f-g-e-b-a
c d e
• a-c-f-d-e-b-a
f g
The length of the shortest cycle and the circumference
of an acyclic graph is defined to be infinity.
Department of Electronics And Electrical Engineering, IIT Guwahati
Graphs - Transpose
Transpose of a directed graph G is another directed graph H on the same set of vertices
with all of the edges reversed compared to the orientation of the corresponding edges in G.
If G contains an edge (u, v) then the transpose of G contains an edge (v, u) and vice versa.
a b a b
c d e c d e
f g f g
Representations
Adjacency Adjacency
Edge List
Matrix List
Traversal
Algorithms
Using DFS and Additional Matrix – O(N×M) time and O(N×M) space
• Initialize count = 0 and Boolean matrix, visited[][] to false.
• For each cell of the input matrix check if the value of the current cell
is 1 and is not visited , call for the DFS for all its 8 neighboring cells.
• If the neighbor is safe to visit and is not visited already Call DFS
recursively and Increment count by 1
• Return count as the final answer.
• Applications:
• Image Processing: Paint bucket tool in applications like Photoshop or MS Paint.
• Pathfinding: In game development or in simulations where regions need to be marked.
• Maze Solving: To explore or mark areas in mazes or grids.
Approach: BFS
• First, enqueue all rotten oranges with
the time set to 0 (since they are already
rotten).
• For each minute, rotten oranges spread
to adjacent fresh oranges, marking
them as rotten and updating their time.
• If any fresh orange cannot be reached
(i.e., no adjacent rotten orange can
spread to it), return -1.
• Time and space complexity O(N×M)