tarjan.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. //
  2. // Copyright (C) 2010 The Android Open Source Project
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. #ifndef UPDATE_ENGINE_PAYLOAD_GENERATOR_TARJAN_H_
  17. #define UPDATE_ENGINE_PAYLOAD_GENERATOR_TARJAN_H_
  18. // This is an implementation of Tarjan's algorithm which finds all
  19. // Strongly Connected Components in a graph.
  20. // Note: a true Tarjan algorithm would find all strongly connected components
  21. // in the graph. This implementation will only find the strongly connected
  22. // component containing the vertex passed in.
  23. #include <vector>
  24. #include "update_engine/payload_generator/graph_types.h"
  25. namespace chromeos_update_engine {
  26. class TarjanAlgorithm {
  27. public:
  28. TarjanAlgorithm() : index_(0), required_vertex_(0) {}
  29. // 'out' is set to the result if there is one, otherwise it's untouched.
  30. void Execute(Vertex::Index vertex,
  31. Graph* graph,
  32. std::vector<Vertex::Index>* out);
  33. private:
  34. void Tarjan(Vertex::Index vertex, Graph* graph);
  35. Vertex::Index index_;
  36. Vertex::Index required_vertex_;
  37. std::vector<Vertex::Index> stack_;
  38. std::vector<std::vector<Vertex::Index>> components_;
  39. };
  40. } // namespace chromeos_update_engine
  41. #endif // UPDATE_ENGINE_PAYLOAD_GENERATOR_TARJAN_H_