Camargue
graph.hpp
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
7 #ifndef CMR_GRAPH_H
8 #define CMR_GRAPH_H
9 
10 #include "util.hpp"
11 
12 #include <array>
13 #include <functional>
14 #include <iostream>
15 #include <memory>
16 #include <queue>
17 #include <stdexcept>
18 #include <utility>
19 #include <vector>
20 
21 #include <cmath>
22 
23 namespace CMR {
24 
26 namespace Graph {
27 
29 struct Edge : EndPts {
30  Edge() : len(0), removable(false) {}
31 
33  Edge(int e0, int e1, int _len) :
34  EndPts(e0, e1), len(_len), removable(false) {}
35 
36  bool operator==(const Edge &rhs) const {
37  return ((end[0] == rhs.end[0]) && (end[1] == rhs.end[1]));
38  }
39 
40  int len;
41  bool removable;
42 };
43 
45 void get_elist(const std::vector<Edge> &edges, std::vector<int> &elist,
46  std::vector<int> &ecap);
47 
51 struct AdjObj {
52  AdjObj() = default;
53 
54  AdjObj(int otherend, int index, double _val) :
55  other_end(otherend), edge_index(index), val(_val) {}
56 
57  int other_end;
58  int edge_index;
59  double val;
60 
61  bool operator==(const AdjObj &rhs) const
62  {
63  return (other_end == rhs.other_end
64  && edge_index == rhs.edge_index
65  && val == rhs.val);
66  }
67 };
68 
71 struct Node {
72  Node() : mark(0) {}
73 
74  int degree() const { return neighbors.size(); }
75  std::vector<AdjObj> neighbors;
76  int mark;
77 };
78 
80 struct AdjList {
81  AdjList() = default;
82 
84  AdjList(int ncount, const std::vector<Edge> &ref_elist);
85 
87  template <typename EndPt_type>
88  AdjList(int ncount, const std::vector<EndPt_type> &elist);
89 
91  AdjList(int ncount,
92  const std::vector<Edge> &ref_elist,
93  const std::vector<double> &ref_elist_caps,
94  const std::vector<int> &keep_indices);
95 
96  AdjList(AdjList &&AL) noexcept;
97 
98  AdjList &operator=(AdjList &&AL) noexcept;
99 
101  bool connected(std::vector<int> &island, int start_node);
102 
104  void dfs(int start_node, std::vector<int> &island);
105 
108  const AdjObj *find_edge(int end0, int end1) const
109  {
110  for (const AdjObj &a : nodelist[end0].neighbors)
111  if (a.other_end == end1)
112  return &a;
113  return nullptr;
114  }
115 
116  AdjObj *find_edge(int end0, int end1)
117  {
118  for (AdjObj &a : nodelist[end0].neighbors)
119  if (a.other_end == end1)
120  return &a;
121  return nullptr;
122  }
123 
125  void add_edge(int end0, int end1, int index, double val);
126 
127  int node_count;
128  int edge_count;
129 
130  std::vector<Node> nodelist;
131 };
132 
133 
140 
143 std::vector<int> delta_inds(const std::vector<int> &node_list,
144  const std::vector<Edge> &edges,
145  int ncount);
146 
148 std::vector<int> delta_inds(const std::vector<int> &node_list,
149  const std::vector<int> &elist,
150  int ncount);
151 
153 
154 
155 
157 
162 template <typename EndPt_type>
163 AdjList::AdjList(int ncount, const std::vector<EndPt_type> &elist) try
164  : node_count(ncount), edge_count(elist.size()),
165  nodelist(std::vector<Node>(node_count))
166 {
167  for (int i = 0; i < edge_count; ++i) {
168  const auto &e = elist[i];
169 
170  nodelist[e.end[0]].neighbors.emplace_back(e.end[1], i, 0.0);
171  nodelist[e.end[1]].neighbors.emplace_back(e.end[0], i, 0.0);
172  }
173 } catch (const std::exception &e) {
174  std::cerr << e.what() << "\n";
175  throw std::runtime_error("AdjList EndPt_type constructor failed.");
176 }
177 
178 }
179 }
180 
181 #endif
Edge(int e0, int e1, int _len)
Construct by endpoints and length.
Definition: graph.hpp:33
void get_elist(const std::vector< Edge > &edges, std::vector< int > &elist, std::vector< int > &ecap)
Get a node-node elist representation of a list of edges.
bool removable
For use by certain erase-remove procedures.
Definition: graph.hpp:41
int edge_index
The index wrt a fixed Graph::Edge vector of (x, y).
Definition: graph.hpp:58
Definition: cliq.hpp:118
const AdjObj * find_edge(int end0, int end1) const
Get a pointer to the AdjObj with end points end0 and end1.
Definition: graph.hpp:108
Representing graph edges and costs.
Definition: graph.hpp:29
Utility functions, macros, and structures.
std::vector< AdjObj > neighbors
The nodes adjacent to this Node.
Definition: graph.hpp:75
A vertex in a Graph::AdjList graph.
Definition: graph.hpp:71
std::vector< int > delta_inds(const std::vector< int > &node_list, const std::vector< Edge > &edges, int ncount)
Cut set node_list, with graph specified by edges with ncount nodes.
int other_end
The node y in the notation above.
Definition: graph.hpp:57
The namespace for this project.
Definition: abc_nodesel.hpp:20
Representation of a graph as an adjacency list.
Definition: graph.hpp:80
Simple base class for storing edge of a graph as a sorted pair of nodes.
Definition: util.hpp:239
int len
The edge length.
Definition: graph.hpp:40
double val
Can represent length or some other value on the edge.
Definition: graph.hpp:59
Object used to represent adjacency in a Graph::AdjList.
Definition: graph.hpp:51
int mark
A value to be used by pricing/delta routines.
Definition: graph.hpp:76