huangcm
2024-10-12 d3acb07ae52cd1e07661d853cb07895d324a847f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//
// Copyright (C) 2010 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
 
#ifndef UPDATE_ENGINE_PAYLOAD_GENERATOR_CYCLE_BREAKER_H_
#define UPDATE_ENGINE_PAYLOAD_GENERATOR_CYCLE_BREAKER_H_
 
// This is a modified implementation of Donald B. Johnson's algorithm for
// finding all elementary cycles (a.k.a. circuits) in a directed graph.
// See the paper "Finding All the Elementary Circuits of a Directed Graph"
// at http://dutta.csc.ncsu.edu/csc791_spring07/wrap/circuits_johnson.pdf
// for reference.
 
// Note: this version of the algorithm not only finds cycles, but breaks them.
// It uses a simple greedy algorithm for cutting: when a cycle is discovered,
// the edge with the least weight is cut. Longer term we may wish to do
// something more intelligent, since the goal is (ideally) to minimize the
// sum of the weights of all cut cycles. In practice, it's intractable
// to consider all cycles before cutting any; there are simply too many.
// In a sample graph representative of a typical workload, I found over
// 5 * 10^15 cycles.
 
#include <set>
#include <vector>
 
#include "update_engine/payload_generator/graph_types.h"
 
namespace chromeos_update_engine {
 
class CycleBreaker {
 public:
  CycleBreaker() : skipped_ops_(0) {}
  // out_cut_edges is replaced with the cut edges.
  void BreakCycles(const Graph& graph, std::set<Edge>* out_cut_edges);
 
  size_t skipped_ops() const { return skipped_ops_; }
 
 private:
  void HandleCircuit();
  void Unblock(Vertex::Index u);
  bool Circuit(Vertex::Index vertex, Vertex::Index depth);
  bool StackContainsCutEdge() const;
 
  std::vector<bool> blocked_;         // "blocked" in the paper
  Vertex::Index current_vertex_;      // "s" in the paper
  std::vector<Vertex::Index> stack_;  // the stack variable in the paper
  Graph subgraph_;                    // "A_K" in the paper
  Graph blocked_graph_;               // "B" in the paper
 
  std::set<Edge> cut_edges_;
 
  // Number of operations skipped b/c we know they don't have any
  // incoming edges.
  size_t skipped_ops_;
};
 
}  // namespace chromeos_update_engine
 
#endif  // UPDATE_ENGINE_PAYLOAD_GENERATOR_CYCLE_BREAKER_H_