Graph




GRAPH:

indegree and outdegree of the vertices:

#include<stdio.h>
 int main()
  {
    int a[10][10],n,j,i,out=0,in=0;
    printf("Enter the How many vertex:");
    scanf("%d",&n);
    for(i=0;i<n;i++)
     {
      for(j=0;j<n;j++)
        {
          a[i][j]=0;
           if(i!=j)
             {
               printf("Is there Edge between %d and %d (1/0):",i+1,j+1);
               scanf("%d",&a[i][j]);
             }
        }
     }
   printf("vertex indegree outdegree \n");
    for(i=0;i<n;i++)
     {
       in=out=0;
        for(j=0;j<n;j++)
          {
            in=in+a[j][i];
            out=out+a[i][j];
          }
      printf("%4d\t%4d\t%4d",i+1,in,out);
      printf("\n");
     }
 }


Enter the How many vertex:4

Is there Edge between 1 and 2 (1/0):1

Is there Edge between 1 and 3 (1/0):0

Is there Edge between 1 and 4 (1/0):1

Is there Edge between 2 and 1 (1/0):1

Is there Edge between 2 and 3 (1/0):0

Is there Edge between 2 and 4 (1/0):1

Is there Edge between 3 and 1 (1/0):1

Is there Edge between 3 and 2 (1/0):0

Is there Edge between 3 and 4 (1/0):0

Is there Edge between 4 and 1 (1/0):1

Is there Edge between 4 and 2 (1/0):0

Is there Edge between 4 and 3 (1/0):1

vertex indegree outdegree 

   1          3               2      

   2          1               2      

   3          1               1      

   4          2               2       



Implement Graph as Adjacency matrix and Adjacency graph  


#include<stdio.h>
#include<stdlib.h>
typedef struct node
  {
    int vertex;
    struct node *next;
  }NODE;
 NODE *list[10];

 int CreateMatrix(int m[10][10],int n)
  {
    int i,j;
    char ans;
    for(i=0;i<n;i++)
    for(j=0;j<n;j++)
     {
       m[i][j]=0;
        if(i!=j)
          {
            printf("\n Is there an edge Between %d and %d (1/0) :",i+1,j+1);
            scanf("%d",&m[i][j]);
          }
     }
  }
 int DisplayMatrix(int m[10][10],int n)
  {
    int i,j;
    printf("\n The Adjacency matrix is :\n");
     for(i=0;i<n;i++)
       {
         for(j=0;j<n;j++)
            {
              printf("%5d",m[i][j]);
            }
        printf("\n");
       }
  }
 
 int CreateList(int m[10][10],int n)
  {
    int i,j;
    struct node *temp, *newnode;
     for(i=0;i<n;i++)
       {
        list[i]=NULL;
        for(j=0;j<n;j++)
          {
            if(m[i][j]==1)
            {
                newnode=(NODE *)malloc(sizeof(NODE));
                newnode->vertex=j+1;
                newnode->next=NULL;
                if(list[i]==NULL)
                    list[i]=temp=newnode;
                else  
                  {
                    temp->next=newnode;
                    temp=newnode;
                  }
            }
         }
      }
  }
 int DisplayList( int m[10][10],int n)
  {
    struct node *temp;
    int i;
    printf("\n The adjacency list is :\n");
     for(i=0;i<n;i++)
       {
        printf("\nv%d->",i+1);
        temp=list[i];
         while(temp)
           {
             printf("v%d->",temp->vertex);
             temp=temp->next;
           }
        printf("NULL");
       }
  }
 
 int main()
{
    int m[10][10],n;
    printf("\n Hey..Plz Enter the number of vertices :");
    scanf("%d",&n);
    CreateMatrix(m,n);
    DisplayMatrix(m,n);
    CreateList(m,n);
    DisplayList(m,n);
}


output:


 Hey..Plz Enter the number of vertices :4


 Is there an edge Between 1 and 2 (1/0) :1


 Is there an edge Between 1 and 3 (1/0) :0


 Is there an edge Between 1 and 4 (1/0) :1


 Is there an edge Between 2 and 1 (1/0) :1


 Is there an edge Between 2 and 3 (1/0) :0


 Is there an edge Between 2 and 4 (1/0) :0


 Is there an edge Between 3 and 1 (1/0) :1


 Is there an edge Between 3 and 2 (1/0) :1


 Is there an edge Between 3 and 4 (1/0) :1    


 Is there an edge Between 4 and 1 (1/0) :0    


 Is there an edge Between 4 and 2 (1/0) :1    


 Is there an edge Between 4 and 3 (1/0) :1    


 The Adjacency matrix is :

    0    1    0    1

    1    0    0    0

    1    1    0    1

    0    1    1    0


 The adjacency list is :


v1->v2->v4->NULL

v2->v1->NULL

v3->v1->v2->v4->NULL

v4->v2->v3->NULL






..

Comments

Popular posts from this blog

Practical slips programs : Machine Learning

Full Stack Developement Practical Slips Programs

Android App Developement Practicals Programs