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>
37 class GraphAdjMatrix :
public DataStructure {
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";
63 for (
const auto& p : vertices) {
65 matrix[k][p.first] = matrix[p.first][k] = 0;
79 void addEdge(
const K& src,
const K& dest,
const unsigned int& wt) {
81 vertices.at(src)->links[vertices.at(dest)];
83 matrix.at(src).at(dest) = wt;
85 catch (
const out_of_range& ) {
86 cerr <<
"Cannot addEdge between non-existent verticies." << endl;
94 const unordered_map<K, unordered_map<K, int>>&
getMatrix()
const {
108 return matrix.at(key);
123 return vertices.at(key);
132 return vertices.at(key);
144 return (vertices.at(src))->getValue();
146 catch (
const out_of_range& ) {
147 cerr <<
"getVertexData(): vertex not found" << endl;
151 throw "getVertexData(): vertex not found";
164 catch (
const out_of_range& ) {
165 cerr <<
"setVertexData(): Nonexistent vertices or " <<
166 " edge not found" << endl;
169 catch (
const char* msg) {
185 return edge_data[src][dest];
187 catch (
const out_of_range& oor) {
188 cerr <<
"getEdgeData(): Nonexistent vertices or " <<
189 " edge not found" << endl;
192 catch (
const char* msg) {
197 throw "getEdgeData(): Edge not found";
210 edge_data[src][dest] = data;
212 catch (
const out_of_range& oor) {
213 cerr <<
"setEdgeData(): Nonexistent vertices or " <<
214 " edge not found" << endl;
217 catch (
const char* msg) {
236 catch (
const out_of_range& oor) {
237 cerr <<
"Graph vertex " << k <<
" not found in graph!" << endl;
257 catch (
const out_of_range& oor) {
258 cerr <<
"Either source or destination node not found in graph!" 271 virtual const string getDataStructureRepresentation()
const override {
278 unordered_map<K, int> node_map;
280 string nodes_JSON =
"", links_JSON =
"";
282 for (
const auto& v : this->vertices) {
283 if (node_map.emplace(v.first, i).second) {
285 nodes_JSON += v.second->getElementRepresentation() +
COMMA;
291 if (nodes_JSON.size()) {
292 nodes_JSON = nodes_JSON.erase(nodes_JSON.size() - 1);
297 for (
const auto& src : vertices) {
298 for (
const auto& dest : vertices) {
299 if (matrix.at(src.first).at(dest.first)) {
311 if (links_JSON.size()) {
312 links_JSON = links_JSON.erase(links_JSON.size() - 1);
315 string graph_amatrix_json =
322 return graph_amatrix_json;
const Element< E1 > * getVertex(const K &key) const
Definition: GraphAdjMatrix.h:122
void setValue(const E &val)
Sets generic object to "val".
Definition: Element.h:229
static const string getLinkRepresentation(const LinkVisualizer &lv, const string &src, const string &dest)
Definition: Element.h:273
LinkVisualizer * getLinkVisualizer(const K &k1, const K &k2)
Definition: GraphAdjMatrix.h:250
const string COLON
Definition: DataStructure.h:51
const unordered_map< K, int > & getMatrix(K key) const
Definition: GraphAdjMatrix.h:107
This class maintains the visual properties of the a Bridges element.
Definition: ElementVisualizer.h:31
const string OPEN_BOX
Definition: DataStructure.h:54
const string CLOSE_CURLY
Definition: DataStructure.h:53
ElementVisualizer * getVisualizer(const K &k)
Definition: GraphAdjMatrix.h:230
these methods convert byte arrays in to base64 codes and are used in BRIDGES to represent the color a...
Definition: alltypes.h:4
E2 const & getEdgeData(const K &src, const K &dest) const
Definition: GraphAdjMatrix.h:181
const string CLOSE_BOX
Definition: DataStructure.h:55
unordered_map< K, Element< E1 > * > * getVertices()
Definition: GraphAdjMatrix.h:115
virtual const string getDStype() const override
Definition: GraphAdjMatrix.h:48
void setEdgeData(const K &src, const K &dest, const E2 &data)
Loads edge information.
Definition: GraphAdjMatrix.h:206
E1 getVertexData(const K &src)
Definition: GraphAdjMatrix.h:141
This class maintains the visual properties of links within data structures.
Definition: LinkVisualizer.h:26
ElementVisualizer * getVisualizer()
Get the element visualizer object.
Definition: Element.h:144
const string COMMA
Definition: DataStructure.h:50
const unordered_map< K, unordered_map< K, int > > & getMatrix() const
Definition: GraphAdjMatrix.h:94
void addEdge(const K &src, const K &dest, const unsigned int &wt)
Definition: GraphAdjMatrix.h:79
void setVertexData(const K &vID, const E1 &data)
Definition: GraphAdjMatrix.h:159
const string QUOTE
Definition: DataStructure.h:49
void addVertex(const K &k, const E1 &e=E1())
Definition: GraphAdjMatrix.h:59
Element< E1 > * getVertex(const K &key)
Definition: GraphAdjMatrix.h:131
std::string JSONencode(const T &d)
Definition: JSONutil.h:37
LinkVisualizer * getLinkVisualizer(const Element *el)
Returns the LinkVisualizer of element.
Definition: Element.h:165