-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbfs.cpp
More file actions
35 lines (31 loc) · 1.03 KB
/
bfs.cpp
File metadata and controls
35 lines (31 loc) · 1.03 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
#include <iostream>
#include <algorithm>
int findCandidates(Graph* graph, int targetUser, int candidates[]) {
Queue q;
int visited[MAX_USERS] = {0};
int distance[MAX_USERS] = {-1};
int candidateCount = 0;
// Initialize BFS
enqueue(&q, targetUser, 0);
visited[targetUser] = 1;
distance[targetUser] = 0;
while (!isEmpty(&q)) {
Node current = dequeue(&q);
// Explore neighbors (max 2 hops)
if (current.distance < 2) {
for (i = 0; i < graph->userCount; i++) {
if (graph->adjMatrix[current.id][i] == 1
&& !visited[i]) {
visited[i] = 1;
distance[i] = current.distance + 1;
enqueue(&q, i, distance[i]);
// Add 2-hop neighbors as candidates
if (distance[i] == 2) {
candidates[candidateCount++] = i;
}
}
}
}
}
return candidateCount;
}