blob: f9e3de6a3095ebc32d0cf914e80156205d200a32 [file] [log] [blame]
Andrew de los Reyes35a7af12010-03-10 16:33:26 -08001// Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "update_engine/cycle_breaker.h"
6#include <set>
7#include <utility>
8#include "update_engine/graph_utils.h"
9#include "update_engine/tarjan.h"
10#include "update_engine/utils.h"
11
12using std::make_pair;
13using std::set;
14using std::vector;
15
16namespace chromeos_update_engine {
17
18// This is the outer function from the original paper.
19void CycleBreaker::BreakCycles(const Graph& graph, set<Edge>* out_cut_edges) {
20 cut_edges_.clear();
21
22 // Make a copy, which we will modify by removing edges. Thus, in each
23 // iteration subgraph_ is the current subgraph or the original with
24 // vertices we desire. This variable was "A_K" in the original paper.
25 subgraph_ = graph;
26
27 // The paper calls for the "adjacency structure (i.e., graph) of
28 // strong (-ly connected) component K with least vertex in subgraph
29 // induced by {s, s + 1, ..., n}".
30 // We arbitrarily order each vertex by its index in the graph. Thus,
31 // each iteration, we are looking at the subgraph {s, s + 1, ..., n}
32 // and looking for the strongly connected component with vertex s.
33
34 TarjanAlgorithm tarjan;
35
36 for (Graph::size_type i = 0; i < subgraph_.size(); i++) {
37 if (i > 0) {
38 // Erase node (i - 1) from subgraph_. First, erase what it points to
39 subgraph_[i - 1].out_edges.clear();
40 // Now, erase any pointers to node (i - 1)
41 for (Graph::size_type j = i; j < subgraph_.size(); j++) {
42 subgraph_[j].out_edges.erase(i - 1);
43 }
44 }
45
46 // Calculate SCC (strongly connected component) with vertex i.
47 vector<Vertex::Index> component_indexes;
48 tarjan.Execute(i, &subgraph_, &component_indexes);
49
50 // Set subgraph edges for the components in the SCC.
51 for (vector<Vertex::Index>::iterator it = component_indexes.begin();
52 it != component_indexes.end(); ++it) {
53 subgraph_[*it].subgraph_edges.clear();
54 for (vector<Vertex::Index>::iterator jt = component_indexes.begin();
55 jt != component_indexes.end(); ++jt) {
56 // If there's a link from *it -> *jt in the graph,
57 // add a subgraph_ edge
58 if (utils::MapContainsKey(subgraph_[*it].out_edges, *jt))
59 subgraph_[*it].subgraph_edges.insert(*jt);
60 }
61 }
62
63 current_vertex_ = i;
64 blocked_.clear();
65 blocked_.resize(subgraph_.size());
66 blocked_graph_.clear();
67 blocked_graph_.resize(subgraph_.size());
68 Circuit(current_vertex_);
69 }
70
71 out_cut_edges->swap(cut_edges_);
72 DCHECK(stack_.empty());
73}
74
75void CycleBreaker::HandleCircuit() {
76 stack_.push_back(current_vertex_);
77 CHECK_GE(stack_.size(), 2);
78 Edge min_edge = make_pair(stack_[0], stack_[1]);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070079 uint64_t min_edge_weight = kuint64max;
Andrew de los Reyes45109892010-07-26 13:49:31 -070080 const int kMaxEdgesToConsider = 4;
81 int edges_considered = 0;
Andrew de los Reyes35a7af12010-03-10 16:33:26 -080082 for (vector<Vertex::Index>::const_iterator it = stack_.begin();
83 it != (stack_.end() - 1); ++it) {
84 Edge edge = make_pair(*it, *(it + 1));
85 if (cut_edges_.find(edge) != cut_edges_.end()) {
86 stack_.pop_back();
87 return;
88 }
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070089 uint64_t edge_weight = graph_utils::EdgeWeight(subgraph_, edge);
Andrew de los Reyes35a7af12010-03-10 16:33:26 -080090 if (edge_weight < min_edge_weight) {
91 min_edge_weight = edge_weight;
92 min_edge = edge;
93 }
Andrew de los Reyes45109892010-07-26 13:49:31 -070094 edges_considered++;
95 if (edges_considered == kMaxEdgesToConsider)
96 break;
Andrew de los Reyes35a7af12010-03-10 16:33:26 -080097 }
98 cut_edges_.insert(min_edge);
99 stack_.pop_back();
100}
101
102void CycleBreaker::Unblock(Vertex::Index u) {
103 blocked_[u] = false;
104
105 for (Vertex::EdgeMap::iterator it = blocked_graph_[u].out_edges.begin();
106 it != blocked_graph_[u].out_edges.end(); ) {
107 Vertex::Index w = it->first;
108 blocked_graph_[u].out_edges.erase(it++);
109 if (blocked_[w])
110 Unblock(w);
111 }
112}
113
Andrew de los Reyes45109892010-07-26 13:49:31 -0700114bool CycleBreaker::StackContainsCutEdge() const {
115 for (std::vector<Vertex::Index>::const_iterator it = ++stack_.begin(),
116 e = stack_.end(); it != e; ++it) {
117 Edge edge = make_pair(*(it - 1), *it);
118 if (utils::SetContainsKey(cut_edges_, edge)) {
119 return true;
120 }
121 }
122 return false;
123}
124
Andrew de los Reyes35a7af12010-03-10 16:33:26 -0800125bool CycleBreaker::Circuit(Vertex::Index vertex) {
126 // "vertex" was "v" in the original paper.
127 bool found = false; // Was "f" in the original paper.
128 stack_.push_back(vertex);
129 blocked_[vertex] = true;
130
131 for (Vertex::SubgraphEdgeMap::iterator w =
132 subgraph_[vertex].subgraph_edges.begin();
133 w != subgraph_[vertex].subgraph_edges.end(); ++w) {
134 if (*w == current_vertex_) {
135 // The original paper called for printing stack_ followed by
136 // current_vertex_ here, which is a cycle. Instead, we call
137 // HandleCircuit() to break it.
138 HandleCircuit();
139 found = true;
140 } else if (!blocked_[*w]) {
Andrew de los Reyes45109892010-07-26 13:49:31 -0700141 if (Circuit(*w)) {
Andrew de los Reyes35a7af12010-03-10 16:33:26 -0800142 found = true;
Andrew de los Reyes45109892010-07-26 13:49:31 -0700143 if (StackContainsCutEdge())
144 break;
145 }
Andrew de los Reyes35a7af12010-03-10 16:33:26 -0800146 }
147 }
148
149 if (found) {
150 Unblock(vertex);
151 } else {
152 for (Vertex::SubgraphEdgeMap::iterator w =
153 subgraph_[vertex].subgraph_edges.begin();
154 w != subgraph_[vertex].subgraph_edges.end(); ++w) {
Andrew de los Reyes8a51e5c2010-07-26 13:40:03 -0700155 if (blocked_graph_[*w].out_edges.find(vertex) ==
156 blocked_graph_[*w].out_edges.end()) {
157 blocked_graph_[*w].out_edges.insert(make_pair(vertex,
158 EdgeProperties()));
159 }
Andrew de los Reyes35a7af12010-03-10 16:33:26 -0800160 }
161 }
162 CHECK_EQ(vertex, stack_.back());
163 stack_.pop_back();
164 return found;
165}
166
167} // namespace chromeos_update_engine