graph_builder.h Source File

CPP API: graph_builder.h Source File
graph_builder.h
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2020-2026 MEmilio
3 *
4 * Authors: Kilian Volmer
5 *
6 * Contact: Martin J. Kuehn <Martin.Kuehn@DLR.de>
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20 #ifndef GRAPH_BUILDER_H
21 #define GRAPH_BUILDER_H
22 
23 #include "memilio/mobility/graph.h"
24 #include "memilio/utils/logging.h"
25 
26 #include <algorithm>
27 
28 namespace mio
29 {
30 
43 template <class NodePropertyT, class EdgePropertyT>
45 {
46 public:
47  using NodeProperty = NodePropertyT;
48  using EdgeProperty = EdgePropertyT;
49 
50  GraphBuilder() = default;
51  GraphBuilder(const size_t num_nodes, const size_t num_edges)
52  {
53  m_nodes.reserve(num_nodes);
54  m_edges.reserve(num_edges);
55  }
56 
64  template <class... Args>
65  void add_node(int id, Args&&... args)
66  {
67  m_nodes.emplace_back(id, std::forward<Args>(args)...);
68  }
69 
77  template <class... Args>
78  void add_edge(size_t start_node_idx, size_t end_node_idx, Args&&... args)
79  {
80  assert(m_nodes.size() > start_node_idx && m_nodes.size() > end_node_idx);
81  m_edges.emplace_back(start_node_idx, end_node_idx, std::forward<Args>(args)...);
82  }
83 
92  Graph<NodeProperty, EdgeProperty> build(bool make_unique = false) &&
93  {
94  sort_edges();
95  if (make_unique) {
97  }
98  Graph<NodeProperty, EdgeProperty> graph(std::move(m_nodes), std::move(m_edges));
99  return graph;
100  }
101 
102 private:
109  void sort_edges()
110  {
111  std::stable_sort(m_edges.begin(), m_edges.end(), [](auto&& e1, auto&& e2) {
112  return e1.start_node_idx == e2.start_node_idx ? e1.end_node_idx < e2.end_node_idx
113  : e1.start_node_idx < e2.start_node_idx;
114  });
115  }
116 
125  {
126  std::vector<Edge<EdgePropertyT>> unique_edges;
127  unique_edges.reserve(m_edges.size());
128  bool duplicate_edges = false;
129  auto curr_elem = m_edges.begin();
130 
131  while (curr_elem != m_edges.end()) {
132  auto next_elem = std::next(curr_elem);
133  if (next_elem != m_edges.end() && curr_elem->start_node_idx == next_elem->start_node_idx &&
134  curr_elem->end_node_idx == next_elem->end_node_idx) {
135  duplicate_edges = true;
136  }
137  else if (next_elem != m_edges.end()) {
138  std::copy(curr_elem, next_elem, std::back_inserter(unique_edges));
139  }
140  curr_elem = next_elem;
141  }
142  std::copy(std::prev(m_edges.end()), m_edges.end(), std::back_inserter(unique_edges));
143  m_edges = std::move(unique_edges);
144  if (duplicate_edges) {
145  mio::log_warning("Removed duplicate edge(s)");
146  }
147  }
148 
149 private:
150  std::vector<Node<NodePropertyT>> m_nodes;
151  std::vector<Edge<EdgePropertyT>> m_edges;
152 };
153 
154 } // namespace mio
155 
156 #endif // GRAPH_BUILDER_H
A builder class for constructing graphs.
Definition: graph_builder.h:45
std::vector< Node< NodePropertyT > > m_nodes
Definition: graph_builder.h:150
EdgePropertyT EdgeProperty
Definition: graph_builder.h:48
void remove_duplicate_edges()
Remove duplicate edges from a sorted edge vector.
Definition: graph_builder.h:124
GraphBuilder(const size_t num_nodes, const size_t num_edges)
Definition: graph_builder.h:51
void sort_edges()
Sort the edge vector of a graph.
Definition: graph_builder.h:109
Graph< NodeProperty, EdgeProperty > build(bool make_unique=false) &&
Build the graph from the added nodes and edges.
Definition: graph_builder.h:92
void add_edge(size_t start_node_idx, size_t end_node_idx, Args &&... args)
Add an edge to the GraphBuilder.
Definition: graph_builder.h:78
void add_node(int id, Args &&... args)
Add a node to the GraphBuilder.
Definition: graph_builder.h:65
std::vector< Edge< EdgePropertyT > > m_edges
Definition: graph_builder.h:151
NodePropertyT NodeProperty
Definition: graph_builder.h:47
GraphBuilder()=default
generic graph structure
Definition: graph.h:153
A collection of classes to simplify handling of matrix shapes in meta programming.
Definition: models/abm/analyze_result.h:30
void log_warning(spdlog::string_view_t fmt, const Args &... args)
Definition: logging.h:112