Skip to content

Commit

Permalink
cleaning adjacency matrix power
Browse files Browse the repository at this point in the history
  • Loading branch information
wrcorcoran committed Sep 26, 2024
1 parent 02453a9 commit 03346c1
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions include/CXXGraph/Graph/Algorithm/Pow_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,13 @@ const PowAdjResult matrixPow(const shared<AdjacencyMatrix<T>> &adj,
PowAdjResult result;
result.success = false;
result.errorMessage = "";
std::unordered_map<std::pair<std::string, std::string>, unsigned long long,
CXXGraph::pair_hash>
powAdj;

// convert back and forth between user ids and index in temporary adj matrix
std::unordered_map<std::string, int> userIdToIdx;
std::unordered_map<int, std::string> idxToUserId;

int n = 0;
for (const auto &[node, list] : *adj) {
for (const auto &[node, _] : *adj) {
userIdToIdx[node->getUserId()] = n;
idxToUserId[n] = node->getUserId();
n++;
Expand All @@ -128,27 +125,30 @@ const PowAdjResult matrixPow(const shared<AdjacencyMatrix<T>> &adj,

// populate temporary adjacency matrix w/ edges
// can handle both directed and undirected
for (const auto &[node, edges] : *adj) {
for (const auto &e : edges) {
const auto edge = e.second->getNodePair();
const auto firstId = edge.first->getUserId();
const auto secondId = edge.second->getUserId();
for (const auto &[_, edges] : *adj) {
for (const auto &[_, edge] : edges) {
const auto &[u, v] = edge->getNodePair();
const auto uIdx = userIdToIdx[u->getUserId()];
const auto vIdx = userIdToIdx[v->getUserId()];

// if undirected, add both sides
if (!(e.second->isDirected().has_value() &&
e.second->isDirected().value()))
tempIntAdj[userIdToIdx[secondId]][userIdToIdx[firstId]] = 1;
tempIntAdj[userIdToIdx[firstId]][userIdToIdx[secondId]] = 1;
if (!(edge->isDirected().has_value() && edge->isDirected().value()))
tempIntAdj[vIdx][uIdx] = 1;
tempIntAdj[uIdx][vIdx] = 1;
}
}

// calculate the power matrix
auto powerMatrix = exponentiation(tempIntAdj, k);
const auto powerMatrix = exponentiation(tempIntAdj, k);

// remap values
std::unordered_map<std::pair<std::string, std::string>, unsigned long long,
CXXGraph::pair_hash>
powAdj;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
auto pr = std::make_pair(idxToUserId[i], idxToUserId[j]);
powAdj[pr] = powerMatrix[i][j];
powAdj[std::make_pair(idxToUserId[i], idxToUserId[j])] =
powerMatrix[i][j];
}
}

Expand Down

0 comments on commit 03346c1

Please sign in to comment.