| 24 | } |
| 25 | |
| 26 | void 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 | |
| 53 | int main() { |
| 54 | Graph g(4); |