Tuesday, August 25, 2009

GRAPH IMPLEMENTATION

//GRAPH IMPLEMENTATION
#include
#include
int graph[10][10];
int visited[10];
void intialize_graph(int no_v)
{
int i,j;
for(i=1;i {
visited[i]=0;
for(j=1;j<=no_v;j++)
graph[i][j]=0;
}
}
void print_graph(int no_v)
{
int i,j;
printf("\n The Graph Matrix Representation: \n");
for(i=1;i<=no_v;i++)
printf("\t%d",i);
for(i=1;i<=no_v;i++)
{
printf("\n %d",i);
for(j=1;j<=no_v;j++)
printf("\t%d",graph[i][j]);
}
}
void main()
{
int no_v,no_e;
int s_v,e_v,start_v;
int i,j;
clrscr();
printf("\n Enter the number of vertices in the graph:-");
scanf("%d",&no_v);
printf("\n Enter the number of edges in the graph:-");
scanf("%d",&no_e);
printf("\n Enter the adjacent vertices of each graph");
for(i=1;i<=no_e;i++)
{
printf("\n Edges: %d-->start & end vertices:",i);
scanf("%d %d",&s_v,&e_v);
graph[s_v][e_v]=1;
graph[e_v][s_v]=1;
}
print_graph(no_v);
getch();
}
/*
Output

Enter the number of vertices in the graph:-4

Enter the number of edges in the graph:-4

Enter the adjacent vertices of each graph
Edges: 1-->start & end vertices:1 2

Edges: 2-->start & end vertices:2 3

Edges: 3-->start & end vertices:3 4

Edges: 4-->start & end vertices:4 1

The Graph Matrix Representation:
1 2 3 4
1 0 1 0 1
2 1 0 1 0
3 0 1 0 1
4 1 0 1 0


*/

No comments:

Post a Comment