ClassNotes 7 - DSA (Graph)
ClassNotes 7 - DSA (Graph)
A graph is an abstract data type (ADT) which consists of a set of objects that are connected to each other via links.
The interconnected objects are represented by points termed as vertices (V),
The links that connect the vertices are called edges(E).
G= (V , E )
📌 Easy Definition:
Formally, a graph is a pair of sets (V, E), where V is the set of vertices and E is the set of edges, connecting the pairs of vertices. Take a look at the following graph
V = {a, b, c, d, e}
Mathematical graphs can be represented in data structure. We can represent a graph using an array of vertices and a two-dimensional array of edges. Before we proceed further, let's
familiarize ourselves with some important terms −
1. Vertex − Each node of the graph is represented as a vertex. In the following example, the labeled circle represents vertices. Thus, A to G are vertices. We can represent them
using an array as shown in the following image. Here A can be identified by index 0. B can be identified using index 1 and so on.
2. Edge − Edge represents a path between two vertices or a line between two vertices. In the following example, the lines from A to B, B to C, and so on represents edges. We can
use a two-dimensional array to represent an array as shown in the following image. Here AB can be represented as 1 at row 0, column 1, BC as 1 at row 1, column 2 and so on,
keeping other combinations as 0.
3. Adjacency − Two node or vertices are adjacent if they are connected to each other through an edge. In the following example, B is adjacent to A, C is adjacent to B, and so on.
4. Path − Path represents a sequence of edges between the two vertices. In the following example, ABCD represents a path from A to D.
Graph Terminology
1. Graph: A collection of nodes (or vertices) and edges that connect pairs of nodes.
2. Vertex (Node): The fundamental unit by which graphs are formed. A vertex represents a point in the graph.
3. Edge: A line connecting two vertices in a graph. It can be directed (having a direction) or undirected (no direction).
4. Directed Graph (Digraph): A graph in which the edges have a direction, indicating a one-way relationship between vertices.
5. Undirected Graph: A graph in which the edges do not have a direction, indicating a two-way relationship.
6. Weighted Graph: A graph in which edges have weights assigned to them, typically representing costs, lengths, or capacities.
8. Adjacent (Neighbors): Two vertices are adjacent if there is an edge connecting them.
For directed graphs, there are in-degree - edges coming into the vertex)
out-degree - edges going out from the vertex).
10. Path: A sequence of edges that allows you to go from one vertex to another.
11. Cycle: A path that starts and ends at the same vertex without traversing any edge more than once.
14. Connected Graph: An undirected graph is connected if there is a path between every pair of vertices.
15. Disconnected Graph: A graph is disconnected if it is not connected, i.e., if there are at least two vertices with no path between them.
16. Complete Graph: A graph in which there is an edge between every pair of vertices.
17. Bipartite Graph: A graph whose vertices can be divided into two disjoint sets such that every edge connects a vertex in one set to a vertex in the other set.
19. Acyclic Graph: A graph with no cycles. In the context of directed graphs, it is often called a Directed Acyclic Graph (DAG).
20. Graph Isomorphism: Two graphs are isomorphic if there is a one-to-one correspondence between their vertex sets that preserves edge connectivity.
21. Parallel edges: In graph theory, parallel edge (also called multiple edges or a multi-edge), are two or more edges that are incident to the same two vertices. A simple graph
has no parallel edges.
22. Topological Graph: Topological sorting is an algorithm that sorts the vertices of a directed acyclic graph (DAG) in a specific order to satisfy all dependencies. This algorithm is
commonly used in several domains, such as task scheduling, software engineering, dependency resolution, and graph theory.
23. Spanning Tree: A spanning tree is a subset of Graph G, which has all the vertices covered with minimum possible number of edges. Hence, a spanning tree does not have
cycles and it cannot be disconnected.
By this definition, we can draw a conclusion that every connected and undirected Graph G has at least one spanning tree. A disconnected graph does not have any spanning
tree, as it cannot be spanned to all its vertices.
Types of Graphs:
There are two basic types of graphs −
1. Directed graph, as the name suggests, consists of edges that possess a direction that goes either away from a vertex or towards the vertex.
1. Adjacency Matrix
An adjacency matrix is a 2D array where each cell [i, j] represents the presence or absence of an edge between vertices i and j. If the graph is weighted, the cell can store the
weight of the edge.
Here's an overview of the key aspects of adjacency matrices:
The adjacency matrix is a square matrix of size N x N, where N is the number of vertices in the graph.
Each element (i, j) of the matrix represents the presence (1) or absence (0) of an edge between vertex i and vertex j.
For undirected graphs, the matrix is symmetric, meaning a[i][j] = a[j][i].
For directed graphs, the adjacency matrix is not necessarily symmetric, meaning a[i][j] ≠ a[j][i].
The n rows of adjacency matrix are represented as n linked lists. There is one list for each vertex in the graph. The nodes in the list i represent the vertices that are
adjacent from vertex i. Each node has at least two fields: vertex and link. The vertex field contains the indices of the vertices adjacent to vertex i. The adjacency lists for
the undirected graph and directed graph, are illustrated below.
Breadth-First Search starts at a specific 'source' node and explores all its neighboring nodes before moving on to their neighbors. This process continues until all nodes in the
component containing the source have been explored.
Level-by-Level Traversal: BFS visits nodes in a level-wise order. It first visits all nodes at one level before moving to the next.
Queue Utilization: BFS uses a queue data structure to manage the order of node traversal. Nodes are dequeued for exploration and their unvisited neighbors are
enqueued.
Marking Visited Nodes: To avoid processing a node more than once, BFS marks each node as visited when it is enqueued.
Uniformity in Path Lengths: BFS is especially useful in finding the shortest path in unweighted graphs, as it visits nodes in order of their distance from the source, ensuring
the shortest path is found first.
2. Select any vertex as starting point for traversal. Visit that vertex and insert it into the Queue.
3. Visit all the non-visited adjacent vertices of the vertex which is in front of the Queue and insert them into the Queue.
4. When there is no new vertex to visited from the vertex at front of the Queue then delete that vertex.
6. When queue becomes empty, then produce final spanning tree by removing unused edges from the graph
May be less efficient than Depth-First Search (DFS) for large graphs.
Pseudo-Code
BFS(graph, start_vertex):
Create a queue Q
Mark start_vertex as visited and enqueue it into Q
While Q is not empty:
vertex = Q.dequeue() // Remove the vertex from the front of the queue
Visit(vertex)
For each neighbor 'n' of vertex:
If n is not visited:
Mark n as visited
Enqueue n into Q
Queue
Front, Rear
9 (visit)
9 3 (visit)
9 3 2 4 10
9 3 2(visit) 4 10 1 5 7 8
9 3 2 4(visit) 10 1 5 7 8
9 3 2 4 10 (visit) 1 5 7 8
9 3 2 4 10 1(visit) 5 7 8
9 3 2 4 10 1 5(visit) 7 8 6
Front Rear
Notes:
Exploration Strategy: Follows a single path as far as possible, backtracking if necessary, before exploring other branches.
Data Structure: Utilizes a stack to keep track of visited nodes and the order of exploration.
Strengths: Efficient for finding paths, identifying connected components, and topological sorting.
Weaknesses: May not find the shortest path in a weighted graph and can be inefficient for finding all nodes in large, sparse graphs.
2. Select any vertex as starting point for traversal. Visit that vertex and push it on to the Stack.
3. Visit any one of the non-visited adjacent vertices of a vertex which is at the top of stack and push it on to the stack.
4. Repeat step 3 until there is no new vertex to be visited from the vertex which is at the top of the stack.
5. When there is no new vertex to visit then use back tracking and pop one vertex from the stack.
7. When stack becomes Empty, then produce final spanning tree by removing unused edges from the graph
1,2,8,7,5,6,4,3,9,10 1,2,3,4,9,10,5,6,7,8
Floyd-Warshall’s Algorithm
✅ Warshall’s Algorithm
Warshall’s Algorithm is used to find the transitive closure of a directed graph.
In simple words: It tells you whether you can go from one node to another — directly or through other nodes.
🔢 Example
Suppose we have 3 nodes: A, B, C
Initial adjacency matrix:
A B C
A 1 1 0
B 0 1 1
C 0 0 1
This means:
A→B
B→C
Each node reaches itself (diagonal = 1)
A B C
A 1 1 1
B 0 1 1
C 0 0 1
Feature Description
Let me know if you'd like to try a step-by-step example with a custom graph!
Formula:
Example:
Complexity
1. Time Complexity: O ( v 3 ) −¿ where V is the number of vertexes.
Reason: For the main iteration, three loops are layered inside of one another. Each loop iterates for V times, and this number changes depending on the input V. As a result, our
temporal complexity is O ( v 3 )