Mackey  V3.3
A C++ library for computing RO(G) graded homology
Graph.hpp
Go to the documentation of this file.
1 #pragma once
2 #include <vector>
3 #include <queue>
4 #include <set>
5 #include <iomanip>
6 
9 
10 namespace mackey {
11 
17  template<typename _node, typename _edge, template<typename ...> typename container_t, typename ...Args>
18  class Neighborhood {
19  public:
20  typedef _node node_t;
21  typedef _edge edge_t;
23  template<typename ... Argshere>
24  void insert(node_t end, Argshere&&... args);
26  void reserve(size_t n);
28  struct const_iterator {
29  auto operator ++()->const_iterator&;
30  auto node() const->node_t;
31  auto edge() const->const edge_t&;
32  bool operator!=(const_iterator other) const;
33  const_iterator() = default;
34  private:
35  typedef typename container_t<std::pair<node_t, edge_t>, Args...>::const_iterator iter_t;
36  const_iterator(iter_t it);
37  iter_t it;
38  friend class Neighborhood;
39  };
40  auto begin() const->const_iterator;
41  auto end() const->const_iterator;
42  private:
43  container_t<std::pair<node_t, edge_t>, Args...> edges;
44  };
45 
48  template<typename _neighborhood>
49  class Graph {
50  public:
51  typedef _neighborhood neighborhood_t;
52  typedef typename neighborhood_t::edge_t edge_t;
53  typedef typename neighborhood_t::node_t node_t;
54  Graph() = default;
55  size_t number_of_nodes() const;
56 
58  template<typename ... Args>
59  void draw_edge(node_t start, node_t end, Args&&... args);
60 
62  void reserve_edges_for_node(size_t num, node_t node);
63 
64  template<typename T>
66  void reserve_edges_per_node(const T& v);
67 
69  void reserve_nodes(size_t n);
70 
72  void adjoin_nodes(size_t new_number_of_nodes);
73 
75  auto neighbors(node_t node) const -> const neighborhood_t&;
76 
78  std::function<std::string(node_t)> node_names;
79 
81  void print_name_labels(std::ostream& os) const;
82  private:
83  std::vector<neighborhood_t> neighborhoods;
84  };
85 
92  template<typename graph_t, typename policy_t>
93  class ShortestPaths {
94  public:
95  typedef typename graph_t::node_t node_t;
96  typedef typename graph_t::neighborhood_t::const_iterator edge_iter_t;
97 
99  void print_path(std::ostream& os, node_t i) const;
100 
104  std::vector<std::vector<edge_iter_t>> paths;
105 
107  std::vector<node_t> root_per_path;
108 
110  void compute_with_root(node_t new_root);
111 
114  bool disconnected(node_t) const;
115 
116  protected:
118  ShortestPaths(const graph_t& G);
119 
121  const graph_t& G;
122 
124  std::vector<size_t> distance_policy;
125 
127  std::vector<std::pair<node_t, edge_iter_t>> previous;
128 
131 
132  private:
133  void print_path_minimal(std::ostream& os, node_t i, std::set<node_t>& nodes_printed) const;
134  void print_path_minimal(std::ostream& os, node_t i) const;
135  void compute_distance();
136  void compute_paths();
137  template<typename _graph, typename _pol>
138  friend std::ostream& operator<<(std::ostream&, const ShortestPaths<_graph, _pol>&);
139  };
140 
141  //Prints graph in .dot format with named nodes if provided
142  template<typename neighborhood_t>
143  std::ostream& operator<<(std::ostream& os, const Graph<neighborhood_t>& G);
144 
145  //Prints graph in .dot format with named nodes if provided
146  template<typename graph_t, typename policy_t>
147  std::ostream& operator<<(std::ostream& os, const ShortestPaths<graph_t, policy_t>& D);
148 
149 }
150 #include "impl/Graph.ipp"
A directed graph.
Definition: Graph.hpp:49
Graph()=default
void reserve_edges_per_node(const T &v)
Reserves v[i] many edges for node i.
neighborhood_t::edge_t edge_t
The edge type.
Definition: Graph.hpp:52
void print_name_labels(std::ostream &os) const
Prints names as .dot style labels to output stream.
neighborhood_t::node_t node_t
The node type.
Definition: Graph.hpp:53
std::function< std::string(node_t)> node_names
Function mapping node index to its name.
Definition: Graph.hpp:78
void draw_edge(node_t start, node_t end, Args &&... args)
Connects start->end. The arguments are forwarded to the edge constructor.
void reserve_nodes(size_t n)
...Reserves n many edges for all nodes
void reserve_edges_for_node(size_t num, node_t node)
Reserves num many edges for given node.
auto neighbors(node_t node) const -> const neighborhood_t &
The adjecency list of a node.
size_t number_of_nodes() const
void adjoin_nodes(size_t new_number_of_nodes)
Adds new nodes so that the total is new_number_of_nodes.
_neighborhood neighborhood_t
The neighborhood type.
Definition: Graph.hpp:51
The adjecency list of a node in a graph.
Definition: Graph.hpp:18
auto begin() const -> const_iterator
Starting iterator.
auto end() const -> const_iterator
Ending iterator.
_edge edge_t
Definition: Graph.hpp:21
void insert(node_t end, Argshere &&... args)
Insert node+edge in neighborhood.
_node node_t
The node type.
Definition: Graph.hpp:20
void reserve(size_t n)
Reserve number of edges.
The shortest paths from a collection of sources to all points in a graph.
Definition: Graph.hpp:93
std::vector< std::pair< node_t, edge_iter_t > > previous
previous[i]=(node,edge) where node is 1 step closer to the source and edge connects node to i
Definition: Graph.hpp:127
friend std::ostream & operator<<(std::ostream &, const ShortestPaths< _graph, _pol > &)
node_t root
The root (source) of the graph being considered.
Definition: Graph.hpp:130
ShortestPaths(const graph_t &G)
Constructor computes the shortest paths given the graph.
void compute_with_root(node_t new_root)
Compute all paths using given root.
std::vector< std::vector< edge_iter_t > > paths
The shortest paths from the sources to all nodes.
Definition: Graph.hpp:104
std::vector< size_t > distance_policy
The distance from a source to a node, computed by some policy (eg weight)
Definition: Graph.hpp:124
graph_t::neighborhood_t::const_iterator edge_iter_t
The iterator type used in the paths.
Definition: Graph.hpp:96
void print_path(std::ostream &os, node_t i) const
Print shortest path to node i to the output stream.
const graph_t & G
Constant reference to the graph.
Definition: Graph.hpp:121
bool disconnected(node_t) const
Checks if node is connected to any one of the sources.
std::vector< node_t > root_per_path
The source of each path.
Definition: Graph.hpp:107
graph_t::node_t node_t
The node type.
Definition: Graph.hpp:95
Everything in this library is under this namespace.
Definition: Box.hpp:9
std::ostream & operator<<(std::ostream &, const Chains< rank_t, diff_t > &)
Prints chain complex.
Constant iterator traversing edges with fixed start.
Definition: Graph.hpp:28
auto edge() const -> const edge_t &
auto operator++() -> const_iterator &
auto node() const -> node_t
The end node of the edge.