-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBFS_in_graph.c
More file actions
132 lines (132 loc) · 2.01 KB
/
BFS_in_graph.c
File metadata and controls
132 lines (132 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
int nv,ne;
struct neel
{
int x;
struct neel *y;
}*first,*s,*u;
void edges();
int ifexist(int,struct neel *);
struct neel * insend(int,struct neel *);
void vertices();
int search(int,struct neel *,int);
int getnext(struct neel **);
void bfs();
void main()
{
vertices();
edges();
s=insend(first->x,s);
printf("\nBreadth First Search Sequence :- ");
bfs();
getch();
}
struct neel * insend(int a,struct neel *f)
{
struct neel *p;
if(f==NULL)
{
f=(struct neel*)malloc(sizeof(struct neel));
f->x=a;
f->y=NULL;
return f;
}
p=f;
for(;p->y!=NULL;)
p=p->y;
p->y=(struct neel*)malloc(sizeof(struct neel));
p=p->y;
p->x=a;
p->y=NULL;
return f;
}
void vertices()
{
int i;
printf("enter the number of vertices...");
scanf("%d",&nv);
first=(struct neel*)calloc(nv,sizeof(struct neel));
for(i=0;i<nv;i++)
{
printf("\nenter %d vertex..",i+1);
scanf("%d",&(first+i)->x);
}
}
void edges()
{
int h,x,y;
printf("enter number of edges...");
scanf("%d",&ne);
for(h=0;h<ne;h++)
{
printf("\nenter starting & ending vertices of %d edge..",h+1);
scanf("%d %d",&x,&y);
{
int t,u;
if(!search(x,first,nv))
{
printf("enter valid vertices..");
h--;
continue;
}
for(t=0;(first+t)->x!=x;t++);
if(!search(y,first,nv))
{
printf("enter valid vertices..");
h--;
continue;
}
for(u=0;(first+u)->x!=y;u++);
insend(y,first+t);
insend(x,first+u);
}
}
}
int search(int z,struct neel *f,int l)
{
int i;
for(i=0;i<l;i++)
if((f+i)!=NULL&&((f+i)->x==z))
return 1;
return 0;
}
void bfs()
{
struct neel *p;
neel:if(!getnext(&p))
return;
if(ifexist(p->x,u))
goto neel;
printf("%d ",p->x);
u=insend(p->x,u);
p=p->y;
for(;p!=NULL;)
{
s=insend(p->x,s);
p=p->y;
}
p=NULL;
bfs();
}
int getnext(struct neel **f)
{
int i;
if(s==NULL)
return 0;
for(i=0;(first+i)->x!=s->x;i++);
*f=first+i;
s=s->y;
return 1;
}
int ifexist(int a,struct neel *p)
{
for(;p!=NULL;)
{
if(p->x==a)
return 1;
p=p->y;
}
return 0;
}