

How to Find the Adjacency Matrix for Directed and Undirected Graphs
The concept of Adjacency Matrix plays a key role in mathematics and is widely applicable to topics in graph theory, discrete mathematics, data structures, and many real-life network problems. This method helps students understand relationships between objects and makes graph algorithms easier to apply in programming or competitive exams. Let's explore this essential topic in a structured, easy-to-read way.
What Is Adjacency Matrix?
An adjacency matrix is a square matrix that represents a graph by showing which vertices (nodes) are connected to which others. If two vertices are connected by an edge, the corresponding position in the matrix contains a 1; otherwise, it contains a 0. This concept is widely used in Graph Theory, Data Structures, and in coding algorithms for computer science.
Key Formula for Adjacency Matrix
Here’s the standard definition: For a graph G with n vertices numbered \( v_1, v_2, ..., v_n \), the adjacency matrix \( A \) is an \( n \times n \) matrix, where each entry \( a_{ij} \) is:
\[ a_{ij} = \begin{cases} 1 & \text{if there is an edge from } v_i \text{ to } v_j \\ 0 & \text{otherwise} \end{cases} \]
Cross-Disciplinary Usage
Adjacency matrices are not only useful in Mathematics, but also play important roles in Physics (network analysis), Computer Science (graph algorithms like BFS and DFS), and even logical reasoning in daily life. Especially for students preparing for exams like JEE, NEET or Olympiads, knowing how to use adjacency matrices makes solving network/graph problems quick and systematic.
Types of Adjacency Matrices
Adjacency matrices can represent both directed and undirected graphs. For undirected graphs, the matrix is symmetric because each edge goes both ways. For directed graphs, the matrix may not be symmetric.
Step-by-Step Illustration
- Suppose we have these vertices: A, B, C, D.
- The edges are: A–B, A–C, B–C, C–D (all undirected).
- Assign numbers: a=1, b=2, c=3, d=4.
- We create the adjacency matrix as follows:
A | B | C | D | |
---|---|---|---|---|
A | 0 | 1 | 1 | 0 |
B | 1 | 0 | 1 | 0 |
C | 1 | 1 | 0 | 1 |
D | 0 | 0 | 1 | 0 |
Each “1” in a cell means the two vertices are connected by an edge.
Properties of the Adjacency Matrix
- If the graph is undirected, the matrix is symmetric: \( a_{ij} = a_{ji} \).
- Diagonal entries show self-loops: 1 if there is a loop at that vertex (otherwise 0).
- The sum of entries in a row or column tells you the degree of the vertex.
- Multiplying the matrix by itself (raising to a power) tells you about paths of longer length.
- For weighted graphs, “1” is replaced by the edge’s weight.
Adjacency Matrix vs Other Graph Representations
Feature | Adjacency Matrix | Adjacency List | Incidence Matrix |
---|---|---|---|
Space Requirement | O(V²) | O(V+E) | O(V×E) |
Edge Lookup Speed | Constant | Linear | Linear |
Best For | Dense Graphs | Sparse Graphs | Theoretical Analysis |
Example Internal Link | Adjacency Matrix | Matrix Representation | Incidence Matrix |
Step-by-Step Example: Adjacency Matrix for a Directed Graph
Suppose we have a directed graph with vertices A, B, C, D. The edges are: A→B, B→C, C→D.
A | B | C | D | |
---|---|---|---|---|
A | 0 | 1 | 0 | 0 |
B | 0 | 0 | 1 | 0 |
C | 0 | 0 | 0 | 1 |
D | 0 | 0 | 0 | 0 |
Notice that only the positions where there is a directed edge have “1”.
Try These Yourself
- Draw an undirected graph with 4 nodes and 3 edges, then write its adjacency matrix.
- Given an adjacency matrix with a “1” in cell [3,2], what does this mean for the underlying graph?
- Convert this adjacency matrix to an adjacency list:
0 | 1 | 0 |
1 | 0 | 1 |
0 | 1 | 0 |
Frequent Errors and Misunderstandings
- Confusing adjacency matrix with adjacency list or incidence matrix.
- Forgetting to include both (i,j) and (j,i) for undirected graphs.
- Assuming graphs with many nodes but few edges should use an adjacency matrix (in fact, use adjacency lists for sparse graphs!).
- Mislabeling vertices when constructing the matrix (always use a clear ordering).
Adjacency Matrix in Programming
Python Example:
# n = number of nodes adj_matrix = [[0 for _ in range(n)] for _ in range(n)] # Add an edge from node i to node j adj_matrix[i][j] = 1
C++ Example:
// n = number of nodes vector<vector<int>> adj_matrix(n, vector<int>(n, 0)); // Add an edge from node i to node j adj_matrix[i][j] = 1;
Algorithms like BFS and DFS can easily use adjacency matrices for graph traversal in coding problems.
Relation to Other Concepts
The idea of adjacency matrix connects closely with topics like matrix representation of graph, symmetric matrix, and incidence matrix. Mastering adjacency matrices also helps when learning about more complex graph algorithms and data structures later on.
Classroom Tip
A useful way to remember adjacency matrices is to imagine the vertices lined up on both sides of a table. Put “1” where an edge connects two vertices. Teachers at Vedantu often use color-coded diagrams and step lists to help students quickly visualize connections during live classes.
We explored Adjacency Matrix — from definition, formulas, worked examples, and differences with other representations, to common mistakes. Keep practicing using Vedantu’s resources and you’ll soon be able to use adjacency matrices confidently in all graph-related problems!
Looking to dive deeper? Visit these related concepts:
FAQs on Adjacency Matrix: Explained with Examples and Applications
1. What is an adjacency matrix and how is it calculated?
An adjacency matrix is a square matrix used to represent a graph's connections. Each row and column represents a vertex. A '1' at position (i,j) indicates an edge between vertex i and vertex j; '0' indicates no edge. For undirected graphs, the matrix is symmetric. For directed graphs, it's not. To calculate it:
- Number the vertices.
- Create a square matrix of size (number of vertices) x (number of vertices).
- For each edge between vertices i and j, set matrix element (i,j) to '1'.
- For no edge, set to '0'.
2. What is the difference between an adjacency matrix and an adjacency list?
Both represent graphs, but differ in storage: An adjacency matrix uses a square matrix where the (i,j) entry indicates an edge between vertices i and j. An adjacency list uses a list for each vertex storing its neighbors. Matrices are efficient for dense graphs; lists are better for sparse graphs.
3. When is an adjacency matrix symmetric?
An adjacency matrix is symmetric only for undirected graphs. Because edges in undirected graphs are bidirectional (vertex i connects to vertex j, and vice-versa), the matrix element (i,j) will always equal (j,i).
4. How are weighted graphs represented using an adjacency matrix?
For weighted graphs, instead of using '1' to represent an edge, the matrix element (i,j) stores the weight of the edge between vertices i and j. If there's no edge, it might contain a '0' or '∞'.
5. What are the applications of adjacency matrices?
Adjacency matrices find use in various algorithms like Breadth-First Search (BFS) and Depth-First Search (DFS), shortest path algorithms (e.g., Dijkstra's), and generally for representing and manipulating graph data in computer science.
6. What is the time complexity of graph traversal using an adjacency matrix?
The time complexity of graph traversal (e.g., BFS or DFS) using an adjacency matrix is O(V²), where V is the number of vertices. This is because checking for adjacency requires examining an entire row (or column) of the matrix.
7. How do self-loops and parallel edges affect an adjacency matrix?
A self-loop (an edge from a vertex to itself) is represented by a non-zero value on the diagonal of the adjacency matrix (element (i,i) for vertex i). Parallel edges (multiple edges between two vertices) are represented by increasing the value of the corresponding matrix entry beyond '1' to reflect the number of edges.
8. How does an adjacency matrix represent a complete graph?
In a complete graph (where every pair of distinct vertices is connected by a unique edge), the adjacency matrix will have all entries as '1' except for the diagonal (which will have '0' unless self-loops are allowed).
9. What are the advantages and disadvantages of using an adjacency matrix?
Advantages: Simple to implement; checking for edge existence is fast (O(1)). Disadvantages: Inefficient for sparse graphs (lots of wasted space); takes O(V²) space, where V is the number of vertices.
10. How can I represent a directed acyclic graph (DAG) using an adjacency matrix?
A Directed Acyclic Graph (DAG) is represented similarly to a directed graph. The matrix will not be symmetric, and you can use topological sorting to arrange the vertices in the matrix to improve readability and simplify certain algorithms.
11. What is the difference between an adjacency matrix and an incidence matrix?
An adjacency matrix shows the connections between vertices. An incidence matrix shows the connections between vertices and edges; it's a V x E matrix, where V is the number of vertices and E is the number of edges. Each row represents a vertex, each column an edge. A '1' indicates incidence.
12. Can you provide an example of an adjacency matrix for a simple undirected graph?
Consider a graph with vertices A, B, C. If A connects to B and C, and B connects to C, the adjacency matrix would be:
A B C
A 0 1 1
B 1 0 1
C 1 1 0

















