Skip to content

Commit

Permalink
network dynamics matrices
Browse files Browse the repository at this point in the history
  • Loading branch information
carlos_felix committed Oct 22, 2023
1 parent b30e7b5 commit 8e21dff
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 1 deletion.
127 changes: 126 additions & 1 deletion include/CXXGraph/Graph/Graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ class Graph {
T_NodeSet<T> isolatedNodesSet = {};

shared<AdjacencyMatrix<T>> cachedAdjMatrix;

shared<DegreeMatrix<T>> cachedDegreeMatrix;
shared<LaplacianMatrix<T>> cachedLaplacianMatrix;
shared<TransitionMatrix<T>> cachedTransitionMatrix;
// Private non-const getter for the set of nodes
std::unordered_set<shared<Node<T>>, nodeHash<T>> nodeSet();

Expand Down Expand Up @@ -304,6 +306,33 @@ class Graph {
virtual shared<AdjacencyMatrix<T>> getAdjMatrix() const;

virtual void cacheAdjMatrix();
/**
* @brief This function generates a list of the degree matrix with every element
* of the matrix containing the node where the link is directed and the
* corresponding edge to the link.
* Note: No Thread Safe
*/
virtual shared<DegreeMatrix<T>> getDegreeMatrix() const;

virtual void cacheDegreeMatrix();
/**
* @brief This function generates a list of the Laplacian matrix with every element
* of the matrix containing the node connected to the current node and the
* corresponding edge to the link.
* Note: No Thread Safe
*/
virtual shared<LaplacianMatrix<T>> getLaplacianMatrix() const;

virtual void cacheLaplacianMatrix();
/**
* @brief This function generates a list of the transition matrix with every element
* of the matrix containing the node that can be transitioned to from the
* current node and the probability of the transition.
* Note: No Thread Safe
*/
virtual shared<TransitionMatrix<T>> getTransitionMatrix() const;

virtual void cacheTransitionMatrix();
/**
* \brief This function generates a set of nodes linked to the provided node
* in a directed graph
Expand Down Expand Up @@ -831,6 +860,9 @@ template <typename T>
Graph<T>::Graph() {
/* Caching the adjacency matrix */
cacheAdjMatrix();
cacheDegreeMatrix();
cacheLaplacianMatrix();
cacheTransitionMatrix();
}

template <typename T>
Expand All @@ -840,6 +872,9 @@ Graph<T>::Graph(const T_EdgeSet<T> &edgeSet) {
}
/* Caching the adjacency matrix */
cacheAdjMatrix();
cacheDegreeMatrix();
cacheLaplacianMatrix();
cacheTransitionMatrix();
}

template <typename T>
Expand All @@ -855,6 +890,8 @@ void Graph<T>::setEdgeSet(const T_EdgeSet<T> &edgeSet) {
}
/* Caching the adjacency matrix */
cacheAdjMatrix();
cacheDegreeMatrix();
cacheLaplacianMatrix();
}

template <typename T>
Expand Down Expand Up @@ -1666,6 +1703,94 @@ void Graph<T>::cacheAdjMatrix() {
this->cachedAdjMatrix = adj;
}

template <typename T>
shared<DegreeMatrix<T>> Graph<T>::getDegreeMatrix() const {
auto degreeMatrix = std::make_shared<DegreeMatrix<T>>();

for (const auto& nodePair : *this->cachedAdjMatrix) {
const shared<const Node<T>>& node = nodePair.first;
const std::vector<std::pair<shared<const Node<T>>, shared<const Edge<T>>>>& neighbors = nodePair.second;

int degree = neighbors.size();

(*degreeMatrix)[node] = {degree};
}

return degreeMatrix;
}

template <typename T>
void Graph<T>::cacheDegreeMatrix() {
const auto degreeMatrix = Graph<T>::getDegreeMatrix();
this->cachedDegreeMatrix = degreeMatrix;
}

template <typename T>
shared<LaplacianMatrix<T>> Graph<T>::getLaplacianMatrix() const {
const auto adjacencyMatrix = this->cachedAdjMatrix;
const auto degreeMatrix = this->cachedDegreeMatrix;

auto laplacianMatrix = std::make_shared<LaplacianMatrix<T>>();
for (const auto& nodePair : *adjacencyMatrix) {
const shared<const Node<T>>& node = nodePair.first;
(*laplacianMatrix)[node] = std::vector<std::pair<shared<const Node<T>>, shared<const Edge<T>>>>();
}

for (const auto& nodePair : *adjacencyMatrix) {
const shared<const Node<T>>& node = nodePair.first;
const std::vector<std::pair<shared<const Node<T>>, shared<const Edge<T>>>>& neighbors = nodePair.second;

int degree = neighbors.size();

(*laplacianMatrix)[node].emplace_back(node, nullptr); // Insere o nó na diagonal
for (const auto& neighborPair : neighbors) {
const shared<const Node<T>>& neighbor = neighborPair.first;
(*laplacianMatrix)[node].emplace_back(neighbor, neighborPair.second); // Insere os pares de vizinhos
}
}

return laplacianMatrix;
}

template <typename T>
void Graph<T>::cacheLaplacianMatrix() {
const auto laplacianMatrix = Graph<T>::getLaplacianMatrix();
this->cachedLaplacianMatrix = laplacianMatrix;
}

template <typename T>
shared<TransitionMatrix<T>> Graph<T>::getTransitionMatrix() const {
const auto adjacencyMatrix = this->cachedAdjMatrix;

auto transitionMatrix = std::make_shared<TransitionMatrix<T>>();
for (const auto& nodePair : *adjacencyMatrix) {
const shared<const Node<T>>& node = nodePair.first;
(*transitionMatrix)[node] = std::vector<std::pair<shared<const Node<T>>, double>>();
}

for (const auto& nodePair : *adjacencyMatrix) {
const shared<const Node<T>>& node = nodePair.first;
const std::vector<std::pair<shared<const Node<T>>, shared<const Edge<T>>>>& neighbors = nodePair.second;

int degree = neighbors.size();

double transitionProbability = 1.0 / degree;

for (const auto& neighborPair : neighbors) {
const shared<const Node<T>>& neighbor = neighborPair.first;
(*transitionMatrix)[node].emplace_back(neighbor, transitionProbability);
}
}

return transitionMatrix;
}

template <typename T>
void Graph<T>::cacheTransitionMatrix() {
const auto transitionMatrix = Graph<T>::getTransitionMatrix();
this->cachedTransitionMatrix = transitionMatrix;
}

template <typename T>
const std::unordered_set<shared<const Node<T>>, nodeHash<T>>
Graph<T>::outNeighbors(const Node<T> *node) const {
Expand Down
18 changes: 18 additions & 0 deletions include/CXXGraph/Utility/Typedef.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,24 @@ using AdjacencyMatrix = std::unordered_map<
std::vector<std::pair<shared<const Node<T>>, shared<const Edge<T>>>>,
nodeHash<T>>;

template <typename T>
using DegreeMatrix = std::unordered_map<
shared<const Node<T>>,
std::vector<int>,
nodeHash<T>>;

template <typename T>
using LaplacianMatrix = std::unordered_map<
shared<const Node<T>>,
std::vector<std::pair<shared<const Node<T>>, shared<const Edge<T>>>>,
nodeHash<T>>;

template <typename T>
using TransitionMatrix = std::unordered_map<
shared<const Node<T>>,
std::vector<std::pair<shared<const Node<T>>, double>>,
nodeHash<T>>;

template <typename T>
using PartitionMap =
std::unordered_map<unsigned int,
Expand Down

0 comments on commit 8e21dff

Please sign in to comment.