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图的判定。