9Google AdSense

Adjacency Matrix (Represent Graph on computer)

#include<stdio.h>

int main()
{
    int graph[50][50];
    int i,j,edge,vertex,p,x,y,q,w;
    printf("Enter the Number of Vertex : ");
    scanf("%d",&vertex);
    printf("\n");
 for(w=1;w<=vertex;w++)
        for(q=1;q<=vertex;q++)
            graph[w][q]=0;
    printf("Enter the Number of Edge : ");
    scanf("%d",&edge);
    printf("\n");

    for(i=1;i<=edge;i++)
    {
        scanf("%d%d",&x,&y);
        graph[x][y]=1;
    }

    printf("\n");
    printf("  ");
    for(p=1;p<=vertex;p++)
           printf("%d\t",p);
    printf("\n");
    for(i=1;i<=vertex;i++)
    {
         printf("%d ",i);
         for(j=1;j<=vertex;j++)
         {
             printf("%d\t",graph[i][j]);
         }
         printf("\n");
    }
    return 0;
}

Input
 Output

No comments: