| CXXGraph 0.3.0 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: MPL v2.0 ***/
18 /***********************************************************/
19
20 #ifndef __CXXGRAPH_GRAPH_TS_H__
21 #define __CXXGRAPH_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 long edgeId) override;
83 const std::list<const Node<T> *> getNodeSet() const override;
93 const std::optional<const Edge<T> *> getEdge(unsigned long 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;
126 const BellmanFordResult bellmanford(const Node<T> &source, const Node<T> &target) const override;
137 const FWResult floydWarshall() const override;
148 const std::vector<Node<T>> breadth_first_search(const Node<T> &start) const override;
159 const std::vector<Node<T>> depth_first_search(const Node<T> &start) const override;
160
169 bool isCyclicDirectedGraphDFS() const override;
170
179 bool isCyclicDirectedGraphBFS() const override;
180
188 bool isDirectedGraph() const override;
189
216 const DialResult dial(const Node<T> &source, int maxWeight) const override;
217
231 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;
232
246 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;
247
257 PartitionMap<T> partitionGraph(PARTITIONING::PartitionAlgorithm algorithm, unsigned int numberOfPartitions,double param1 = 0.0,double param2 = 0.0,double param3 = 0.0,unsigned int numberOfthreads = std::thread::hardware_concurrency()) const override;
258 };
259
260 template <typename T>
261 Graph_TS<T>::Graph_TS(const std::list<const Edge<T> *> &edgeSet) : Graph<T>(edgeSet), ThreadSafe() {}
262
263 template <typename T>
264 Graph_TS<T>::Graph_TS(const Graph<T> &graph) : Graph<T>(graph), ThreadSafe() {}
265
266 template <typename T>
267 const std::list<const Edge<T> *> &Graph_TS<T>::getEdgeSet() const
268 {
269 std::lock_guard<std::mutex> lock(mutex);
270 return Graph<T>::getEdgeSet();
271 }
272
273 template <typename T>
274 void Graph_TS<T>::setEdgeSet(std::list<const Edge<T> *> &edgeSet)
275 {
276 getLock();
277 Graph<T>::setEdgeSet(edgeSet);
278 releaseLock();
279 }
280
281 template <typename T>
282 void Graph_TS<T>::addEdge(const Edge<T> *edge)
283 {
284 getLock();
285 Graph<T>::addEdge(edge);
286 releaseLock();
287 }
288
289 template <typename T>
290 void Graph_TS<T>::removeEdge(unsigned long long edgeId)
291 {
292 getLock();
293 Graph<T>::removeEdge(edgeId);
294 releaseLock();
295 }
296
297 template <typename T>
298 const std::list<const Node<T> *> Graph_TS<T>::getNodeSet() const
299 {
300 getLock();
301 auto ns = Graph<T>::getNodeSet();
302 releaseLock();
303 return ns;
304 }
305
306 template <typename T>
307 const std::optional<const Edge<T> *> Graph_TS<T>::getEdge(unsigned long long edgeId) const
308 {
309 getLock();
310 auto e = Graph<T>::getEdge(edgeId);
311 releaseLock();
312 return e;
313 }
314
315 template <typename T>
316 const AdjacencyMatrix<T> Graph_TS<T>::getAdjMatrix() const
317 {
318 getLock();
319 auto adjm = Graph<T>::getAdjMatrix();
320 releaseLock();
321 return adjm;
322 }
323
324 template <typename T>
325 const DijkstraResult Graph_TS<T>::dijkstra(const Node<T> &source, const Node<T> &target) const
326 {
327 getLock();
328 auto dij = Graph<T>::dijkstra(source, target);
329 releaseLock();
330 return dij;
331 }
332
333 template <typename T>
334 const BellmanFordResult Graph_TS<T>::bellmanford(const Node<T> &source, const Node<T> &target) const
335 {
336 getLock();
337 auto bellford = Graph<T>::bellmanford(source, target);
338 releaseLock();
339 return bellford;
340 }
341
342 template <typename T>
343 const FWResult Graph_TS<T>::floydWarshall() const
344 {
345 getLock();
346 auto fw = Graph<T>::floydWarshall();
347 releaseLock();
348 return fw;
349 }
350
351 template <typename T>
352 const std::vector<Node<T>> Graph_TS<T>::breadth_first_search(const Node<T> &start) const
353 {
354 getLock();
355 auto bfs = Graph<T>::breadth_first_search(start);
356 releaseLock();
357 return bfs;
358 }
359
360 template <typename T>
361 const std::vector<Node<T>> Graph_TS<T>::depth_first_search(const Node<T> &start) const
362 {
363 getLock();
364 auto dfs = Graph<T>::depth_first_search(start);
365 releaseLock();
366 return dfs;
367 }
368
369 template <typename T>
370 bool Graph_TS<T>::isCyclicDirectedGraphDFS() const
371 {
372 getLock();
373 auto result = Graph<T>::isCyclicDirectedGraphDFS();
374 releaseLock();
375 return result;
376 }
377
378 template <typename T>
379 bool Graph_TS<T>::isCyclicDirectedGraphBFS() const
380 {
381 getLock();
382 auto result = Graph<T>::isCyclicDirectedGraphBFS();
383 releaseLock();
384 return result;
385 }
386
387 template <typename T>
388 bool Graph_TS<T>::isDirectedGraph() const
389 {
390 getLock();
391 auto result = Graph<T>::isDirectedGraph();
392 releaseLock();
393 return result;
394 }
395
396 template <typename T>
397 const DialResult Graph_TS<T>::dial(const Node<T> &source, int maxWeight) const
398 {
399 getLock();
400 auto dial = Graph<T>::dial(source, maxWeight);
401 releaseLock();
402 return dial;
403 }
404
405 template <typename T>
406 int Graph_TS<T>::writeToFile(InputOutputFormat format, const std::string &workingDir, const std::string &OFileName, bool compress, bool writeNodeFeat, bool writeEdgeWeight) const
407 {
408 getLock();
409 auto result = Graph<T>::writeToFile(format, workingDir, OFileName, compress, writeNodeFeat, writeEdgeWeight);
410 releaseLock();
411 return result;
412 }
413
414 template <typename T>
415 int Graph_TS<T>::readFromFile(InputOutputFormat format, const std::string &workingDir, const std::string &OFileName, bool compress, bool readNodeFeat, bool readEdgeWeight)
416 {
417 getLock();
418 auto result = Graph<T>::readFromFile(format, workingDir, OFileName, compress, readNodeFeat, readEdgeWeight);
419 releaseLock();
420 return result;
421 }
422
423 template <typename T>
424 PartitionMap<T> Graph_TS<T>::partitionGraph(PARTITIONING::PartitionAlgorithm algorithm, unsigned int numberOfPartitions,double param1,double param2,double param3,unsigned int numberOfthreads) const
425 {
426 getLock();
427 auto partitions = Graph<T>::partitionGraph(algorithm, numberOfPartitions, param1, param2, param3, numberOfthreads);
428 releaseLock();
429 return partitions;
430 }
431 }
432 #endif // __CXXGRAPH_GRAPH_TS_H__
CXXGRAPH::Edge
Definition: Edge.hpp:39
CXXGRAPH::Graph_TS
Class that implement the Thread Safe Graph.
Definition: Graph_TS.hpp:33
CXXGRAPH::Graph_TS::getNodeSet
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:298
CXXGRAPH::Graph_TS::setEdgeSet
void setEdgeSet(std::list< const Edge< T > * > &edgeSet) override
Function set the Edge Set of the Graph Note: Thread Safe.
Definition: Graph_TS.hpp:274
CXXGRAPH::Graph_TS::isDirectedGraph
bool isDirectedGraph() const override
This function checks if a graph is directed Note: Thread Safe.
Definition: Graph_TS.hpp:388
CXXGRAPH::Graph_TS::dijkstra
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:325
CXXGRAPH::Graph_TS::partitionGraph
PartitionMap< T > partitionGraph(PARTITIONING::PartitionAlgorithm algorithm, unsigned int numberOfPartitions, double param1=0.0, double param2=0.0, double param3=0.0, unsigned int numberOfthreads=std::thread::hardware_concurrency()) const override
This function partition a graph in a set of partitions Note: Thread Safe.
Definition: Graph_TS.hpp:424
CXXGRAPH::Graph_TS::removeEdge
void removeEdge(unsigned long long edgeId) override
Function remove an Edge from the Graph Edge Set Note: Thread Safe.
Definition: Graph_TS.hpp:290
CXXGRAPH::Graph_TS::writeToFile
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:406
CXXGRAPH::Graph_TS::addEdge
void addEdge(const Edge< T > *edge) override
Function add an Edge to the Graph Edge Set Note: Thread Safe.
Definition: Graph_TS.hpp:282
CXXGRAPH::Graph_TS::isCyclicDirectedGraphBFS
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:379
CXXGRAPH::Graph_TS::readFromFile
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:415
CXXGRAPH::Graph_TS::breadth_first_search
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:352
CXXGRAPH::Graph_TS::getEdge
const std::optional< const Edge< T > * > getEdge(unsigned long long edgeId) const override
Function that return an Edge with specific ID if Exist in the Graph Note: Thread Safe.
Definition: Graph_TS.hpp:307
CXXGRAPH::Graph_TS::bellmanford
const BellmanFordResult bellmanford(const Node< T > &source, const Node< T > &target) const override
Function runs the bellmanford algorithm for some source node and target node in the graph and returns...
Definition: Graph_TS.hpp:334
CXXGRAPH::Graph_TS::getEdgeSet
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:267
CXXGRAPH::Graph_TS::depth_first_search
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:361
CXXGRAPH::Graph_TS::floydWarshall
const FWResult floydWarshall() const override
Function runs the floyd-Warshall algorithm for every node in the graph and returns the shortest dista...
Definition: Graph_TS.hpp:343
CXXGRAPH::Graph_TS::dial
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:397
CXXGRAPH::Graph_TS::isCyclicDirectedGraphDFS
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:370
CXXGRAPH::Graph_TS::getAdjMatrix
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:316
CXXGRAPH::Graph
Class that implement the Graph. ( This class is not Thread Safe )
Definition: Graph.hpp:79
CXXGRAPH::Graph::floydWarshall
virtual const FWResult floydWarshall() const
Function runs the floyd-warshall algorithm and returns the shortest distance of all pair of nodes....
Definition: Graph.hpp:1147
CXXGRAPH::Graph::depth_first_search
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:1521
CXXGRAPH::Graph::getEdgeSet
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:429
CXXGRAPH::Graph::isCyclicDirectedGraphBFS
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:1686
CXXGRAPH::Graph::writeToFile
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:1954
CXXGRAPH::Graph::isDirectedGraph
virtual bool isDirectedGraph() const
This function checks if a graph is directed Note: No Thread Safe.
Definition: Graph.hpp:1755
CXXGRAPH::Graph::getEdge
virtual const std::optional< const Edge< T > * > getEdge(unsigned long long edgeId) const
Function that return an Edge with specific ID if Exist in the Graph Note: No Thread Safe.
Definition: Graph.hpp:490
CXXGRAPH::Graph::dial
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:1787
CXXGRAPH::Graph::partitionGraph
virtual PartitionMap< T > partitionGraph(PARTITIONING::PartitionAlgorithm algorithm, unsigned int numberOfPartitions, double param1=0.0, double param2=0.0, double param3=0.0, unsigned int numberOfthreads=std::thread::hardware_concurrency()) const
This function partition a graph in a set of partitions Note: No Thread Safe.
Definition: Graph.hpp:2067
CXXGRAPH::Graph::dijkstra
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:923
CXXGRAPH::Graph::isCyclicDirectedGraphDFS
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:1553
CXXGRAPH::Graph::addEdge
virtual void addEdge(const Edge< T > *edge)
Function add an Edge to the Graph Edge Set Note: No Thread Safe.
Definition: Graph.hpp:449
CXXGRAPH::Graph::readFromFile
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:2015
CXXGRAPH::Graph::removeEdge
virtual void removeEdge(unsigned long long edgeId)
Function remove an Edge from the Graph Edge Set Note: No Thread Safe.
Definition: Graph.hpp:459
CXXGRAPH::Graph::getAdjMatrix
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:897
CXXGRAPH::Graph::getNodeSet
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:470
CXXGRAPH::Graph::bellmanford
virtual const BellmanFordResult bellmanford(const Node< T > &source, const Node< T > &target) const
Function runs the bellman-ford algorithm for some source node and target node in the graph and return...
Definition: Graph.hpp:1039
CXXGRAPH::Graph::breadth_first_search
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:1481
CXXGRAPH::Graph::setEdgeSet
virtual void setEdgeSet(std::list< const Edge< T > * > &edgeSet)
Function set the Edge Set of the Graph Note: No Thread Safe.
Definition: Graph.hpp:435
CXXGRAPH::Node
Definition: Node.hpp:38
CXXGRAPH::ThreadSafe
Definition: ThreadSafe.hpp:30
CXXGRAPH::BellmanFordResult_struct
Struct that contains the information about Dijsktra's Algorithm results.
Definition: Typedef.hpp:79
CXXGRAPH::DialResult_struct
Struct that contains the information about Dijsktra's Algorithm results.
Definition: Typedef.hpp:130
CXXGRAPH::DijkstraResult_struct
Struct that contains the information about Dijsktra's Algorithm results.
Definition: Typedef.hpp:70
CXXGRAPH::FWResult_struct
Struct that contains the information about Floyd-Warshall Algorithm results.
Definition: Typedef.hpp:110