Unit 3 Notes
Unit 3 Notes
These notes are the intellectual property of the author and are protected
under the Indian Copyright Act, 1957.
Unauthorized copying, selling, forwarding, or distributing these notes in any
form (digital or physical) is a criminal offense under the following laws:
Ja
property.
te
l-
SECURITY NOTICE
78
Each page of these notes contains visible and hidden watermarks linked to the
07
identity of the original buyer. If these notes are found with anyone other than
54
• Both the sender and the receiver will be held legally accountable.
2
Unit – III
Graphs
This PDF is watermarked and traceable. Unauthorized sharing will result in
permanent access ban and legal action.
1. Basic Concepts
A graph is a non-linear data structure consisting of nodes (vertices) and edges (connections)
Ja
between them. It is used to represent relationships between objects, such as social networks,
road maps, or dependency graphs. In the field of sports data science, graph data structure can
im
be used to analyze and understand the dynamics of team performance and player interactions
il
on the field.
Pa
te
l-
+9
18
78
07
Components of a Graph
54
• V = Set of vertices
• E = Set of edges
Types of Graphs
1. Null Graph: A graph is known as a null graph if there are no edges in the graph.
3|Page © Haris Chaus 2025 | ALL RIGHTS ARE RESERVED as per Copyright Act, 1957.
2. Trivial Graph: Graph having only a single vertex, it is also the smallest graph possible.
3. Undirected Graph: A graph in which edges do not have any direction. That is the nodes are
Ja
4. Directed Graph: A graph in which edge has direction. That is the nodes are ordered pairs
+9
5. Connected Graph: The graph in which from one node we can visit any other node in the
2
6. Disconnected Graph: The graph in which at least one node is not reachable from a node is
known as a disconnected graph.
7. Regular Graph: The graph in which the degree of every vertex is equal to K is called K
Ja
regular graph.
im
il
Pa
te
l-
+9
8. Complete Graph: The graph in which from each node there is an edge to each other node.
18
78
07
54
9. Cycle Graph: The graph in which the graph is a cycle in itself, the minimum value of degree
27
of each vertex is 2.
2
10. Cyclic Graph: A graph containing at least one cycle is known as a Cyclic graph.
5|Page © Haris Chaus 2025 | ALL RIGHTS ARE RESERVED as per Copyright Act, 1957.
11. Directed Acyclic Graph: A Directed Graph that does not contain any cycle.
Ja
im
il
12. Bipartite Graph: A graph in which vertex can be divided into two sets such that vertex in
Pa
13. Weighted Graph: A graph in which the edges are already specified with suitable weight is
54
14. Multigraph: A graph in which multiple edges may connect the same pair of vertices is
called a multigraph.
Ja
im
Properties of Graphs
il
𝑛(𝑛−1)
1. Complete Graph: If an undirected graph of n vertices consists of number of edges,
2
Pa
2. Subgraph: A subgraph G’ of graph G is a graph such that the set of vertices and set of edges
07
3. Connected Graph: An undirected graph is said to be connected if for every pair of distinct
vertices Vi and Vj in V(G) there is an edge Vi to Vj in G.
7|Page © Haris Chaus 2025 | ALL RIGHTS ARE RESERVED as per Copyright Act, 1957.
For example:
• V1 – V2
• V1 – V2 – V4
Ja
• V1 – V3
im
4. Degree, in-degree and out-degree: The degree of vertex is the number of edges associated
il
with the vertex. In-degree of a vertex is the number of edges that are incident on that vertex.
Pa
Out-degree of the vertex is total number of edges that are going away from the vertex.
te
V1 2 1 1
V2 3 2 1
+9
V3 2 1 1
18
V4 3 1 2
78
07
6. Path: A path is denoted using sequence of vertices and there exists an edge from one vertex
to the next vertex. Path of the following graph is 1-2-3-4-5
27
2
7. Cycle: A closed walk through the graph with repeated vertices, mostly having the same
starting and ending vertex is called a cycle.
8|Page © Haris Chaus 2025 | ALL RIGHTS ARE RESERVED as per Copyright Act, 1957.
Edges Can have multiple edges and Only one edge between any two nodes
loops. (no loops).
2
Applications of Graphs
1. Google Maps & GPS Navigation → Graphs help find the shortest route between
locations using algorithms like Dijkstra’s.
2. Social Networks (Facebook, LinkedIn, Twitter) → Users are nodes, and connections
are edges, helping in friend suggestions and network analysis.
3. Computer Networks & Internet Routing → Graphs model network topology for
routing protocols like OSPF & BGP.
4. AI & Game Development (Pathfinding) → Games use A* and BFS/DFS for character
Ja
There are multiple ways to store a graph: The following are the most common representations.
l-
• Adjacency Matrix
+9
• Adjacency List
18
Adjacency Matrix is a square matrix used to represent a finite graph by storing the relationships
07
between the nodes in their respective cells. For a graph with V vertices, the adjacency matrix
54
A is a V × V matrix or 2D array.
27
• Diagonal Entries: The diagonal entries A[i][j] are usually set to 0 (in case of
unweighted) and INF in case of weighted, assuming the graph has no self-loops.
• Undirected Graphs: For undirected graphs, the adjacency matrix is symmetric. This
means A[i][j] = A[j][i] for all i and j.
An adjacency list is a data structure used to represent a graph where each node in the graph
im
• An adjacency list representation uses a list of lists. We store all adjacent of every node
te
together.
l-
• The size of the list is determined by the number of vertices in the graph.
+9
• All adjacent of a vertex are easily available. To find all adjacent, we need only O(n)
18
An Adjacency Multi List is a graph representation that is a mix of adjacency list and edge
list, mainly used for undirected graphs. It efficiently stores edges while allowing easy
18
Here M is a one-bit field that is used to represent whether the edge is visited or not.
2
Here the edges are (1, 2), (1, 4), (2, 3), (2, 4) and (3, 4). These edges are represented as nodes.
The vertices are 1, 2, 3, 4.
13 | P a g e © Haris Chaus 2025 | ALL RIGHTS ARE RESERVED as per Copyright Act, 1957.
In node N1, the edge (1, 2) is specified. Third field N2 is for edge (1, 4), this link is the adjacent
edge of edge (1, 2) considering vertex V1, i.e., 1 forth-field-N3 is for edge (2, 3), this link is
Ja
Similarly, vertex 2 is connected to (1, 2), (2, 3) and (2, 4) i.e. N2, N3 and N4
te
Vertex 1: N1 → N2
l-
Vertex 2: N2 → N3 → N4
+9
Vertex 3: N3 → N5
18
Vertex 4: N2 → N4 → N5
78
An Inverse Adjacency List is a variation of the adjacency list used for directed graphs.
54
Instead of storing outgoing edges (as in a standard adjacency list), it stores incoming edges for
27
each vertex.
2
14 | P a g e © Haris Chaus 2025 | ALL RIGHTS ARE RESERVED as per Copyright Act, 1957.
Q. Draw any directed graph with minimum 6 nodes and represent graph using adjacency
matrix, adjacency list, adjacency multilist and inverse adjacency list.
i) Adjacency Matrix
Ja
0 1 2 3 4 5
im
0 0 0 1 0 0 0
1 1 0 0 0 0 0
il
2 0 0 0 1 0 0
Pa
3 0 1 0 0 1 1
te
4 0 0 0 0 0 0
l-
5 0 0 0 0 0 0
+9
18
In above multilist various edges are represented. In the node of edge 1, the edge (1, 0) is
represented. For vertex 1, the incident edge is E3. Hence, we enter ‘Edge 3’ in the fourth field.
For vertex 0, the incident edge is E1 itself. So, we need not have to mention this self-defining
edge. So, we enter NULL in fifth field.
Again, for Edge 2, for vertex 0 the incident edge is 1 – 0 (Edge 1) but this edge is already
defined. Hence, we mention NULL in 4th field of Edge 2.
Ja
2. Traversals
DFS (Depth-First Search) is a graph traversal algorithm that explores as far as possible along
each branch before backtracking. It uses recursion (or a stack in an iterative approach) to visit
nodes.
3. In dfsRec() function:
visited[node] = true;
te
l-
if (!visited[neighbor]) {
DFSRec(adj, visited, neighbor);
54
}
27
}
2
Step 1: Start with a visited[] array to mark which spots (nodes) we've seen.
Ja
im
il
Pa
te
l-
0→1→2→3→4
20 | P a g e © Haris Chaus 2025 | ALL RIGHTS ARE RESERVED as per Copyright Act, 1957.
3. Push the starting node onto the stack and mark it as visited.
te
o Push all unvisited neighbors onto the stack and mark them as visited.
18
while (!s.empty()) {
int node = s.top(); // Get the top node
s.pop(); // Remove it from stack
cout << node << " "; // Process the node
21 | P a g e © Haris Chaus 2025 | ALL RIGHTS ARE RESERVED as per Copyright Act, 1957.
}
im
Step 1: Start with vertex a, mark it visited by writing 1 to index ‘a’ of visited array. Then print
a as an output.
a 1 a
b
c
d
e
Ja
f
im
g
il
Pa
Step 2: Find adjacent of a, and mark it visited. Push it onto the stack. Later b will be popped
te
and printed.
l-
a 1 a, b
78
b 1
b c
07
d
54
e
f
27
g
2
Step 3: Find adjacent of b. The a is already visited, hence ignore. Next node will be c. Insert c
onto the stack, mark it as visited. Push it. Later c will be popped and printed.
23 | P a g e © Haris Chaus 2025 | ALL RIGHTS ARE RESERVED as per Copyright Act, 1957.
a 1 a, b, c
b 1
c c 1
d
e
f
g
Ja
im
Step 4: Find adjacent of c. Push d, mark it visited. Later d will be popped and printed.
il
a 1 a, b, c, d
l-
b 1
d c 1
+9
d 1
18
e
78
f
g
07
54
Step 5: Find adjacent of d. Push f, mark it as visited. Later f will be popped and printed
27
a 1 a, b, c, d, f
b 1
f c 1
d 1
e
f 1
g
24 | P a g e © Haris Chaus 2025 | ALL RIGHTS ARE RESERVED as per Copyright Act, 1957.
Step 6: Find adjacent of f. Push e, mark it as visited. Later e will be popped and printed
a 1 a, b, c, d, f, e
b 1
e c 1
d 1
e 1
f 1
Ja
g
im
il
Step 7: Find adjacent of e. Push g, mark it as visited. Later g will be popped and printed
Pa
a 1
l-
a, b, c, d, f, e, g
b 1
+9
g c 1
18
d 1
e 1
78
f 1
07
g 1
54
27
Applications of DFS:
4. Maze & Pathfinding → Helps navigate mazes and find paths in AI/game development.
Breadth-First Search (BFS) is a graph traversal algorithm that explores all neighbors of a
node before moving to the next level. It follows a queue-based approach and ensures nodes
are visited in increasing order of their distance from the starting node.
while (!q.empty()) {
int node = q.front();
q.pop();
cout << node << " "; // Process the node
26 | P a g e © Haris Chaus 2025 | ALL RIGHTS ARE RESERVED as per Copyright Act, 1957.
a 1
a
b
c
d
e
f
Ja
g
im
a 1
a
l-
b
c
+9
d
18
e
78
f
g
07
54
Step 3: Find adjacent of a and insert them in a queue. Mark proper adjacent nodes as visited.
27
a 1
b e g a
b 1
c
d
e 1
f
g 1
28 | P a g e © Haris Chaus 2025 | ALL RIGHTS ARE RESERVED as per Copyright Act, 1957.
Step 4: Delete b and print. Find adjacent of b and if those nodes are not visited then insert them
in the queue. Mark corresponding nodes as visited.
a 1
e g c a, b
b 1
c 1
d
e 1
Ja
f
im
g 1
il
Pa
Step 5: Delete e and print. Find adjacent of e and if those nodes are not visited then insert them
in a queue. Mark corresponding nodes as visited.
te
l-
a 1
g c f a, b, e
18
b 1
c 1
78
d
07
e 1
54
f 1
g 1
27
2
Step 6: Delete g and print. Find adjacent of g and if those nodes are not visited then insert them
in a queue. Mark corresponding nodes as visited.
29 | P a g e © Haris Chaus 2025 | ALL RIGHTS ARE RESERVED as per Copyright Act, 1957.
a 1
c f d a, b, e, g
b 1
c 1
d 1
e 1
f 1
g 1
Ja
Step 7: Delete c and print. Find adjacent of c and if those nodes are not visited then insert them
im
a 1
te
f d a, b, e, g, c
b 1
l-
c 1
+9
d 1
e 1
18
f 1
78
g 1
07
Step 8: Delete f and print. Find adjacent of f and if those nodes are not visited then insert them
54
a 1
d a, b, e, g, c, f
b 1
c 1
d 1
e 1
f 1
g 1
30 | P a g e © Haris Chaus 2025 | ALL RIGHTS ARE RESERVED as per Copyright Act, 1957.
Step 9: Delete d and print. Find adjacent of d and if those nodes are not visited then insert them
in a queue. Mark corresponding nodes as visited.
a 1
a, b, e, g, c, f, d
b 1
c 1
d 1
e 1
Ja
f 1
im
g 1
il
Step 10: As queue is empty, and all entries in the visited array are visited, then stop. At display
Pa
BFS Sequence is a, b, e, g, c, f, d.
l-
Applications of BFS
+9
1. Shortest Path in an Unweighted Graph → Finds the shortest path between two nodes.
18
components.
07
3. Bipartite Graph Checking → Determines if a graph can be colored with two colors.
54
3. Has the minimum possible total edge weight among all spanning trees.
The Greedy technique is an algorithmic method for obtaining optimized solution for a given
Ja
problem. For example, for obtaining the shortest path from source vertex to a destination vertex
im
The Greedy technique is applied to find out minimum spanning tree in a graph.
Pa
Prim’s Algorithm: In Prim’s Algorithm, the pair with the minimum weight is to be chosen.
+9
Then adjacent to these vertices whichever is the edge having minimum weight is selected. This
18
process will be continued till all the vertices are not covered. The necessary condition in this
case is that the circuit should not be formed.
78
Prim(Graph, V):
54
// Step 3: Pick the vertex u with the minimum key[] value not
in MST
u = vertex with min key[u] such that inMST[u] == false
inMST[u] = true // Include u in MST
key[v] = weight
im
parent[v] = u
il
for i = 1 to V - 1:
l-
Q. For the graph given below, construct a minimum spanning tree using Prim’s algorithm.
Show the table created during each pass of the algorithm.
18
78
07
54
27
2
In Prim’s algorithm the minimum path length is selected. We only need to make sure that the
circuit should not be formed.
weight has to selected from the graph. It’s not necessary in this algorithm to have edges of
minimum weights to be adjacent.
Kruskal(Graph, V):
// Step 1: Create a list of all edges and sort them by weight
edges = list of all edges in the form (u, v, weight)
sort(edges by increasing weight)
Ja
Spanning Tree
l-
return MST
2
union(x, y):
rootX = findParent(x)
rootY = findParent(y)
parent[rootY] = rootX
im
rank[rootX]++
il
Q. Obtain Minimum Spanning Tree by Kruskal’s algorithm for the following graph:
Pa
te
l-
+9
18
78
Dijkstra’s Algorithm is used to find the shortest path from a single source vertex to all other
vertices in a weighted graph (with non-negative weights). It works by iteratively selecting the
vertex with the minimum distance and updating the distances of its neighboring vertices.
neighbors
54
dist[v]:
2
Q. Find the shortest path in the following graph from node A, using Dijkstra’s Algorithm.
S = {A}, T = {B, C, D, E}
il
L(C) = min{∞, 0 + ∞} = ∞
te
L(D) = min{∞, 0 + ∞} = ∞
l-
L(C) = min{∞, 3 + 8} = 11
07
L(D) = min{∞, 3 + 2} = 5
54
L(E) = min{∞, 4 + 2} = 6
2
1. Floyd-Warshall Algorithm
Key Features:
• All-Pairs Shortest Path Algorithm: Finds the shortest paths between every pair of
vertices.
Ja
• Works with Negative Weights: Unlike Dijkstra’s algorithm, it handles graphs with
im
Algorithm/Pseudocode:
18
Algorithm FloydWarshall(dist[][], n)
78
{
07
}
}
}
Floyd-Warshall Algorithm.
im
il
Pa
te
l-
+9
18
78
07
1. Create a matrix A0 of dimension n*n where n is the number of vertices. The row and the
54
column are indexed as i and j respectively. i and j are the vertices of the graph.
27
Each cell A[i][j] is filled with the distance from the ith vertex to the jth vertex. If there is
no path from ith vertex to jth vertex, the cell is left as infinity.
2
A0 =
42 | P a g e © Haris Chaus 2025 | ALL RIGHTS ARE RESERVED as per Copyright Act, 1957.
Create a matrix A1 using matrix A0. The elements in the first column and the first row are left
as they are. Let k be the intermediate vertex in the shortest path from source to destination.
Here, k = A. The remaining cells are filled in the following way. Examples:
A1 =
il
Pa
te
l-
To find A2: A2 =
+9
Here, k = B
18
78
07
To find A3: A3 =
54
27
Here, k = C
2
To find A4: A4 =
Here k = D
43 | P a g e © Haris Chaus 2025 | ALL RIGHTS ARE RESERVED as per Copyright Act, 1957.
To find A5: A5 =
Here k = E
2. Topological Ordering
This algorithm is a direct implementation of the Decrease and Conquer method. The steps
involved are as follows:
Ja
1. Identify a vertex in the given graph that has no incoming edges. Remove this vertex
im
along with all its outgoing edges. If multiple such vertices exist, choose one randomly.
il
3. The sequence of recorded vertices forms the topologically sorted order of the graph.
te
TopologicalSort_Kahn(Graph):
l-
in_degree[neighbor] += 1
07
54
if in_degree[node] == 0:
queue.enqueue(node)
current = queue.dequeue()
topo_order.append(current)
// Step 6: Check for cycles (if all nodes were not processed)
im
else:
print(topo_order)
te
Step 1: Choose vertex ‘1’ as it has no incoming edge. Delete it along with its adjacent-edges.
27
Delete 1:
2
45 | P a g e © Haris Chaus 2025 | ALL RIGHTS ARE RESERVED as per Copyright Act, 1957.
Delete 3:
Delete 2 or 4:
Ja
im
il
Pa
te
Delete 4 or 2:
l-
+9
18
78
07
Delete 6:
54
27
2
Delete 5:
Delete 7.
• 1, 3, 2, 4, 6, 5, 7
• 1, 3, 4, 2, 6, 5, 7