GraphLibrary  0.0.1
A simple library that implements various graph algorithms.
 All Classes Namespaces Files Functions Variables Typedefs Macros
LaplacianSTL.hpp
Go to the documentation of this file.
1 #ifndef GL_LAPLACIAN_STL_HPP
2 #define GL_LAPLACIAN_STL_HPP
3 
4 #include "../structures/Graph.hpp"
5 
6 namespace gl {
7 namespace algorithm {
8 
15 #ifndef DOXYGEN_SHOULD_SKIP_THIS
16 template <class S, class STORAGE, class DIR, GL_ENABLE_IF_DIRECTED_T>
17 #endif
18 #ifdef DOXYGEN_SHOULD_SKIP_THIS
19 template <class class SCALAR, class STORAGE_KIND, class DIRECTION>
20 #endif
21 std::vector<float> LaplacianSTL(const Graph<S,STORAGE,DIR>& g)
22 {
23  using matrix_t = std::vector<float>;
24  auto numNodes = g.numNodes();
25  matrix_t matrix (numNodes*numNodes,0);
26 
27  // build degree matrix
28  for (typename Graph<S,STORAGE,DIR>::idx_t i = 0; i < numNodes; i++)
29  {
30  matrix[i*numNodes+i] = g.getNodeOutDegree(i);
31  }
32 
33  // build adjacency matrix
34  for (auto it = g.edge_cbegin(); it != g.edge_cend(); it++)
35  {
36  matrix[it->source()*numNodes+it->dest()] -= 1;
37  }
38 
39  return matrix;
40 }
41 
42 #ifndef DOXYGEN_SHOULD_SKIP_THIS
43 template <class S, class STORAGE, class DIR, GL_ENABLE_IF_MATRIX_UNDIRECTED_T>
44 std::vector<float> LaplacianSTL(const Graph<S,STORAGE,DIR>& g)
45 {
46  using matrix_t = std::vector<float>;
47  auto numNodes = g.numNodes();
48  matrix_t matrix (numNodes*numNodes,0);
49 
50  // build degree matrix
51  for (typename Graph<S,STORAGE,DIR>::idx_t i = 0; i < numNodes; i++)
52  {
53  matrix[i*numNodes+i] = g.getNodeOutDegree(i);
54  }
55 
56  // build adjacency matrix
57  for (auto it = g.edge_cbegin(); it != g.edge_cend(); it++)
58  {
59  matrix[it->source()*numNodes+it->dest()] -= 1;
60  matrix[it->dest()*numNodes+it->source()] -= 1;
61  }
62 
63  return matrix;
64 }
65 
66 template <class S, class STORAGE, class DIR, GL_ENABLE_IF_LIST_UNDIRECTED_T>
67 std::vector<float> LaplacianSTL(const Graph<S,STORAGE,DIR>& g)
68 {
69  using matrix_t = std::vector<float>;
70  auto numNodes = g.numNodes();
71  matrix_t matrix (numNodes*numNodes,0);
72 
73  // build degree matrix
74  for (typename Graph<S,STORAGE,DIR>::idx_t i = 0; i < numNodes; i++)
75  {
76  matrix[i*numNodes+i] = g.getNodeOutDegree(i);
77  }
78 
79  // build adjacency matrix
80  for (auto it = g.edge_cbegin(); it != g.edge_cend(); it++)
81  {
82  matrix[it->source()*numNodes+it->dest()] -= 1;
83  }
84 
85  return matrix;
86 }
87 #endif // DOXYGEN_SHOULD_SKIP_THIS
88 
89 
90 } // namespace algorithm
91 } // namespace gl
92 
93 #endif // GL_LAPLACIAN_STL_HPP
ConstEdgeIterator edge_cbegin() const
ConstEdgeIterator to the first edge.
Definition: Graph.hpp:967
gl::index_type idx_t
Index type.
Definition: Graph.hpp:44
Stores and implements a Graph.
Definition: Graph.hpp:39
idx_t getNodeOutDegree(const idx_t &id) const
Gets the in-degree of a node (i.e. count of all outgoing edges).
Definition: Graph.hpp:1316
std::vector< float > LaplacianSTL(const Graph< S, STORAGE, DIR > &g)
Compute the Laplacian Matrix of a graph in an STL vector.
Definition: LaplacianSTL.hpp:21
ConstEdgeIterator edge_cend() const
ConstEdgeIterator to the last edge.
Definition: Graph.hpp:989
idx_t numNodes() const
Returns the number of nodes currently in the graph.
Definition: Graph.hpp:406