CXXGraph  0.1.6
CXXGraph is a header only, that manages the Graphs and it's algorithm in C++
Graph_TS.hpp
1 /***********************************************************/
2 /*** ______ ____ ______ _ ***/
3 /*** / ___\ \/ /\ \/ / ___|_ __ __ _ _ __ | |__ ***/
4 /*** | | \ / \ / | _| '__/ _` | '_ \| '_ \ ***/
5 /*** | |___ / \ / \ |_| | | | (_| | |_) | | | | ***/
6 /*** \____/_/\_\/_/\_\____|_| \__,_| .__/|_| |_| ***/
7 /*** |_| ***/
8 /***********************************************************/
9 /*** Header-Only C++ Library for Graph ***/
10 /*** Representation and Algorithms ***/
11 /***********************************************************/
12 /*** Author: ZigRazor ***/
13 /*** E-Mail: zigrazor@gmail.com ***/
14 /***********************************************************/
15 /*** Collaboration: ----------- ***/
16 /***********************************************************/
17 /*** License: AGPL v3.0 ***/
18 /***********************************************************/
19 
20 #ifndef __GRAPH_TS_H__
21 #define __GRAPH_TS_H__
22 
23 #pragma once
24 
25 #include "Graph/Graph.hpp"
26 
27 namespace CXXGRAPH
28 {
29 
31  template <typename T>
32  class Graph_TS : public Graph<T>, public ThreadSafe
33  {
34  public:
35  Graph_TS() = default;
36  Graph_TS(const std::list<const Edge<T> *> &edgeSet);
37  Graph_TS(const Graph<T> &graph);
38  ~Graph_TS() = default;
47  const std::list<const Edge<T> *> &getEdgeSet() const override;
56  void setEdgeSet(std::list<const Edge<T> *> &edgeSet) override;
65  void addEdge(const Edge<T> *edge) override;
74  void removeEdge(unsigned long edgeId) override;
83  const std::list<const Node<T> *> getNodeSet() const override;
93  const std::optional<const Edge<T> *> getEdge(unsigned long edgeId) const override;
99  const AdjacencyMatrix<T> getAdjMatrix() const override;
112  const DijkstraResult dijkstra(const Node<T> &source, const Node<T> &target) const override;
123  const std::vector<Node<T>> breadth_first_search(const Node<T> &start) const override;
134  const std::vector<Node<T>> depth_first_search(const Node<T> &start) const override;
135 
144  bool isCyclicDirectedGraphDFS() const override;
145 
154  bool isCyclicDirectedGraphBFS() const override;
155 
163  bool isDirectedGraph() const override;
164 
191  const DialResult dial(const Node<T> &source, int maxWeight) const override;
192 
206  int writeToFile(InputOutputFormat format = InputOutputFormat::STANDARD_CSV, const std::string &workingDir = ".", const std::string &OFileName = "graph", bool compress = false, bool writeNodeFeat = false, bool writeEdgeWeight = false) const override;
207 
221  int readFromFile(InputOutputFormat format = InputOutputFormat::STANDARD_CSV, const std::string &workingDir = ".", const std::string &OFileName = "graph", bool compress = false, bool readNodeFeat = false, bool readEdgeWeight = false) override;
222 
232  PartitionMap<T> partitionGraph(PARTITIONING::PartitionAlgorithm algorithm, unsigned int numberOfPartitions) const override;
233  };
234 
235  template <typename T>
236  Graph_TS<T>::Graph_TS(const std::list<const Edge<T> *> &edgeSet) : Graph<T>(edgeSet), ThreadSafe() {}
237 
238  template <typename T>
239  Graph_TS<T>::Graph_TS(const Graph<T> &graph) : Graph<T>(graph), ThreadSafe() {}
240 
241  template <typename T>
242  const std::list<const Edge<T> *> &Graph_TS<T>::getEdgeSet() const
243  {
244  std::lock_guard<std::mutex> lock(mutex);
245  return Graph<T>::getEdgeSet();
246  }
247 
248  template <typename T>
249  void Graph_TS<T>::setEdgeSet(std::list<const Edge<T> *> &edgeSet)
250  {
251  getLock();
252  Graph<T>::setEdgeSet(edgeSet);
253  releaseLock();
254  }
255 
256  template <typename T>
257  void Graph_TS<T>::addEdge(const Edge<T> *edge)
258  {
259  getLock();
260  Graph<T>::addEdge(edge);
261  releaseLock();
262  }
263 
264  template <typename T>
265  void Graph_TS<T>::removeEdge(unsigned long edgeId)
266  {
267  getLock();
268  Graph<T>::removeEdge(edgeId);
269  releaseLock();
270  }
271 
272  template <typename T>
273  const std::list<const Node<T> *> Graph_TS<T>::getNodeSet() const
274  {
275  getLock();
276  auto ns = Graph<T>::getNodeSet();
277  releaseLock();
278  return ns;
279  }
280 
281  template <typename T>
282  const std::optional<const Edge<T> *> Graph_TS<T>::getEdge(unsigned long edgeId) const
283  {
284  getLock();
285  auto e = Graph<T>::getEdge(edgeId);
286  releaseLock();
287  return e;
288  }
289 
290  template <typename T>
291  const AdjacencyMatrix<T> Graph_TS<T>::getAdjMatrix() const
292  {
293  getLock();
294  auto adjm = Graph<T>::getAdjMatrix();
295  releaseLock();
296  return adjm;
297  }
298 
299  template <typename T>
300  const DijkstraResult Graph_TS<T>::dijkstra(const Node<T> &source, const Node<T> &target) const
301  {
302  getLock();
303  auto dij = Graph<T>::dijkstra(source, target);
304  releaseLock();
305  return dij;
306  }
307 
308  template <typename T>
309  const std::vector<Node<T>> Graph_TS<T>::breadth_first_search(const Node<T> &start) const
310  {
311  getLock();
312  auto bfs = Graph<T>::breadth_first_search(start);
313  releaseLock();
314  return bfs;
315  }
316 
317  template <typename T>
318  const std::vector<Node<T>> Graph_TS<T>::depth_first_search(const Node<T> &start) const
319  {
320  getLock();
321  auto dfs = Graph<T>::depth_first_search(start);
322  releaseLock();
323  return dfs;
324  }
325 
326  template <typename T>
328  {
329  getLock();
330  auto result = Graph<T>::isCyclicDirectedGraphDFS();
331  releaseLock();
332  return result;
333  }
334 
335  template <typename T>
337  {
338  getLock();
339  auto result = Graph<T>::isCyclicDirectedGraphBFS();
340  releaseLock();
341  return result;
342  }
343 
344  template <typename T>
346  {
347  getLock();
348  auto result = Graph<T>::isDirectedGraph();
349  releaseLock();
350  return result;
351  }
352 
353  template <typename T>
354  const DialResult Graph_TS<T>::dial(const Node<T> &source, int maxWeight) const
355  {
356  getLock();
357  auto dial = Graph<T>::dial(source, maxWeight);
358  releaseLock();
359  return dial;
360  }
361 
362  template <typename T>
363  int Graph_TS<T>::writeToFile(InputOutputFormat format, const std::string &workingDir, const std::string &OFileName, bool compress, bool writeNodeFeat, bool writeEdgeWeight) const
364  {
365  getLock();
366  auto result = Graph<T>::writeToFile(format, workingDir, OFileName, compress, writeNodeFeat, writeEdgeWeight);
367  releaseLock();
368  return result;
369  }
370 
371  template <typename T>
372  int Graph_TS<T>::readFromFile(InputOutputFormat format, const std::string &workingDir, const std::string &OFileName, bool compress, bool readNodeFeat, bool readEdgeWeight)
373  {
374  getLock();
375  auto result = Graph<T>::readFromFile(format, workingDir, OFileName, compress, readNodeFeat, readEdgeWeight);
376  releaseLock();
377  return result;
378  }
379 
380  template <typename T>
381  PartitionMap<T> Graph_TS<T>::partitionGraph(PARTITIONING::PartitionAlgorithm algorithm, unsigned int numberOfPartitions) const
382  {
383  getLock();
384  auto partitions = Graph<T>::partitionGraph(algorithm, numberOfPartitions);
385  releaseLock();
386  return partitions;
387  }
388 }
389 #endif // __GRAPH_TS_H__
Definition: Edge.hpp:40
Class that implement the Thread Safe Graph.
Definition: Graph_TS.hpp:33
const std::optional< const Edge< T > * > getEdge(unsigned long edgeId) const override
Function that return an Edge with specific ID if Exist in the Graph Note: Thread Safe.
Definition: Graph_TS.hpp:282
const std::list< const Node< T > * > getNodeSet() const override
Function that return the Node Set of the Graph Note: Thread Safe.
Definition: Graph_TS.hpp:273
void setEdgeSet(std::list< const Edge< T > * > &edgeSet) override
Function set the Edge Set of the Graph Note: Thread Safe.
Definition: Graph_TS.hpp:249
bool isDirectedGraph() const override
This function checks if a graph is directed Note: Thread Safe.
Definition: Graph_TS.hpp:345
const DijkstraResult dijkstra(const Node< T > &source, const Node< T > &target) const override
Function runs the dijkstra algorithm for some source node and target node in the graph and returns th...
Definition: Graph_TS.hpp:300
int writeToFile(InputOutputFormat format=InputOutputFormat::STANDARD_CSV, const std::string &workingDir=".", const std::string &OFileName="graph", bool compress=false, bool writeNodeFeat=false, bool writeEdgeWeight=false) const override
This function write the graph in an output file Note: Thread Safe.
Definition: Graph_TS.hpp:363
void addEdge(const Edge< T > *edge) override
Function add an Edge to the Graph Edge Set Note: Thread Safe.
Definition: Graph_TS.hpp:257
PartitionMap< T > partitionGraph(PARTITIONING::PartitionAlgorithm algorithm, unsigned int numberOfPartitions) const override
This function partition a graph in a set of partitions Note: Thread Safe.
Definition: Graph_TS.hpp:381
bool isCyclicDirectedGraphBFS() const override
This function uses BFS to check for cycle in the graph. Pay Attention, this function work only with d...
Definition: Graph_TS.hpp:336
int readFromFile(InputOutputFormat format=InputOutputFormat::STANDARD_CSV, const std::string &workingDir=".", const std::string &OFileName="graph", bool compress=false, bool readNodeFeat=false, bool readEdgeWeight=false) override
This function read the graph from an input file Note: Thread Safe.
Definition: Graph_TS.hpp:372
const std::vector< Node< T > > breadth_first_search(const Node< T > &start) const override
Function performs the breadth first search algorithm over the graph Note: Thread Safe.
Definition: Graph_TS.hpp:309
void removeEdge(unsigned long edgeId) override
Function remove an Edge from the Graph Edge Set Note: Thread Safe.
Definition: Graph_TS.hpp:265
const std::list< const Edge< T > * > & getEdgeSet() const override
Function that return the Edge set of the Graph Note: Thread Safe.
Definition: Graph_TS.hpp:242
const std::vector< Node< T > > depth_first_search(const Node< T > &start) const override
Function performs the depth first search algorithm over the graph Note: Thread Safe.
Definition: Graph_TS.hpp:318
const DialResult dial(const Node< T > &source, int maxWeight) const override
This function write the graph in an output file Note: Thread Safe.
Definition: Graph_TS.hpp:354
bool isCyclicDirectedGraphDFS() const override
This function uses DFS to check for cycle in the graph. Pay Attention, this function work only with d...
Definition: Graph_TS.hpp:327
const AdjacencyMatrix< T > getAdjMatrix() const override
This function generate a list of adjacency matrix with every element of the matrix contain the node w...
Definition: Graph_TS.hpp:291
Class that implement the Graph. ( This class is not Thread Safe )
Definition: Graph.hpp:76
virtual const std::vector< Node< T > > depth_first_search(const Node< T > &start) const
Function performs the depth first search algorithm over the graph Note: No Thread Safe.
Definition: Graph.hpp:1013
virtual const std::list< const Edge< T > * > & getEdgeSet() const
Function that return the Edge set of the Graph Note: No Thread Safe.
Definition: Graph.hpp:307
virtual const std::optional< const Edge< T > * > getEdge(unsigned long edgeId) const
Function that return an Edge with specific ID if Exist in the Graph Note: No Thread Safe.
Definition: Graph.hpp:368
virtual bool isCyclicDirectedGraphBFS() const
This function uses BFS to check for cycle in the graph. Pay Attention, this function work only with d...
Definition: Graph.hpp:1135
virtual void removeEdge(unsigned long edgeId)
Function remove an Edge from the Graph Edge Set Note: No Thread Safe.
Definition: Graph.hpp:337
virtual int writeToFile(InputOutputFormat format=InputOutputFormat::STANDARD_CSV, const std::string &workingDir=".", const std::string &OFileName="graph", bool compress=false, bool writeNodeFeat=false, bool writeEdgeWeight=false) const
This function write the graph in an output file Note: No Thread Safe.
Definition: Graph.hpp:1348
virtual bool isDirectedGraph() const
This function checks if a graph is directed Note: No Thread Safe.
Definition: Graph.hpp:1204
virtual const DialResult dial(const Node< T > &source, int maxWeight) const
This function write the graph in an output file Note: No Thread Safe.
Definition: Graph.hpp:1220
virtual const DijkstraResult dijkstra(const Node< T > &source, const Node< T > &target) const
Function runs the dijkstra algorithm for some source node and target node in the graph and returns th...
Definition: Graph.hpp:863
virtual bool isCyclicDirectedGraphDFS() const
This function uses DFS to check for cycle in the graph. Pay Attention, this function work only with d...
Definition: Graph.hpp:1045
virtual void addEdge(const Edge< T > *edge)
Function add an Edge to the Graph Edge Set Note: No Thread Safe.
Definition: Graph.hpp:327
virtual PartitionMap< T > partitionGraph(PARTITIONING::PartitionAlgorithm algorithm, unsigned int numberOfPartitions) const
This function partition a graph in a set of partitions Note: No Thread Safe.
Definition: Graph.hpp:1485
virtual int readFromFile(InputOutputFormat format=InputOutputFormat::STANDARD_CSV, const std::string &workingDir=".", const std::string &OFileName="graph", bool compress=false, bool readNodeFeat=false, bool readEdgeWeight=false)
This function read the graph from an input file Note: No Thread Safe.
Definition: Graph.hpp:1421
virtual const AdjacencyMatrix< T > getAdjMatrix() const
This function generate a list of adjacency matrix with every element of the matrix contain the node w...
Definition: Graph.hpp:836
virtual const std::list< const Node< T > * > getNodeSet() const
Function that return the Node Set of the Graph Note: No Thread Safe.
Definition: Graph.hpp:348
virtual const std::vector< Node< T > > breadth_first_search(const Node< T > &start) const
Function performs the breadth first search algorithm over the graph Note: No Thread Safe.
Definition: Graph.hpp:973
virtual void setEdgeSet(std::list< const Edge< T > * > &edgeSet)
Function set the Edge Set of the Graph Note: No Thread Safe.
Definition: Graph.hpp:313
Definition: Node.hpp:35
Definition: ThreadSafe.hpp:30
Struct that contains the information about Dijsktra's Algorithm results.
Definition: Typedef.hpp:71
Struct that contains the information about Dijsktra's Algorithm results.
Definition: Typedef.hpp:62