MCPcopy Create free account
hub / github.com/codemistic/Data-Structures-and-Algorithms / BFS

Method BFS

BFS in C++/BFS.cpp:26–51  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

24}
25
26void Graph::BFS(int startVertex) {
27 visited = new bool[numVertices];
28 for (int i = 0; i < numVertices; i++)
29 visited[i] = false;
30
31 list<int> queue;
32
33 visited[startVertex] = true;
34 queue.push_back(startVertex);
35
36 list<int>::iterator i;
37
38 while (!queue.empty()) {
39 int currVertex = queue.front();
40 cout << "Visited " << currVertex << " ";
41 queue.pop_front();
42
43 for (i = adjLists[currVertex].begin(); i != adjLists[currVertex].end(); ++i) {
44 int adjVertex = *i;
45 if (!visited[adjVertex]) {
46 visited[adjVertex] = true;
47 queue.push_back(adjVertex);
48 }
49 }
50 }
51}
52
53int main() {
54 Graph g(4);

Callers 1

mainFunction · 0.45

Calls 1

push_backMethod · 0.80

Tested by

no test coverage detected