Apollo 11.0
自动驾驶开放平台
connected_component_analysis.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2018 The Apollo Authors. All Rights Reserved.
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 *****************************************************************************/
17
18namespace apollo {
19namespace perception {
20namespace algorithm {
21
22void ConnectedComponentAnalysis(const std::vector<std::vector<int>>& graph,
23 std::vector<std::vector<int>>* components) {
24 if (components == nullptr) {
25 AERROR << "components is not available";
26 return;
27 }
28 int num_item = static_cast<int>(graph.size());
29 std::vector<int> visited;
30 visited.resize(num_item, 0);
31 std::queue<int> que;
32 std::vector<int> component;
33 component.reserve(num_item);
34 components->clear();
35
36 for (int index = 0; index < num_item; ++index) {
37 if (visited[index]) {
38 continue;
39 }
40 component.push_back(index);
41 que.push(index);
42 visited[index] = 1;
43 while (!que.empty()) {
44 int current_id = que.front();
45 que.pop();
46 for (size_t sub_index = 0; sub_index < graph[current_id].size();
47 ++sub_index) {
48 int neighbor_id = graph[current_id][sub_index];
49 if (visited[neighbor_id] == 0) {
50 component.push_back(neighbor_id);
51 que.push(neighbor_id);
52 visited[neighbor_id] = 1;
53 }
54 }
55 }
56 components->push_back(component);
57 component.clear();
58 }
59}
60
61} // namespace algorithm
62} // namespace perception
63} // namespace apollo
#define AERROR
Definition log.h:44
void ConnectedComponentAnalysis(const std::vector< std::vector< int > > &graph, std::vector< std::vector< int > > *components)
class register implement
Definition arena_queue.h:37