Advertisement
Guest User

Untitled

a guest
Jan 21st, 2018
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.51 KB | None | 0 0
  1.    
  2. Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <list>
  7. #include <queue>
  8.  
  9. using namespace std;
  10. typedef list<list<int>> Graph;          //define a list of list of integers as graph
  11.  
  12. void addRib(Graph& g, int a, int b) {               //function to add a rib from 'a' to 'b' to a graph
  13.     for (Graph::iterator git = g.begin(); git != g.end(); git++) {
  14.         if (git->front() == a) {
  15.             git->push_back(b);
  16.             return;
  17.         }
  18.     }
  19.     list<int> vertexToAdd;            
  20.     vertexToAdd.push_back(a);
  21.     vertexToAdd.push_back(b);
  22.     g.push_back(vertexToAdd);
  23. }
  24.  
  25. Graph::iterator point(Graph& g, int a) {        //Helper function which returns an iterator, pointing to a list<int> (vertex) with first
  26.     Graph::iterator git = g.begin();            //element 'a'.  *basically returns itearator to the vertex 'a' *
  27.     for (; git != g.end(); git++) {
  28.         if (git->front() == a)
  29.             return git;
  30.     }
  31.     return git;
  32. }
  33.  
  34. bool isInList(list<int> v, int a) {         //Helper function which returns whether the number 'a' is contained into the list v.
  35.     for (list<int>::iterator vit = v.begin(); vit != v.end(); vit++)   //Not really sure if std::list has it
  36.         if (*vit == a)
  37.             return true;
  38.     return false;
  39. }
  40.  
  41. template<typename F, typename... Targs>
  42. void bfsTemplate(F f, Graph& g, Targs&... args) {       //The template function
  43.     queue<int> q;
  44.     q.push(g.front().front());     
  45.     list<int> visited;
  46.     visited.push_back(g.front().front());               //BF traversing
  47.     Graph::iterator currentVertex;
  48.     while (!q.empty()) {
  49.         currentVertex = point(g, q.front());
  50.         if (currentVertex != g.end()) {
  51.             visited.push_back(currentVertex->front());
  52.             f(currentVertex, args...);                  //execute f with first parameter currentVertex and unknown following parameters
  53.             for (list<int>::iterator vit = currentVertex->begin(); vit != currentVertex->end(); vit++) {
  54.                 if (!isInList(visited, *vit)) {
  55.                     q.push(*vit);
  56.                 }
  57.             }
  58.         }
  59.         q.pop();                                    //more BF
  60.     }
  61. }
  62.  
  63. void printTest(Graph::iterator git, int& counter) {       //Function to test the template
  64.     cout << git->front() << " ";
  65.     counter += git->front();
  66. }
  67.  
  68. int main()
  69. {
  70.     Graph g;
  71.     addRib(g, 1, 2);
  72.     addRib(g, 1, 3);
  73.     addRib(g, 2, 5);
  74.     addRib(g, 3, 7);
  75.     addRib(g, 5, 7);
  76.     addRib(g, 7, 2);   
  77.     int count = 0;
  78.     bfsTemplate(printTest, g, count);
  79.     return 0;
  80.     cout << count;
  81. }
  82.  
  83.  
  84.     //Output (1 + 2 + 3 + 5 + 7): 18
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement