Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.89 KB | None | 0 0
  1. /*
  2.     Stewart, Garrett
  3.     Benjamin, Michael
  4.     Kakar, David
  5.  
  6.     February 12, 2020
  7.  
  8.     Lab 2
  9.     CS A200
  10. */
  11. #include <iostream>
  12. #include <queue>
  13. using namespace std;
  14.  
  15.  
  16. void bfs(const string n[], const int matrix[][10], const string& name, const int numOfElem)
  17. {
  18.     queue<int> nameQueue;
  19.     string traversal = "";
  20.    
  21.     int startVal;
  22.     int nameLen = numOfElem;
  23.     int visited[7] = { 0 };
  24.     int i = 0;
  25.     bool found = false;
  26.  
  27.     while (i < nameLen && !found)
  28.     {
  29.         if (name == n[i])
  30.         {
  31.             found = true;
  32.             startVal = i;
  33.             visited[i] = 2;
  34.             traversal += n[i] + ", ";
  35.         }
  36.         i++;
  37.     }
  38.  
  39.     if (!found)
  40.         cout << "Name not found" << endl;
  41.     else
  42.     {
  43.         do
  44.         {
  45.             for (int i = 0; i < nameLen; i++)
  46.             {
  47.                 if (matrix[startVal][i] == 1 && visited[i] == 0)
  48.                 {
  49.                     nameQueue.push(i);
  50.                     visited[i] = 1;
  51.                 }
  52.             }
  53.  
  54.             traversal += n[nameQueue.front()] + ", ";
  55.             visited[nameQueue.front()] = 2;
  56.             startVal = nameQueue.front();
  57.             nameQueue.pop();
  58.  
  59.         } while (!nameQueue.empty());
  60.     }
  61.     cout << traversal;
  62. }
  63.  
  64. int main()
  65. {
  66.     int numOfElem = 7;
  67.     string names[10] = {"Bill", "Cinderella", "Jane", "Jill", "Martha", "Sean", "Susan"};
  68.     int adjMatrix[10][10] = {0};
  69.  
  70.     //for (int i = 0; i < numOfElem; i++)
  71.     //  cout << names[i] << " ";
  72.     //Bill
  73.     adjMatrix[0][2] = 1;
  74.     adjMatrix[0][3] = 1;
  75.     adjMatrix[0][5] = 1;
  76.     //Cinderella
  77.     adjMatrix[1][3] = 1;
  78.     adjMatrix[1][4] = 1;
  79.     adjMatrix[1][5] = 1;
  80.     adjMatrix[1][6] = 1;
  81.     //Jane
  82.     adjMatrix[2][0] = 1;
  83.     adjMatrix[2][4] = 1;
  84.     adjMatrix[2][6] = 1;
  85.     //Jill
  86.     adjMatrix[3][0] = 1;
  87.     adjMatrix[3][1] = 1;
  88.     //Martha
  89.     adjMatrix[4][1] = 1;
  90.     adjMatrix[4][2] = 1;
  91.     //Sean
  92.     adjMatrix[5][0] = 1;
  93.     adjMatrix[5][1] = 1;
  94.     //Sean
  95.     adjMatrix[6][1] = 1;
  96.     adjMatrix[6][2] = 1;
  97.  
  98.     bfs(names, adjMatrix, "Susan", numOfElem);
  99.     cout << endl;
  100.     bfs(names, adjMatrix, "Bill", numOfElem);
  101.     cout << endl;
  102.     bfs(names, adjMatrix, "Cinderella", numOfElem);
  103.     cout << endl;
  104.    
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement