/
main.cpp
69 lines (57 loc) · 2.21 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <fmt/core.h>
#include <graaflib/graph.h>
#include <graaflib/io/dot.h>
#include <graaflib/types.h>
#include <filesystem>
#include <string>
/**
* @brief A user defined vertex type.
*
* Each vertex holds some arbitrary data.
*/
struct my_vertex {
int number{};
std::string name{};
};
enum class edge_priority { LOW, HIGH };
/**
* @brief A user defined edge type.
*
* Each edge has a priority and a weight set.
*/
struct my_edge {
edge_priority priority{edge_priority::LOW};
float weight{};
};
auto create_graph() {
graaf::directed_graph<my_vertex, my_edge> graph{};
const auto vertex_1{graph.add_vertex(my_vertex{10, "some data"})};
const auto vertex_2{graph.add_vertex(my_vertex{20, "some more data"})};
const auto vertex_3{graph.add_vertex(my_vertex{30, "abc"})};
const auto vertex_4{graph.add_vertex(my_vertex{40, "123"})};
const auto vertex_5{graph.add_vertex(my_vertex{50, "xyz"})};
graph.add_edge(vertex_1, vertex_2, my_edge{edge_priority::HIGH, 3.3});
graph.add_edge(vertex_2, vertex_1, my_edge{edge_priority::HIGH, 5.0});
graph.add_edge(vertex_1, vertex_3, my_edge{edge_priority::HIGH, 1.0});
graph.add_edge(vertex_3, vertex_4, my_edge{edge_priority::LOW, 2.0});
graph.add_edge(vertex_3, vertex_5, my_edge{edge_priority::HIGH, 3.0});
graph.add_edge(vertex_2, vertex_5, my_edge{edge_priority::LOW, 42.0});
return graph;
}
int main() {
const auto my_graph{create_graph()};
const auto vertex_writer{[](graaf::vertex_id_t vertex_id,
const my_vertex& vertex) -> std::string {
const auto color{vertex.number <= 25 ? "lightcyan" : "mediumspringgreen"};
return fmt::format("label=\"{}: {}\", fillcolor={}, style=filled",
vertex_id, vertex.name, color);
}};
const auto edge_writer{[](const graaf::edge_id_t& /*edge_id*/,
const auto& edge) -> std::string {
const auto style{edge.priority == edge_priority::HIGH ? "solid" : "dashed"};
return fmt::format("label=\"{}\", style={}, color=gray, fontcolor=gray",
edge.weight, style);
}};
const std::filesystem::path dof_file_path{"./dot_example.dot"};
graaf::io::to_dot(my_graph, dof_file_path, vertex_writer, edge_writer);
}