A class representing a symmetric adjacency matrix. More...
#include "symmetric-adjacency-matrix.h"
Public Member Functions | |
SymmetricAdjacencyMatrix (size_t numRows=0, T value={}) | |
Default constructor. | |
void | AddRow () |
Add new row to the adjacency matrix. | |
size_t | GetRows () |
Retrieve number of rows in the adjacency matrix. | |
T | GetValue (size_t row, size_t column) |
Retrieve the value of matrix (row, column) node. | |
void | SetValue (size_t row, size_t column, T value) |
Set the value of matrix (row, column) node. | |
void | SetValueAdjacent (size_t row, T value) |
Set the value of adjacent nodes of a given node (all columns of a given row, and its reflection) | |
Private Attributes | |
std::vector< T > | m_matrix |
The adjacency matrix. | |
std::vector< size_t > | m_rowOffsets |
Precomputed row starting offsets of m_matrix. | |
size_t | m_rows |
Number of rows in matrix. | |
A class representing a symmetric adjacency matrix.
Since the matrix is symmetric, we save up on memory by storing only the lower left triangle, including the main diagonal.
In pseudocode, the matrix is stored as a vector m_matrix, where each new row is accessed via an offset precomputed in m_rowOffsets. We also keep track of the number of rows in m_rows.
A 4x4 matrix would be represented as follows:
To add a new row (AddRow()
) in the adjacency matrix (equivalent to an additional node in a bidirected graph), we need to first add a new offset, then increment the number of rows and finally resize the vector.
The resulting state would be:
In this previous example, the elements of the matrix are the offset of the values from the beginning of the vector.
In practice, this matrix could store the state between a given pair of a link between two nodes. The state could be a boolean value, in case just tracking valid/invalid, connected/disconnected link, or numerical types to store weights, which can be used for routing algorithms.
The adjacency-matrix-example
illustrates the usage of the adjacency matrix in a routing example.
First we set up the matrix with capacity for 10 nodes. All values are initialized to maximum, to indicate a disconnected node.
We can then map graph nodes into the table rows
Then proceed to populate the matrix to reflect the graph
Then we set the weights from the nodes to themselves as 0
Create the known shortest paths
And we finally can proceed to assemble paths between nodes and store them in a routing table. In this case, by brute-force
After this, we have both the complete route, weight of the route, and the weights for each hop in the route.
We can print all this information for a given route between nodes srcNodeOpt and dstNodeOpt with
Which, for example, between nodes A and I, would print
route between A and I (length 24):--0-->A--5-->B--3-->F--4-->G--3-->H--1-->J--1-->C--7-->I
In case one of the links is disconnected, the weights of the adjacency matrix can be reset with SetValueAdjacent(disconnectedNode, maxFloat).
Note that, in this implementation, all the routes containing the node need to be removed from routeMap, and the search needs to be re-executed.
Definition at line 232 of file symmetric-adjacency-matrix.h.
|
inline |
Default constructor.
[in] | numRows | The number of rows in the matrix |
[in] | value | The default initialization value of matrix |
Definition at line 240 of file symmetric-adjacency-matrix.h.
|
inline |
Add new row to the adjacency matrix.
Definition at line 305 of file symmetric-adjacency-matrix.h.
References ns3::SymmetricAdjacencyMatrix< T >::m_matrix, ns3::SymmetricAdjacencyMatrix< T >::m_rowOffsets, and ns3::SymmetricAdjacencyMatrix< T >::m_rows.
Referenced by ns3::PhasedArrayModel::PhasedArrayModel(), and SymmetricAdjacencyMatrixTestCase::DoRun().
|
inline |
Retrieve number of rows in the adjacency matrix.
Definition at line 316 of file symmetric-adjacency-matrix.h.
References ns3::SymmetricAdjacencyMatrix< T >::m_rows.
Referenced by SymmetricAdjacencyMatrixTestCase::DoRun().
|
inline |
Retrieve the value of matrix (row, column) node.
[in] | row | The row of the matrix to retrieve the value |
[in] | column | The column of the matrix to retrieve the value |
Definition at line 257 of file symmetric-adjacency-matrix.h.
References ns3::SymmetricAdjacencyMatrix< T >::m_matrix, and ns3::SymmetricAdjacencyMatrix< T >::m_rowOffsets.
Referenced by SymmetricAdjacencyMatrixTestCase::DoRun(), and ns3::PhasedArrayModel::IsChannelOutOfDate().
|
inline |
Set the value of matrix (row, column) node.
[in] | row | The row of the matrix to set the value |
[in] | column | The column of the matrix to set the value |
[in] | value | value to be assigned to matrix (row, column) or matrix (column, row) |
Definition at line 271 of file symmetric-adjacency-matrix.h.
References ns3::SymmetricAdjacencyMatrix< T >::m_matrix, and ns3::SymmetricAdjacencyMatrix< T >::m_rowOffsets.
Referenced by SymmetricAdjacencyMatrixTestCase::DoRun(), and ns3::PhasedArrayModel::IsChannelOutOfDate().
|
inline |
Set the value of adjacent nodes of a given node (all columns of a given row, and its reflection)
[in] | row | The row of the matrix to set the value |
[in] | value | Value to be assigned to matrix (row, column) or matrix (column, row) |
Definition at line 285 of file symmetric-adjacency-matrix.h.
References ns3::SymmetricAdjacencyMatrix< T >::m_matrix, ns3::SymmetricAdjacencyMatrix< T >::m_rowOffsets, and ns3::SymmetricAdjacencyMatrix< T >::m_rows.
Referenced by ns3::PhasedArrayModel::PhasedArrayModel(), SymmetricAdjacencyMatrixTestCase::DoRun(), and ns3::PhasedArrayModel::InvalidateChannels().
|
private |
The adjacency matrix.
For efficiency purposes, we store only lower left half, including the main diagonal. It also is stored as a vector not to introduce gaps between different rows or items (in case T = bool)
Definition at line 324 of file symmetric-adjacency-matrix.h.
Referenced by ns3::SymmetricAdjacencyMatrix< T >::AddRow(), ns3::SymmetricAdjacencyMatrix< T >::GetValue(), ns3::SymmetricAdjacencyMatrix< T >::SetValue(), and ns3::SymmetricAdjacencyMatrix< T >::SetValueAdjacent().
|
private |
Precomputed row starting offsets of m_matrix.
Definition at line 327 of file symmetric-adjacency-matrix.h.
Referenced by ns3::SymmetricAdjacencyMatrix< T >::AddRow(), ns3::SymmetricAdjacencyMatrix< T >::GetValue(), ns3::SymmetricAdjacencyMatrix< T >::SetValue(), and ns3::SymmetricAdjacencyMatrix< T >::SetValueAdjacent().
|
private |
Number of rows in matrix.
Definition at line 322 of file symmetric-adjacency-matrix.h.
Referenced by ns3::SymmetricAdjacencyMatrix< T >::AddRow(), ns3::SymmetricAdjacencyMatrix< T >::GetRows(), and ns3::SymmetricAdjacencyMatrix< T >::SetValueAdjacent().