1 #ifndef GRAPH_ADJ_MATRIX_H
2 #define GRAPH_ADJ_MATRIX_H
10 namespace datastructure {
36 template <
typename K,
typename E1 = K,
typename E2 = E1>
39 unordered_map<K, Element<E1>* > vertices;
40 unordered_map<K, unordered_map<K, int> > matrix;
42 unordered_map<K, unordered_map<K, E2> > edge_data;
49 return "GraphAdjacencyMatrix";
53 for (
auto it : vertices)
75 for (
const auto& p : vertices) {
77 matrix[k][p.first] = matrix[p.first][k] = 0;
91 void addEdge(
const K& src,
const K& dest,
const unsigned int& wt) {
93 vertices.at(src)->links[vertices.at(dest)];
95 matrix.at(src).at(dest) = wt;
97 catch (
const out_of_range& ) {
98 cerr <<
"Cannot addEdge between non-existent verticies." << endl;
106 const unordered_map<K, unordered_map<K, int >> &
getMatrix()
const {
120 return matrix.at(key);
135 return vertices.at(key);
144 return vertices.at(key);
156 return (vertices.at(src))->
getValue();
158 catch (
const out_of_range& ) {
159 cerr <<
"getVertexData(): vertex not found" << endl;
163 throw "getVertexData(): vertex not found";
176 catch (
const out_of_range& ) {
177 cerr <<
"setVertexData(): Nonexistent vertices or " <<
178 " edge not found" << endl;
181 catch (
const char* msg) {
197 return edge_data[src][dest];
199 catch (
const out_of_range& oor) {
200 cerr <<
"getEdgeData(): Nonexistent vertices or " <<
201 " edge not found" << endl;
204 catch (
const char* msg) {
209 throw "getEdgeData(): Edge not found";
222 edge_data[src][dest] = data;
224 catch (
const out_of_range& oor) {
225 cerr <<
"setEdgeData(): Nonexistent vertices or " <<
226 " edge not found" << endl;
229 catch (
const char* msg) {
248 catch (
const out_of_range& oor) {
249 cerr <<
"Graph vertex " << k <<
" not found in graph!" << endl;
269 catch (
const out_of_range& oor) {
270 cerr <<
"Either source or destination node not found in graph!"
283 virtual const string getDataStructureRepresentation()
const override {
290 unordered_map<K, int> node_map;
292 string nodes_JSON =
"", links_JSON =
"";
294 for (
const auto& v : this->vertices) {
295 if (node_map.emplace(v.first, i).second) {
297 nodes_JSON += v.second->getElementRepresentation() +
COMMA;
303 if (nodes_JSON.size()) {
304 nodes_JSON = nodes_JSON.erase(nodes_JSON.size() - 1);
309 for (
const auto& src : vertices) {
310 for (
const auto& dest : vertices) {
311 if (matrix.at(src.first).at(dest.first)) {
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)),
323 if (links_JSON.size()) {
324 links_JSON = links_JSON.erase(links_JSON.size() - 1);
327 string graph_amatrix_json =
334 return graph_amatrix_json;
This is the superclass of all data structure types in BRIDGES.
Definition: DataStructure.h:74
void setValue(const E &val)
Sets generic object to "val".
Definition: Element.h:226
LinkVisualizer * getLinkVisualizer(const Element *el)
Returns the LinkVisualizer of element.
Definition: Element.h:162
E const & getValue() const
Gets the object held in the generic object E.
Definition: Element.h:207
ElementVisualizer * getVisualizer()
Get the element visualizer object.
Definition: Element.h:141
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
const unordered_map< K, int > & getMatrix(K key) const
Definition: GraphAdjMatrix.h:119
E1 getVertexData(const K &src)
Definition: GraphAdjMatrix.h:153
virtual const string getDStype() const override
Definition: GraphAdjMatrix.h:48
const Element< E1 > * getVertex(const K &key) const
Definition: GraphAdjMatrix.h:134
Element< E1 > * getVertex(const K &key)
Definition: GraphAdjMatrix.h:143
unordered_map< K, Element< E1 > * > * getVertices()
Definition: GraphAdjMatrix.h:127
GraphAdjMatrix & operator=(const GraphAdjMatrix &)=delete
const unordered_map< K, unordered_map< K, int > > & getMatrix() const
Definition: GraphAdjMatrix.h:106
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
LinkVisualizer * getLinkVisualizer(const K &k1, const K &k2)
Definition: GraphAdjMatrix.h:262
E2 const & getEdgeData(const K &src, const K &dest) const
Definition: GraphAdjMatrix.h:193
ElementVisualizer * getVisualizer(const K &k)
Definition: GraphAdjMatrix.h:242
virtual ~GraphAdjMatrix()
Definition: GraphAdjMatrix.h:52
This class maintains the visual properties of links within data structures.
Definition: LinkVisualizer.h:26
std::string JSONencode(const T &d)
Definition: JSONutil.h:38
Support for drawing Bar charts.
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