-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.cpp
More file actions
39 lines (32 loc) · 933 Bytes
/
Graph.cpp
File metadata and controls
39 lines (32 loc) · 933 Bytes
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
#include "Graph.h"
#include <algorithm>
#include <queue>
Graph::Graph(int vertices) : V(vertices), adjList(vertices) {}
void Graph::addEdge(int src, int dest) {
if (src < 0 || dest < 0 || src >= V || dest >= V) return;
auto& vec = adjList[src];
if (std::find(vec.begin(), vec.end(), dest) == vec.end()) {
vec.push_back(dest);
}
}
void Graph::parallelBFS(int /*startVertex*/) {
// Заглушка, как в Java-версии
}
void Graph::bfs(int startVertex) {
if (startVertex < 0 || startVertex >= V) return;
std::vector<char> visited(V, 0);
std::queue<int> q;
visited[startVertex] = 1;
q.push(startVertex);
while (!q.empty()) {
int u = q.front();
q.pop();
for (int n : adjList[u]) {
if (!visited[n]) {
visited[n] = 1;
q.push(n);
}
}
}
}
int Graph::vertices() const { return V; }