Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // BFS()
- // runs the BFS algorithm on the Graph G with source s,
- // setting the color, distance, parent, and source fields of G accordingly
- void BFS(Graph G, int s) {
- for (int i = 0; i < getOrder(G); i++) {
- if (s != getSource(G)) {
- G->color = WHITE;
- G->distance[i] = INF;
- G->parent[i] = NIL;
- }
- }
- printf("completes initial for loop\n");
- G->source = s;
- G->color[s] = GRAY; // discover the source S
- G->distance[s] = 0;
- G->parent[s] = NIL;
- List list = newList();
- printf("successfully initializes list = newList()\n");
- append(list, s); // equivalent to enqueue
- printf("successfully appends to list\n");
- while (length(list) > 0) {
- printf("started while(length(list) > 0)\n");
- // equivalent to dequeue
- int x = front(list);
- deleteFront(list);
- printf("successfully deleteFront(list)\n");
- List getAdjacency = G->adjacency[x];
- printf("successfully initializes getAdjacency\n");
- moveFront(getAdjacency);
- while (index(getAdjacency) != -1) {
- int y = get(getAdjacency);
- printf("successfully assigns y\n");
- if (G->color[y] == WHITE) { // y is undiscovered
- G->color[y] = GRAY; // discover y
- G->distance[y] = G->distance[x] + 1;
- G->parent[y] = x;
- append(list, y);
- printf("successfully appends y\n");
- }
- moveNext(getAdjacency);
- }
- G->color[x] = BLACK; // finish x
- }
- freeList(&list);
- printf("successfully frees list\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement