NEP -DAA -DESIGN AND ANALYSIS OF ALOGRITHM

PROGRAM 1 PROGRAM 2 PROGRAM 3 PROGRAM 4 PROGRAM 5 PROGRAM 6 PROGRAM 7 PROGRAM 8 PROGRAM 9 PROGRAM 10 PROGRAM 11 PROGRAM 12

PART B

PROGRAM B1 PROGRAM B2 PROGRAM B3 PROGRAM B4 PROGRAM B5 PROGRAM B6 PROGRAM B7 PROGRAM B8 . . .

 
     


# 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()