An Adjacency matrix is a representation of a graph using a 2D matrix, where the rows and columns correspond to the vertices (nodes) of the graph. The value in the matrix indicates whether a pair of vertices is connected by an edge. It is often represented using Boolean values: 0 indicates no connection, and 1 indicates the presence of an edge between the vertices.
//adjacencymatrixfordirectedunweighted.cpp#include <iostream>#include <vector>using namespace std;int main(){int vertex, edges;cin >> vertex >> edges;vector<vector<bool>> adjMat(vertex, vector<bool>(vertex,0));int u, v;for (int i = 0; i < edges; i++){ cin >> u >> v; adjMat[u][v] = 1;}for (int i = 0; i < vertex; i++){ for (int j = 0; j < vertex; j++) { cout << adjMat[i][j] << " "; } cout << endl;}}
# adjacencymatrixfordirectedunweighted.py# Read number of vertices and edgesvertex, edges = map(int, input().split())# Initialize adjacency matrix with Falseadj_mat = [[False for _ in range(vertex)] for _ in range(vertex)]# Read directed edges and populate matrixfor _ in range(edges):u, v = map(int, input().split())adj_mat[u][v] = True # Only set one direction for directed graph# Print the adjacency matrixfor i in range(vertex):for j in range(vertex): print(1 if adj_mat[i][j] else 0, end=" ")print()# Example usage:# Input: 4 5# 0 1# 0 2# 1 3# 2 1# 3 2# Output: 0 1 1 0# 0 0 0 1# 0 1 0 0# 0 0 1 0
// adjacencymatrixfordirectedunweighted.jsfunction createDirectedUnweightedMatrix(vertex, edges, edgeList) {// Initialize adjacency matrix with falseconst adjMat = Array(vertex).fill(null).map(() => Array(vertex).fill(false));// Add directed edges to matrixfor (const [u, v] of edgeList) { adjMat[u][v] = true; // Only set one direction for directed graph}// Print the adjacency matrixfor (let i = 0; i < vertex; i++) { let row = ""; for (let j = 0; j < vertex; j++) { row += (adjMat[i][j] ? 1 : 0) + " "; } console.log(row);}return adjMat;}// Example usage:const vertex = 4;const edges = 5;const edgeList = [[0,1], [0,2], [1,3], [2,1], [3,2]];createDirectedUnweightedMatrix(vertex, edges, edgeList);// Output: 0 1 1 0// 0 0 0 1// 0 1 0 0// 0 0 1 0
//adjacencymatrixfordirectedweighted.cpp#include <iostream>#include <vector>using namespace std;int main(){int vertex, edges;cin >> vertex >> edges;vector<vector<int>> adjMat(vertex, vector<int>(vertex, INT_MAX));int u, v, weight;for (int i = 0; i < edges; i++){ cin >> u >> v >> weight; adjMat[u][v] = weight;}for (int i = 0; i < vertex; i++){ adjMat[i][i] = 0;}for (int i = 0; i < vertex; i++){ for (int j = 0; j < vertex; j++) { if (adjMat[i][j] == INT_MAX) cout << "INF" << " "; else cout << adjMat[i][j] << " "; } cout << endl;}}
# adjacencymatrixfordirectedweighted.pyimport sys# Read number of vertices and edgesvertex, edges = map(int, input().split())# Initialize adjacency matrix with infinityINF = float('inf')adj_mat = [[INF for _ in range(vertex)] for _ in range(vertex)]# Read directed edges with weights and populate matrixfor _ in range(edges):u, v, weight = map(int, input().split())adj_mat[u][v] = weight # Only set one direction for directed graph# Set diagonal elements to 0 (distance from vertex to itself)for i in range(vertex):adj_mat[i][i] = 0# Print the adjacency matrixfor i in range(vertex):for j in range(vertex): if adj_mat[i][j] == INF: print("INF", end=" ") else: print(adj_mat[i][j], end=" ")print()# Example usage:# Input: 4 5# 0 1 2# 0 2 4# 1 3 5# 2 1 1# 3 2 3# Output: 0 2 4 INF# INF 0 INF 5# INF 1 0 INF# INF INF 3 0
// adjacencymatrixfordirectedweighted.jsfunction createDirectedWeightedMatrix(vertex, edges, edgeList) {const INF = Number.MAX_SAFE_INTEGER;// Initialize adjacency matrix with infinityconst adjMat = Array(vertex).fill(null).map(() => Array(vertex).fill(INF));// Add directed weighted edges to matrixfor (const [u, v, weight] of edgeList) { adjMat[u][v] = weight; // Only set one direction for directed graph}// Set diagonal elements to 0for (let i = 0; i < vertex; i++) { adjMat[i][i] = 0;}// Print the adjacency matrixfor (let i = 0; i < vertex; i++) { let row = ""; for (let j = 0; j < vertex; j++) { if (adjMat[i][j] === INF) { row += "INF "; } else { row += adjMat[i][j] + " "; } } console.log(row);}return adjMat;}// Example usage:const vertex = 4;const edges = 5;const edgeList = [[0,1,2], [0,2,4], [1,3,5], [2,1,1], [3,2,3]];createDirectedWeightedMatrix(vertex, edges, edgeList);// Output: 0 2 4 INF// INF 0 INF 5// INF 1 0 INF// INF INF 3 0