mfgnik

Untitled

Apr 25th, 2020
493
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.02 KB | None | 0 0
  1. template<class VisitorClass>
  2. void DFSVisit(const Graph &graph, int vertex, VisitorClass visitor, std::vector<bool> *used) {
  3.     (*used)[vertex] = true;
  4.     visitor.OnStartVertexVisit(vertex);
  5.     for (auto edge: graph.GetOutcomingEdgesList(vertex)) {
  6.         if (!(*used)[edge.to]) {
  7.             DFSVisit(graph, edge.to, visitor, used);
  8.         }
  9.     }
  10.     visitor.OnEndVertexVisit(vertex);
  11. }
  12.  
  13.  
  14. template<class VisitorClass>
  15. void DFS(const Graph &graph, const std::vector<int> &vertice_order, VisitorClass visitor) {
  16.     std::vector<bool> used(graph.GetNumberOfVertices());
  17.     for (auto vertex: vertice_order) {
  18.         if (!used[vertex]) {
  19.             visitor.OnStartVertex(vertex);
  20.             DFSVisit(graph, vertex, visitor, &used);
  21.         }
  22.     }
  23. }
  24.  
  25.  
  26. template<class VisitorClass>
  27. void DFS(const Graph &graph, VisitorClass visitor) {
  28.     std::vector<int> standart_order(graph.GetNumberOfVertices());
  29.     std::iota(standart_order.begin(), standart_order.end(), 0);
  30.     DFS(graph, standart_order, visitor);
  31. }
Advertisement
Add Comment
Please, Sign In to add comment