cycle_breaker.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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_CYCLE_BREAKER_H_
  17. #define UPDATE_ENGINE_PAYLOAD_GENERATOR_CYCLE_BREAKER_H_
  18. // This is a modified implementation of Donald B. Johnson's algorithm for
  19. // finding all elementary cycles (a.k.a. circuits) in a directed graph.
  20. // See the paper "Finding All the Elementary Circuits of a Directed Graph"
  21. // at http://dutta.csc.ncsu.edu/csc791_spring07/wrap/circuits_johnson.pdf
  22. // for reference.
  23. // Note: this version of the algorithm not only finds cycles, but breaks them.
  24. // It uses a simple greedy algorithm for cutting: when a cycle is discovered,
  25. // the edge with the least weight is cut. Longer term we may wish to do
  26. // something more intelligent, since the goal is (ideally) to minimize the
  27. // sum of the weights of all cut cycles. In practice, it's intractable
  28. // to consider all cycles before cutting any; there are simply too many.
  29. // In a sample graph representative of a typical workload, I found over
  30. // 5 * 10^15 cycles.
  31. #include <set>
  32. #include <vector>
  33. #include "update_engine/payload_generator/graph_types.h"
  34. namespace chromeos_update_engine {
  35. class CycleBreaker {
  36. public:
  37. CycleBreaker() : skipped_ops_(0) {}
  38. // out_cut_edges is replaced with the cut edges.
  39. void BreakCycles(const Graph& graph, std::set<Edge>* out_cut_edges);
  40. size_t skipped_ops() const { return skipped_ops_; }
  41. private:
  42. void HandleCircuit();
  43. void Unblock(Vertex::Index u);
  44. bool Circuit(Vertex::Index vertex, Vertex::Index depth);
  45. bool StackContainsCutEdge() const;
  46. std::vector<bool> blocked_; // "blocked" in the paper
  47. Vertex::Index current_vertex_; // "s" in the paper
  48. std::vector<Vertex::Index> stack_; // the stack variable in the paper
  49. Graph subgraph_; // "A_K" in the paper
  50. Graph blocked_graph_; // "B" in the paper
  51. std::set<Edge> cut_edges_;
  52. // Number of operations skipped b/c we know they don't have any
  53. // incoming edges.
  54. size_t skipped_ops_;
  55. };
  56. } // namespace chromeos_update_engine
  57. #endif // UPDATE_ENGINE_PAYLOAD_GENERATOR_CYCLE_BREAKER_H_