EXPERIMENT 1
AIM: Adjacency Matrix
THEORY: An adjacency matrix is a tool in graph theory for representing finite graphs. It provides a
compact and efficient way to describe the relationships between vertices in a graph, particularly useful for
dense graphs where the number of edges is close to the maximum possible.
Structure of an Adjacency Matrix
1. Square Matrix: The adjacency matrix is always square matrix, having same number of rows and
columns. If a graph has n vertices, the adjacency matrix will be n×n.
2. Matrix Elements:
The element at position (i, j) in the matrix is defined as follows:
Value 1: Indicates that there is an edge from vertex i to vertex j.
Value 0: Indicates that there is no edge between vertex i and vertex j.
3. Undirected Graphs:
For undirected graphs, the adjacency matrix is symmetric. This means that if there is an edge
between vertex i and vertex j, then both (i,j) and (j,i) will have a value of 1. Thus, the matrix
satisfies the property:
A[i][j]=A[j][i]
4. Directed Graphs:
In directed graphs, the adjacency matrix may not be symmetric. An edge from vertex i to
vertex j does not imply an edge from vertex j to vertex i.
Advantages of Adjacency Matrix
Easy to Implement: The adjacency matrix is simple and straightforward to set up.
Space-Efficient for Dense Graphs: It utilizes space effectively when representing dense graphs, as it
requires a fixed amount of space based on the number of vertices.
Constant-Time Edge Checks: It allows for quick, constant-time access to determine if an edge exists
between any two vertices.
Disadvantages of Adjacency Matrix
High Space Consumption for Sparse Graphs: It uses more memory even when dealing with sparse
graphs that have few edges.
Costly Vertex Modifications: Adding or removing vertices can be expensive, as it necessitates resizing
the entire matrix.
Slower Traversal Operations: Graph traversal methods like Depth-First Search (DFS) and Breadth-First
Search (BFS) can be less efficient compared to using an adjacency list.
CODE:
def create_adjacency_matrix(vertices, edges, is_directed=False):
adjacency_matrix = [[0 for _ in range(vertices)] for _ in range(vertices)]
for edge in edges:
u, v = edge
adjacency_matrix[u][v] = 1
if not is_directed:
1
adjacency_matrix[v][u] = 1
return adjacency_matrix
def print_matrix(matrix):
for row in matrix:
print(" ".join(map(str, row)))
# Example
if __name__ == "__main__":
vertices = 5 # Number of vertices
edges = [(0, 1), (0, 4), (1, 2), (1, 3), (1, 4), (2, 3), (3, 4)] # Edges of the graph
print("Undirected Graph:")
adjacency_matrix_undirected = create_adjacency_matrix(vertices, edges, is_directed=False)
print_matrix(adjacency_matrix_undirected)
print("\nDirected Graph:")
adjacency_matrix_directed = create_adjacency_matrix(vertices, edges, is_directed=True)
print_matrix(adjacency_matrix_directed)
OUTPUT:
Conclusion:
The adjacency matrix is a simple and efficient way to represent graphs, especially dense ones. It allows
constant-time edge lookups and easy implementation, though it may consume more memory for sparse
graphs and is less optimal for traversal operations. The provided Python code demonstrates how to build and
display adjacency matrices for both directed and undirected graphs