You cannot copy content of this page.

## Graphs

### Program to sketch a Graph using Adjacency matrix

/*Program to sketch a graph using adjacency matrix*/

#include <stdio.h>
#include <conio.h>
#include <string.h>

#define MAX 4

struct vertex
{int num;
};

struct edge
};

struct graph
{struct vertex nodes[MAX];
struct edge edges[MAX][MAX];
}g;

int return_subscript(int val)
{int i;
for(i=0;i<MAX;i++)
{if(g.nodes[i].num==val)
return i;
}
return -1;
}
void display()
{
int i,j;
clrscr();
printf(“t”);
for(i=0;i<MAX;i++)
printf(“%dt”,g.nodes[i].num);
printf(“n”);
for(i=0;i<MAX;i++)
{printf(“%dt”,g.nodes[i].num);
for(j=0;j<MAX;j++)
printf(“n”);
}
}
void main()
{
int i,j,ans,n1,n2;
clrscr();
for(i=0;i<MAX;i++)
{printf(“nEnter vertex value?”);
scanf(“%d”,&g.nodes[i].num);
}
do
{
printf(“nEnter two nodes?”);
scanf(“%d %d”,&n1,&n2);
i=return_subscript(n1);
j=return_subscript(n2);

printf(“nDoes route exists(1/0)?”);
printf(“nContinue(1/0)?”);
scanf(“%d”,&ans);
}while(ans==1);

do
{
printf(“nNow enter two nodes numbers?”);
printf(“nI will tell you whether route exists or not…”);
scanf(“%d %d”,&n1,&n2);
i=return_subscript(n1);
j=return_subscript(n2);
while(i==-1 || j==-1)
{printf(“nInvalid numbers! Try Again.”);
scanf(“%d %d”,&n1,&n2);
i=return_subscript(n1);
j=return_subscript(n2);
}
printf(“nRoute exists…”);
else
printf(“nRoute does not exists…”);
printf(“nConfirm more entries(1/0)?”);
scanf(“%d”,&ans);
}while(ans==1);
display();
getch();
}