利用关系矩阵求图的通路和回路个数(离散数学课程报告)

//利用图的邻接矩阵求图中通路长度为l的通路数和回路数
#include <iostream>
#include <vector>
#include <numeric> // for accumulate

using namespace std;

// 计算图中长度为 l 的通路数
int countPathsLenL(vector<vector<int>>& adj, int n, int l) {    //adj为方阵,n为矩阵的阶,l为要找的长度
    vector<vector<int>> a = adj;     // 初始化邻接矩阵的幂为邻接矩阵本身

    while (--l > 0) {   // 循环 l - 1 次,因为第一次循环后邻接矩阵的幂已经是 A^2 了
        vector<vector<int>> b(n, 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) {
                    b[i][j] += a[i][k] * adj[k][j];     // 计算 A^l 的每个元素
                }
            }
        }
        a = b; // 更新邻接矩阵的幂
    }

    int count = 0;
    for (int i = 0; i < n; i++) {        // 统计所有元素的和
        for (int j = 0; j < n; j++) {
            count += a[i][j];
        }
    }
    return count;
}

// 计算图中长度为 l 的回路数
int countCyclesLenL(vector<vector<int>>& adj, int n, int l) {
    vector<vector<int>> a = adj;    // 初始化邻接矩阵的幂为邻接矩阵本身

    while (--l > 0) {   // 循环 l - 1 次,因为第一次循环后邻接矩阵的幂已经是 A^2 了
        vector<vector<int>> b(n, 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) {
                    b[i][j] += a[i][k] * adj[k][j];     // 计算 A^l 的每个元素
                }
            }
        }
        a = b;   // 更新邻接矩阵的幂
    }

    int count = 0;
    for (int i = 0; i < n; ++i) {
        count += a[i][i];   // 累加对角线上的元素,即为长度为l的回路数
    }
    return count;
}



int main() {
    int rank;
    cout << "Please input the number of point:\n";
    cin >> rank ;    //输入顶点数(矩阵的阶)和边数

    vector<vector<int>> adj(rank, vector<int>(rank, 0)); // 邻接矩阵,初始化为 0
    cout << "Please input the matrix:\n";
    for (int i = 0; i < rank; ++i) {                   //输入邻接矩阵
        for (int j = 0; j < rank; ++j) {
            cin >> adj[i][j];
        }
    }

    int l, from = 0, to = 0;
    cout << "Please input the from_point,the to_point and the lenth you want:\n";

    while (cin >> from >> to >> l) {
        if (from == 0 || to == 0 || l == 0)break;
        else {
            vector<vector<int>> b(rank, vector<int>(rank, 0));    // 用来保存更新后的邻接矩阵
            while (--l > 0) {   // 循环 l - 1 次,因为第一次循环后邻接矩阵的幂已经是 A^2 了
                for (int i = 0; i < rank; ++i) {                   //矩阵乘法
                    for (int j = 0; j < rank; ++j) {
                        for (int k = 0; k < rank; ++k) {
                            b[i][j] += adj[i][k] * adj[k][j];     // 计算 A^l 的每个元素
                        }
                    }
                }
            }
            cout << "Paths are " << b[from - 1][to - 1];
        }
    }
    cout << "Paths of length " << l << ": " << countPathsLenL(adj, rank, l) << endl;    //输出长为l的通路数
    cout << "Cycles of length " << l << ": " << countCyclesLenL(adj, rank, l) << endl;  //输出长为l的回路数

    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值