Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void bfs(Graph *graph, Node *s, Operation *op)
- {
- // TOOD: implement the BFS algorithm on the graph, starting from the node s
- // at the end of the algorithm, every node reachable from s should have the color BLACK
- // for all the visited nodes, the minimum distance from s (dist) and the parent in the BFS tree should be set
- // for counting the number of operations, the optional op parameter is received
- // since op can be NULL (when we are calling the bfs for display purposes), you should check it before counting:
- // if(op != NULL) op->count();
- int poz = 0;
- for (int w = 0; w < graph->nrNodes; w++) {
- if (graph->v[w] == s) {
- graph->v[w]->color = 1;
- break;
- }
- }
- list<Node*> coada;
- coada.push_back(s);
- while (!coada.empty()) {
- Node* u = coada.front();
- coada.pop_front();
- for (int w = 0; w < u->adjSize; w++) {
- if (u->adj[w]->color == COLOR_WHITE) {
- for (int j = 0; j < graph->nrNodes; j++) {
- if (graph->v[j] == u->adj[w]) {
- poz = j;
- break;
- }
- }
- u->color = COLOR_GRAY;
- //graph->v[poz]->color = COLOR_GRAY;
- graph->v[poz]->dist = u->dist + 1;
- graph->v[poz]->parent = u;
- coada.push_back(u->adj[w]);
- }
- }
- graph->v[poz]->color = COLOR_BLACK;
- u->color = COLOR_BLACK;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement