-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathShortest Distance - Unweighted Graph - BFS.c
174 lines (137 loc) · 4.26 KB
/
Shortest Distance - Unweighted Graph - 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
typedef struct Node { //Node of Adj List
int value;
struct Node* next;
int parent; //Parent to calculate distance
} Node;
typedef struct Graph { //Graph
int n;
int m;
Node** Adj;
} Graph;
typedef struct QNode { //Node of Queue
int key;
struct QNode *next;
} QNode;
typedef struct Queue { //Queue
struct QNode *front, *rear;
} Queue;
Graph *createGraph (int n);
void addEdge (Graph *G, int u, int v);
QNode *newNode (int k);
Queue *createQueue ();
void enQueue (Queue *Q, int key);
void deQueue (Queue *Q);
int bfs (Graph *G, int u);
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int t;
scanf("%d", &t);
while (t--) {
int n, m;
scanf("%d", &n);
scanf("%d", &m);
Graph *G = createGraph(n); //Create Graph
while (m--) {
int u, v;
scanf("%d", &u);
scanf("%d", &v);
addEdge(G, u-1, v-1); //Add Edge
}
int count = bfs(G, 0);
printf("%d\n", count); //Print shortest distance
}
return 0;
}
QNode *newNode (int key) { //Create newnode to add into Queue
QNode *p = (QNode *)malloc(sizeof(QNode));
p->key = key;
p->next = NULL;
return p;
}
Queue *createQueue () { //Create Queue
Queue *Q = (Queue *)malloc(sizeof(Queue));
Q->front = Q->rear = NULL;
return Q;
}
void enQueue (Queue *Q, int key) { //Enqueue in the Queue
QNode *p = newNode(key);
if (Q->rear == NULL) {
Q->front = Q->rear = p; //If queue is empty then add p as front and rear
return;
}
Q->rear->next = p; //Else add as rear
Q->rear = p;
}
void deQueue (Queue *Q) { //Dequeue in the Queue
if (Q->front == NULL) { //If queue is empty then retuen
return;
}
QNode *p = Q->front;
Q->front = Q->front->next; //Else assign front as the next of the front in the Queue
if (Q->front == NULL) { //If queue becomes empty then assign rear as NULL
Q->rear = NULL;
}
free(p); //Free the pointer
}
Graph *createGraph (int n) { //Create Graph
Graph *G = (Graph *)malloc(sizeof(Graph));
G->n = n; //Vertexes
G->m = 0; //Edges
G->Adj = (Node **)malloc(n*sizeof(Node *)); //Adj List
for (int i = 0; i < n; i++) {
G->Adj[i] = NULL; //NULL Adj List Nodes initially
}
return G;
}
void addEdge (Graph *G, int u, int v) { //Adding edge
Node* temp1 = (Node *)malloc(sizeof(Node));
temp1->value = v;
temp1->parent = u;
temp1->next = G->Adj[u];
G->Adj[u] = temp1; //Adding u - v
Node* temp2 = (Node *)malloc(sizeof(Node));
temp2->value = u;
temp2->parent = v;
temp2->next = G->Adj[v];
G->Adj[v] = temp2; //Adding v - u
G->m ++; //Incrementing number of edges
}
int bfs (Graph *G, int u) { //BFS
Queue *Q = createQueue(); //Creating queue
enQueue(Q, u); //Enque the source
int n = G->n;
int distance[n];
distance[u] = 0; //Array to store distence from source
if (u == n-1) { //If the source is equal to destination return 0
return 0;
}
int visited[n];
for (int i = 0; i < n; i++) {
visited[i] = 0; //Initialize visited array as 0
}
visited[u] = 1; //Source is visited
while (Q->front) { //While queue is not empty
int v = Q->front->key;
deQueue(Q); //Deque first element
Node *temp = G->Adj[v]; //Go to its Adj List members
while (temp) {
if (visited[temp->value] == 0) { //If it is not visited vist it and enque
enQueue(Q, temp->value);
visited[temp->value] = 1;
distance[temp->value] = distance[temp->parent] + 1; //Update its distance from the source
}
if (temp->value == n-1) { //If it is destination then retuen the distance
return distance[n-1];
}
temp = temp->next;
}
}
return distance[n-1]; //This retuen will not be called if there is a path from 1 to N (Which is assured in the question)
}