-
Notifications
You must be signed in to change notification settings - Fork 1
/
bfs_easier.c
103 lines (93 loc) · 1.66 KB
/
bfs_easier.c
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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct listnode
{
int vertex,weight;
struct listnode *next;
};
struct graphnode
{
int visited,distance,parent;
struct listnode head;
}graph[100];
int v,e;
void bfs(int s);
void insert (int from,int to,int w);
int main()
{
int i;
printf(" Enter number of vertices : ");
scanf("%d",&v);
printf(" Enter number of edges : ");
scanf("%d",&e);
printf(" Enter edges in from , to format :\n");
for(i=0;i<e;i++)
{
int from, to;
scanf("%d%d",&from,&to);
insert(from,to,1);
}
printf(" Enter start node : ");
int s;
scanf("%d",&s);
bfs(s);
}
int queue[100],front=0,rear=-1;
void enque(int data)
{
rear++;
queue[rear]=data;
}
int deque()
{
front++;
return queue[front-1];
}
int isempty()
{
if(front>rear)
return 1;
else
return 0;
}
void bfs(int s)
{
int i;
printf(" | vertex | parent | distance |\n");
for(i=1;i<=v;i++)
{
graph[i].visited=0;
graph[i].distance=0;
graph[i].parent=0;
}
graph[s].visited=1;
graph[s].distance=0;
graph[s].parent=0;
enque(s);
while(isempty()==0)
{
int u = deque();
printf(" %d %d %d \n",u,graph[u].parent,graph[u].distance);
struct listnode *cur= graph[u].head.next;
while( cur != NULL )
{
if(graph[cur -> vertex].visited ==0 )
{
graph[cur -> vertex].visited =1;
graph[cur -> vertex].distance = graph[u].distance +1;
graph[cur -> vertex].parent =u;
enque(cur->vertex);
}
cur =cur->next;
}
}
}
void insert(int from,int to,int w)
{
struct listnode *new = (struct listnode *) malloc(sizeof(struct listnode));
new -> vertex = to;
new -> weight = w;
new -> next = graph[from].head.next ;
graph[from].head.next = new;
}