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