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 | 81ebcd8a | 2010-03-09 15:56:18 -0800 | [diff] [blame] | 16 | |
Alex Vakulenko | 072359c | 2014-07-18 11:41:07 -0700 | [diff] [blame] | 17 | #ifndef UPDATE_ENGINE_PAYLOAD_GENERATOR_TARJAN_H_ |
| 18 | #define UPDATE_ENGINE_PAYLOAD_GENERATOR_TARJAN_H_ |
Andrew de los Reyes | 81ebcd8a | 2010-03-09 15:56:18 -0800 | [diff] [blame] | 19 | |
Alex Vakulenko | 072359c | 2014-07-18 11:41:07 -0700 | [diff] [blame] | 20 | // This is an implementation of Tarjan's algorithm which finds all |
Andrew de los Reyes | 81ebcd8a | 2010-03-09 15:56:18 -0800 | [diff] [blame] | 21 | // Strongly Connected Components in a graph. |
| 22 | |
| 23 | // Note: a true Tarjan algorithm would find all strongly connected components |
| 24 | // in the graph. This implementation will only find the strongly connected |
| 25 | // component containing the vertex passed in. |
| 26 | |
| 27 | #include <vector> |
Alex Deymo | 161c4a1 | 2014-05-16 15:56:21 -0700 | [diff] [blame] | 28 | |
| 29 | #include "update_engine/payload_generator/graph_types.h" |
Andrew de los Reyes | 81ebcd8a | 2010-03-09 15:56:18 -0800 | [diff] [blame] | 30 | |
| 31 | namespace chromeos_update_engine { |
| 32 | |
| 33 | class TarjanAlgorithm { |
| 34 | public: |
| 35 | TarjanAlgorithm() : index_(0), required_vertex_(0) {} |
| 36 | |
| 37 | // 'out' is set to the result if there is one, otherwise it's untouched. |
| 38 | void Execute(Vertex::Index vertex, |
| 39 | Graph* graph, |
| 40 | std::vector<Vertex::Index>* out); |
Amin Hassani | 232f8f9 | 2019-01-14 16:15:31 -0800 | [diff] [blame] | 41 | |
Andrew de los Reyes | 81ebcd8a | 2010-03-09 15:56:18 -0800 | [diff] [blame] | 42 | private: |
| 43 | void Tarjan(Vertex::Index vertex, Graph* graph); |
| 44 | |
| 45 | Vertex::Index index_; |
| 46 | Vertex::Index required_vertex_; |
| 47 | std::vector<Vertex::Index> stack_; |
Ben Chan | f9cb98c | 2014-09-21 18:31:30 -0700 | [diff] [blame] | 48 | std::vector<std::vector<Vertex::Index>> components_; |
Andrew de los Reyes | 81ebcd8a | 2010-03-09 15:56:18 -0800 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | } // namespace chromeos_update_engine |
| 52 | |
Alex Vakulenko | 072359c | 2014-07-18 11:41:07 -0700 | [diff] [blame] | 53 | #endif // UPDATE_ENGINE_PAYLOAD_GENERATOR_TARJAN_H_ |