Hamiton图系列文章 (1) Hamilton图证明算法的道路矩阵基础数据结构与类的设计

本文介绍了道路矩阵在求解简单图Hamilton圈问题中的应用,详细阐述了PathArray4SimpleGraph类的设计,包括邻接矩阵的随机初始化、矩阵转换为路径数组的方法,以及用于生成和打印调试信息的辅助函数。后续章节将探讨矩阵运算及Hamilton图的判定算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.简介:

       多年前就有老外提到用道路矩阵算Hamilton圈的算法以及判定方法,后来国内也有多人吃透原理,发表了类似文章。结论就是简单图G(V,E)为Hamilton图的充要条件是:

如果G对应的道路矩阵是P,|V| = n,则

G是H图 等价于 P^n(P*P*P..*P) !=0;

这里的乘法是基于道路矩阵定义的乘法,具体请参考相关文献。

 算法复杂度暂且不论,这篇文章先写道路矩阵的基础数据结构与类设计,后续再补充。

2. 类的设计

//头文件patharray4simplegraph.h

#ifndef PATHARRAY4SIMPLEGRAPH_H
#define PATHARRAY4SIMPLEGRAPH_H
 
#include <iostream>
#include <vector>
using namespace std;
 
typedef vector<int> PATH;
typedef vector<PATH> PATHS;
typedef vector<vector<bool> > GRAPH_ADJ;
typedef vector<vector<PATH> > SINGLE_PATH_ARRAY;
typedef vector<vector<PATHS> > MULTI_PATHS_ARRAY;
 
class PathArray4SimpleGraph
{
public:
    explicit PathArray4SimpleGraph(const int ndimen);
 
private:
    int m_ndimension;
    GRAPH_ADJ m_srcAdjArray;
    SINGLE_PATH_ARRAY m_srcPathArray;
 
private:
    void initSrcAdjArrayRandom(const int ndimen);
    void printAdjArray(const GRAPH_ADJ &adjArray);
    void printPathArray(const SINGLE_PATH_ARRAY &pathArray);
 
public:
    void adjArray2PathArray(const GRAPH_ADJ &srcAdjArray, SINGLE_PATH_ARRAY &dstPathArray);
};
 
#endif // PATHARRAY4SIMPLEGRAPH_H
//源文件patharray4simplegraph.cpp
#include "patharray4simplegraph.h"
#include <iostream>
#include<QRandomGenerator>
 
const int MAXRANDOM=1000;
 
PathArray4SimpleGraph::PathArray4SimpleGraph(const int ndimen):
m_ndimension(ndimen)
{
    initSrcAdjArrayRandom(ndimen);
    printAdjArray(m_srcAdjArray);
 
    adjArray2PathArray(m_srcAdjArray,m_srcPathArray);
    printPathArray(m_srcPathArray);
}
 
//initial the source adjacency matrices with random number 0 or 1
void PathArray4SimpleGraph::initSrcAdjArrayRandom(const int ndimen)
{
    m_srcAdjArray.resize(ndimen);
    for (int i = 0; i < ndimen; ++i){
        m_srcAdjArray[i].resize(ndimen);
    }
 
    for (int i = 0; i < ndimen; ++i){
        m_srcAdjArray[i][i] = 0;
    }
 
    for (int i = 0; i < ndimen-1; ++i){
        for(int j= i+1; j< ndimen; ++j)
        {
            qint16 rand= QRandomGenerator::global()->bounded(MAXRANDOM);
            bool brand = (rand >= MAXRANDOM / 2)? true: false;
            m_srcAdjArray[i][j] = brand;
            m_srcAdjArray[j][i] = brand;
         }
    }
}
 
//print the source adjacency matrices to debug
void PathArray4SimpleGraph::printAdjArray(const GRAPH_ADJ &adjArray)
{
    if(adjArray.size() == 0)
    {
        cout << "Adjacency Array vector is null\n";
        return;
    }
 
    for (auto& line : adjArray) {
        for (const auto& v : line) {
            cout << v << " ";
        }
        cout << endl;
    }
}
 
//print the source path Array to debug
void PathArray4SimpleGraph::printPathArray(const SINGLE_PATH_ARRAY &pathArray)
{
    if(pathArray.size() == 0)
    {
        cout << "Path Array vector is null\n";
        return;
    }
 
    for (auto& line : pathArray) {
        for (const auto& path : line) {
            if (path.size() == 1) continue;
            cout << "[";
            for (size_t i = 0; i < path.size(); ++i) {
                cout << path[i];
                if (i != path.size() - 1)
                    cout << "->";
            }
            cout << "]";
        }
        cout << endl;
    }
}
 
void PathArray4SimpleGraph::adjArray2PathArray(const GRAPH_ADJ &srcAdjArray, SINGLE_PATH_ARRAY &dstPathArray)
{
    dstPathArray.resize(srcAdjArray.size());
    for (int i = 0; i < srcAdjArray.size(); ++i){
        dstPathArray[i].resize(srcAdjArray.size());
    }
 
    for (int i = 0; i < srcAdjArray.size(); ++i){
        for(int j= 0; j< srcAdjArray.size(); ++j)
        {
            if(srcAdjArray[i][j] == 0)
            {
                dstPathArray[i][j].push_back(0);
            } else {
                dstPathArray[i][j].push_back(i);
                dstPathArray[i][j].push_back(j);
            }
 
        }
    }
}
 

3. 应用

//main.cpp

#include "patharray4simplegraph.h"
 
#include <iostream>
#include <vector>
 
using namespace std;
 
int main(int argc, char *argv[])
{
    PathArray4SimpleGraph pathArr4SimpleGraph(8);
    return 1;
}

4.运行结果:

下图显示用随机生成的邻接矩阵,可以正常生成道路矩阵,下篇文章设计矩阵的运算以及Hamilton图的判定。 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值