Slip 12 - A) Write a C program that accepts the graph as an adjacency matrix and checks if the graph is undirected. The matrix for an undirected graph is symmetric. Also calculate in degree of all vertices - Read a graph as adjacency Matrix - Check the matrix is symmetric or not - Calculate indegree of all vertices

Solution:

#include<stdio.h>
#include<stdlib.h>

int n,g[10][10];

void inoutdegree()
    int i,j,id=0,od=0;
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            od+=g[i][j];
            id+=g[j][i];
        }
        printf("v%d indegree =%d \t Outdegree= %d\n",i,id,od);
    }
}

int main()
    int i,j,cnt=0;
    printf("How many vertices:\n");
    scanf("%d",&n);
    printf("Enter matrix elements:\n");

    for(i=0;i<n;i++)
    { 
        for(j=0;j<n;j++)
        { 
            scanf("%d",&g[i][j]);
        }
    }
    printf("\nAdjacency matrix Is:\n");
    for(i=0;i<n;i++)
    { 
        for(j=0;j<n;j++)
        { 
            if(g[i][j]!=g[j][i])
            cnt++;
            printf("%d\t",g[i][j]);
        }
        printf("\n");
    }

    if(cnt!=0)
    {
        printf("\nGiven graph is Directed\n\n");
        inoutdegree();
    }
    else
    {
        printf("Graph is undirected\n\n");
    }
    return 0;
}

Post a Comment

0 Comments