diff --git a/graph.cpp b/graph.cpp new file mode 100644 index 000000000..1d61bc131 --- /dev/null +++ b/graph.cpp @@ -0,0 +1,32 @@ +#include "graph.h" +#include + +// Implementation of powMatrix function +std::vector> powMatrix(std::vector> A, int p) { + int n = A.size(); + std::vector> result(n, std::vector(n, 0)); + + // Initialize identity matrix + for(int i = 0; i < n; i++) result[i][i] = 1; + + while(p) { + if(p % 2) { + std::vector> temp(n, std::vector(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> temp(n, std::vector(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; +} diff --git a/graph.h b/graph.h new file mode 100644 index 000000000..c9393db02 --- /dev/null +++ b/graph.h @@ -0,0 +1,9 @@ +#ifndef GRAPH_H +#define GRAPH_H + +#include + +// Declaration of powMatrix function +std::vector> powMatrix(std::vector> A, int p); + +#endif