blob: 231d63aaa6c5a03c957b0880d7ec7deb0c2a7b05 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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 Reyes35a7af12010-03-10 16:33:26 -080016
Alex Vakulenko072359c2014-07-18 11:41:07 -070017#ifndef UPDATE_ENGINE_PAYLOAD_GENERATOR_CYCLE_BREAKER_H_
18#define UPDATE_ENGINE_PAYLOAD_GENERATOR_CYCLE_BREAKER_H_
Andrew de los Reyes35a7af12010-03-10 16:33:26 -080019
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 Deymo161c4a12014-05-16 15:56:21 -070037
38#include "update_engine/payload_generator/graph_types.h"
Andrew de los Reyes35a7af12010-03-10 16:33:26 -080039
40namespace chromeos_update_engine {
41
42class CycleBreaker {
43 public:
Andrew de los Reyes182a9f52010-10-05 16:33:51 -070044 CycleBreaker() : skipped_ops_(0) {}
Andrew de los Reyes35a7af12010-03-10 16:33:26 -080045 // out_cut_edges is replaced with the cut edges.
46 void BreakCycles(const Graph& graph, std::set<Edge>* out_cut_edges);
Alex Deymo161c4a12014-05-16 15:56:21 -070047
Andrew de los Reyes182a9f52010-10-05 16:33:51 -070048 size_t skipped_ops() const { return skipped_ops_; }
Andrew de los Reyes35a7af12010-03-10 16:33:26 -080049
50 private:
51 void HandleCircuit();
52 void Unblock(Vertex::Index u);
Andrew de los Reyes21ab31c2010-08-19 14:59:00 -070053 bool Circuit(Vertex::Index vertex, Vertex::Index depth);
Andrew de los Reyes45109892010-07-26 13:49:31 -070054 bool StackContainsCutEdge() const;
Andrew de los Reyes35a7af12010-03-10 16:33:26 -080055
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 Deymo161c4a12014-05-16 15:56:21 -070063
Andrew de los Reyes182a9f52010-10-05 16:33:51 -070064 // Number of operations skipped b/c we know they don't have any
65 // incoming edges.
66 size_t skipped_ops_;
Andrew de los Reyes35a7af12010-03-10 16:33:26 -080067};
68
69} // namespace chromeos_update_engine
70
Alex Vakulenko072359c2014-07-18 11:41:07 -070071#endif // UPDATE_ENGINE_PAYLOAD_GENERATOR_CYCLE_BREAKER_H_