-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement matrices used in network dynamics (#366)
* network dynamics matrices * added example and readme update --------- Co-authored-by: carlos_felix <[email protected]>
- Loading branch information
1 parent
a19c100
commit 7cd6b0c
Showing
6 changed files
with
238 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
62
examples/NetworkDynamicsExample/network_dynamics_example.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters