Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions graph.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "graph.h"
#include <vector>

// Implementation of powMatrix function
std::vector<std::vector<int>> powMatrix(std::vector<std::vector<int>> A, int p) {
int n = A.size();
std::vector<std::vector<int>> result(n, std::vector<int>(n, 0));

// Initialize identity matrix
for(int i = 0; i < n; i++) result[i][i] = 1;

while(p) {
if(p % 2) {
std::vector<std::vector<int>> temp(n, std::vector<int>(n, 0));
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
for(int k = 0; k < n; k++)
temp[i][j] += result[i][k] * A[k][j];
result = temp;
}

std::vector<std::vector<int>> temp(n, std::vector<int>(n, 0));
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
for(int k = 0; k < n; k++)
temp[i][j] += A[i][k] * A[k][j];
A = temp;
p /= 2;
}

return result;
}
9 changes: 9 additions & 0 deletions graph.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef GRAPH_H
#define GRAPH_H

#include <vector>

// Declaration of powMatrix function
std::vector<std::vector<int>> powMatrix(std::vector<std::vector<int>> A, int p);

#endif