Advertisement
MRtecno98

Graph Visitor

Feb 11th, 2020
424
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.63 KB | None | 0 0
  1. #include <vector>
  2. #include <functional>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. typedef vector<vector<int>> graph;
  8.  
  9. void _visit_graph(graph g, int start, vector<int> *visited, function<void(int)> action) {
  10.     if(find(visited->begin(), visited->end(), start) != visited->end()) return;
  11.     action(start);
  12.     visited->push_back(start);
  13.     for_each(g.at(start).begin(), g.at(start).end(),
  14.              [&g, &visited, &action] (int next) { _visit_graph(g, next, visited, action); });
  15. }
  16.  
  17. void visit_graph(graph g, int start, function<void(int)> action) {
  18.     vector<int> v;
  19.     return _visit_graph(g, start, &v, action);
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement