[英]How to generate every possible Adjacency Matrix for graphs with N nodes?
我目前正在為與圖論相關的課程做一個小項目。 我遇到的當前任務是生成每個具有 N 個節點(其中 N <= 4)的非同構圖。
更具體地說,我不確定如何實現算法的某個“步驟”。 這個想法是生成具有 N 個節點的每個可能的圖,或者換句話說,生成每個可能的 N 階鄰接矩陣,將它們中的每一個發送到 function 以檢查同構,如果它們是同構的,則將它們打印出來。
整個任務涉及一些功能和步驟,但在這個問題中,我將專注於給我帶來問題的一個 - 生成鄰接矩陣的所有可能組合。
到目前為止,我能夠做的是生成所有具有一條邊的矩陣,或者換句話說,只有一個“1”。 這是我的 function(在 C++ 中):
void generateAdjacencyMatrices(int N) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (i != j) {
adjacencyMatrix[i][j] = 1;
//logic for isomorphism
//logic for printing the graph
}
adjacencyMatrix[i][j] = 0;
}
}
}
這會生成這樣的矩陣(以 N = 3 為例):
0 1 0 0 0 1 0 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0 0 1
0 0 0 0 0 0 0 0 0 0 0 0
等等。
我需要做的是生成每個具有任意數量邊的單個矩陣(矩陣中任意數量的“1”),我不確定如何在正常時間內完成。 任何提示將不勝感激。
此代碼將為大小N
生成所有2^(N*N)
鄰接矩陣:
#include <iostream>
#include <vector>
unsigned int pow(unsigned int b, unsigned int e) {
if (e == 0) return 1;
if (e == 1) return b;
if (e % 2 == 0) {
return pow(b, e/2) * pow(b, e/2);
}
return b * pow(b, e - 1);
}
void generateAdjacencyMatrices(int N) {
const auto max = pow(2, N*N);
for (unsigned int counter = 0; counter < max; ++counter) {
auto c = counter;
std::vector<std::vector<int>> adjacencyMatrix(N, std::vector<int>(N));
for (int i = 0; i < N && c != 0; i++) {
for (int j = 0; j < N && c != 0; j++) {
adjacencyMatrix[i][j] = c % 2;
c /= 2;
}
}
for (const auto &row : adjacencyMatrix) {
for (const auto value : row) {
std::cout << value << ' ';
}
std::cout << '\n';
}
std::cout << '\n';
}
}
int main() {
generateAdjacencyMatrices(3);
}
它遍歷從0
到N-1
的數字,並使用每個數字的二進制表示來填充矩陣的值。
通過簡單的更改,您可以保持零對角線並生成2^(N*N - N)
鄰接矩陣:
#include <iostream>
#include <vector>
unsigned int pow(unsigned int b, unsigned int e) {
if (e == 0) return 1;
if (e == 1) return b;
if (e % 2 == 0) {
return pow(b, e/2) * pow(b, e/2);
}
return b * pow(b, e - 1);
}
void generateAdjacencyMatrices(int N) {
const auto max = pow(2, N*N - N);
for (unsigned int counter = 0; counter < max; ++counter) {
auto c = counter;
std::vector<std::vector<int>> adjacencyMatrix(N, std::vector<int>(N));
for (int i = 0; i < N && c != 0; i++) {
for (int j = 0; j < N && c != 0; j++) {
if (i == j) continue;
adjacencyMatrix[i][j] = c % 2;
c /= 2;
}
}
for (const auto &row : adjacencyMatrix) {
for (const auto value : row) {
std::cout << value << ' ';
}
std::cout << '\n';
}
std::cout << '\n';
}
}
int main() {
generateAdjacencyMatrices(3);
}
您可以從counter = 1
開始跳過零矩陣:
#include <iostream>
#include <vector>
unsigned int pow(unsigned int b, unsigned int e) {
if (e == 0) return 1;
if (e == 1) return b;
if (e % 2 == 0) {
return pow(b, e/2) * pow(b, e/2);
}
return b * pow(b, e - 1);
}
void generateAdjacencyMatrices(int N) {
const auto max = pow(2, N*N - N);
for (unsigned int counter = 1; counter < max; ++counter) {
auto c = counter;
std::vector<std::vector<int>> adjacencyMatrix(N, std::vector<int>(N));
for (int i = 0; i < N && c != 0; i++) {
for (int j = 0; j < N && c != 0; j++) {
if (i == j) continue;
adjacencyMatrix[i][j] = c % 2;
c /= 2;
}
}
for (const auto &row : adjacencyMatrix) {
for (const auto value : row) {
std::cout << value << ' ';
}
std::cout << '\n';
}
std::cout << '\n';
}
}
int main() {
generateAdjacencyMatrices(3);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.