计算邻接矩阵最大环中的节点

Counting nodes in the largest cycle of an adjacency matrix

我目前正在使用无向图编写程序。我通过创建连接的邻接矩阵来表示这一点。

if(adjacency_matrix[i][j] == 1){
    //i and j have and edge between them
}
else{
    //i and j are not connected
}

我试图做的是找到给定节点连接到的所有节点(通过任何一系列边)。我试图编写一个函数来为给定节点和 returns 一个向量,该向量在向量中的每个点中包含 1 或 0,该向量由给定节点是否可以通过任何路径到达该节点来确定图。然后我想获取这个向量并将其中的所有值相加,这将 return 该节点可到达的节点数量。然后我想为图中的每个节点获取所有这些值并将它们放入一个向量中,然后找到最大值,这将代表最大循环中的节点数量。

我的问题不是我的函数定义代码收到错误(尽管我是),而是我意识到我写的这个递归函数不正确,但不知道如何修改它以满足我的需要。我在下面包含了我的代码,非常感谢任何指导。

函数定义:

vector<int> get_conn_vect(int u, int conns, vector<vector<int>> vv, vector<int> seen)
{
    seen.at(u) = 1;
    while(v < conns)
    {
        if(seen.at(v) == 0 && vv.at(u).at(v) == 1)
        {
            get_conn_vect(v, conns, vv, seen);
            v++;
        }

        else
        {
            v++;
        }
    }
    return seen;
}

来电main.cpp:

std::vector<int> conn_vect;
int sum_of_elems = 0;
for(int i = 0; i < num_nodes; i++)
{
    std::vector<int> seen_vect(matrix.size());
    sum_of_elems = 0;
    seen_vect = get_conn_vect(i, num_conn, matrix, seen_vect);
    conn_vect.push_back(sum_of_elems = std::accumulate(seen_vect.begin(), seen_vect.end(), 0));
}

正如 Linxi 的评论所说,您在每次调用递归函数时都复制 seen 向量,以便返回给 main 函数的值在索引 i。改为传递引用(我还将矩阵作为对 const 的引用传递,以防止不必要的复制):

void get_conn_vect(int u, const vector< vector<int> > &vv, vector<int> &seen)
{
    seen.at(u) = 1;

    // Assuming that vv is square.
    for(int v = 0; v < vv.size(); ++v)
    {
        if(seen.at(v) == 0 && vv.at(u).at(v) == 1)
        {
            get_conn_vect(v, conns, vv, seen);
        }
    }
}

在递归结束时,您最初传递的向量包含连接到节点 i 的每个节点的非零条目。


一旦意识到邻接矩阵 A 的幂 A^n 的条目 (i,j) 描述了从节点 i 到节点 j 刚好 n 步。因此,可以通过简单地将邻接矩阵 B = A + A^2 + ... + A^N 的幂相加来同时计算所有节点的连通性集,其中 N 是节点数(不同节点之间的路径不能比这更长)。要获得连接到节点 i 的节点数,只需计算 B.

的第 i 列(或行)中的非零数

您要执行的操作称为 "finding the transitive closure"。 Floyd-Warshall algorithm 用于查找此内容(尽管可能有更新、更快的内容,但我不是真正了解该主题的最新信息)。

下面是一些您可以使用的代码:

#include <iostream>
#include <vector>
#include <functional>

using namespace std;

static const size_t NUM_NODES = 4;

// test case, four nodes, A, B, C, D
// A is connected to B which is connected to D
// C is not connected to anyone
//                                    A  B  C  D
unsigned int adjacency_matrix[NUM_NODES][NUM_NODES] = 
                                    {{1, 1, 0, 0}, 
                                     {1, 1, 0, 1}, 
                                     {0, 0, 1, 0},
                                     {0, 1, 0, 1}};


void Warshalls() {
   size_t len = NUM_NODES;
   for (size_t k=0; k<len; ++k) {
      for (size_t i=0; i<len; ++i) {
         for (size_t j=0; j<len; ++j) {
            unsigned int& d = adjacency_matrix[i][j];
            unsigned int& s1 = adjacency_matrix[i][k];
            unsigned int& s2 = adjacency_matrix[k][j];

            if (s1 != 0 && s2 != 0) {
               unsigned int sum = s1 + s2;
               if (d == 0 || d > sum) {
                  d = sum;
               } 
            }
         }
      }
   }
}

vector<size_t> GetNodes(size_t index) {
   if (index >= NUM_NODES) { throw runtime_error("invalid index"); }
   vector<size_t> ret;
   for (size_t i=0; i<NUM_NODES; ++i) {
      if (adjacency_matrix[index][i] != 0) {
         ret.push_back(i);
      }
   }
   return ret;
}

void PrintNodes(const vector<size_t>& nodes) {
   for (auto i=nodes.begin(); i!=nodes.end(); ++i) {
      cout << *i << endl;
   }
}

int main() {
   Warshalls();

   // where can you get to from D
   cout << "Possible destinations for D" << endl;
   vector<size_t> nodes_reachable = GetNodes(3);
   PrintNodes(nodes_reachable);

   // where can you get from C
   cout << "Possible destinations for C" << endl;
   nodes_reachable = GetNodes(2);
   PrintNodes(nodes_reachable);

   return 0;
}