Skip to content

Commit 30158d4

Browse files
committed
very stupid shortest path algorithm
1 parent a1bd406 commit 30158d4

File tree

3 files changed

+33
-51
lines changed

3 files changed

+33
-51
lines changed

include/reactor-cpp/graph.hh

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ namespace reactor {
1919
template <class E, class P> class Graph {
2020
private:
2121
std::map<E, std::vector<std::pair<P, E>>> graph_;
22-
std::size_t index{0};
23-
std::map<E, std::string> name_map{};
22+
std::set<E> nodes_{};
2423

2524
// custom compare operator this is required if u want special key values in std::map
2625
// this is required for the Graph::get_edges() method
@@ -54,6 +53,8 @@ public:
5453

5554
// adds a single edge to the graph structure
5655
void add_edge(E source, E destination, P properties) noexcept {
56+
nodes_.insert(source);
57+
nodes_.insert(destination);
5758
if (graph_.find(source) == std::end(graph_)) {
5859
Path edges{std::make_pair(properties, destination)};
5960
graph_[source] = edges;
@@ -62,6 +63,8 @@ public:
6263
}
6364
}
6465

66+
auto get_nodes() -> std::set<E> { return nodes_; }
67+
6568
// this groups connections by same source and properties
6669
[[nodiscard]] auto get_edges() const noexcept -> std::map<map_key, std::vector<E>, map_key_compare> {
6770
std::map<map_key, std::vector<E>, map_key_compare> all_edges{};
@@ -89,39 +92,16 @@ public:
8992
return keys;
9093
}
9194

92-
auto name_resolver(E object) -> std::string {
93-
char names[] = "ABCDEFGHIJKLMNOPQRSTUVGXYZabcdefghijklmnopqrstuvgxyz";
94-
if(name_map.find(object) == std::end(name_map)) {
95-
name_map[object] = names[index];
96-
index++;
97-
return std::string{names[index - 1], 1};
98-
}
99-
return name_map[object];
100-
}
101-
10295
// the return type looks a little bit cursed what is happening here ?
10396
// we have a map from the destination as a key to a list of paths through the graph.
10497
// A path here is modelled by a list of edges (with properties and the next vertex).
10598
auto naive_spanning_tree(E source) noexcept -> std::vector<std::vector<std::pair<P, E>>> {
106-
auto result = recursive_spanning_tree(source, std::vector<E>{});
107-
108-
std::string mermaid_string = "graph TD;\n";
109-
110-
for (auto path : result ) {
111-
mermaid_string += std::string(" ") + name_resolver(source);
112-
for (auto edge : path) {
113-
mermaid_string += std::string("-->")+ name_resolver(edge.second);
114-
}
115-
mermaid_string += std::string(";\n");
116-
117-
}
118-
//std::cout << "EDGE: \n" << mermaid_string << std::endl;
119-
return result;
99+
return recursive_spanning_tree(source, std::vector<E>{});
120100
}
121101

122-
123102
// this function goes recursively though the graph and tries to find every possible path
124-
auto recursive_spanning_tree(E source_node, std::vector<E> visited_nodes) -> std::vector<std::vector<std::pair<P, E>>> {
103+
auto recursive_spanning_tree(E source_node, std::vector<E> visited_nodes)
104+
-> std::vector<std::vector<std::pair<P, E>>> {
125105
std::vector<Path> paths{};
126106

127107
if (graph_[source_node].empty()) {
@@ -138,7 +118,7 @@ public:
138118
auto temp_nodes = visited_nodes;
139119
temp_nodes.push_back(current_node);
140120

141-
for (auto path: recursive_spanning_tree(current_node, temp_nodes)) {
121+
for (auto path : recursive_spanning_tree(current_node, temp_nodes)) {
142122
path.insert(std::begin(path), child);
143123
paths.push_back(path);
144124
}
@@ -154,9 +134,8 @@ public:
154134
auto spanning_tre = naive_spanning_tree(source);
155135
std::vector<Path> relevant_paths{};
156136

157-
std::copy_if(spanning_tre.begin(), spanning_tre.end(), std::back_inserter(relevant_paths), [destination](Path path) {
158-
return (*path.end()).second == destination;
159-
});
137+
std::copy_if(spanning_tre.begin(), spanning_tre.end(), std::back_inserter(relevant_paths),
138+
[&, destination](Path path) { return path[path.size() - 1].second == destination; });
160139

161140
if (relevant_paths.empty()) {
162141
return std::nullopt;
@@ -185,12 +164,25 @@ public:
185164
}
186165
}
187166

188-
auto to_mermaid() noexcept -> std::string {
167+
auto to_mermaid() const noexcept -> std::string {
168+
std::size_t index{0};
169+
std::map<E, std::string> name_map{};
189170
std::string mermaid_string = "graph TD;\n";
190171

172+
auto name_resolver = [&](E object) -> std::string {
173+
char names[] = "ABCDEFGHIJKLMNOPQRSTUVGXYZabcdefghijklmnopqrstuvgxyz";
174+
if (name_map.find(object) == std::end(name_map)) {
175+
name_map[object] = names[index];
176+
index++;
177+
return std::string{names[index - 1], 1};
178+
}
179+
return name_map[object];
180+
};
181+
191182
for (const auto& [source, destinations] : graph_) {
192183
for (auto dest : destinations) {
193-
mermaid_string += std::string(" ") + name_resolver(source) + std::string("-->") + name_resolver(dest.second) + std::string(";\n");
184+
mermaid_string += std::string(" ") + name_resolver(source) + std::string("-->") +
185+
name_resolver(dest.second) + std::string(";\n");
194186
}
195187
}
196188
return mermaid_string;

lib/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ else()
2222
set(REACTOR_CPP_INCLUDE "include")
2323
endif()
2424

25-
add_library(${LIB_TARGET} STATIC ${SOURCE_FILES})
25+
add_library(${LIB_TARGET} SHARED ${SOURCE_FILES})
2626
target_include_directories(${LIB_TARGET} PUBLIC
2727
"$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>"
2828
"$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/include>"

lib/environment.cc

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,9 @@ void Environment::optimize() {
6868
#else
6969
constexpr bool enable_optimizations = false;
7070
#endif
71-
std::cout << "OPTIMIZATIONS: " << enable_optimizations << std::endl;
7271
if constexpr (enable_optimizations) {
73-
std::cout << graph_.to_mermaid() << std::endl;
7472
expand_and_merge();
7573
strip_and_optimize();
76-
77-
std::cout << optimized_graph_.to_mermaid() << std::endl;
7874
} else {
7975
// no optimizations
8076
optimized_graph_ = graph_;
@@ -155,9 +151,7 @@ void Environment::expand_and_merge() {
155151
ConnectionProperties merged_properties{};
156152
auto* current_source = source;
157153

158-
std::cout << std::string(" ") << graph_.name_resolver(source);
159154
for (auto element : path) {
160-
std::cout << std::string("-->") << graph_.name_resolver(element.second);
161155
auto property = element.first;
162156

163157
auto return_type =
@@ -180,30 +174,26 @@ void Environment::expand_and_merge() {
180174
merged_properties.delay_ += property.delay_;
181175

182176
// updating target enclave if not nullptr
183-
merged_properties.enclave_ =
184-
(property.enclave_ != nullptr) ? property.enclave_ : merged_properties.enclave_;
177+
merged_properties.enclave_ = (property.enclave_ != nullptr) ? property.enclave_ : merged_properties.enclave_;
185178

186179
optimized_graph_.add_edge(current_source, element.second, merged_properties);
187180
}
188181
}
189-
std::cout << std::endl;
190182
}
191183
}
192184
}
193185

194186
void Environment::strip_and_optimize() {
195187
Graph<BasePort*, ConnectionProperties> striped_graph{};
196188

197-
auto nodes = optimized_graph_.keys();
189+
auto nodes = optimized_graph_.get_nodes();
198190
std::vector<BasePort*> has_downstream_reactions{};
199-
std::copy_if(nodes.begin(), nodes.end(), std::back_inserter(has_downstream_reactions), [](BasePort* port) {
200-
return port->has_anti_dependencies();
201-
});
191+
std::copy_if(nodes.begin(), nodes.end(), std::back_inserter(has_downstream_reactions),
192+
[](BasePort* port) { return port->has_anti_dependencies(); });
202193

203194
std::vector<BasePort*> has_triggers{};
204-
std::copy_if(nodes.begin(), nodes.end(), std::back_inserter(has_triggers), [](BasePort* port) {
205-
return port->has_dependencies();
206-
});
195+
std::copy_if(nodes.begin(), nodes.end(), std::back_inserter(has_triggers),
196+
[](BasePort* port) { return port->has_dependencies(); });
207197

208198
for (auto downstream : has_downstream_reactions) {
209199
for (auto upstream : has_triggers) {

0 commit comments

Comments
 (0)