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

Method BFS

CPP/BFS GRAPH ALGO/BFS-GRAPH-ALGO.cpp:33–58  ·  view source on GitHub ↗

BFS algorithm

Source from the content-addressed store, hash-verified

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

Callers 1

mainFunction · 0.45

Calls 1

push_backMethod · 0.80

Tested by

no test coverage detected