Skip to content

Commit

Permalink
Implement matrices used in network dynamics (#366)
Browse files Browse the repository at this point in the history
* network dynamics matrices

* added example and readme update

---------

Co-authored-by: carlos_felix <[email protected]>
  • Loading branch information
carlosfrodrigues and carlos_felix authored Oct 24, 2023
1 parent a19c100 commit 7cd6b0c
Show file tree
Hide file tree
Showing 6 changed files with 238 additions and 1 deletion.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,20 @@ Eva(u,v)(i) =I(u ∈ keep[i]) + I(v ∈ keep[i]) +α * \frac{ecount[i]}{(|E|/p)}

The lowest value is taken as partition Id.

## Network Dynamics

### Degree Matrix

The Degree Matrix is a square matrix that provides insights into the connectivity of nodes in a graph. For directed graphs, it reflects the number of incoming and outgoing edges for each node, while for undirected graphs, it represents the number of edges incident to each node.

### Laplacian Matrix

The Laplacian Matrix is a square matrix derived from the adjacency matrix and degree matrix of a graph. It is instrumental in analyzing various properties of the graph, such as connectedness, the count of spanning trees, and other spectral characteristics.

### Transition Matrix

The Transition Matrix is commonly used in the study of Markov Chains and stochastic processes. Within the context of a graph, it denotes the probabilities of transitioning from one node to another, often based on the edge weights or predetermined criteria. This matrix finds applications in various fields such as network analysis, machine learning, and optimization.

## How to contribute

[![GitHub contributors](https://img.shields.io/github/contributors/ZigRazor/CXXGraph.svg)](https://GitHub.com/ZigRazor/CXXGraph/graphs/contributors/)
Expand Down
1 change: 1 addition & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ if(EXAMPLES)

add_subdirectory(DialExample)
add_subdirectory(DijkstraExample)
add_subdirectory(NetworkDynamicsExample)
add_subdirectory(PartitionExample)

endif(EXAMPLES)
17 changes: 17 additions & 0 deletions examples/NetworkDynamicsExample/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
cmake_minimum_required(VERSION 3.9)
project(NetworkDynamicsExample)

# specify the C++ standard
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)

set(CMAKE_CXX_IMPLICIT_LINK_DIRECTORIES /usr/local/lib ${CMAKE_CXX_IMPLICIT_LINK_DIRECTORIES})

add_executable(network_dynamics_example network_dynamics_example.cpp)
target_include_directories(network_dynamics_example PUBLIC "${CMAKE_SOURCE_DIR}/include")

target_link_libraries(network_dynamics_example
pthread
ssl
crypto
z)
62 changes: 62 additions & 0 deletions examples/NetworkDynamicsExample/network_dynamics_example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include "CXXGraph/CXXGraph.hpp"

#include <memory>

using std::make_shared;

int main() {
CXXGraph::Node<int> node0("0", 0);
CXXGraph::Node<int> node1("1", 1);
CXXGraph::Node<int> node2("2", 2);
CXXGraph::Node<int> node3("3", 3);

CXXGraph::UndirectedWeightedEdge<int> edge1(1, node1, node2, 2.0);
CXXGraph::UndirectedWeightedEdge<int> edge2(2, node2, node3, 2.0);
CXXGraph::UndirectedWeightedEdge<int> edge3(3, node0, node1, 2.0);
CXXGraph::UndirectedWeightedEdge<int> edge4(4, node0, node3, 1.0);

CXXGraph::T_EdgeSet<int> edgeSet;
edgeSet.insert(make_shared<CXXGraph::UndirectedWeightedEdge<int>>(edge1));
edgeSet.insert(make_shared<CXXGraph::UndirectedWeightedEdge<int>>(edge2));
edgeSet.insert(make_shared<CXXGraph::UndirectedWeightedEdge<int>>(edge3));
edgeSet.insert(make_shared<CXXGraph::UndirectedWeightedEdge<int>>(edge4));

CXXGraph::Graph<int> graph(edgeSet);

auto degreeMatrix = graph.getDegreeMatrix();
for (const auto& nodePair : *degreeMatrix) {
const CXXGraph::shared<const CXXGraph::Node<int>>& node = nodePair.first;
const std::vector<int>& degrees = nodePair.second;

std::cout << "Node: " << node->getId() << ", Degree: " << degrees[0] << "\n";
}
auto laplacianMatrix = graph.getLaplacianMatrix();
for (const auto& nodePair : *laplacianMatrix) {
const auto& node = nodePair.first;
const auto& neighbors = nodePair.second;

std::cout << "Node " << node->getId() << " connected to:" << std::endl;
for (const auto& neighbor : neighbors) {
if (neighbor.first == node) {
std::cout << " -> Itself" << std::endl;
} else {
std::cout << " -> Node " << neighbor.first->getId() << " with Edge ID " << (neighbor.second ? neighbor.second->getId() : -1) << std::endl;
}
}
std::cout << std::endl;
}

auto transitionMatrix = graph.getTransitionMatrix();
for (const auto& nodePair : *transitionMatrix) {
const auto& node = nodePair.first;
const auto& transitions = nodePair.second;

std::cout << "Transitions from Node " << node->getId() << ":" << std::endl;
for (const auto& transition : transitions) {
std::cout << " -> To Node " << transition.first->getId() << " with Probability " << transition.second << std::endl;
}
std::cout << std::endl;
}

return 0;
}
127 changes: 126 additions & 1 deletion include/CXXGraph/Graph/Graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,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 @@ -346,6 +348,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 @@ -873,6 +902,9 @@ template <typename T>
Graph<T>::Graph() {
/* Caching the adjacency matrix */
cacheAdjMatrix();
cacheDegreeMatrix();
cacheLaplacianMatrix();
cacheTransitionMatrix();
}

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

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

template <typename T>
Expand Down Expand Up @@ -1738,6 +1775,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 @@ -256,6 +256,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 7cd6b0c

Please sign in to comment.