An Adjacency Matrix is a 2D array used to represent a graph. It’s ideal for dense graphs where most pairs of nodes are connected.
i
and node j
, then matrix[i][j] = 1
(or weight w
).matrix[i][j] = 1
means there’s a one-way edge from i
to j
.0 -- 1
| |
2 -- 3
Adjacency Matrix:
0 1 2 3
0 [0 1 1 0]
1 [1 0 0 1]
2 [1 0 0 1]
3 [0 1 1 0]
class GraphMatrix:
def __init__(self, num_nodes):
self.V = num_nodes
self.matrix = [[0] * num_nodes for _ in range(num_nodes)]
def add_edge(self, u, v):
self.matrix[u][v] = 1
self.matrix[v][u] = 1 # Remove this for directed graphs
def print_matrix(graph):
for row in graph.matrix:
print(row)
def add_weighted_edge(self, u, v, weight):
self.matrix[u][v] = weight
self.matrix[v][u] = weight # Remove for directed graph
from collections import deque
def bfs_matrix(graph, start):
visited = [False] * graph.V
queue = deque([start])
while queue:
node = queue.popleft()
if not visited[node]:
print(node, end=' ')
visited[node] = True
for neighbor in range(graph.V):
if graph.matrix[node][neighbor] and not visited[neighbor]:
queue.append(neighbor)
def dfs_matrix(graph, node, visited=None):
if visited is None:
visited = [False] * graph.V
if not visited[node]:
print(node, end=' ')
visited[node] = True
for neighbor in range(graph.V):
if graph.matrix[node][neighbor] and not visited[neighbor]:
dfs_matrix(graph, neighbor, visited)