# 8. Implement function to print In-Degree, Out-Degree and to display that adjacency matrix.
class Graph:
def __init__(self, vertices):
self.vertices = vertices
self.adj_matrix = [[0] * vertices for _ in range(vertices)]
def add_edge(self, u, v):
self.adj_matrix[u][v] = 1
def display_adjacency_matrix(self):
print("Adjacency Matrix:")
for row in self.adj_matrix:
print(" ".join(map(str, row)))
def print_degrees(self):
print("\nIn-Degree and Out-Degree of each vertex:")
for i in range(self.vertices):
in_degree = sum(row[i] for row in self.adj_matrix)
out_degree = sum(self.adj_matrix[i])
print(f"Vertex {i}: In-Degree = {in_degree}, Out-Degree = {out_degree}")
# Example usage
if __name__ == "__main__":
g = Graph(4)
g.add_edge(0, 1)
g.add_edge(0, 2)
g.add_edge(1, 2)
g.add_edge(2, 0)
g.add_edge(2, 3)
g.add_edge(3, 3)
g.display_adjacency_matrix()
g.print_degrees()