Advertisement
Pweebs

Untitled

Nov 24th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. // BFS()
  2. // runs the BFS algorithm on the Graph G with source s,
  3. // setting the color, distance, parent, and source fields of G accordingly
  4. void BFS(Graph G, int s) {
  5.  
  6. for (int i = 0; i < getOrder(G); i++) {
  7. if (s != getSource(G)) {
  8. G->color = WHITE;
  9. G->distance[i] = INF;
  10. G->parent[i] = NIL;
  11. }
  12. }
  13.  
  14. printf("completes initial for loop\n");
  15.  
  16. G->source = s;
  17.  
  18. G->color[s] = GRAY; // discover the source S
  19. G->distance[s] = 0;
  20. G->parent[s] = NIL;
  21. List list = newList();
  22. printf("successfully initializes list = newList()\n");
  23. append(list, s); // equivalent to enqueue
  24. printf("successfully appends to list\n");
  25. while (length(list) > 0) {
  26.  
  27. printf("started while(length(list) > 0)\n");
  28. // equivalent to dequeue
  29. int x = front(list);
  30. deleteFront(list);
  31. printf("successfully deleteFront(list)\n");
  32.  
  33. List getAdjacency = G->adjacency[x];
  34. printf("successfully initializes getAdjacency\n");
  35. moveFront(getAdjacency);
  36.  
  37. while (index(getAdjacency) != -1) {
  38. int y = get(getAdjacency);
  39. printf("successfully assigns y\n");
  40. if (G->color[y] == WHITE) { // y is undiscovered
  41. G->color[y] = GRAY; // discover y
  42. G->distance[y] = G->distance[x] + 1;
  43. G->parent[y] = x;
  44. append(list, y);
  45. printf("successfully appends y\n");
  46. }
  47. moveNext(getAdjacency);
  48. }
  49. G->color[x] = BLACK; // finish x
  50. }
  51. freeList(&list);
  52. printf("successfully frees list\n");
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement