6 #include <unordered_map> 15 namespace datastructure {
107 template<
typename K,
typename E1 = K,
typename E2 = E1>
108 class GraphAdjList :
public DataStructure {
112 unordered_map<K, Element<E1>* > vertices;
114 unordered_map<K, SLelement<Edge<K, E2> >*> adj_list;
117 static const int LargeGraphVertSize = 2000;
119 bool forceLargeViz =
false;
120 bool forceSmallViz =
false;
123 GraphAdjList(
const GraphAdjList& gr) =
delete;
124 const GraphAdjList& operator= (
const GraphAdjList& gr) =
delete;
127 GraphAdjList() =
default;
128 GraphAdjList(GraphAdjList&& gr) =
default;
131 for (
auto& v : vertices) {
132 if (adj_list[v.first]) {
136 adj_list[v.first]; sle !=
nullptr; ) {
141 adj_list[v.first] =
nullptr;
143 delete vertices[v.first];
144 vertices[v.first] =
nullptr;
154 if (forceLargeViz || (!forceSmallViz &&
155 vertices.size() > LargeGraphVertSize &&
156 areAllVerticesLocated())) {
159 return "GraphAdjacencyList";
177 if (vertices.find(k) == vertices.end()) {
180 adj_list[k] =
nullptr;
197 void addEdge(
const K& src,
const K& dest,
const E2& data = E2()) {
203 vertices[src]->links[vertices.at(dest)];
210 Edge<K, E2> (src, dest, &(vertices[src]->links[vertices.at(dest)]), data), conv.str());
213 catch (
const out_of_range& ) {
214 cerr <<
"addEdge(): Nonexistent vertex?" << endl <<
215 "Create vertices first prior to adding edges that use that vertex" << endl
216 <<
"Cannot add edge between non-existent verticies" 231 return (vertices.at(src))->getValue();
233 catch (
const out_of_range& ) {
234 cerr <<
"getVertexData(): vertex not found" << endl;
238 throw "getVertexData(): vertex not found";
252 catch (
const out_of_range& ) {
253 cerr <<
"setVertexData(): Nonexistent vertex" << endl;
256 catch (
const char* msg) {
275 if (ed.getVertex() == dest) {
280 throw "Edge not found!";
282 catch (
const out_of_range& ) {
283 cerr <<
"getEdgeData(): Edge not found" << endl;
286 catch (
const char* msg) {
290 throw "getEdgeData(): Edge not found";
307 if (ed.getVertex() == dest) {
312 throw "Edge not found!";
314 catch (
const out_of_range& ) {
315 cerr <<
"getEdgeData(): Edge not found" << endl;
318 catch (
const char* msg) {
322 throw "getEdgeData(): Edge not found";
341 if (ed.getVertex() == dest) {
348 throw "getEdgeData(): Edge not found!";
350 catch (
const out_of_range& ) {
351 cerr <<
"setEdgeData(): Nonexistent vertices or " <<
352 " edge not found" << endl;
355 catch (
const char* msg) {
359 throw "getEdgeData(): Edge not found";
385 return vertices.at(key);
387 catch (
const std::out_of_range& oor) {
402 return vertices.at(key);
404 catch (
const std::out_of_range& oor) {
424 while (sle !=
nullptr) {
426 if (edge_dest == dest)
431 catch (
const std::out_of_range& oor) {
433 std::cout <<
"one or both vertices are likely missing from graph\n";
442 const unordered_map<K, SLelement<Edge<K, E2> >*>&
457 return adj_list.at(k);
459 catch (
const out_of_range& ) {
460 cerr <<
"Cannot getAdjacencyList() of a non-existent vertex!" 476 return adj_list.at(k);
478 catch (
const out_of_range& ) {
479 cerr <<
"Cannot getAdjacencyList() of a non-existent vertex!" 500 catch (
const out_of_range& ) {
501 cerr <<
"Graph vertex " << k <<
" not found in graph!" << endl;
517 return getEdge(k1, k2).getLinkVisualizer();
519 catch (
const out_of_range& ) {
520 cerr <<
"Either source or destination node not found in graph!\n";
530 virtual const string getDataStructureRepresentation()
const override {
540 vertices.size() > LargeGraphVertSize &&
541 areAllVerticesLocated())) {
542 return getDataStructureRepresentationLargeGraph();
545 unordered_map<K, int> node_map;
547 string nodes_JSON =
"", links_JSON =
"";
549 for (
const auto& v : vertices) {
550 if (node_map.emplace(v.first, i).second) {
552 nodes_JSON += v.second->getElementRepresentation() +
COMMA;
558 if (nodes_JSON.size()) {
559 nodes_JSON = nodes_JSON.erase(nodes_JSON.size() - 1);
564 for (
const auto& v : vertices) {
569 it = it->getNext()) {
570 Element<E1>* dest_vert = vertices.at(it->getValue().to() );
579 if (links_JSON.size()) {
580 links_JSON = links_JSON.erase(links_JSON.size() - 1);
583 string graph_alist_json =
591 return graph_alist_json;
603 string getDataStructureRepresentationLargeGraph ()
const {
610 unordered_map<K, int> node_map;
612 string nodes_JSON =
"";
614 for (
const auto& v : vertices) {
615 if (node_map.emplace(v.first, i).second) {
618 vertices.at(v.first)->getVisualizer();
636 if (nodes_JSON.size()) {
637 nodes_JSON = nodes_JSON.erase(nodes_JSON.size() - 1);
641 string links_JSON =
"";
642 for (
const auto& v : vertices) {
647 it = it->getNext()) {
648 Element<E1>* dest_vert = vertices.at(it->getValue().to() );
650 string src =
JSONencode(node_map.at(v.first));
651 string dest =
JSONencode(node_map.at(it->getValue().to()));
661 if (links_JSON.size())
662 links_JSON = links_JSON.erase(links_JSON.size() - 1);
664 string graph_alist_json =
671 return graph_alist_json;
677 bool areAllVerticesLocated()
const {
678 for (
const auto& v : vertices) {
708 forceLargeViz =
true;
709 forceSmallViz =
false;
712 forceLargeViz =
false;
734 forceSmallViz =
true;
735 forceLargeViz =
false;
738 forceSmallViz =
false;
746 std::unordered_map<K, Element<E1>* >
const & underlying_map;
761 return this->it != it.it;
821 typename std::unordered_map<K, Element<E1>* > & underlying_map;
834 typename std::unordered_map<K, Element<E1>* >
::iterator it;
841 return this->it != it.it;
867 return this->it != it.it;
882 return iterator(underlying_map.begin());
886 return iterator(underlying_map.end());
914 typename std::unordered_map<K, Element<E1>* >
const & underlying_map;
930 return this->it != it.it;
VertexElementSet_listhelper vertexSet()
Definition: GraphAdjList.h:903
void forceSmallVisualization(bool f)
Force the rendering engine to use small graph visualization.
Definition: GraphAdjList.h:732
This is a helper class to return sets of vertices in a way that are iterable with range for loops...
Definition: GraphAdjList.h:833
double getLocationY() const
get Y coordinate of element location
Definition: ElementVisualizer.h:173
const_iterator & operator++()
Definition: GraphAdjList.h:874
double getLocationX() const
get X coordinate of element location
Definition: ElementVisualizer.h:166
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
This is a helper class to return sets of vertices ina way that are iterable with range for loops...
Definition: GraphAdjList.h:922
void setEdgeData(const E2 &data)
Sets edge data to "data".
Definition: Edge.h:94
ElementVisualizer * getVisualizer(const K &k)
Returns the visualizer corresponding to a graph vertex. convenient method to set attributes of the gr...
Definition: GraphAdjList.h:494
bool operator!=(const const_iterator &it) const
Definition: GraphAdjList.h:866
E2 & getEdgeData(const K &src, const K &dest)
Gets edge data for the edge from "src" to "dest".
Definition: GraphAdjList.h:268
SLelement< Edge< K, E2 > > * getAdjacencyList(const K &k)
Returns adjacency list of a vertex with name k.
Definition: GraphAdjList.h:455
iterator end()
Definition: GraphAdjList.h:885
const string COLON
Definition: DataStructure.h:51
E const & getValue() const
Gets the object held in the generic object E.
Definition: Element.h:210
iterator(typename std::unordered_map< K, Element< E1 > * >::iterator i)
Definition: GraphAdjList.h:836
constVertexElementSet_listhelper(std::unordered_map< K, Element< E1 > * > const &um)
Definition: GraphAdjList.h:917
SLelement< Edge< K, E2 > >::SLelement_listhelper outgoingEdgeSetOf(K const &k)
This method is useful for iterating through a set of outgoing edges from a vertex.
Definition: GraphAdjList.h:801
This class maintains the visual properties of the a Bridges element.
Definition: ElementVisualizer.h:31
const string OPEN_BOX
Definition: DataStructure.h:54
const_iterator end() const
Definition: GraphAdjList.h:947
const_iterator & operator++()
Definition: GraphAdjList.h:937
Element< E1 > const * operator*() const
Definition: GraphAdjList.h:870
const_iterator(typename std::unordered_map< K, Element< E1 > * >::const_iterator i)
Definition: GraphAdjList.h:925
const_iterator begin() const
Definition: GraphAdjList.h:774
const_iterator(typename std::unordered_map< K, Element< E1 > * >::const_iterator i)
Definition: GraphAdjList.h:862
const string CLOSE_CURLY
Definition: DataStructure.h:53
Definition: GraphAdjList.h:745
const Element< E1 > * getVertex(const K &key) const
Return the vertex corresponding to a key.
Definition: GraphAdjList.h:383
const E1 & getVertexData(const K &src) &
Gets vertex data for a graph vertex.
Definition: GraphAdjList.h:228
const_iterator end() const
Definition: GraphAdjList.h:778
Element< E1 > * getVertex(const K &key)
Definition: GraphAdjList.h:400
Color getColor() const
Return the link color.
Definition: LinkVisualizer.h:124
The singly linked list element, derived from Element.
Definition: SLelement.h:27
This is a helper class to return sets of vertices in a way that are iterable with range for loops...
Definition: GraphAdjList.h:859
const_iterator end() const
Definition: GraphAdjList.h:893
void forceLargeVisualization(bool f)
Force the rendering engine to use large graph visualization.
Definition: GraphAdjList.h:706
void addVertex(const K &k, const E1 &e=E1())
Adds a vertex to the graph.
Definition: GraphAdjList.h:174
const_iterator(typename std::unordered_map< K, Element< E1 > * >::const_iterator i)
Definition: GraphAdjList.h:756
bool operator!=(const iterator &it) const
Definition: GraphAdjList.h:840
these methods convert byte arrays in to base64 codes and are used in BRIDGES to represent the color a...
Definition: alltypes.h:4
const_iterator begin() const
Definition: GraphAdjList.h:943
virtual const string getDStype() const override
Get the string representation of this data structure type.
Definition: GraphAdjList.h:153
const string CLOSE_BOX
Definition: DataStructure.h:55
bool operator!=(const const_iterator &it) const
Definition: GraphAdjList.h:929
const E2 & getEdgeData() const
Return the edge data.
Definition: Edge.h:102
const string getCSSRepresentation() const
Definition: Color.h:404
bool operator!=(const const_iterator &it) const
Definition: GraphAdjList.h:760
Definition: GraphAdjList.h:753
This is a helper class to return sets of vertices in a way that are iterable with range for loops...
Definition: GraphAdjList.h:913
const_iterator begin() const
Definition: GraphAdjList.h:889
KeySet_helper(std::unordered_map< K, Element< E1 > * > const &um)
Definition: GraphAdjList.h:749
unordered_map< K, Element< E1 > * > * getVertices()
Return the graph nodes.
Definition: GraphAdjList.h:366
constVertexElementSet_listhelper vertexSet() const
Definition: GraphAdjList.h:955
Edge< K, E2 > getEdge(const K &src, const K &dest)
Get the edge between src and dest vertices.
Definition: GraphAdjList.h:418
const_iterator & operator++()
Definition: GraphAdjList.h:768
void setVertexData(const K &vertID, E1 const &data)
Loads vertex specific information for a graph vertex.
Definition: GraphAdjList.h:247
virtual SLelement * getNext()
Returns the next element in the list.
Definition: SLelement.h:75
iterator & operator++()
Definition: GraphAdjList.h:848
const unordered_map< K, Element< E1 > * > * getVertices() const
Return the graph nodes - const version.
Definition: GraphAdjList.h:375
const K & operator*() const
Definition: GraphAdjList.h:764
This is a helper class to return sets of vertices in a way that are iterable with range for loops...
Definition: GraphAdjList.h:820
Element< E1 > const * operator*() const
Definition: GraphAdjList.h:933
const SLelement< Edge< K, E2 > > * getAdjacencyList(const K &k) const
Definition: GraphAdjList.h:474
This class maintains the visual properties of links within data structures.
Definition: LinkVisualizer.h:26
VertexElementSet_listhelper(std::unordered_map< K, Element< E1 > * > &um)
Definition: GraphAdjList.h:824
KeySet_helper keySet() const
Definition: GraphAdjList.h:792
Element< E1 > * operator*()
Definition: GraphAdjList.h:844
Color getColor() const
Return the element color.
Definition: ElementVisualizer.h:110
virtual ~GraphAdjList() override
Definition: GraphAdjList.h:130
ElementVisualizer * getVisualizer()
Get the element visualizer object.
Definition: Element.h:144
const string COMMA
Definition: DataStructure.h:50
iterator begin()
Definition: GraphAdjList.h:881
const string QUOTE
Definition: DataStructure.h:49
const unordered_map< K, SLelement< Edge< K, E2 > > * > & getAdjacencyList() const
Return the adjacency list.
Definition: GraphAdjList.h:443
LinkVisualizer * getLinkVisualizer(const K &k1, const K &k2)
Returns the link visualizer corresponding to an edge. Returns the link visualizer corresponding to tw...
Definition: GraphAdjList.h:515
SLelement< Edge< K, E2 > >::SLelement_constlisthelper outgoingEdgeSetOf(K const &k) const
This method is useful for iterating through a set of outgoing edges from a vertex - const version...
Definition: GraphAdjList.h:810
E2 const & getEdgeData(const K &src, const K &dest) const
Gets edge data for the edge from "src" to "dest" - const version.
Definition: GraphAdjList.h:300
void addEdge(const K &src, const K &dest, const E2 &data=E2())
Add an edge with data.
Definition: GraphAdjList.h:197
This helper class is used by the graph classes - GraphAdjList , GraphAdjMatrix - to keep track of edg...
Definition: Edge.h:36
void setEdgeData(const K &src, const K &dest, E2 &data)
Loads edge specific information for the edge from "src" to "dest".
Definition: GraphAdjList.h:334
std::string JSONencode(const T &d)
Definition: JSONutil.h:37
LinkVisualizer * getLinkVisualizer(const Element *el)
Returns the LinkVisualizer of element.
Definition: Element.h:165