-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path31-bfs.c
82 lines (67 loc) · 1.86 KB
/
31-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
#include <stdio.h>
#include <stdlib.h>
/*
Funtion Name: bfs
Input Params: Graph input as matrix, number of vertices and source vertex
Return Type: void
Description: performs bfs traversal on the given graph
*/
void bfs(int m[10][10], int v, int source)
{
/// A queue will be used during the traversal
int queue[20];
int front = 0;
int rear = 0;
/// Temporary variables
int u;
int i;
/// Array to maintain the visited vertices
int visited[10];
/// Set all the vertices to not visited
for (i= 0; i < v; i ++)
visited[i] = 0;
/// Initilaize the queue with source and mark it visited
queue[rear] = source;
visited[source] = 1;
printf("The BFS Traversal is... \n");
/// Until the queue is empty
while (front <= rear)
{
/// Pick the vertex from queue front
/// This is also when we mark the traversal and print it
u = queue[front];
printf("%d\t", u);
front = front + 1;
/// All the vertices that are reachable from considered vertex
/// and not yet visited,
/// Mark them as visited and Add them to queue
for(i=0;i<v;i++)
{
if(m[u][i] == 1 && visited[i] == 0)
{
visited[i] = 1;
rear = rear + 1;
queue[rear] = i;
}
}
}
}
int main()
{
/// Variable to hold the number of vertices
int v = 5;
/// Variable to hold the graph as matrix
int m[10][10] =
{
{0, 1, 1, 0, 0},
{1, 0, 0, 1, 1},
{1, 0, 0, 0, 1},
{0, 1, 0, 0, 0},
{0, 1, 1, 0, 0}
};
/// Variable to hold source vertex
int source = 4;
/// Call the bfs traversal
bfs(m, v, source);
return 0;
}