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)
76 for (
const auto& p : vertices) {
78 matrix[k][p.first] = matrix[p.first][k] = 0;
92 void addEdge(
const K& src,
const K& dest,
const unsigned int& wt) {
94 vertices.at(src)->links[vertices.at(dest)];
96 matrix.at(src).at(dest) = wt;
98 catch (
const out_of_range& ) {
99 cerr <<
"Cannot addEdge between non-existent verticies." << endl;
107 const unordered_map<K, unordered_map<K, int>>&
getMatrix()
const {
121 return matrix.at(key);
136 return vertices.at(key);
145 return vertices.at(key);
157 return (vertices.at(src))->
getValue();
159 catch (
const out_of_range& ) {
160 cerr <<
"getVertexData(): vertex not found" << endl;
164 throw "getVertexData(): vertex not found";
177 catch (
const out_of_range& ) {
178 cerr <<
"setVertexData(): Nonexistent vertices or " <<
179 " edge not found" << endl;
182 catch (
const char* msg) {
198 return edge_data[src][dest];
200 catch (
const out_of_range& oor) {
201 cerr <<
"getEdgeData(): Nonexistent vertices or " <<
202 " edge not found" << endl;
205 catch (
const char* msg) {
210 throw "getEdgeData(): Edge not found";
223 edge_data[src][dest] = data;
225 catch (
const out_of_range& oor) {
226 cerr <<
"setEdgeData(): Nonexistent vertices or " <<
227 " edge not found" << endl;
230 catch (
const char* msg) {
249 catch (
const out_of_range& oor) {
250 cerr <<
"Graph vertex " << k <<
" not found in graph!" << endl;
270 catch (
const out_of_range& oor) {
271 cerr <<
"Either source or destination node not found in graph!"
284 virtual const string getDataStructureRepresentation()
const override {
291 unordered_map<K, int> node_map;
293 string nodes_JSON =
"", links_JSON =
"";
295 for (
const auto& v : this->vertices) {
296 if (node_map.emplace(v.first, i).second) {
298 nodes_JSON += v.second->getElementRepresentation() +
COMMA;
304 if (nodes_JSON.size()) {
305 nodes_JSON = nodes_JSON.erase(nodes_JSON.size() - 1);
310 for (
const auto& src : vertices) {
311 for (
const auto& dest : vertices) {
312 if (matrix.at(src.first).at(dest.first)) {
313 Element<E1>* src_v = vertices.at(src.first);
314 Element<E1>* dest_v = vertices.at(dest.first);
315 links_JSON += src_v->getLinkRepresentation(
316 *(src_v->getLinkVisualizer(dest_v)),
324 if (links_JSON.size()) {
325 links_JSON = links_JSON.erase(links_JSON.size() - 1);
328 string graph_amatrix_json =
335 return graph_amatrix_json;
This is the superclass of all data structure types in BRIDGES.
Definition: DataStructure.h:73
void setValue(const E &val)
Sets generic object to "val".
Definition: Element.h:229
LinkVisualizer * getLinkVisualizer(const Element *el)
Returns the LinkVisualizer of element.
Definition: Element.h:165
E const & getValue() const
Gets the object held in the generic object E.
Definition: Element.h:210
ElementVisualizer * getVisualizer()
Get the element visualizer object.
Definition: Element.h:144
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:120
E1 getVertexData(const K &src)
Definition: GraphAdjMatrix.h:154
virtual const string getDStype() const override
Definition: GraphAdjMatrix.h:48
const Element< E1 > * getVertex(const K &key) const
Definition: GraphAdjMatrix.h:135
Element< E1 > * getVertex(const K &key)
Definition: GraphAdjMatrix.h:144
unordered_map< K, Element< E1 > * > * getVertices()
Definition: GraphAdjMatrix.h:128
GraphAdjMatrix & operator=(const GraphAdjMatrix &)=delete
void setVertexData(const K &vID, const E1 &data)
Definition: GraphAdjMatrix.h:172
void setEdgeData(const K &src, const K &dest, const E2 &data)
Loads edge information.
Definition: GraphAdjMatrix.h:219
void addVertex(const K &k, const E1 &e=E1())
Definition: GraphAdjMatrix.h:72
const unordered_map< K, unordered_map< K, int > > & getMatrix() const
Definition: GraphAdjMatrix.h:107
void addEdge(const K &src, const K &dest, const unsigned int &wt)
Definition: GraphAdjMatrix.h:92
LinkVisualizer * getLinkVisualizer(const K &k1, const K &k2)
Definition: GraphAdjMatrix.h:263
E2 const & getEdgeData(const K &src, const K &dest) const
Definition: GraphAdjMatrix.h:194
ElementVisualizer * getVisualizer(const K &k)
Definition: GraphAdjMatrix.h:243
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:37
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:51
const string OPEN_BOX
Definition: DataStructure.h:54
const string COMMA
Definition: DataStructure.h:50
const string CLOSE_BOX
Definition: DataStructure.h:55
const string CLOSE_CURLY
Definition: DataStructure.h:53
const string QUOTE
Definition: DataStructure.h:49