-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.h
More file actions
38 lines (34 loc) · 1.01 KB
/
graph.h
File metadata and controls
38 lines (34 loc) · 1.01 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
36
37
38
#include <queue>
using namespace std;
bool
is_connected()
{
bool visited[numNodes_];
for (auto i = 0; i < numNodes_; i++) { visited[i] = false; }
queue<int> q;
q.push(0);
while (q.size() > 0) {
// pop the first element from the queue
auto id = q.pop();
// check if the element is not been visited yet
if (visited[id] == true) {
continue;
}
// if not visited yet, mark it visited and iterate over its netigbor list
visited[id] = true;
while ((neighbor = g.next_neighbor(id)) != nullptr) {
// add neighbors to the queue, if they have not been visited yet.
if (visited[neighbor.first] == false) {
q.push(neighbor.first);
}
}
}
// check the number of elements to have been visited
int nodesVisited = 0;
for (auto i = 0; i < numNodes_; i++) {
if (visited[i] = true) {
nodesVisited++;
}
}
return (nodesVisited == numNodes_);
}