In this post, you will learn how to represent a graph in matrix form. By using this concept we can easily create implementation of adjacency matrix.
If there is an edge between two vertices then we will represent it with 1 and if there is no edge then with 0.
Adjacency Matrix Representation of Graphs
For Undirected Graph:
1 | 2 | 3 | 4 | 5 | |
1 | 0 | 1 | 0 | 0 | 1 |
2 | 1 | 0 | 1 | 0 | 0 |
3 | 0 | 1 | 0 | 1 | 0 |
4 | 0 | 0 | 1 | 0 | 1 |
5 | 1 | 0 | 0 | 1 | 0 |
Directed Graphs:
1 | 2 | 3 | 4 | 5 | |
1 | 0 | 0 | 0 | 0 | 1 |
2 | 1 | 0 | 0 | 0 | 0 |
3 | 0 | 1 | 0 | 0 | 0 |
4 | 0 | 0 | 1 | 0 | 0 |
5 | 0 | 0 | 0 | 1 | 0 |