My Profile

I, Das ShrikKrishna J. MCA III IMSCD&R, Ahmednagar.
Showing posts with label Create an Adjacency Matrix. Show all posts
Showing posts with label Create an Adjacency Matrix. Show all posts

Monday, 15 August 2011

Create an Adjacency Matrix

Write a Program in C to Create an Adjacency Matrix
#include<stdio.h>
#include<conio.h>
void main()
{
int edges,i,j,tail,head,gtype,adj[10][10],n;
clrscr();
printf("Enter number of nodes : ");
scanf("%d",&n);
printf("1) Directed \n2) Undirected \nChoice : ");
scanf("%d",&gtype);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
adj[i][j]=0; //Initialize the array of n*n size with 0
}
}
if(gtype==2)
{
edges=n*(n-1)/2; //maximum no. of edges in undirected
}
else
{
edges=n*(n-1); //maximum no. of edges in directed
}
printf("\nEnter -1 -1 to exit\n\n");
for(i=1;i<=edges;i++)
{
printf("Edge %d : ",i);
scanf("%d %d",&tail,&head);
if(tail==-1||head==-1)
break;
if(tail>n||head>n||tail<=0||head<=0)
{
printf("Invalid edge!\n");
i--;
}
else
{
adj[tail][head]=1;
if(gtype==2) //undirected
{
adj[head][tail]=1;
}
}
}
printf("\nAdjacency Matrix \n\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("\t%d",adj[i][j]);
}
printf("\n");
}
getch();
}

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | cheap international calls