9Google AdSense

Find Indegree and Outdegree of all vertex of a directed graph.

#include<stdio.h>

int main()
{
    int graph[50][50];
    int i,j,edge,vertex,p,x,y,indegree1[90]={0},outdegree1[90]={0},a,q,w;
    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++)
    {
         for(j=1;j<=vertex;j++)
         {
              if(graph[i][j]==1)
              {
                    outdegree1[i]++;
                    indegree1[j]++;
              }
         }

    }
    printf("Indegree\n");
    for(p=1;p<=vertex;p++)
    {
        printf("%d : %d\n",p,indegree1[p]);
    }
    printf("Outdegree\n");
    for(a=1;a<=vertex;a++)
    {
        printf("%d : %d\n",a,outdegree1[a]);
    }

    return 0;
}


Input/Output

 

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;
}

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