-
Notifications
You must be signed in to change notification settings - Fork 1
/
19_bfs.c
133 lines (115 loc) · 2.75 KB
/
19_bfs.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
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
133
#include<stdio.h>
#include<stdlib.h>
#define INF 9999
enum {WHITE,BLACK,GRAY};
// linked list data type for adjacency list
typedef struct node
{
int vertex,weight;
struct node *next;
} List ;
// each node of graph
typedef struct {
List *link;
int color,distance,parent;
} Node;
// graph data type
typedef struct {
Node nodes[100]; // Array of nodes
int n_v; // number of vertices
int n_e; // number of edges
} Graph;
// function to insert to linked list
void insert(List *link,int vertex,int weight){
// creating new node to insert
List *new=(List*) malloc(sizeof(List));
new->next= NULL ;
new -> vertex = vertex ;
new -> weight = weight ;
// List won't be empty(since create function is used ) so iterating to end of list
List *cur=link;
while ( cur -> next != NULL )
cur=cur->next;
// attaching new at end
cur -> next = new;
}
Graph inputGraph()
{
int i;
Graph g;
// filling first data in link, which is a sentinal element
printf("Enter no of vertices \n");
scanf("%d",&(g.n_v));
for(i=0;i<g.n_v;i++)
{
g.nodes[i].link = (List*) malloc(sizeof(List));
g.nodes[i].link -> next = NULL;
g.nodes[i].link -> vertex = -1;
}
printf("Enter no of edges \n");
scanf("%d",&(g.n_e));
printf("Enter %d edges each as from, to ( 0 indexed ) : \n",g.n_e );
for(i=0;i<g.n_e;i++)
{
int x,y;
scanf("%d%d",&x,&y);
insert(g.nodes[x].link,y,1);
}
return g;
}
// Queue for bfs implementation
int queue[100],front=0,rear=-1;
// simple implementation, no overflow/underflow check
// so, it's assumed that isempty wll be checked before dequeing
void enque(int n)
{
rear++;
queue[rear]=n;
}
int deque()
{
front++;
return queue[front-1];
}
int isempty(){
return front>rear;
}
void bfs(Graph *g)
{
printf("Enter Starting node : ");
int s,u,i;
scanf("%d",&s);
printf("\tBFS : \n\n| Vertex | Parent | Distance |\n");
// Algorithm exactly as in clrs textbook
for(i=0; i < g -> n_v ; i++){
g -> nodes[i].color = WHITE;
g -> nodes[i].distance = INF;
g -> nodes[i].parent = -1;
}
g->nodes[s].color=GRAY;
g->nodes[s].distance=0;
enque(s);
while(!isempty())
{
u=deque();
List *cur= g -> nodes[u].link -> next; // first one is sentinel, so next
while(cur != NULL)
{
if(g -> nodes[cur -> vertex].color == WHITE)
{
g -> nodes[cur -> vertex].color = GRAY;
g -> nodes[cur -> vertex].distance = g -> nodes[u].distance + 1;
g -> nodes[cur -> vertex].parent = u;
enque(cur -> vertex);
}
cur=cur->next;
}
g -> nodes[u].color = BLACK;
printf("| %6d | %6d | %8d |\n",u,g ->nodes[u].parent,g->nodes[u].distance );
}
}
int main(){
int i,j;
Graph g=inputGraph();
bfs(&g);
}