Infrared
Loading...
Searching...
No Matches
graph.hpp
1#ifndef GRAPH_HPP
2#define GRAPH_HPP
3
4/*
5 * InfraRed --- A generic engine for Boltzmann sampling over constraint networks
6 * (C) Sebastian Will, 2018
7 *
8 * This file is part of the InfraRed source code.
9 *
10 * InfraRed provides a generic framework for tree decomposition-based
11 * Boltzmann sampling over constraint networks
12 */
13
20namespace ired {
21namespace graph {
24 template<class Vertex, class Edge>
26 public:
27 using vertex_descriptor_t = size_t;
28
29 using vertex_t = Vertex;
30 using edge_info_t = Edge;
31
32 class edge_t: public edge_info_t {
33 public:
36 const edge_info_t &edge_info)
37 : source_(source),
38 target_(target),
39 edge_info_t(edge_info) {
40 }
41
42 auto
43 source() const {
44 return this->source_;
45 }
46
47 auto
48 target() const {
49 return this->target_;
50 }
51
52 private:
53 vertex_descriptor_t source_;
54 vertex_descriptor_t target_;
55 };
56
59 }
60
63 add_vertex(const vertex_t &v) {
64 auto idx = vertices_.size();
65 vertices_.push_back(v);
66 adj_edges_.push_back(std::vector<edge_t>());
67 return idx;
68 }
69
73 return add_vertex(vertex_t());
74 }
75
77 auto &
80 const edge_info_t &e) {
81 assert(source < vertices_.size());
82 adj_edges_[source].push_back(edge_t(source,target,e));
83 return adj_edges_[source].back();
84 }
85
87 auto &
89 vertex_descriptor_t target) {
90 return add_edge(source, target, edge_info_t());
91 }
92
93 const auto &
95 return vertices_[i];
96 }
97
98 auto &
100 return vertices_[i];
101 }
102
104 const auto &
106 return adj_edges_[i];
107 }
108
110 auto &
112 return adj_edges_[i];
113 }
114
116 auto
118 return vertices_.size();
119 }
120
121 private:
123 std::vector<vertex_t> vertices_;
124
126 std::vector<std::vector<edge_t>> adj_edges_;
127 }; // end class adjacency_list
128
129 // Example code for dfs; implementing visitors (like e.g. in boost::graph)
130 // seems to be overkill for our simple purposes
131 //
132 // template<class Graph>
133 // void
134 // dfs(const Graph &g, Graph::vertex_descriptor_t root, std::set<Graph::vertex_descriptor_t> &visited) {
135 // if(visited.in(root)) {
136 // return;
137 // }
138 // visited.add(root);
139 //
140 // for(auto e: g.adj_edges(root)) {
141 // examine_edge(g,e);
142 // dfs(g, e.target(), visited);
143 // finish_edge(g,e);
144 // }
145 // };
146} //end namespace graph
147} //end namespace ired
148#endif // GRAPH_HPP
auto target() const
Definition graph.hpp:48
edge_t(vertex_descriptor_t source, vertex_descriptor_t target, const edge_info_t &edge_info)
Definition graph.hpp:34
auto source() const
Definition graph.hpp:43
Graph in adjacency list representation.
Definition graph.hpp:25
Edge edge_info_t
Definition graph.hpp:30
size_t vertex_descriptor_t
Definition graph.hpp:27
auto & adj_edges(vertex_descriptor_t i)
iterable of (descriptors of) adjacent edges
Definition graph.hpp:111
const auto & adj_edges(vertex_descriptor_t i) const
iterable of (descriptors of) adjacent edges
Definition graph.hpp:105
const auto & operator[](vertex_descriptor_t i) const
Definition graph.hpp:94
auto & add_edge(vertex_descriptor_t source, vertex_descriptor_t target, const edge_info_t &e)
add egde
Definition graph.hpp:78
Vertex vertex_t
Definition graph.hpp:29
vertex_descriptor_t add_vertex()
add vertex
Definition graph.hpp:72
adjacency_list()
constructor
Definition graph.hpp:58
auto size()
number of vertices
Definition graph.hpp:117
vertex_descriptor_t add_vertex(const vertex_t &v)
add vertex
Definition graph.hpp:63
auto & add_edge(vertex_descriptor_t source, vertex_descriptor_t target)
add egde
Definition graph.hpp:88
Definition assignment.hpp:29