Bridges-C++ 3.5.0
Bridges(C++ API)
GraphAdjMatrix.h
Go to the documentation of this file.
1#ifndef GRAPH_ADJ_MATRIX_H
2#define GRAPH_ADJ_MATRIX_H
3
4#include <stdexcept> //out of range
5#include <sstream> //stringstream
6
7#include "Element.h" //DataStructure, unordered_map, string, cerr, using std
8
9namespace bridges {
10 namespace datastructure {
36 template <typename K, typename E1 = K, typename E2 = E1>
38 private:
39 unordered_map<K, Element<E1>* > vertices; // graph vertices
40 unordered_map<K, unordered_map<K, int> > matrix; // adjacency matrix
41 // maintain edge specific data
42 unordered_map<K, unordered_map<K, E2> > edge_data;
43
44 public:
48 virtual const string getDStype() const override {
49 return "GraphAdjacencyMatrix";
50 }
51
52 virtual ~GraphAdjMatrix() {
53 for (auto it : vertices)
54 delete it.second;
55 }
56 GraphAdjMatrix() = default;
57
58 // The default version of these functions would be incorrect.
59 // So marking them delete to avoid problems.
60 // We could write them if necessary
63
71 void addVertex(const K& k, const E1& e = E1()) {
72 stringstream conv;
73 conv << k; //Converts key into string
74 vertices[k] = new Element<E1>(e, conv.str());
75 for (const auto& p : vertices) {
76 // edge weights are 0
77 matrix[k][p.first] = matrix[p.first][k] = 0;
78 }
79 }
80
91 void addEdge(const K& src, const K& dest, const unsigned int& wt) {
92 try { //create default link data
93 vertices.at(src)->links[vertices.at(dest)];
94 // add edge
95 matrix.at(src).at(dest) = wt;
96 }
97 catch (const out_of_range& ) {
98 cerr << "Cannot addEdge between non-existent verticies." << endl;
99 throw;
100 }
101 }
106 const unordered_map<K, unordered_map<K, int >> & getMatrix() const {
107 return matrix;
108 }
109
119 const unordered_map<K, int>& getMatrix(K key) const {
120 return matrix.at(key);
121 }
122
127 unordered_map<K, Element<E1> *>* getVertices() {
128 return &vertices;
129 }
134 const Element<E1>* getVertex(const K& key) const {
135 return vertices.at(key);
136 }
137
143 Element<E1>* getVertex(const K& key) {
144 return vertices.at(key);
145 }
153 E1 getVertexData (const K& src) {
154 try {
155 Element<E1> *el = vertices.at(src);
156 return (vertices.at(src))->getValue();
157 }
158 catch ( const out_of_range& ) {
159 cerr << "getVertexData(): vertex not found" << endl;
160 throw;
161 }
162 // should never reach here
163 throw "getVertexData(): vertex not found";
164 }
171 void setVertexData (const K& vID, const E1& data) {
172 try {
173 Element<E1> *el = vertices.at(vID);
174 el->setValue (data);
175 }
176 catch ( const out_of_range& ) {
177 cerr << "setVertexData(): Nonexistent vertices or " <<
178 " edge not found" << endl;
179 throw;
180 }
181 catch (const char* msg) {
182 cerr << msg << endl;
183 }
184 }
193 E2 const & getEdgeData (const K& src, const K& dest) const {
194 try {
195 vertices.at(src);
196 vertices.at(dest);
197 return edge_data[src][dest];
198 }
199 catch ( const out_of_range& oor) {
200 cerr << "getEdgeData(): Nonexistent vertices or " <<
201 " edge not found" << endl;
202 throw;
203 }
204 catch (const char* msg) {
205 cerr << msg << endl;
206 throw;
207 }
208 // should never reach here
209 throw "getEdgeData(): Edge not found";
210 }
218 void setEdgeData (const K& src, const K& dest, const E2& data) {
219 try {
220 vertices.at(src);
221 vertices.at(dest);
222 edge_data[src][dest] = data;
223 }
224 catch ( const out_of_range& oor) {
225 cerr << "setEdgeData(): Nonexistent vertices or " <<
226 " edge not found" << endl;
227 throw;
228 }
229 catch (const char* msg) {
230 cerr << msg << endl;
231 throw;
232 }
233 }
243 try {
244 Element<E1> *el = vertices.at(k);
245
246 return el->getVisualizer();
247 }
248 catch (const out_of_range& oor) {
249 cerr << "Graph vertex " << k << " not found in graph!" << endl;
250 throw;
251 }
252 }
262 LinkVisualizer *getLinkVisualizer (const K& k1, const K& k2) {
263 try {
264 Element<E1> *el1 = vertices.at(k1);
265 Element<E1> *el2 = vertices.at(k2);
266
267 return el1->getLinkVisualizer(el2);
268 }
269 catch (const out_of_range& oor) {
270 cerr << "Either source or destination node not found in graph!"
271 << endl;
272 throw;
273 }
274 }
275
276 private:
277
283 virtual const string getDataStructureRepresentation() const override {
285
286 // map the nodes to a sequence of ids, 0...N-1
287 // then get the JSON string for nodes placeholder
288 // nullptr prevents insertion of other nullptrs
289
290 unordered_map<K, int> node_map;
291 int i = 0;
292 string nodes_JSON = "", links_JSON = "";
293
294 for (const auto& v : this->vertices) {
295 if (node_map.emplace(v.first, i).second) {
296 i++;
297 nodes_JSON += v.second->getElementRepresentation() + COMMA;
298 }
299 }
300
301 //Remove trailing comma and nullptr entry
302
303 if (nodes_JSON.size()) {
304 nodes_JSON = nodes_JSON.erase(nodes_JSON.size() - 1);
305 }
306
307 // iterate through the N squared entries vertices and form the links JSON
308
309 for (const auto& src : vertices) {
310 for (const auto& dest : vertices) {
311 if (matrix.at(src.first).at(dest.first)) { // link exists
312 Element<E1>* src_v = vertices.at(src.first);
313 Element<E1>* dest_v = vertices.at(dest.first);
314 links_JSON += src_v->getLinkRepresentation(
315 *(src_v->getLinkVisualizer(dest_v)),
316 JSONencode(node_map.at(src.first)),
317 JSONencode(node_map.at(dest.first))) + COMMA;
318 }
319 }
320 }
321
322 //Remove trailing comma
323 if (links_JSON.size()) {
324 links_JSON = links_JSON.erase(links_JSON.size() - 1);
325 }
326
327 string graph_amatrix_json =
328 QUOTE + "nodes" + QUOTE + COLON +
329 OPEN_BOX + nodes_JSON + CLOSE_BOX + COMMA +
330 QUOTE + "links" + QUOTE + COLON + OPEN_BOX +
331 links_JSON + CLOSE_BOX +
333
334 return graph_amatrix_json;
335 }
336 //virtual void getDataStructureRepresentation(rapidjson::Document& d)
337 //const final {
338 //}
339 }; //end of GraphAdjList class
340 }
341}//end of bridges namespace
342#endif
This is the superclass of all data structure types in BRIDGES.
Definition: DataStructure.h:74
ElementVisualizer * getVisualizer()
Get the element visualizer object.
Definition: Element.h:141
void setValue(const E &val)
Sets generic object to "val".
Definition: Element.h:226
E const & getValue() const
Gets the object held in the generic object E.
Definition: Element.h:207
LinkVisualizer * getLinkVisualizer(const Element *el)
Returns the LinkVisualizer of element.
Definition: Element.h:162
This class maintains the visual properties of the a Bridges element.
Definition: ElementVisualizer.h:31
This class provides methods to represent adjacency matrix based graphs.
Definition: GraphAdjMatrix.h:37
GraphAdjMatrix(const GraphAdjMatrix &)=delete
E1 getVertexData(const K &src)
Definition: GraphAdjMatrix.h:153
const unordered_map< K, int > & getMatrix(K key) const
Definition: GraphAdjMatrix.h:119
GraphAdjMatrix & operator=(const GraphAdjMatrix &)=delete
const Element< E1 > * getVertex(const K &key) const
Definition: GraphAdjMatrix.h:134
const unordered_map< K, unordered_map< K, int > > & getMatrix() const
Definition: GraphAdjMatrix.h:106
virtual const string getDStype() const override
Definition: GraphAdjMatrix.h:48
LinkVisualizer * getLinkVisualizer(const K &k1, const K &k2)
Definition: GraphAdjMatrix.h:262
ElementVisualizer * getVisualizer(const K &k)
Definition: GraphAdjMatrix.h:242
unordered_map< K, Element< E1 > * > * getVertices()
Definition: GraphAdjMatrix.h:127
E2 const & getEdgeData(const K &src, const K &dest) const
Definition: GraphAdjMatrix.h:193
Element< E1 > * getVertex(const K &key)
Definition: GraphAdjMatrix.h:143
void setVertexData(const K &vID, const E1 &data)
Definition: GraphAdjMatrix.h:171
void setEdgeData(const K &src, const K &dest, const E2 &data)
Loads edge information.
Definition: GraphAdjMatrix.h:218
void addVertex(const K &k, const E1 &e=E1())
Definition: GraphAdjMatrix.h:71
void addEdge(const K &src, const K &dest, const unsigned int &wt)
Definition: GraphAdjMatrix.h:91
virtual ~GraphAdjMatrix()
Definition: GraphAdjMatrix.h:52
std::string JSONencode(const T &d)
Definition: JSONutil.h:38
these methods convert byte arrays in to base64 codes and are used in BRIDGES to represent the color a...
Definition: alltypes.h:4
const string COLON
Definition: DataStructure.h:52
const string OPEN_BOX
Definition: DataStructure.h:55
const string COMMA
Definition: DataStructure.h:51
const string CLOSE_BOX
Definition: DataStructure.h:56
const string CLOSE_CURLY
Definition: DataStructure.h:54
const string QUOTE
Definition: DataStructure.h:50