Learning coding means GreatToCode Be more than a Coder ! Greattocode , Join GreatToCode Community,1000+ Students Trusted On Us .If You want to learn coding, Then GreatToCode Help You.No matter what It Takes !


CODE YOUR WAY TO A MORE FULFILLING And HIGHER PAYING CAREER IN TECH, START CODING FOR FREE Camp With GreatToCode - Join the Thousands learning to code with GreatToCode
Interactive online coding classes for at-home learning with GreatToCode . Try ₹Free Per Month Coding Classes With The Top Teachers . Implementation of graph as adjaceny matrix and adhjancy list

Implementation of graph as adjaceny matrix and adhjancy list

/* Program To Read A Graph As An Adjaency Matrix & Convert
  It In Adjency List Form, Print The Edges Of The Graph 
                     */



# include <stdio.h>
# include <stdlib.h>
#include <termios.h>
struct vertex
{
 int ver;
 struct vertex *adj;
}*head[10],*t1,*t2;


int main()
{
 int n,i,j,a[10][10];
 char ch;
 printf("Enter The Number Of Vertices : ");
 scanf("%d",&n);

 for(i=1;i<=n;i++)
 {
  for(j=1;j<=n;j++)
  {
   printf("\nIs %d Is A Adjacent To %d : ",i,j);
   ch=getchar();
   if(ch=='y' || ch =='Y')
   a[i][j]=1;
   else
   a[i][j]=0;
  }
 }

 /*---------------------------Printing The Adjacency Matrix --------*/
 printf("\nThe Adjacency Matrix Is : \n");

 for(i=1;i<=n;i++)
 {
  for(j=1;j<=n;j++)
  printf("%4d",a[i][j]);
  printf("\n");
 }

 for(i=1;i<=n;i++)
 {
  head[i]=(struct vertex *)malloc(sizeof(struct vertex));
  head[i]->ver=i;
  head[i]->adj=NULL;
  t1=head[i];

  for(j=1;j<=n;j++)
  {
   if(a[i][j]==1)
   {
    t2=(struct vertex *)malloc(sizeof(struct vertex));
    t2->ver=j;
    t2->adj=NULL;

    if(head[i]->adj==NULL)
    {
    head[i]->adj=t2;
    t1=t1->adj;
    }
    else
    t1->adj=t2;
    t1= t2;
   }
  }
 }
 printf("\nThe Adjancey List Is : \n");

 for(i=1;i<=n;i++)
 {
  printf("%d : ",head[i]->ver);
  for(t1=head[i]->adj;t1!=NULL;t1=t1->adj)
  printf(" %d ==> ",t1->ver);
  printf(" NULL \n");

 }
 printf("\n************************************************************************");
 printf("\n Printing The Edges");
 printf("\n************************************************************************\n");

 for(i=1;i<=n;i++)
 {
  for(j=1;j<=n;j++)
  {
   if(a[i][j]==1)
   printf(" < %d, %d > ",i,j);
  }
 }
printf("\n************************************************************************");
return(1);
}

Post a Comment

0 Comments

•Give The opportunity to your child with GreatToCode Kid's • Online Coding Classes for Your Kid • Introduce Your kid To the world's of coding
•Fuel You Career with our 100+ Hiring Partners, Advance Your Career in Tech with GreatToCode. •Join The Largest Tech and coding Community and Fast Forward Your career with GreatToCode. •10000+ Learner's+ 90 % placement Guarantee. • Learning Coding is Better with the GreatToCode community .
•Greattocode Kid's •GreatToCode Career •GreatToCode Interview •GreatToCode Professional •GreatToCode for schools •GreatToCode For colleges •GreatToCods For Businesses.
Are you ready to join the millions of people learning to code? GreatToCode Pass is your one-stop-shop to get access to 1000+ courses, top-notch support, and successful job placement. What are you waiting for? Sign up now and get your future in motion with GreatToCode Pass.