9Google AdSense

Adjacency List (Represent Graph on computer)

#include<stdio.h>

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

No comments: